C语言小游戏源代码

void left_down(int x,int y)
{
int x1,y1;
draw_face(2);
y1=(x-x0)/(m_wh+1);
x1=(y-y0)/(m_wh+1);
if (flag[2][x1][y1]==1)
return ;
if (!flag[1][x1][y1])
draw_box(y1*(m_wh+1)+x0,x1*(m_wh+1)+y0,y1*(m_wh+1)+x0+m_wh,x1*(m_wh+1)+y0+m_wh,2);
}
void left_up(int x,int y)
{
int x1,y1;
draw_face(1);
y1=(x-x0)/(m_wh+1);
x1=(y-y0)/(m_wh+1);
if (flag[2][x1][y1]==1)
return ;
if (!flag[1][x1][y1])
clickl(x1,y1);
}

//扫雷游戏 #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); } }
}

}
if (n==4)
{
setcolor(BLACK);
circle(x-m_wh*3/8,y-m_wh*3/8,m_wh/4);
circle(x+m_wh*3/8,y-m_wh*3/8,m_wh/4);
line(x-m_wh*3/8,y-m_wh*3/8,x+m_wh*3/8,y-m_wh*3/8);
setfillcolor(BLACK);
fillcircle(x-m_wh*3/8,y-m_wh*3/8,m_wh/4);
fillcircle(x+m_wh*3/8,y-m_wh*3/8,m_wh/4);
arc(x-m_wh/2,y,x+m_wh/2,y+m_wh/2,4.14,0);
}

//贪吃蛇 #define N 200 #include <graphics.h> #include
<stdlib.h> #include <dos.h> #define LEFT 0x4b00 #define
RIGHT 0x4d00 #define DOWN 0x5000 #define UP 0x4800 #define ESC 0x011b
int i,key; int score=0; int gamespeed=50000; struct Food { int x; int y;
int yes; }food; struct Snake { int x[N]; int y[N]; int node; int
direction; int life; }snake; void Init(void); void Close(void); void
DrawK(void); void GameOver(void); void GamePlay(void); void
PrScore(void); void main(void) { Init(); DrawK(); GamePlay(); Close(); }
void Init(void) { int gd=DETECT,gm; initgraph(&gd,&gm,”D://tc20//BGI”);
cleardevice(); } void DrawK(void) { setcolor(11);
setlinestyle(SOLID_LINE,0,THICK_WIDTH); for(i=50;i<=600;i+=10) {
rectangle(i,40,i+10,49); rectangle(i,451,i+10,460); }
for(i=40;i<=450;i+=10) { rectangle(50,i,59,i+10);
rectangle(601,i,610,i+10); } } void GamePlay(void) { randomize();
food.yes=1; snake.life=0; snake.direction=1;
snake.x[0]=100;snake.y[0]=100; snake.x[1]=110;snake.y[1]=100;
snake.node=2; PrScore(); while(1) { while(!kbhit()) { if(food.yes==1) {
food.x=rand()%400+60; food.y=rand()%350+60; while(food.x%10!=0)
food.x++; while(food.y%10!=0) food.y++; food.yes=0; } if(food.yes==0) {
setcolor(GREEN); rectangle(food.x,food.y,food.x+10,food.y-10); }
for(i=snake.node-1;i>0;i–) { snake.x[i]=snake.x[i-1];
snake.y[i]=snake.y[i-1]; } switch(snake.direction) { case
1:snake.x[0]+=10;break; case 2: snake.x[0]-=10;break; case 3:
snake.y[0]-=10;break; case 4: snake.y[0]+=10;break; }
for(i=3;i<snake.node;i++) {
if(snake.x[i]==snake.x[0]&&snake.y[i]==snake.y[0]) { GameOver();
snake.life=1; break; } }
if(snake.x[0]<55||snake.x[0]>595||snake.y[0]<55||
snake.y[0]>455) { GameOver(); snake.life=1; } if(snake.life==1)
break; if(snake.x[0]==food.x&&snake.y[0]==food.y) { setcolor(0);
rectangle(food.x,food.y,food.x+10,food.y-10);
snake.x[snake.node]=-20;snake.y[snake.node]=-20; snake.node++;
food.yes=1; score+=10; PrScore(); } setcolor(4);
for(i=0;i<snake.node;i++)
rectangle(snake.x[i],snake.y[i],snake.x[i]+10, snake.y[i]-10);
delay(gamespeed); setcolor(0);
rectangle(snake.x[snake.node-1],snake.y[snake.node-1],
snake.x[snake.node-1]+10,snake.y[snake.node-1]-10); }
if(snake.life==1) break; key=bioskey(0); if(key==ESC) break; else
if(key==UP&&snake.direction!=4) snake.direction=3; else
if(key==RIGHT&&snake.direction!=2) snake.direction=1; else
if(key==LEFT&&snake.direction!=1) snake.direction=2; else
if(key==DOWN&&snake.direction!=3) snake.direction=4; } } void
GameOver(void) { cleardevice(); PrScore(); setcolor(RED);
settextstyle(0,0,4); outtextxy(200,200,”GAME OVER”); getch(); } void
PrScore(void) { char str[10]; setfillstyle(SOLID_FILL,YELLOW);
bar(50,15,220,35); setcolor(6); settextstyle(0,0,2);
sprintf(str,”score:%d”,score); outtextxy(55,20,str); } void Close(void)
{ getch(); closegraph(); }

void draw_flag(int i,int j)
{
int x,y;
x=j*(m_wh+1)+x0;
y=i*(m_wh+1)+y0;
setcolor(RED);
setfillcolor(RED);
line(x+m_wh/4,y+m_wh/4,x+m_wh/4,y+m_wh-1);
line(x+m_wh/4,y+m_wh/4,x+m_wh/4*3,y+m_wh/3*2);
line(x+m_wh/4*3,y+m_wh/3*2,x+m_wh/4,y+m_wh/3*2);
floodfill(x+m_wh/4+1,y+m_wh/3*2-1,RED);
}
int set_mine()
{
int i,j,count=0;
if (minenum > m_row*m_col)
return FALSE;
srand(time(NULL));
while (count < minenum)
{
i=rand()%m_澳门新葡亰,row;
j=rand()%m_col;
if (p[i][j]!=-1)
{
p[i][j]=-1;
count++;
}
}
RoundNum();
return TRUE;
}


void char_int(int number,char *str)
{
int n,m,k;
if (number < 0)
{
char_int(-number,str);
str[4]=str[3];
str[3]=str[2];
str[2]=str[1];
str[1]=str[0];
str[0]=’-‘;
}
else if (number ==0)
{
str[0]=’0′;
str[1]=’’;
}
else if (number < 10)
{
str[0]=’0′;
str[1]=number+’0′;
str[2]=’’;
}
else if (number < 100)
{
n=number/10;
m=number%10;
str[0]=’0′;
str[1]=n+’0′;
str[2]=m+’0′;
str[3]=’’;
}
else if(number < 1000)
{
n=number/100;
m=number/10%10;
k=number%10;
str[0]=n+’0′;
str[1]=m+’0′;
str[2]=k+’0′;
str[3]=’’;
}
}

//鼠标控制 #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(); }

void fun(int i,int j)
{
if (p[i][j]!=0)
{
out_num(i,j);
flag[1][i][j]=1;
}
else
{
out_num(i,j);
flag[1][i][j]=1;
if (i-1 >=0 && flag[1][i-1][j]==0)
fun(i-1,j);
if (j-1 >=0 && flag[1][i][j-1]==0)
fun(i,j-1);
if (i+1 < m_row && flag[1][i+1][j]==0)
fun(i+1,j);
if (j+1 < m_col && flag[1][i][j+1]==0)
fun(i,j+1);
if (i-1 >=0 && j-1>=0 && flag[1][i-1][j-1]==0)
fun(i-1,j-1);
if (i+1<m_row && j-1 >=0 && flag[1][i+1][j-1]==0)
fun(i+1,j-1);
if (i-1>=0 && j+1 <m_col && flag[1][i-1][j+1]==0)
fun(i-1,j+1);
if (i+1<m_row && j+1<m_col && flag[1][i+1][j+1]==0)
fun(i+1,j+1);
}
}

 

int draw_area()
{
int i,j,x,y;
if (!count_x0_y0())
return FALSE;
if (set_mine())
{
draw_back_rectangle();
draw_face(1);
y=y0;
for (i=0;i<m_row;i++)
{
x=x0;
for (j=0;j<m_col;j++)
{

 

void windows()
{
int gdriver,gmode;
initgraph(&gdriver,&gmode,””);
MOUSEMSG m;
rectangle(200,70,400,140);
outtextxy(275,100,”初级”);
rectangle(200,160,400,230);
outtextxy(275,190,”中级”);
rectangle(200,250,400,320);
outtextxy(275,280,”高级”);
rectangle(200,340,400,410);
outtextxy(275,360,”自定义”);
while (1)
{
m=GetMouseMsg();
if (m.uMsg==WM_LBUTTONDOWN)
{
if (m.x>=200 && m.x < 400 && m.y >= 70 && m.y < 140)
{
m_row=9;
m_col=9;
minenum=10;
m_wh=25;
return ;
}
if (m.x>=200 && m.x <=400 && m.y >= 160 && m.y  <230)
{
m_row=16;
m_col=16;
m_wh=20;
minenum=30;
return;
}
if (m.x>=200 && m.x <=400 && m.y >= 250 && m.y <320)
{
m_row=16;
m_col=30;
minenum=99;
m_wh=18;
return;
}
if (m.x>=200 && m.x <=400 && m.y >= 340 && m.y < 410)
{
define();
return;
}
}
}
}
void write()
{
FILE *fp;
char name[20];
system(“cls”);
if ((fp=fopen(“排行榜”,”a”))==NULL)
{
printf(“打开失败”);
exit(0);
}
printf(“输入你的大名:”);
scanf(“%s”,name);
fprintf(fp,”姓名:  %sn”,name);
fprintf(fp,”成绩:  %dn”,start/1000);
printf(“保存成功!”);
exit(0);
fclose(fp);
}
void read()
{
FILE *fp;
char name[20],str[8];
int n;
if ((fp=fopen(“排行榜”,”r”))==NULL)
{
printf(“打开失败”);
exit(0);
}
system(“cls”);
while(!feof(fp))
{
fscanf(fp,”姓名:  %sn”,name);
fscanf(fp,”成绩:  %dn”,&n);
printf(“姓名:  %sn”,name);
printf(“成绩:  %dn”,n);
}
fclose(fp);
}
int init_system()
{
int i,j,k;
m_maxx=getmaxx();
m_maxy=getmaxy();
countMine=0;
countTrueMine=0;
p=(int **)malloc(sizeof(int *)*m_row);
if (!p)
return FALSE;
for (i=0;i<m_row;i++)
{
p[i]=(int *)malloc(sizeof(int)*m_col);
if (!p[i])
return FALSE;
}
for (i=0;i<m_row;i++)
for (j=0;j<m_col;j++)
{
p[i][j]=0;
}
flag=(int ***)malloc(sizeof(int **)*3*m_row*m_col);
if (!flag)
return FALSE;
for (i=0;i<3;i++)
{
flag[i]=(int **)malloc(sizeof(int *)*m_row);
if (!flag[i])
return FALSE;
}
for (i=0;i<3;i++)
{
for (j=0;j<m_row;j++)
{
flag[i][j]=(int *)malloc(sizeof(int)*m_col);
if (!flag[i][j])
return FALSE;
}
}
for (i=0;i<3;i++)
for (j=0;j<m_row;j++)
for (k=0;k<m_col;k++)
{
flag[i][j][k]=0;
}

 

void clickl(int i,int j)
{
if (p[i][j]==-1)
{
game_over(i,j);
}
else if (p[i][j]==0)
{
fun(i,j);
}
else
{
out_num(i,j);
flag[1][i][j]=1;
}
}

 在此提供C语言小游戏源码,包括扫雷游戏,贪吃蛇游戏,时钟等。

void right_down(int x,int y)
{
int x1,y1;
draw_face(2);
y1=(x-x0)/(m_wh+1);
x1=(y-y0)/(m_wh+1);
if (!flag[2][x1][y1] && !flag[1][x1][y1] &&
p[x1][y1]==-1)
{
draw_flag(x1,y1);
countMine++;
countTrueMine++;
}
else if (!flag[2][x1][y1] && !flag[1][x1][y1] &&
p[x1][y1]!=-1)
{
draw_flag(x1,y1);
countMine++;
}
if (flag[2][x1][y1]==1 && !flag[1][x1][y1] &&
p[x1][y1]==-1)
{
draw_box(y1*(m_wh+1)+x0,x1*(m_wh+1)+y0,y1*(m_wh+1)+x0+m_wh,x1*(m_wh+1)+y0+m_wh,1);
countMine–;
countTrueMine–;
flag[2][x1][y1]=0;
}
else if(flag[2][x1][y1]==1 && !flag[1][x1][y1] &&
p[x1][y1]!=-1)
{
draw_box(y1*(m_wh+1)+x0,x1*(m_wh+1)+y0,y1*(m_wh+1)+x0+m_wh,x1*(m_wh+1)+y0+m_wh,1);
countMine–;
flag[2][x1][y1]=0;
}
else
flag[2][x1][y1]=1;
}
void middle_up_draw_box(int x1,int y1)
{
int i,j;
for (i=-1;i<=1;i++)
for (j=-1;j<=1;j++)
{
if (x1+i >=0 && x1+i<m_row && y1+j >=0 && y1+j<m_col )
{
if (!flag[1][x1+i][y1+j] && !flag[2][x1+i][y1+j])
draw_box((j+y1)*(m_wh+1)+x0,(i+x1)*(m_wh+1)+y0,+(j+y1)*(m_wh+1)+x0+m_wh,(i+x1)*(m_wh+1)+y0+m_wh,1);
}
}
}
void middle_up(int x,int y)
{
int x1,y1,countmine=0,countruemine=0,i,j;
draw_face(1);
y1=(x-x0)/(m_wh+1);
x1=(y-y0)/(m_wh+1);
if (flag[1][x1][y1]==0)
{
middle_up_draw_box(x1, y1);
return;
}
for (i=-1;i<=1;i++)
for (j=-1;j<=1;j++)
{
if (j!=0 || i!=0) 
{
if (x1+i >=0 && x1+i<m_row && y1+j >=0 && y1+j<m_col)
{
if (flag[2][x1+i][y1+j]==1)
{
countmine++;
if (p[x1+i][y1+j]==-1)
countruemine++;
}
}
}
}
if (countmine==p[x1][y1] && countruemine==p[x1][y1])
{
for (i=-1;i<=1;i++)
for (j=-1;j<=1;j++)
{
if (j!=0 || i!=0) 
{
if (x1+i >=0 && x1+i<m_row && y1+j >=0 && y1+j<m_col )
{
if (!flag[1][x1+i][y1+j] && !flag[2][x1+i][y1+j])
clickl(x1+i,y1+j);
}
}
}
}
else
{
middle_up_draw_box(x1, y1);
}
}
void middle_down(int x,int y)
{
int x1,y1,i,j;
draw_face(2);
y1=(x-x0)/(m_wh+1);
x1=(y-y0)/(m_wh+1);
for (i=-1;i<=1;i++)
for (j=-1;j<=1;j++)
{
if (i!=0 || j!=0)
{
if (x1+i >=0 && x1+i<m_row && y1+j >=0 && y1+j<m_col )
{
if (!flag[1][x1+i][y1+j] && !flag[2][x1+i][y1+j])
draw_box((j+y1)*(m_wh+1)+x0,(i+x1)*(m_wh+1)+y0,+(j+y1)*(m_wh+1)+x0+m_wh,(i+x1)*(m_wh+1)+y0+m_wh,2);
}
}
}
}
void found_mine()
{
FlushMouseMsgBuffer();
MOUSEMSG m;
int x, y,x1,y1,x2,y2;
char str[4];
x=x0+m_col*(m_wh+1)/2;
y=y0-1.5*m_wh;
x1=x0-m_wh/2;
y1=y0-m_wh*3;
x2=x0+m_col*(m_wh+1)+m_wh/2;
y2=y0+m_row*(m_wh+1)+m_wh/2;
while (1)
{
out_mine_num();
start=clock()-finish;
char_int(start/1000,str);
setcolor(RED);
outtextxy(x2-2.5*m_wh,y0-1.75*m_wh,str);
win();
if (MouseHit()==true)
{
m=GetMouseMsg();
start=clock();
outtextxy(x2-2.5*m_wh,y0-1.75*m_wh,str);
if (m.x>=x0 && m.x<x0+m_col*(m_wh+1) && m.y>=y0 &&
m.y<y0+m_row*(m_wh+1))
{
switch(m.uMsg)
{
case WM_LBUTTONDOWN:
left_down(m.x,m.y);
break;
case  WM_LBUTTONUP:
left_up(m.x,m.y);
break;
case WM_RBUTTONDOWN:
right_down(m.x,m.y);
break;
case WM_RBUTTONUP :
draw_face(1);
break;
case WM_MBUTTONDOWN:
middle_down(m.x,m.y);
break;
case WM_MBUTTONUP:
middle_up(m.x,m.y);
break;
}
}
if (m.x>=x-m_wh && m.x<=x+m_wh && m.y>=y-m_wh &&
m.y<=y+m_wh)
{
switch (m.uMsg)
{
case WM_LBUTTONDOWN:
draw_face(2);
break;
case  WM_LBUTTONUP:
draw_face(1);
again_game();
break;
}
}
}
}
}
void again_game()
{
int x,y;
FlushMouseMsgBuffer();
MOUSEMSG m;
x=x0+m_col*(m_wh+1)/2;
y=y0-1.5*m_wh;
while (1)
{
m=GetMouseMsg();
switch (m.uMsg)
{
case WM_LBUTTONDOWN:
if (m.x>=x-m_wh && m.x<=x+m_wh && m.y>=y-m_wh &&
m.y<=y+m_wh)
draw_face(2);
break;
case WM_LBUTTONUP:
if (m.x>=x-m_wh && m.x<=x+m_wh && m.y>=y-m_wh &&
m.y<=y+m_wh)
{
cleardevice();
close_system();
init_system();
finish=clock();
draw_face(1);
draw_area();
found_mine();
}
break;
}
}

//时钟 #include<graphics.h> #include<math.h>
#include<dos.h> #define pi 3.1415926 #define X(a,b,c)
x=a*cos(b*c*pi/180-pi/2)+300; #define Y(a,b,c)
y=a*sin(b*c*pi/180-pi/2)+240; #define d(a,b,c)
X(a,b,c);Y(a,b,c);line(300,240,x,y) void init() {int i,l,x1,x2,y1,y2;
setbkcolor(0); circle(300,240,200); circle(300,240,205);
circle(300,240,5); for(i=0;i<60;i++) {if(i%5==0) l=15; else l=5;
x1=200*cos(i*6*pi/180)+300; y1=200*sin(i*6*pi/180)+240;
x2=(200-l)*cos(i*6*pi/180)+300; y2=(200-l)*sin(i*6*pi/180)+240;
line(x1,y1,x2,y2); } } main() { int x,y; int gd=VGA,gm=2; unsigned char
h,m,s; struct time t[1]; initgraph(&gd,&gm,”d://tc20//BGI”); init();
setwritemode(1); gettime(t); h=t[0].ti_hour; m=t[0].ti_min;
s=t[0].ti_sec; setcolor(7); d(150,h,30); setcolor(14); d(170,m,6);
setcolor(4); d(190,s,6); while(!kbhit()) {while(t[0].ti_sec==s)
gettime(t); sound(400); delay(70); sound(200); delay(30); nosound();
setcolor(4); d(190,s,6); s=t[0].ti_sec; d(190,s,6); if
(t[0].ti_min!=m) { setcolor(14); d(170,m,6); m=t[0].ti_min;
d(170,m,6); } if (t[0].ti_hour!=h) { setcolor(7); d(150,h,30);
h=t[0].ti_hour; d(150,h,30); sound(1000); delay(240); nosound();
delay(140); sound(2000); delay(240); nosound(); } } getch();
closegraph(); }

void win()
{
int i,j;
if (countMine==minenum && countTrueMine==minenum)
{
draw_face(4);
for (i=0;i<m_row;i++)
for (j=0;j<m_col;j++)
{
if (p[i][j]!=-1 && !flag[1][i][j])
out_num(i,j);
}
setcolor(WHITE);
settextstyle(16,0,_T(“宋体”));
outtextxy(0,0,”你胜利了!”);
Sleep(1000);
closegraph();
write();
again_game();
}
}

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

}
void define()
{
int gdriver=DETECT,gmode;
closegraph();
m_row=16;
m_col=30;
printf(“nn雷数:”);
scanf(“%d”,&minenum);
m_wh=18;
fflush(stdin);
initgraph(&gdriver,&gmode,””);
}

return TRUE;
}
void close_system()
{
int i,j;
for (i=0;i<m_row;i++)
free(p[i]);
free(p);
for (i=0;i<3;i++)
for (j=0;j<m_row;j++)
free(flag[i][j]);
for (i=0;i<3;i++)
free(flag[i]);
free(flag);
}

网站地图xml地图