|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
/*#include"stdio.h"
#include"stdlib.h"
#include"conio.h"
#include"windows.h"
#define High 20
#define Wide 30
//函数外全局变量定义
int map[High][Wide] = {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[food_x][food_y] = -2;
score++;
}
void startup()//数据初始化
{
HideCursor();//隐藏光标
snakemove = 'd';
snakelong = 5;
score = 0;
food_x = rand() % High;
food_y = rand() % Wide;
map[food_x][food_y] = -2;
for (int i = 0; i < High; i++)
{
map[i][0] = -1;
map[i][Wide - 1] = -1;
}
for (int j = 0; j < Wide; j++)
{
map[0][j] = -1;
map[High - 1][j] = -1;
}
for (int k = 0; k < 5; k++)
{
map[High/2][Wide/2 - k] = k + 1;
}
}
void show()//显示画面
{
gotoxy(0, 0);//光标移动到原点位置,一下重画清屏
for (int i = 0; i < High; i++)
{
for (int j = 0; j < Wide; j++)
{
if (map[i][j] == -1)
{
printf("#");
}
else if (map[i][j] == 0)
{
printf(" ");
}
else if (map[i][j] == -2)
{
printf("F");
}
else if (map[i][j] == 1)
{
printf("@");
}
else if (map[i][j] > 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[i][j] > 0)
{
map[i][j]++;
}
}
}
for (int i = 0; i < High; i++)
{
for (int j = 0; j < Wide; j++)
{
if (map[i][j] > snakelong)
{
map[i][j] = 0;
m = i;
n = j;
}
}
}
for (int i = 0; i < High; i++)
{
for (int j = 0; j < Wide; j++)
{
if (map[i][j] == 2)
{
if (snakemove == 'a')
{
if (map[i][j - 1] == 0)
{
map[i][j - 1] = 1;
}
else if (map[i][j - 1] == -2)
{
map[i][j - 1] = 1;
snakelong++;
map[m][n] = snakelong;
food();
}
else
{
die();
}
}
else if (snakemove == 'd')
{
if (map[i][j + 1] == 0)
{
map[i][j + 1] = 1;
}
else if (map[i][j + 1] == -2)
{
map[i][j + 1] = 1;
snakelong++;
map[m][n] = snakelong;
food();
}
else
{
die();
}
}
else if (snakemove == 's')
{
if (map[i+1][j] == 0)
{
map[i+1][j] = 1;
}
else if (map[i+1][j] == -2)
{
map[i+1][j] = 1;
snakelong++;
map[m][n] = snakelong;
food();
}
else
{
die();
}
Sleep(50);
}
else if (snakemove == 'w')
{
if (map[i - 1][j] == 0)
{
map[i - 1][j] = 1;
}
else if (map[i-1][j] == -2)
{
map[i - 1][j] = 1;
snakelong++;
map[m][n] = snakelong;
food();
}
else
{
die();
}
Sleep(50);
}
}
}
}
}
void updateWithInput() //与用户输入有关的更新
{
if (kbhit())
{
snakemove = getch();
}
}
int main()
{
startup(); //数据初始化
while (1) //游戏循环执行
{
show(); //显示画面
updateWithoutInput(); //与用户无关的更新
updateWithInput(); //与用户输入有关的更新
}
return 0;
}*/ |
|