满意答案
p44s8g6
2014.12.10
采纳率:55% 等级:11
已帮助:3999人
当然可以边游戏了,汉字可以用点阵显示,如果是windows c 的话显示汉字就更简单了!游戏窗体可以用windows c 编程实现!
给你一个贪吃蛇的游戏吧,在tc 下编译成功!可以q聊:969370950
#include
#include
#include
#include
#define ESC 0x011b
#define LEFT 0x4b00
#define RIGHT 0x4d00
#define UP 0x4800
#define DOWN 0x5000
#define F5 0x3f00
#define S 0x1f73
int score=0;
unsigned level=0;
void GAMEOVER()
{char ch;
while(1)
{clrscr();gotoxy(35,10);
highvideo();
cprintf("Game Over!\n");
cprintf("\n\n\r You Score:%d",score);
cprintf("\n\n\r Press Q To Exit The Game!");
cprintf("\n\n\r Press S To Start The Game Again!");
ch=getch();
if(ch=='q'||ch=='Q')exit(0);
if(ch=='s'||ch=='S')
{DRAWGAME();
GAMEPLAY();
}
}
}
int DRAWGAME()
{int i;
clrscr();
textcolor(CYAN);
for(i=1;i<25;i++)
{gotoxy(1,i);
highvideo();
cprintf("%c",5);
gotoxy(60,i);
highvideo();
cprintf("%c",5);
gotoxy(79,i);
highvideo();
cprintf("%c",5);
}
for(i=1;i<80;i++)
{gotoxy(i,1);
highvideo();
cprintf("%c",5);
gotoxy(i,24);
highvideo();
cprintf("%c",5);
}
gotoxy(65,3);
cprintf("Snake Game! ");
gotoxy(63,7);
cprintf("Made By ZhiPing");
gotoxy(63,11);
cprintf("QQ:1017257402");
gotoxy(63,15);
cprintf("Level:");
gotoxy(63,18);
cprintf("Score:");
gotoxy(72,18);cprintf("%d",score);
gotoxy(68,19);
cprintf("%c",24);
gotoxy(68,23);
cprintf("%c",25);
gotoxy(65,21);
cprintf("%c",27);
gotoxy(71,21);
cprintf("%c",26);
gotoxy(67,21);
cprintf("Esc");
}
int GAMEPLAY()
{int snakex[30],snakey[30],key,direction=4,food=0,foodx,foody,node=2,i;
char ch;
snakex[0]=2;snakex[1]=3;snakey[0]=12;snakey[1]=12;
while(1)
{while(!kbhit())
{textcolor(YELLOW);
if(food==0)
{randomize();
foodx=rand()%59;
foody=rand()%23;
while(foodx<2)foodx++;
while(foody<2)foody++;
food=1;
}
for(i=0;i
{if(i>0)
textcolor(i);
gotoxy(snakex[i],snakey[i]);
highvideo();
cprintf("%c",2); textcolor(YELLOW);
}
delay(5000000000-level*1000);
gotoxy(snakex[node-1],snakey[node-1]);
cprintf("%c",32);
gotoxy(foodx,foody);
highvideo();
cprintf("%c",3);
for(i=node-1;i>0;i--)
{snakex[i]=snakex[i-1];
snakey[i]=snakey[i-1];
}
if(direction==1)snakey[0]--;
if(direction==2)snakey[0]++;
if(direction==3)snakex[0]--;
if(direction==4)snakex[0]++;
for(i=4;i
if(snakex[0]==snakex[i]&&snakey[0]==snakey[i])GAMEOVER();
if(snakex[0]==foodx&&snakey[0]==foody)
{food=0;score+=10;node++;
if(node>30){level++;
for(i=0;i
{gotoxy(snakex[i],snakey[i]);
cprintf("%c",32);
}
node=2;
}
snakex[node-1]=snakex[node-2];
snakey[node-1]=snakey[node-2];
gotoxy(72,18);cprintf("%d",score);
}
gotoxy(72,15);
highvideo();
cprintf("%d",level);
if(snakex[0]==1||snakex[0]==60){GAMEOVER();}
if(snakey[0]==1||snakey[0]==24){GAMEOVER();}
}
key=bioskey(0);
if(key==ESC)
{gotoxy(13,12);
textcolor(GREEN);
highvideo();
cprintf("Are you sure to exit the game!(y/n?):");
ch=getch();
if(ch=='y'||ch=='Y')exit(0);
else {gotoxy(13,12); cprintf(" ");}
}
if(key==LEFT&&direction!=4)direction=3;
if(key==RIGHT&&direction!=3)direction=4;
if(key==UP&&direction!=2)direction=1;
if(key==DOWN&&direction!=1)direction=2;
if(key==S){gotoxy(5,12);
highvideo();
cprintf("Please select the level you want to play(1~55):");
scanf("%d",&level);
gotoxy(5,12);
cprintf(" ");
}
if(key==F5)getch();
}
}
void GAMEHELP()
{int i=1;
textcolor(CYAN);
highvideo();
cprintf("Welcome To This Program,This Program is Called Snake Game!");
cprintf("\n\n\rAnd As We Know This is A Very Old Game,But It Still Worths Playing!");
cprintf("\n\n\rI Just Write It To Improve My Ability For The C Program!");
cprintf("\n\n\rThe Operation Is As follows:");
cprintf("\n\n\rPress The Direction Key To control The Snake,And Press Esc To Exit The Game!");
cprintf("\n\n\rAnd Press F5 To Pause The Game,And Anykey To Start!");
cprintf("\n\n\rWhile You are playing the game,\n\n\rYou can press S to select the game level you want to play!");
cprintf("\n\n\rHope You Will Enjoy The Game!And If You Have Any New Ideas,\n\n\rWilling To Hearing From You!Made By ZhiPing<969370950>");969370950>
printf("\n\n\rPress Anykey To Start The Game Now!Or after 30 seconds auto to run! ");
while(!kbhit()&&i<30)
{gotoxy(40,23);
textcolor(i);
highvideo();
cprintf("%d",i);
sleep(1);
i++;
}
}
void main()
{GAMEHELP();
DRAWGAME();
GAMEPLAY();
}
00分享举报