#include
#include
#include
#include
#define SIZEMAX 100 // 定义蛇的最大长度
#define SNAKELEN 10 // 蛇的大小和蛇每次移动的距离
#define SPEED 100 // 初始速度
/****************************************/
typedef enum // 定义表示方向的枚举变量
{
left, right, up, down
}DIR;
typedef struct // 定义一个结构体表示坐标
{
int x;
int y;
}COOR;
struct FOOD // 定义结构体,来存储食物的相关信息
{
COOR crd; // 食物出现的坐标
int flag; // 食物的状态,1表示食物还没被吃,0表示食物被吃了
}food;
struct SNAKE // 定义结构体,来存储蛇的相关信息
{
COOR crd[SIZEMAX]; // 保存蛇的每个结数的坐标,最多有100节
int node; // 保存蛇当前节数
DIR dir; // 当前蛇行进的方向
}snake;
int speed = SPEED; // 方便以后在程序中改变速度
/***************************************/
void init(); // 初始化游戏
void setfoodcrd(); // 设置食物要出现的坐标
void showfood(); // 在相应的坐标画出食物
void snakemove(); // 移动蛇,实际就是改变蛇各节的坐标
void judgeslod(); // 判断蛇是否撞墙或者撞到自己
void judgesef(); // 判断蛇是否吃到食物了
void showsnake(); // 在屏幕上画出蛇
void changeskdir(); // 接受用户输入,改变蛇的前进方向
void gameover(); // 游戏结束
/**************************************/
void main()
{
init();
while(1)
{
while(!kbhit())
{
if (!food.flag)
{
setfoodcrd();
}
showfood();
judgeslod();
judgesef();
snakemove();
Sleep(speed);
}
changeskdir();
}
}
/***********************************/
void init()
{
initgraph(640, 480);
srand(time(NULL));
food.flag = 0;
snake.crd[0].x = 0 + SNAKELEN;
snake.crd[0].y = 0;
snake.crd[1].x = 0;
snake.crd[1].y = 0;
snake.node = 2;
snake.dir = right;
}
void setfoodcrd()
{
food.crd.x = rand() % (600 / SNAKELEN) * SNAKELEN;
food.crd.y = rand() % (480 / SNAKELEN) * SNAKELEN;
food.flag = 1;
}
void showfood()
{
rectangle(food.crd.x, food.crd.y, food.crd.x+SNAKELEN, food.crd.y+SNAKELEN);
}
void snakemove()
{
int i; for (i=snake.node; i>0; i--)
{
snake.crd[i].x = snake.crd[i-1].x;
snake.crd[i].y = snake.crd[i-1].y;
}
switch (snake.dir)
{
case up:
snake.crd[0].y -= SNAKELEN;
break;
case down:
snake.crd[0].y += SNAKELEN;
break;
case left:
snake.crd[0].x -= SNAKELEN;
break;
case right:
snake.crd[0].x += SNAKELEN;
} showsnake();
}
void judgeslod()
{
int i;
if (snake.crd[0].x<0 || snake.crd[0].x >= 640 ||
snake.crd[0].y<0 || snake.crd[0].y >= 480)
{
gameover();
}
for (i=snake.node-1; i>0; i--)
{
if (snake.crd[0].x==snake.crd[i].x && snake.crd[0].y==snake.crd[i].y)
{
gameover();
}
}
}
void judgesef()
{
if (snake.crd[0].x==food.crd.x && snake.crd[0].y==food.crd.y )
{
snake.node++;
setcolor(BLACK);
rectangle(food.crd.x, food.crd.y, food.crd.x+SNAKELEN, food.crd.y+SNAKELEN);
setcolor(WHITE);
food.flag = 0;
}
}
void showsnake()
{
int i;
for (i=snake.node-1; i>=0; i--)
{
rectangle(snake.crd[i].x, snake.crd[i].y, snake.crd[i].x+SNAKELEN, snake.crd[i].y+SNAKELEN);
}
setcolor(BLACK);
rectangle(snake.crd[snake.node].x, snake.crd[snake.node].y, snake.crd[snake.node].x+SNAKELEN, snake.crd[snake.node].y+SNAKELEN);
setcolor(WHITE); rectangle(snake.crd[snake.node-1].x, snake.crd[snake.node-1].y, snake.crd[snake.node-1].x+SNAKELEN, snake.crd[snake.node-1].y+SNAKELEN);
}
void changeskdir()
{
char key;
key = getch();
switch (key)
{
case 'w':
case 'W':
if (snake.dir != down)
{
snake.dir = up;
}
break;
case 's':
case 'S':
if (snake.dir != up)
{
snake.dir = down;
}
break;
case 'a':
case 'A':
if (snake.dir != right)
{
snake.dir = left;
}
break;
case 'd':
case 'D':
if (snake.dir != left)
{
snake.dir = right;
}
break;
}
}
void gameover()
{
closegraph();
}
#include
#include
#include
#define SIZEMAX 100 // 定义蛇的最大长度
#define SNAKELEN 10 // 蛇的大小和蛇每次移动的距离
#define SPEED 100 // 初始速度
/****************************************/
typedef enum // 定义表示方向的枚举变量
{
left, right, up, down
}DIR;
typedef struct // 定义一个结构体表示坐标
{
int x;
int y;
}COOR;
struct FOOD // 定义结构体,来存储食物的相关信息
{
COOR crd; // 食物出现的坐标
int flag; // 食物的状态,1表示食物还没被吃,0表示食物被吃了
}food;
struct SNAKE // 定义结构体,来存储蛇的相关信息
{
COOR crd[SIZEMAX]; // 保存蛇的每个结数的坐标,最多有100节
int node; // 保存蛇当前节数
DIR dir; // 当前蛇行进的方向
}snake;
int speed = SPEED; // 方便以后在程序中改变速度
/***************************************/
void init(); // 初始化游戏
void setfoodcrd(); // 设置食物要出现的坐标
void showfood(); // 在相应的坐标画出食物
void snakemove(); // 移动蛇,实际就是改变蛇各节的坐标
void judgeslod(); // 判断蛇是否撞墙或者撞到自己
void judgesef(); // 判断蛇是否吃到食物了
void showsnake(); // 在屏幕上画出蛇
void changeskdir(); // 接受用户输入,改变蛇的前进方向
void gameover(); // 游戏结束
/**************************************/
void main()
{
init();
while(1)
{
while(!kbhit())
{
if (!food.flag)
{
setfoodcrd();
}
showfood();
judgeslod();
judgesef();
snakemove();
Sleep(speed);
}
changeskdir();
}
}
/***********************************/
void init()
{
initgraph(640, 480);
srand(time(NULL));
food.flag = 0;
snake.crd[0].x = 0 + SNAKELEN;
snake.crd[0].y = 0;
snake.crd[1].x = 0;
snake.crd[1].y = 0;
snake.node = 2;
snake.dir = right;
}
void setfoodcrd()
{
food.crd.x = rand() % (600 / SNAKELEN) * SNAKELEN;
food.crd.y = rand() % (480 / SNAKELEN) * SNAKELEN;
food.flag = 1;
}
void showfood()
{
rectangle(food.crd.x, food.crd.y, food.crd.x+SNAKELEN, food.crd.y+SNAKELEN);
}
void snakemove()
{
int i; for (i=snake.node; i>0; i--)
{
snake.crd[i].x = snake.crd[i-1].x;
snake.crd[i].y = snake.crd[i-1].y;
}
switch (snake.dir)
{
case up:
snake.crd[0].y -= SNAKELEN;
break;
case down:
snake.crd[0].y += SNAKELEN;
break;
case left:
snake.crd[0].x -= SNAKELEN;
break;
case right:
snake.crd[0].x += SNAKELEN;
} showsnake();
}
void judgeslod()
{
int i;
if (snake.crd[0].x<0 || snake.crd[0].x >= 640 ||
snake.crd[0].y<0 || snake.crd[0].y >= 480)
{
gameover();
}
for (i=snake.node-1; i>0; i--)
{
if (snake.crd[0].x==snake.crd[i].x && snake.crd[0].y==snake.crd[i].y)
{
gameover();
}
}
}
void judgesef()
{
if (snake.crd[0].x==food.crd.x && snake.crd[0].y==food.crd.y )
{
snake.node++;
setcolor(BLACK);
rectangle(food.crd.x, food.crd.y, food.crd.x+SNAKELEN, food.crd.y+SNAKELEN);
setcolor(WHITE);
food.flag = 0;
}
}
void showsnake()
{
int i;
for (i=snake.node-1; i>=0; i--)
{
rectangle(snake.crd[i].x, snake.crd[i].y, snake.crd[i].x+SNAKELEN, snake.crd[i].y+SNAKELEN);
}
setcolor(BLACK);
rectangle(snake.crd[snake.node].x, snake.crd[snake.node].y, snake.crd[snake.node].x+SNAKELEN, snake.crd[snake.node].y+SNAKELEN);
setcolor(WHITE); rectangle(snake.crd[snake.node-1].x, snake.crd[snake.node-1].y, snake.crd[snake.node-1].x+SNAKELEN, snake.crd[snake.node-1].y+SNAKELEN);
}
void changeskdir()
{
char key;
key = getch();
switch (key)
{
case 'w':
case 'W':
if (snake.dir != down)
{
snake.dir = up;
}
break;
case 's':
case 'S':
if (snake.dir != up)
{
snake.dir = down;
}
break;
case 'a':
case 'A':
if (snake.dir != right)
{
snake.dir = left;
}
break;
case 'd':
case 'D':
if (snake.dir != left)
{
snake.dir = right;
}
break;
}
}
void gameover()
{
closegraph();
}
贪吃蛇核心C代码
最新推荐文章于 2022-04-09 12:35:31 发布