最近编了个贪吃蛇小游戏请大家提点建议
#include <stdio.h>
#include <bios.h>
#include <conio.h>
#include <stdlib.h> /*调用randomize()函数*/
#include <time.h>
#define SPACE 0X3920
#define ESC 0x011b
#define UP 0x4800
#define DOWN 0x5000
#define LEFT 0x4b00
#define RIGHT 0x4d00
typedef struct
{
int x;
int y;
}point;
DrawBody(int x, int y)
{
gotoxy(x, y);
printf("%c\b", 3);
}
DrawHead(int x, int y)
{
gotoxy(x, y);
printf("%c\b", 2);
}
DrawSpace(int x, int y)
{
gotoxy(x, y);
printf(" ");
}
void bring_food(point man[],int n,point *food) /*随机产生食物*/
{
int x,y;
int i,k = 0;
randomize();
while (k == 0)
{k = 1;
x = rand() % 58;
y = rand() % 23;
if (x < 3 || y < 3) /*产生的食物不在框内*/
{k = 0;
continue;
}
for (i = 0; i < n; ++i)
if ((x == man[i].x) && (y == man[i].y)) /*如果产生的食物坐标和蛇身的某个坐标相同,则不符合条件*/
{ k = 0;
break;
}
}
food->x = x;
food->y = y;
gotoxy(x,y); /*在x,y出产生食物*/
printf("*");
}
void DrawWall() /*画边框函数*/
{ int i,j;
for (j = 2, i = 2; i < 60; ++i)
{ gotoxy(i, j);
textcolor(GREEN);
putch(219);
gotoxy(i, j+22);
textcolor(GREEN);
putch(219);
}
for (j = 2, i = 2; i < 25; ++i)
{ gotoxy(j, i);
textcolor(GREEN);
putch(219);
gotoxy(j+57, i);
textcolor(GREEN);
putch(219);
}
}
void message(int score) /*该函数输出信息*/
{
if (score == 10 || score == 20 || score == 35)
{
gotoxy(65,6);
printf("Score: %d",score);
gotoxy(20,10);
textattr(5);
cprintf(" You Win ");
gotoxy(20,12);
cprintf("Thank you baby!");
getch();
return ;
}
gotoxy(65,6);
printf("Score: %d",score);
gotoxy(65,7);
printf("Space: pause");
}
int main() /*该程序运行将用方向键控制蛇的移动*/
{
point man[60]; /*记录蛇坐标*/
point food; /*记录食物坐标*/
int key = DOWN,keyb,key1 = DOWN;
int i;
int n = 10;
int score = 0;
long j,time;
clrscr();
for (i = 0; i < 10; ++i)
man[i].x = 12 - i;
for (i = 0; i < 10; ++i)
man[i].y = 3;
DrawWall(); /*画边框*/
for (i = 9; i > 0; --i)
DrawBody(man[i].x, man[i].y); /*画蛇身*/
DrawHead(man[i].x, man[i].y); /*画蛇头*/
message(score);
bring_food(man,10,&food); /*产生食物*/
while (key != ESC)
{
if (bioskey(1) != 0) /*如果有键按下就接收键盘值*/
keyb = bioskey(0);
if ((keyb == SPACE) || (keyb == ESC) || (keyb == UP) || (keyb == DOWN) || (keyb == LEFT) || (keyb == RIGHT))
key = keyb;
switch (key)
{
case UP:
if (key1 == DOWN)
key = key1;
break;
case DOWN:
if (key1 == UP)
key = key1;
break;
case LEFT:
if (key1 == RIGHT)
key = key1;
break;
case RIGHT:
if (key1 == LEFT)
key = key1;
break;
case SPACE:
sleep(3);
if (man[0].x == man[1].x) /*暂停前沿y方向移动*/
{if (man[0].y < man[1].y)
key = UP;
else
key = DOWN;
}
else{if (man[0].x < man[1].x)
key = LEFT;
else
key = RIGHT;
}
break;
defualt:
break;
}
switch (key)
{
case UP:
DrawSpace(man[n-1].x, man[n-1].y);
for (i = n-1; i > 0 ; --i)
{
man[i].x = man[i-1].x;
man[i].y = man[i-1].y;
}
--man[0].y;
for (i = n-1; i > 0; --i)
DrawBody(man[i].x, man[i].y);
DrawHead(man[i].x, man[i].y);
break;
case DOWN:
DrawSpace(man[n-1].x, man[n-1].y);
for (i = n-1; i > 0 ; --i)
{
man[i].x = man[i-1].x;
man[i].y = man[i-1].y;
}
++man[0].y;
for (i = n-1; i > 0; --i)
DrawBody(man[i].x, man[i].y);
DrawHead(man[i].x, man[i].y);
break;
case LEFT:
DrawSpace(man[n-1].x, man[n-1].y);
for (i = n-1; i > 0 ; --i)
{
man[i].x = man[i-1].x;
man[i].y = man[i-1].y;
}
--man[0].x;
for (i = n-1; i > 0; --i)
DrawBody(man[i].x, man[i].y);
DrawHead(man[i].x, man[i].y);
break;
case RIGHT:
DrawSpace(man[n-1].x, man[n-1].y);
for (i = n-1; i > 0 ; --i)
{
man[i].x = man[i-1].x;
man[i].y = man[i-1].y;
}
++man[0].x;
for (i = n-1; i > 0; --i)
DrawBody(man[i].x, man[i].y);
DrawHead(man[i].x, man[i].y);
break;
/* defualt:
break; */
}
if ((man[0].x < 3) || (man[0].x > 58) || (man[0].y < 3) || (man[0].y > 23)) /*碰到边框*/
break;
for (i = 2; i < n; ++i)
if (man[0].x == man[i].x && man[0].y == man[i].y) /*碰到自己身体*/
{key = ESC;
break;
}
if ((man[0].x == food.x) && (man[0].y == food.y)) /*吃到食物*/
{ score ++;
message(score);
n ++; /*蛇身长度加1*/
bring_food(man,n,&food); /*再产生一个食物*/
}
time = (score < 10) ? 500000 : ((score < 20) ? 400000 : ((score <30) ? 300000 : 200000));
for (j = 1; j < time ; ++j) /*该循环用来控制时间*/
if ((j != 0) && (j % 8 == 0))
j = j + 2 - 1*2;
else if ((j != 0) && (j % 9 != 1))
j = j + 1 - 1 * 1 * 1;
else j = j+1;
key1 = key; /*记录下本次的有效按键*/
}
if (score != 10 && score != 20 && score != 35)
{ gotoxy(20,10);
textattr(2);
cprintf(" Game Over ");
gotoxy(20,12);
cprintf("Thank you baby!");
getch();
}
return 0;
}
[此贴子已经被作者于2006-6-28 19:25:49编辑过]