C语言小游戏源代码,自身写的贪吃蛇代码

运行时只要把红色部分改为自己电脑上TC目录的BGI分目录即可。

自己写的贪吃蛇代码,找不出bug,运行出来是黑屏,请大家帮忙看一下
#include
#include
#include


#include

//扫雷游戏 #include <graphics.h> #include <stdlib.h>
#include <dos.h> #define LEFTPRESS 0xff01 #define LEFTCLICK
0xff10 #define LEFTDRAG 0xff19 #define MOUSEMOVE 0xff08 struct { int
num; int roundnum; int flag; }Mine[10][10]; int gameAGAIN=0; int
gamePLAY=0; int mineNUM; char randmineNUM[3]; int Keystate; int
MouseExist; int MouseButton; int MouseX; int MouseY; void Init(void);
void MouseOn(void); void MouseOff(void); void MouseSetXY(int,int); int
LeftPress(void); int RightPress(void); void MouseGetXY(void); void
Control(void); void GameBegain(void); void DrawSmile(void); void
DrawRedflag(int,int); void DrawEmpty(int,int,int,int); void
GameOver(void); void GameWin(void); int MineStatistics(int,int); int
ShowWhite(int,int); void GamePlay(void); void Close(void); void
main(void) { Init(); Control(); Close(); } void Init(void) { int
gd=DETECT,gm; initgraph(&gd,&gm,"D://tc20//BGI"); } void Close(void) {
closegraph(); } void MouseOn(void) { _AX=0x01; geninterrupt(0x33); }
void MouseOff(void) { _AX=0x02; geninterrupt(0x33); } void
MouseSetXY(int x,int y) { _CX=x; _DX=y; _AX=0x04; geninterrupt(0x33);
} int LeftPress(void) { _AX=0x03; geninterrupt(0x33); return(_BX&1); }
int RightPress(void) { _AX=0x03; geninterrupt(0x33); return(_BX&2); }
void MouseGetXY(void) { _AX=0x03; geninterrupt(0x33); MouseX=_CX;
MouseY=_DX; } void Control(void) { int gameFLAG=1; while(1) {
if(gameFLAG) { GameBegain(); GamePlay(); if(gameAGAIN==1) { gameAGAIN=0;
continue; } } MouseOn(); gameFLAG=0; if(LeftPress()) { MouseGetXY();
if(MouseX>280&&MouseX<300&&MouseY>65&&MouseY<85) {
gameFLAG=1; continue; } } if(kbhit()) break; } MouseOff(); } void
DrawSmile(void) { setfillstyle(SOLID_FILL,YELLOW);
fillellipse(290,75,10,10); setcolor(YELLOW);
setfillstyle(SOLID_FILL,BLACK); fillellipse(285,75,2,2);
fillellipse(295,75,2,2); setcolor(BLACK); bar(287,80,293,81); } void
DrawRedflag(int i,int j) { setcolor(7); setfillstyle(SOLID_FILL,RED);
bar(198+j*20,95+i*20,198+j*20+5,95+i*20+5); setcolor(BLACK);
line(198+j*20,95+i*20,198+j*20,95+i*20+10); } void DrawEmpty(int
i,int j,int mode,int color) { setcolor(color);
setfillstyle(SOLID_FILL,color); if(mode==0)
bar(200+j*20-8,100+i*20-8,200+j*20+8,100+i*20+8); else if(mode==1)
bar(200+j*20-7,100+i*20-7,200+j*20+7,100+i*20+7); } void
GameBegain(void) { int i,j; cleardevice(); if(gamePLAY!=1) {
MouseSetXY(290,70); MouseX=290; MouseY=70; } gamePLAY=1; mineNUM=0;
setfillstyle(SOLID_FILL,7); bar(190,60,390,290); for(i=0;i<10;i++)
for(j=0;j<10;j++) DrawEmpty(i,j,0,8); setcolor(7); DrawSmile();
randomize();//__page_break__ for(i=0;i<10;i++)
for(j=0;j<10;j++) { Mine[i][j].num=random(8);
if(Mine[i][j].num==1) mineNUM++; else Mine[i][j].num=2;
Mine[i][j].flag=0; } sprintf(randmineNUM,"%d",mineNUM); setcolor(1);
settextstyle(0,0,2); outtextxy(210,70,randmineNUM); mineNUM=100-mineNUM;
MouseOn(); } void GameOver(void) { int i,j; setcolor(0);
for(i=0;i<10;i++) for(j=0;j<10;j++) if(Mine[i][j].num==1) {
DrawEmpty(i,j,0,RED); setfillstyle(SOLID_FILL,BLACK);
fillellipse(200+j*20,100+i*20,7,7); } } void GameWin(void) {
setcolor(11); settextstyle(0,0,2); outtextxy(230,30,"YOU WIN!"); } int
MineStatistics(int i,int j) { int nNUM=0; if(i==0&&j==0) {
if(Mine[0][1].num==1) nNUM++; if(Mine[1][0].num==1) nNUM++;
if(Mine[1][1].num==1) nNUM++; } else if(i==0&&j==9) {
if(Mine[0][8].num==1) nNUM++; if(Mine[1][9].num==1) nNUM++;
if(Mine[1][8].num==1) nNUM++; } else if(i==9&&j==0) {
if(Mine[8][0].num==1) nNUM++; if(Mine[9][1].num==1) nNUM++;
if(Mine[8][1].num==1) nNUM++; } else if(i==9&&j==9) {
if(Mine[9][8].num==1) nNUM++; if(Mine[8][9].num==1) nNUM++;
if(Mine[8][8].num==1) nNUM++; } else if(j==0) {
if(Mine[i][j+1].num==1) nNUM++; if(Mine[i+1][j].num==1) nNUM++;
if(Mine[i-1][j].num==1) nNUM++; if(Mine[i-1][j+1].num==1)
nNUM++; if(Mine[i+1][j+1].num==1) nNUM++; } else if(j==9) {
if(Mine[i][j-1].num==1) nNUM++; if(Mine[i+1][j].num==1) nNUM++;
if(Mine[i-1][j].num==1) nNUM++; if(Mine[i-1][j-1].num==1)
nNUM++; if(Mine[i+1][j-1].num==1) nNUM++; } else if(i==0) {
if(Mine[i+1][j].num==1) nNUM++; if(Mine[i][j-1].num==1) nNUM++;
if(Mine[i][j+1].num==1) nNUM++; if(Mine[i+1][j-1].num==1)
nNUM++; if(Mine[i+1][j+1].num==1) nNUM++; } else if(i==9) {
if(Mine[i-1][j].num==1) nNUM++; if(Mine[i][j-1].num==1) nNUM++;
if(Mine[i][j+1].num==1) nNUM++; if(Mine[i-1][j-1].num==1)
nNUM++; if(Mine[i-1][j+1].num==1) nNUM++; } else {
if(Mine[i-1][j].num==1) nNUM++; if(Mine[i-1][j+1].num==1)
nNUM++; if(Mine[i][j+1].num==1) nNUM++;
if(Mine[i+1][j+1].num==1) nNUM++; if(Mine[i+1][j].num==1)
nNUM++; if(Mine[i+1][j-1].num==1) nNUM++;
if(Mine[i][j-1].num==1) nNUM++; if(Mine[i-1][j-1].num==1)
nNUM++; }//__page_break__ return (nNUM); } int ShowWhite(int i,int
j) { if(Mine[i][j].flag==1||Mine[i][j].num==0) return;
mineNUM--; if(Mine[i][j].roundnum==0&&Mine[i][j].num!=1) {
DrawEmpty(i,j,1,7); Mine[i][j].num=0; } else
if(Mine[i][j].roundnum!=0) { DrawEmpty(i,j,0,8);
sprintf(randmineNUM,"%d",Mine[i][j].roundnum); setcolor(RED);
outtextxy(195+j*20,95+i*20,randmineNUM); Mine[i][j].num=0; return
; } if(i!=0&&Mine[i-1][j].num!=1) ShowWhite(i-1,j);
if(i!=0&&j!=9&&Mine[i-1][j+1].num!=1) ShowWhite(i-1,j+1);
if(j!=9&&Mine[i][j+1].num!=1) ShowWhite(i,j+1);
if(j!=9&&i!=9&&Mine[i+1][j+1].num!=1) ShowWhite(i+1,j+1);
if(i!=9&&Mine[i+1][j].num!=1) ShowWhite(i+1,j);
if(i!=9&&j!=0&&Mine[i+1][j-1].num!=1) ShowWhite(i+1,j-1);
if(j!=0&&Mine[i][j-1].num!=1) ShowWhite(i,j-1);
if(i!=0&&j!=0&&Mine[i-1][j-1].num!=1) ShowWhite(i-1,j-1); } void
GamePlay(void) { int i,j,Num; for(i=0;i<10;i++) for(j=0;j<10;j++)
Mine[i][j].roundnum=MineStatistics(i,j); while(!kbhit()) {
if(LeftPress()) { MouseGetXY();
if(MouseX>280&&MouseX<300&&MouseY>65&&MouseY<85) {
MouseOff(); gameAGAIN=1; break; }
if(MouseX>190&&MouseX<390&&MouseY>90&&MouseY<290) {
j=(MouseX-190)/20; i=(MouseY-90)/20; if(Mine[i][j].flag==1)
continue; if(Mine[i][j].num!=0) { if(Mine[i][j].num==1) {
MouseOff(); GameOver(); break; } else { MouseOff();
Num=MineStatistics(i,j); if(Num==0) ShowWhite(i,j); else {
sprintf(randmineNUM,"%d",Num); setcolor(RED);
outtextxy(195+j*20,95+i*20,randmineNUM); mineNUM--; } MouseOn();
Mine[i][j].num=0; if(mineNUM<1) { GameWin(); break; } } } } }
if(RightPress()) { MouseGetXY();
if(MouseX>190&&MouseX<390&&MouseY>90&&MouseY<290) {
j=(MouseX-190)/20; i=(MouseY-90)/20; MouseOff();
if(Mine[i][j].flag==0&&Mine[i][j].num!=0) { DrawRedflag(i,j);
Mine[i][j].flag=1; } else if(Mine[i][j].flag==1) {
DrawEmpty(i,j,0,8); Mine[i][j].flag=0; } } MouseOn(); sleep(1); } }
}

#include
#include
#define N 4
struct snake
{ int x;
int y;
}snk[100];
struct food
{ int x;
int y;
}food;
void snake//初始化蛇身
{ int i=0;
srand(time;
snk[i].x=rand()%50+10;
snk[i].y=rand()%30+10;
switch
{ case 0:
for(i=1;i {snk[i].x=snk[i-1].x+1;
snk[i].y=snk[i-1].y;}break;
case 1:
for(i=1;i {snk[i].x=snk[i-1].x-1;
snk[i].y=snk[i-1].y;}break;
case 2:
for(i=1;i {snk[i].x=snk[i-1].x;
snk[i].y=snk[i-1].y+1;}break;
case 3:
for(i=1;i {snk[i].x=snk[i-1].x;
snk[i].y=snk[i-1].y-1;}break;
}
}
void delay//调节速度
{ time_t start_time, cur_time;
time(&start_time);
do { time(&cur_time); }
while((cur_time - start_time) }
void move//移动以及转向
{ char direct;
cin>>direct;
struct snake head={snk[0].x,snk[0].y};
if(direct==NULL)
{ snk[0].x=2*snk[1].x-snk[0].x;
snk[0].y=2*snk[1].y-snk[0].y;
}
if(direct=='W')
{ snk[0].x=3*snk[1].x-2*snk[0].x;
snk[0].y=3*snk[1].y-2*snk[0].y;
for(int i=len-1;i>1;i--)
{ snk[i].x=snk[i-1].x;
snk[i].x=snk[i-1].x;
}
snk[1].x=head.x;
snk[1].y=head.y;
}
if(direct=='A'||direct=='D')
{ snk[0].x=snk[0].x+snk[0].y-snk[1].y;
snk[0].y=snk[0].y+snk[0].x-snk[1].x;
}
for(int i=len-1;i>1;i--)
{ snk[i].x=snk[i-1].x;
snk[i].x=snk[i-1].x;
}
snk[1].x=head.x;
snk[1].y=head.y;
}
void FOOD//食物出现如出现在蛇身上再次生成
{ while
{ int flag=0;
srand(time;
food.x=rand()%50+10;
food.y=rand()%30+10;
for(int i=0;i if(food.x==snk[i].x&&food.y==snk[i].y)
flag=1;
if
break;
}
}
void print
{ int i,j,t;
for(i=0;i cout for(j=1;j { cout for(i=1;i for(t=0;t {
if(snk[t].x==i&&snk[t].y==j)
cout else
cout }
cout }
for(i=0;i cout}
void main()
{ int num;
coutcin>>num;
if
{ int static len=N,score=0;
snake;
FOOD;
while
{

 

move;
delay;
if(snk[0].x==food.x&&snk[0].y==food.y)
{
len++;
score++;
FOOD;
}
if(snk[0].x==0||snk[0].x==69||snk[0].y==0||snk[0].y==49)
{
cout<<"game over"<<endl;
cout<<score;
break;
}
print;
Sleep;//_sleep();
system;
}
}
else
exit;
}

//鼠标控制 #include <dos.h> #include <bios.h> #include
<malloc.h> #include <stdlib.h> #include <stdio.h>
#include "graphics.h" #define R 15 void initgr(void) { int gd =
DETECT, gm = 0; initgraph(&gd, &gm, "D://TC20//BGI"); } void
getmouse(int *x,int *y,int *key) { union REGS inregs,outregs;
inregs.x.ax=3; int86(0x33,&inregs,&outregs); *x=outregs.x.cx;
*y=outregs.x.dx; *key=outregs.x.bx; } void visbilemouse() { union REGS
inregs,outregs; inregs.x.ax=0x01; int86(0x33,&inregs,&outregs); } void
mouse(int *x,int *y,int *z) { int a=0,b=0,c=0,a_old=0,b_old=0; int
*ball; ball=malloc(imagesize(a,b,a+R,b+R)); getimage(a,b,a+R,b+R,ball);
while(c==0) { getmouse(&a,&b,&c); if(a<0) a=0; if(b<0) b=0;
if(a>getmaxx()-R) a=getmaxx()-R; if(b>getmaxy()-R) b=getmaxy()-R;
if(a!=a_old || b!=b_old) { putimage(a_old,b_old,ball,0);
getimage(a,b,a+R,b+R,ball); setcolor(GREEN); setlinestyle(0,0,1);
line(a,b,a+R,b+R/2); line(a,b,a+R/2,b+R); line(a+R,b+R/2,a+R/2,b+R);
line(a+R*3/4,b+R*3/4,a+R,b+R); } a_old=a;b_old=b; }
*x=a;*y=b;*z=c; putimage(a,b,ball,0); free(ball); } void main() { int
x,y,z; initgr(); visbilemouse(); do { setcolor(WHITE);
rectangle(175,8,220,25); outtextxy(180,10,"exit"); mouse(&x,&y,&z);
if(z==1) { setlinestyle(0,0,1); setcolor(12); circle(x,y,1); } if(z==2)
{setfillstyle(1,BLACK); bar(0,0,getmaxx(),getmaxy()); } }while(x<175
|| x>220 || y<8 || y>25); closegraph(); }

相关文章

Comment ()
评论是一种美德,说点什么吧,否则我会恨你的。。。