#include"role.h"
#define LevUp 5
using namespace std;
void start_game();
void init_game();
void creatSnake();
void showSnake();
void showFood();
void getKey();
void changeSnake();
void eatFood();
void upScore();
int die();
struct MYsnake{
int x;
int y;
};
struct Food{
};
struct MYsnake body[100];
struct MYsnake head;
struct Food food;
int length = 0;
int mapX = 50;
int mapY = 50;
int keyF;
int score;
int lev = 9;
int Flag = 1;
int main()
{
srand(time(0));
start_game();
init_game();
showFood();
creatSnake();
showSnake();
while(true){
getKey();
changeSnake();
showSnake();
eatFood();
upScore();
void upScore();
if(score % LevUp == 0 && score != 0){
if(Flag == 1){
}
void start_game()
{
color(15);
gotoxy(12,13);
cout <<"★贪 吃 蛇 ★";
gotoxy(12,15);
cout<<"输入y开始游戏:";
char c;
cin>>c;
system("cls");
}
void init_game(){
color(15);
for(int j = 1; j <=mapX-1; j++){
for(int i = 1;i <= mapY-1; i++){
}
void creatSnake()
{
head.x = rand()%(mapX-1)+1;
head.y = rand()%(mapY-1)+1;
}
void showSnake(){
int i;
color(9);
}
void showFood(){
int i = 0;
while(true){
food.x = (rand() % (mapX - 1))+1;
food.y = (rand() % (mapY - 1))+1;
// gotoxy(0, 80);
// cout << food.x << " " << food.y;
gotoxy(food.x ,food.y);
color(6);
cout<<"●";
}
void getKey(){
char c;
if(kbhit()){
c = getch();
if(c == 'w' || c == 'W'){
keyF = 1;
}
if(c == 'a' || c == 'A'){
keyF = 2;
}
if(c == 's' || c == 'S'){
keyF = 3;
}
if(c == 'd' || c == 'D'){
keyF = 4;
}
if(c == ' ' ){
keyF = 5;
}
}
}
void changeSnake()
{
struct MYsnake tmp = head;
if(keyF == 1)
{
head.y--;
}
if(keyF == 2)
{
head.x--;
}
if(keyF == 3)
{
head.y++;
}
if(keyF == 4)
{
head.x++;
}
if(keyF == 5)
{
}
void eatFood(){
if(food.x == head.x && food.y == head.y){
body[length].x = food.x;
body[length].y = food.y;
length++;
Flag = 1;
showFood();
}
}
void upScore(){
score = length;
}
int die(){
if(head.x == 0 || head.y == 0 ||head.x >= mapX || head.y >= mapY){
return 0;
}
return 1;
}