C语言小游戏的源代码
在此提供C语言小游戏源码,包括扫雷游戏,贪吃蛇游戏,时钟等。
运行时只要把红色部分改为自己电脑上TC目录的BGI分目录即可。
//扫雷游戏
include
include
include
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();
}//代码效果参考:http://www.zidongmutanji.com/zsjx/333697.html
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;
void MouseSetXY(int x,int y)
_CX=x;
_DX=y;
_AX=0x04;
int LeftPress(void)
_AX=0x03;
return(_BX&1);
int RightPress(void)
return(_BX&2);
void MouseGetXY(void)
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; 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); 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); DrawSmile(); randomize();//__page_break__ Mine[i][j].num=random(8); if(Mine[i][j].num==1) mineNUM++; 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; void GameOver(void) setcolor(0); DrawEmpty(i,j,0,RED); fillellipse(200+j*20,100+i*20,7,7); void GameWin(void) setcolor(11); 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) if(Mine[1][1].num==1) if(i==0&&j==9) if(Mine[0][8].num==1) if(Mine[1][9].num==1) if(Mine[1][8].num==1) if(i==9&&j==0) if(Mine[8][0].num==1) if(Mine[9][1].num==1) if(Mine[8][1].num==1) if(i==9&&j==9) if(Mine[9][8].num==1) if(Mine[8][9].num==1) if(Mine[8][8].num==1) else if(j==0) if(Mine[i][j+1].num==1) if(Mine[i+1][j].num==1) if(Mine[i-1][j].num==1) if(Mine[i-1][j+1].num==1) if(Mine[i+1][j+1].num==1) else if(j==9) if(Mine[i][j-1].num==1) if(Mine[i-1][j-1].num==1) if(Mine[i+1][j-1].num==1) else if(i==0) else if(i==9) }//__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; if(Mine[i][j].roundnum!=0) sprintf(randmineNUM,"%d",Mine[i][j].roundnum); setcolor(RED); outtextxy(195+j*20,95+i*20,randmineNUM); 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; Mine[i][j].roundnum=MineStatistics(i,j); while(!kbhit()) gameAGAIN=1; if(MouseX>190&&MouseX<390&&MouseY>90&&MouseY<290)
j=(MouseX-190)/20;
i=(MouseY-90)/20;
if(Mine[i][j].flag==1)
if(Mine[i][j].num!=0)
GameOver();
Num=MineStatistics(i,j);
if(Num==0)
ShowWhite(i,j);
sprintf(randmineNUM,"%d",Num);
if(mineNUM<1)
GameWin();
if(RightPress())
if(Mine[i][j].flag==0&&Mine[i][j].num!=0)
DrawRedflag(i,j);
Mine[i][j].flag=1;
sleep(1);
//时钟
//代码效果参考:http://www.zidongmutanji.com/zsjx/19327.html
include
include
include
define pi 3.1415926
define X(a,b,c) x=acos(bc*pi/180-pi/2)+300;
define Y(a,b,c) y=asin(bc*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=200cos(i6pi/180)+300;
y1=200sin(i6pi/180)+240;
x2=(200-l)cos(i6pi/180)+300;
y2=(200-l)sin(i6pi/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;
d(150,h,30);
setcolor(14);
d(170,m,6);
setcolor(4);
d(190,s,6);
{while(t[0].ti_sec==s)
sound(400);
delay(70);
sound(200);
delay(30);
nosound();
if (t[0].ti_min!=m)
if (t[0].ti_hour!=h)
{ setcolor(7);
sound(1000);
delay(240);
delay(140);
sound(2000);
getch();
//贪吃蛇
define N 200
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 DrawK(void);
void PrScore(void);
DrawK();
void DrawK(void)
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);
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();
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;i595||snake.y[0]<55|| snake.y[0]>455)
if(snake.life==1)
if(snake.x[0]==food.x&&snake.y[0]==food.y)
snake.x[snake.node]=-20;snake.y[snake.node]=-20;
snake.node++;
score+=10;
//代码效果参考:http://www.zidongmutanji.com/bxxx/264068.html
for(i=0;i<snake.node;i++)
rectangle(snake.x[i],snake.y[i],snake.x[i]+10,
snake.y[i]-10);
delay(gamespeed);
rectangle(snake.x[snake.node-1],snake.y[snake.node-1],
snake.x[snake.node-1]+10,snake.y[snake.node-1]-10);
key=bioskey(0);
if(key==ESC)
if(key==UP&&snake.direction!=4)
snake.direction=3;
if(key==RIGHT&&snake.direction!=2)
if(key==LEFT&&snake.direction!=1)
snake.direction=2;
if(key==DOWN&&snake.direction!=3)
snake.direction=4;
settextstyle(0,0,4);
outtextxy(200,200,"GAME OVER");
void PrScore(void)
char str[10];
bar(50,15,220,35);
setcolor(6);
sprintf(str,"score:%d",score);
outtextxy(55,20,str);
//鼠标控制
include
include
include
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()
inregs.x.ax=0x01;
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);
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+R3/4,b+R3/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)
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);
相关知识
c语言如何用手机开发小游戏
又是一个小游戏――数码游戏 的源代码,大家边玩边学吧!
c语言游戏代码大全
游戏代码,小游戏开发,游戏源代码
游戏程序开发,游戏源代码,网页小游戏源码
用C++编写的小游戏源代码
游戏源代码是什么
c语言如何写网络游戏
怎么用c语言编写游戏吗
c语言如何写游戏外挂
推荐资讯
- 1老六爱找茬美女的烦恼怎么过- 4999
- 2博德之门3黄金雏龙法杖怎么得 4869
- 3《大侠立志传》剿灭摸金门任务 4312
- 4代号破晓官方正版角色介绍 4023
- 5赛马娘锻炼到底的伙伴支援卡事 3802
- 6闪烁之光11月兑换码大全20 3774
- 7原神原海异种刷怪路线-原神原 3547
- 8爆梗找茬王厕所特工怎么通关- 3542
- 9《我的世界》领地删除指令是什 3440
- 10原神开局星落湖怎么出去 原神 3426