贪吃蛇:看哈啊
#include<conio.h>#include<bios.h>
#include<stdlib.h>
#define LEFT 0x4b00
#define RIGHT 0x4d00
#define UP 0x4800
#define DOWN 0x5000
#define ENTER 0x000d
#define ESC 0x0011b
struct
{
int x;
int y;
}body[300];
struct
{
int x;
int y;
}heart[500];
int k=0;
int n=0;
int score=0;
void prin();
void loadheart();
void gameover();
void load();
void find();
void eat();
void game ();
void pringame();
void main()
{
prin();
clrscr();
game();
getch();
}
void prin()
{ textmode(C80);
window(0,0,80,25);
textbackground(GREEN);
textcolor(BLUE);
clrscr();
gotoxy(30,9);
cprintf("Welcome to my game!");
gotoxy(30,10);
cprintf("Scores");
gotoxy(30,11);
cprintf("Are you ready" );
gotoxy(30,12);
cprintf("Press anykey to beagin!");
getch();
}
void game()
{
int i;
int key=0;
load();
loadheart();
randomize();
pringame();
gotoxy(heart[n-1].x,heart[n-1].y);
putch(3);
for(i=0;i<k;i++)
{
gotoxy(body[i].x,body[i].y);
putch(4);
}
do
{
while(!kbhit())
{
switch(key)
{
case ESC :
exit(1);break;
case LEFT:
for(i=0;i<k-1;i++)
{
body[i].x=body[i+1].x;
body[i].y=body[i+1].y;
}
body[k-1].x--;
break;
case RIGHT :
for(i=0;i<k-1;i++)
{
body[i].x=body[i+1].x;
body[i].y=body[i+1].y;
}
body[k-1].x++;
break;
case UP: for(i=0;i<k-1;i++)
{
body[i].x=body[i+1].x;
body[i].y=body[i+1].y;
}
body[k-1].y--;
break;
case DOWN : for(i=0;i<k-1;i++)
{
body[i].x=body[i+1].x;
body[i].y=body[i+1].y;
}
body[k-1].y++;
break;
case ENTER:
getch(); break;
}
find();
eat();
pringame();
delay(60000);
clrscr();
for(i=0;i<k;i++)
{
gotoxy(body[i].x,body[i].y);
putch(4);
}
gotoxy(heart[n-1].x,heart[n-1].y);
putch(3);
}
key=bioskey(0);
}while(1);
}
void load()
{
body[0].x=1;
body[0].y=3;
body[1].x=2;
body[1].y=3;
body[2].x=3;
body[2].y=3;
k=3;
}
void pringame()
{
int i;
gotoxy(35,1);
cputs("score:");
gotoxy(43,1);
cprintf("%d",score);
for(i=1;i<=80;i++)
{
gotoxy(i,2);
putch(23);
}
}
void find()
{ int i;
for(i=0;i<k;i++)
{
if(body[i].x==0||body[i].x==85||body[i].y==2||body[i].y==26)
gameover();
}
for(i=0;i<k-2;i++)
{
if(body[k-1].x==body[i].x&&body[k-1].y==body[i].y)
gameover();
}
}
void gameover()
{
int i;
window(0,0,80,25);
textbackground(GREEN);
textcolor(BLUE);
clrscr();
gotoxy(33,10);
cputs("Game Over");
gotoxy(35,12);
cputs("Score");
gotoxy(42,12);
cprintf("%d",score);
getch();
}
void eat()
{
if(body[k-1].x==heart[n-1].x&&body[k-1].y==heart[n-1].y)
{ score=score+5;
body[k].x=heart[n-1].x;
body[k].y=heart[n-1].y;
k++;
loadheart();
}
}
void loadheart()
{
heart[n].x=(rand()%83)+2;
heart[n].y=(rand()%22)+3;
n++ ;
}
下次再注释:先看哈: