新手写的贪吃蛇
以下代码包含的头文件conio.h只能在windows系统下编译运行!!!//??????
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <windows.h>
#include <conio.h>
#include <time.h>
//??????
#define MAX 32
//??????
char a = {"################################################",
"# #",
"# #",
"# #",
"# #",
"# #",
"# #",
"# #",
"# #",
"# #",
"# #",
"# #",
"# #",
"# #",
"# #",
"# #",
"# #",
"# #",
"# #",
"################################################"};
int game_s_long = 4;
//??
struct Body
{
int x;
int y;
} body;
struct Head
{
int x;
int y;
char turn;
} head;
struct Food
{
int x;
int y;
} food;
//??????
void game_s_put(void)
{
system("cls");
for (int i = 0; i < 20; i++)
{
puts(a);
}
printf("score:%d\n", game_s_long);
}
void game_s_start(void)
{
a = '@';
for (int i = 1; i < 5; i++)
{
a = 'O';
}
head.x = 10, head.y = 24;
body.x = 11, body.y = 24;
body.x = 12, body.y = 24;
body.x = 13, body.y = 24;
body.x = 14, body.y = 24;
head.turn = 'w';
}
int game_s_move(void)
{
int x1 = 0, x2 = 0, y1 = 0, y2 = 0, i = 0;
if (head.turn == 'w')
{
if (a == 'O' || a == '#')
{
return 0;
}
}
if (head.turn == 's')
{
if (a == 'O' || a == '#')
{
return 0;
}
}
if (head.turn == 'a')
{
if (a == 'O' || a == '#')
{
return 0;
}
}
if (head.turn == 'd')
{
if (a == 'O' || a == '#')
{
return 0;
}
}
switch (head.turn)
{
case 'w':
if (head.turn != 's')
{
x1 = head.x;
y1 = head.y;
head.x--;
for (i = 0; i < game_s_long; i += 2)
{
x2 = body.x;
y2 = body.y;
body.x = x1;
body.y = y1;
if (game_s_long - i == 1)
{
break;
}
x1 = body.x;
y1 = body.y;
body.x = x2;
body.y = y2;
}
body.x = x2;
body.y = y2;
}
break;
case 's':
if (head.turn != 'w')
{
x1 = head.x;
y1 = head.y;
head.x++;
for (i = 0; i < game_s_long; i += 2)
{
x2 = body.x;
y2 = body.y;
body.x = x1;
body.y = y1;
if (game_s_long - i == 1)
{
break;
}
x1 = body.x;
y1 = body.y;
body.x = x2;
body.y = y2;
}
body.x = x2;
body.y = y2;
}
break;
case 'a':
if (head.turn != 'd')
{
x1 = head.x;
y1 = head.y;
head.y--;
for (i = 0; i < game_s_long; i += 2)
{
x2 = body.x;
y2 = body.y;
body.x = x1;
body.y = y1;
if (game_s_long - i == 1)
{
break;
}
x1 = body.x;
y1 = body.y;
body.x = x2;
body.y = y2;
}
}
body.x = x2;
body.y = y2;
break;
case 'd':
if (head.turn != 'a')
{
x1 = head.x;
y1 = head.y;
head.y++;
for (i = 0; i < game_s_long; i += 2)
{
x2 = body.x;
y2 = body.y;
body.x = x1;
body.y = y1;
if (game_s_long - i == 1)
{
break;
}
x1 = body.x;
y1 = body.y;
body.x = x2;
body.y = y2;
}
}
body.x = x2;
body.y = y2;
break;
}
}
void game_s_get(void)
{
if (kbhit())
{
head.turn = getch();
}
}
void game_s_release(void)
{
strcpy(a, "##################################################");
strcpy(a, "# #");
strcpy(a, "# #");
strcpy(a, "# #");
strcpy(a, "# #");
strcpy(a, "# #");
strcpy(a, "# #");
strcpy(a, "# #");
strcpy(a, "# #");
strcpy(a, "# #");
strcpy(a, "# #");
strcpy(a, "# #");
strcpy(a, "# #");
strcpy(a, "# #");
strcpy(a, "# #");
strcpy(a, "# #");
strcpy(a, "# #");
strcpy(a, "# #");
strcpy(a, "# #");
strcpy(a, "##################################################");
a = '@';
if (food.x != '\0' && food.y != '\0')
{
a = '*';
}
for (int i = 0; i < game_s_long; i++)
{
a.x].y] = 'O';
}
}
void game_s_food(void)
{
int x, y;
time_t t;
while(1)
{
srand((unsigned)time(&t));
x = rand() % 20;
srand((unsigned)time(&t));
y = rand() % 50;
if (a != '#' && a != 'O' && a != '@')
{
food.x = x;
food.y = y;
break;
}
}
}
int game_s_eat(void)
{
if (head.turn == 'w')
{
if (a == '*')
{
food.x = '\0';
food.y = '\0';
game_s_long++;
return 1;
}
}
if (head.turn == 's')
{
if (a == '*')
{
food.x = '\0';
food.y = '\0';
game_s_long++;
return 1;
}
}
if (head.turn == 'a')
{
if (a == '*')
{
food.x = '\0';
food.y = '\0';
game_s_long++;
return 1;
}
}
if (head.turn == 'd')
{
if (a == '*')
{
food.x = '\0';
food.y = '\0';
game_s_long++;
return 1;
}
}
return 0;
}
void game_s(void)
{
char ch;
int q,p;
game_s_food();
game_s_start();
while (1)
{
game_s_get();
q = game_s_move();
if (!q)
{
exit(0);
}
p=game_s_eat();
if(p)
{
game_s_food();
}
game_s_release();
game_s_put();
Sleep(250);
}
}
//??????
int main(void)
{
game_s();
return 0;
}
不喜勿喷
页:
[1]