#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <Windows.h>
#include <time.h>
#include <conio.h> /键盘输入获取/
bool gameOver;
bool stop = false;
bool hit = false;
/游戏的边框大小/
const int width = 50;
const int height = 20;
/蛇的坐标,食物的坐标还有分数/
int x,y,fruitX,fruitY,score;
/蛇每个点的坐标/
int tailX[200],tailY[200];
/蛇的默认长度/
int ntail=3;
typedef enum
{
STOP = 0,
LEFT,
RIGHT,
UP,
DOWN
}Direction;
Direction Dir;
/开始菜单/
void menu()
{
int a;
printf("------------------------------------------------------------------\n");
printf("| 贪吃蛇游戏 |\n");
printf("| 1) 新游戏 |\n");
printf("| 2) 开始边界 |\n");
printf("| 3) 退出游戏 |\n");
printf("------------------------------------------------------------------\n");
printf("---->请输入你的选择:");
scanf("%d", &a);
}
/初始化状态/
void setup()
{
gameOver = false;
/根据当前时间设置“随机数种子”/
srand(time(NULL));
Dir = STOP;
}
/绘制界面/
void draw()
{
if(stop == true)
{
return;
}
system("cls");/清除屏幕/
printf("分数:%d",score);
printf("\n");
}
/按键输入控制/
void input()
{
if(_kbhit())
{
/获取键盘的输入字符/
switch(_getch())
{
case '4':
case 75:/左键/
Dir = LEFT;
hit= true;
break;
case '8':
case 72:/上键/
Dir = UP;
hit= true;
break;
case '6':
case 77:/右键/
Dir = RIGHT;
hit= true;
break;
case '2':
case 80:/*向下键盘键 */
Dir = DOWN;
hit= true;
break;
case 'x':
case 27:/ESE/
gameOver = true;
break;
case 32:/空格 暂停键/
stop = !stop;
break;
}
}
else if(!hit && stop == false)/如果没有改变方向/
{
x++;
}
}
/判断贪吃蛇的长度/
void logic()
{
if(stop == true)
{
return;
}
}
int main()
{
#if 0
while(1)
{
printf("%d\n",_getch());
}
#endif
menu();
setup();
draw();
/循环画贪吃蛇的界面/
while(!gameOver)
{
draw();
input();
logic();
Sleep(70);
}
}