学完C打了个贪吃蛇,代码分享,多多指教
/*#include"stdio.h"#include"stdlib.h"
#include"conio.h"
#include"windows.h"
#define High 20
#define Wide 30
//函数外全局变量定义
int map = {0};//游戏画面
char snakemove;
int snakelong;
int score;
int food_x,food_y;
void gotoxy(int x, int y) //光标移动到(x,y)位置
{
HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);
COORD pos;
pos.X = x;
pos.Y = y;
SetConsoleCursorPosition(handle, pos);
}
void HideCursor()
{
CONSOLE_CURSOR_INFO cursor_info = { 1, 0 };
SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cursor_info);
}
void die()//判断游戏结束
{
system("cls");
printf("游戏结束\n您的得分是:%d\n", score);
exit(0);
}
void food()
{
food_x = rand() % High;
food_y = rand() % Wide;
map = -2;
score++;
}
void startup()//数据初始化
{
HideCursor();//隐藏光标
snakemove = 'd';
snakelong = 5;
score = 0;
food_x = rand() % High;
food_y = rand() % Wide;
map = -2;
for (int i = 0; i < High; i++)
{
map = -1;
map = -1;
}
for (int j = 0; j < Wide; j++)
{
map = -1;
map = -1;
}
for (int k = 0; k < 5; k++)
{
map = k + 1;
}
}
void show()//显示画面
{
gotoxy(0, 0);//光标移动到原点位置,一下重画清屏
for (int i = 0; i < High; i++)
{
for (int j = 0; j < Wide; j++)
{
if (map == -1)
{
printf("#");
}
else if (map == 0)
{
printf(" ");
}
else if (map == -2)
{
printf("F");
}
else if (map == 1)
{
printf("@");
}
else if (map > 1)
{
printf("*");
}
}
printf("\n");
}
printf("\n得分:%d", score);
}
void updateWithoutInput() //与用户无关的更新
{
Sleep(50);
int m, n;
for (int i = 0; i < High; i++)
{
for (int j = 0; j < Wide; j++)
{
if (map > 0)
{
map++;
}
}
}
for (int i = 0; i < High; i++)
{
for (int j = 0; j < Wide; j++)
{
if (map > snakelong)
{
map = 0;
m = i;
n = j;
}
}
}
for (int i = 0; i < High; i++)
{
for (int j = 0; j < Wide; j++)
{
if (map == 2)
{
if (snakemove == 'a')
{
if (map == 0)
{
map = 1;
}
else if (map == -2)
{
map = 1;
snakelong++;
map = snakelong;
food();
}
else
{
die();
}
}
else if (snakemove == 'd')
{
if (map == 0)
{
map = 1;
}
else if (map == -2)
{
map = 1;
snakelong++;
map = snakelong;
food();
}
else
{
die();
}
}
else if (snakemove == 's')
{
if (map == 0)
{
map = 1;
}
else if (map == -2)
{
map = 1;
snakelong++;
map = snakelong;
food();
}
else
{
die();
}
Sleep(50);
}
else if (snakemove == 'w')
{
if (map == 0)
{
map = 1;
}
else if (map == -2)
{
map = 1;
snakelong++;
map = snakelong;
food();
}
else
{
die();
}
Sleep(50);
}
}
}
}
}
void updateWithInput() //与用户输入有关的更新
{
if (kbhit())
{
snakemove = getch();
}
}
int main()
{
startup(); //数据初始化
while (1) //游戏循环执行
{
show(); //显示画面
updateWithoutInput(); //与用户无关的更新
updateWithInput(); //与用户输入有关的更新
}
return 0;
}*/ 建议发代码格式哦
页:
[1]