-
2021-05-19 12:45:48
在此提供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();
}
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
#include
#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();
}
//贪吃蛇
#define N 200
#include
#include
#include
#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
{
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
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();
}
//鼠标控制
#include
#include
#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()
{
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();
}
本源码整理于互联网
更多相关内容 -
60多套C语言小游戏代码(完整可运行)
2021-09-02 12:59:48个人收集,简单易懂,思路清晰,直接运行。 包括推箱子、超级玛丽、俄罗斯方块等游戏。比淘宝的项目好。买到就是赚到! -
C语言小项目代码大全.一些小游戏及管理系统
2019-11-24 21:05:22C语言小项目代码大全.一些小游戏及管理系统 -
C语言游戏代码大全
2018-02-18 11:29:44双人贪吃蛇,别踩白方块,玫瑰花,矿井逃生等多种C语言游戏代码等你来实践! -
五子棋,贪吃蛇,俄罗斯方块小游戏 c语言源代码
2019-04-03 21:14:07五子棋,贪吃蛇,俄罗斯方块小游戏 c语言源代码 实测可以玩 -
c语言经典游戏代码
2021-05-20 01:10:20//C语言多线程-主角和怪物#include#include#define bool int //定义int变量为bool变量,bool不是真就是假int a=0,b=20;//主角的坐标int x=1,y=0;//怪物的坐标int i=1;//i值为真HANDLE hMutex;//1.坐标void ...//C语言多线程-主角和怪物
#include
#include
#define bool int //定义int变量为bool变量,bool不是真就是假
int a=0,b=20;//主角的坐标
int x=1,y=0;//怪物的坐标
int i=1;//i值为真
HANDLE hMutex;
//1.坐标
void GamePosition(HANDLE g_hout,int x,int y)
{
COORD pos;//点的结构体
pos.X=x;//横坐标
pos.Y=y;//纵坐标
SetConsoleCursorPosition(g_hout,pos);
//设置控制平台光标位置
}
DWORD WINAPI Func(LPVOID lpParamter)//多线程的功能函数6.线程是画怪物
{
HANDLE hout=GetStdHandle(STD_OUTPUT_HANDLE);//7.拿到这张纸
WaitForSingleObject(hMutex, INFINITE);//13.自己进来,自己用洗手间
GamePosition(hout,x,y),printf('●');//8.在纸上画怪物
ReleaseMutex(hMutex);//14.放弃使用权
while(1)//9.怪物在横坐标为从0-10依次循环移动
{
if(x>=0&&i==1)
{
printf(' ');
GamePosition(hout,++x,y);
printf('●');
Sleep(1000);
if(x==10)
i=0;//i为假
}
else if(x<>
{
printf(' ');
GamePosition(hout,--x,y);
printf('●');
Sleep(1000);
if(x==0)
i=1;
}
}
return 0;
}
int main()
{
HANDLE hThread = CreateThread(NULL, 0, Func, NULL, 0, NULL);//5.创建线程
hMutex = CreateMutexA(NULL, FALSE, '123');//创建互斥锁(量)//10.关上洗手间
HANDLE hout=GetStdHandle(STD_OUTPUT_HANDLE);//2.拿到这张纸
WaitForSingleObject(hMutex, INFINITE);//11.等待你的同事出来 15步接着
GamePosition(hout,a,b),printf('☆');//3.在纸上画主角
ReleaseMutex(hMutex);//12.同事出来了,放弃了洗手间的使用权
while(1)
{
if(kbhit())
switch(getch())//控制左右 4.主角上下左右移动
{
case 'w':
case 'W':
if(b>3)GamePosition(hout,a,b),printf(' '),GamePosition(hout,a,--b),printf('☆');
break;
case 's':
case 'S':
if(b<20)gameposition(hout,a,b),printf('>
break;
case 'a':
case 'A':
if(a>0)printf(' '),GamePosition(hout,--a,b),printf('☆');
break;
case 'd':
case 'D':
if(a<20)printf('>
break;
}
}
CloseHandle(hThread);
system('pause');
return 0;
}
扫雷游戏代码源码
#include
#include
#include
int main (){
int delta[8][2] = {{-1,-1},{-1,0},{-1,1},{0,-1},{0,1},{1,-1},{1,0},{1,1}};
int row =0,col = 0,num = 0;
char map[10][10] = {0};
char show[10][10] = {0};
srand(time(0));
for(row = 0;row <=>
for(col = 0;col <=>
map[row][col] = '0';
}
}
do{
row = rand() % 10;
col = rand() % 10;
if(map[row][col] == '0'){
map[row][col] = 'x';
num++;
}
}while(num <>
for (row = 0;row <=>
for (col = 0;col <=>
if(map[row][col] != 'x'){
int cnt = 0;
for (num = 0;num <=>
if(row + delta[num][0] <>
continue;
}
if(row + delta[num][0] > 9){
continue;
}
if(col + delta[num][1] <>
continue;
}
if(col + delta[num][1] > 9){
continue;
}
if(map[row + delta[num][0]][col + delta[num][1]]== 'x'){
cnt++;
}
}
map[row][col] = '0' + cnt;
}
}
}
for (row = 0;row <>
for(col = 0;col < 10;col="">
printf('* ');
}
printf('\n');
}
num = 0;
int x,y;
do{
printf('please enter the coordinate of array:');
scanf('%d%d',&x,&y);
show[x-1][y-1] = 1;
if(map[x-1][y-1] == '0'){
for (num = 0;num <=>
if(x-1 + delta[num][0] <>
continue;
}
if(x-1 + delta[num][0] > 9){
continue;
}
if(y -1+ delta[num][1] <>
continue;
}
if(y-1 + delta[num][1] > 9){
continue;
}
show[x-1+delta[num][0]][y-1+delta[num][1]] = 1;
}
}
if (map[x-1][y-1]!= 'x'&&map[x-1][y-1] != '0'){
for (num = 0;num <=>
int cnt = 0;
if(x-1 + delta[num][0] <>
continue;
}
if(x-1 + delta[num][0] > 9){
continue;
}
if(y-1 + delta[num][1] <>
continue;
}
if(y-1 + delta[num][1] > 9){
continue;
}
if( map[x -1 + delta[num][0]][y -1+ delta[num][1]] != 'x'){
show[x-1 + delta[num][0]][y -1+ delta[num][1]] = 1 ;
}
}
}
if(map[x-1][y-1] == 'x') {
printf('game over!\n');
for (row = 0;row <>
for(col = 0;col < 10;col="">
printf('%c ',map[row][col]);
}
printf('\n');
}
return 0;
}
system('cls');
printf('mine sweeping:\n');
for (row = 0;row <>
for(col = 0;col < 10;col="">
if (show[row][col] == 1)
{
printf('%c ', map[row][col]);
}
else
{
printf('* ');
}
}
printf('\n');
}
num = 0;
for (row = 0;row <>
for(col = 0;col < 10;col="">
if (show[row][col] == 1 )
{
num++;
}
}
}
printf('num:%d\n',num);
}while(num <>
printf('you win!');
return 0;
}
-
C语言c++游戏源代码大全
2019-06-19 11:25:42使用vs编译的二维小游戏,里面有很多小游戏和网站模板,比如超级玛丽、坦克大战、推箱子、别踩白块、贪吃蛇、种地浇水、盒子接球、连连看、军棋、五子棋、火车订票系统、图书馆借阅、涂格子游戏、火柴人游戏、大丰收... -
200个C语言小游戏源代码
2010-03-15 21:12:25200个小游戏的C语言源代码,和大家分享,喜欢的就下载吧! -
C语言经典游戏代码
2021-05-19 12:00:15C语言精品游戏主角和怪物源码//C语言多线程-主角和怪物#include#include#define bool int //定义int变量为bool变量,bool不是真就是假int a=0,b=20;//主角的坐标int x=1,y=0;//怪物的坐标int i=1;//i值为真HANDLE ...C语言精品游戏主角和怪物源码
//C语言多线程-主角和怪物
#include
#include
#define bool int //定义int变量为bool变量,bool不是真就是假
int a=0,b=20;//主角的坐标
int x=1,y=0;//怪物的坐标
int i=1;//i值为真
HANDLE hMutex;
//1.坐标
void GamePosition(HANDLE g_hout,int x,int y)
{
COORD pos;//点的结构体
pos.X=x;//横坐标
pos.Y=y;//纵坐标
SetConsoleCursorPosition(g_hout,pos);
//设置控制平台光标位置
}
DWORD WINAPI Func(LPVOID lpParamter)//多线程的功能函数6.线程是画怪物
{
HANDLE hout=GetStdHandle(STD_OUTPUT_HANDLE);//7.拿到这张纸
WaitForSingleObject(hMutex, INFINITE);//13.自己进来,自己用洗手间
GamePosition(hout,x,y),printf("");//8.在纸上画怪物
ReleaseMutex(hMutex);//14.放弃使用权
while(1)//9.怪物在横坐标为从0-10依次循环移动
{
if(x>=0&&i==1)
{
printf(" ");
GamePosition(hout,++x,y);
printf("");
Sleep(1000);
if(x==10)
i=0;//i为假
}
else if(x
{
printf(" ");
GamePosition(hout,--x,y);
printf("");
Sleep(1000);
if(x==0)
i=1;
}
}
return 0;
}
int main()
{
HANDLE hThread = CreateThread(NULL, 0, Func, NULL, 0, NULL);//5.创建线程
hMutex = CreateMutexA(NULL, FALSE, "123");//创建互斥锁(量)//10.关上洗手间
HANDLE hout=GetStdHandle(STD_OUTPUT_HANDLE);//2.拿到这张纸
WaitForSingleObject(hMutex, INFINITE);//11.等待你的同事出来 15步接着
GamePosition(hout,a,b),printf("☆");//3.在纸上画主角
ReleaseMutex(hMutex);//12.同事出来了,放弃了洗手间的使用权
while(1)
{
if(kbhit())
switch(getch())//控制左右 4.主角上下左右移动
{
case 'w':
case 'W':
if(b>3)GamePosition(hout,a,b),printf(" "),GamePosition(hout,a,--b),printf("☆");
break;
case 's':
case 'S':
if(b
break;
case 'a':
case 'A':
if(a>0)printf(" "),GamePosition(hout,--a,b),printf("☆");
break;
case 'd':
case 'D':
if(a
break;
}
}
CloseHandle(hThread);
system("pause");
return 0;
}
扫雷游戏代码源码
#include
#include
#include
int main (){
int delta[8][2] = {{-1,-1},{-1,0},{-1,1},,,,,};
int row =0,col = 0,num = 0;
char map[10][10] = ;
char show[10][10] = ;
srand(time(0));
for(row = 0;row
for(col = 0;col
map[row][col] = '0';
}
}
do{
row = rand() % 10;
col = rand() % 10;
if(map[row][col] == '0'){
map[row][col] = 'x';
num++;
}
}while(num
for (row = 0;row
for (col = 0;col
if(map[row][col] != 'x'){
int cnt = 0;
for (num = 0;num
if(row + delta[num][0]
continue;
}
if(row + delta[num][0] > 9){
continue;
}
if(col + delta[num][1]
continue;
}
if(col + delta[num][1] > 9){
continue;
}
if(map[row + delta[num][0]][col + delta[num][1]]== 'x'){
cnt++;
}
}
map[row][col] = '0' + cnt;
}
}
}
for (row = 0;row
for(col = 0;col
printf("* ");
}
printf("\n");
}
num = 0;
int x,y;
do{
printf("please enter the coordinate of array:");
scanf("%d%d",&x,&y);
if(map[x-1][y-1] == '0'){
for (num = 0;num
if(x-1 + delta[num][0]
continue;
}
if(x-1 + delta[num][0] > 9){
continue;
}
if(y -1+ delta[num][1]
continue;
}
if(y-1 + delta[num][1] > 9){
continue;
}
}
}
if (map[x-1][y-1]!= 'x'&&map[x-1][y-1] != '0'){
for (num = 0;num
int cnt = 0;
if(x-1 + delta[num][0]
continue;
}
if(x-1 + delta[num][0] > 9){
continue;
}
if(y-1 + delta[num][1]
continue;
}
if(y-1 + delta[num][1] > 9){
continue;
}
if( map[x -1 + delta[num][0]][y -1+ delta[num][1]] != 'x'){
}
}
}
if(map[x-1][y-1] == 'x') {
printf("game over!\n");
for (row = 0;row
for(col = 0;col
printf("%c ",map[row][col]);
}
printf("\n");
}
return 0;
}
system("cls");
printf("mine sweeping:\n");
for (row = 0;row
for(col = 0;col
if (show[row][col] == 1)
{
printf("%c ", map[row][col]);
}
else
{
printf("* ");
}
}
printf("\n");
}
num = 0;
for (row = 0;row
for(col = 0;col
if (show[row][col] == 1 )
{
num++;
}
}
}
printf("num:%d\n",num);
}while(num
printf("you win!");
return 0;
}
-
62套C语言小游戏源码
2018-12-31 19:35:15这是楼主珍藏的62套C语言小游戏源码,包含吃豆人、俄罗斯方块、别踩白块等诸多经典小游戏。感兴趣的朋友可以下载下来研究。 -
c语言小游戏c语言项目源码大全合集(60例).zip
2022-04-08 15:09:39c语言小游戏c语言项目源码大全合集(60例): c语言24点游戏源码 c语言Turbo C下写的俄罗斯方块 c语言UDP传输系统源码 c语言万年历源码 c语言五子棋源码 c语言俄罗斯方块 c语言做的一个任务管理器 c语言做的播放器... -
c语言小游戏代码.rar
2021-05-01 17:30:043994107_IvCPBvoZGSGIaCBDVINFnDVuO-1.rar -
收集整理的简单易懂的60套C语言小游戏源代码(可运行,供学习设计参考).zip
2022-04-20 17:09:32收集整理的简单易懂的60套C语言小游戏源代码(可运行,供学习设计参考): c语言24点游戏源码 c语言Turbo C下写的俄罗斯方块 c语言UDP传输系统源码 c语言万年历源码 c语言五子棋源码 c语言俄罗斯方块 c语言做的一个... -
C语言贪吃蛇经典小游戏
2021-05-19 13:21:30一、贪吃蛇小游戏简介:用上下左右控制蛇的方向,寻找吃的东西,每吃一口就能得到一定的积分,而且蛇的身子会越吃越长,身子越长玩的难度就越大,不能碰墙,也不能咬到自己的身体,等到了一定的分数,就能过关。...一、贪吃蛇小游戏简介:
用上下左右控制蛇的方向,寻找吃的东西,每吃一口就能得到一定的积分,而且蛇的身子会越吃越长,身子越长玩的难度就越大,不能碰墙,也不能咬到自己的身体,等到了一定的分数,就能过关。
二、函数框架
三、数据结构
typedef struct Snake
{
size_t x; //行
size_t y; //列
struct Snake* next;
}Snake, *pSnake;
定义蛇的结构体,利用单链表来表示蛇,每个结点为蛇身体的一部分。
四、代码实现(vs2010 c语言)
1.Snake.h
#ifndef __SNAKE_H__
#define __SNAKE_H__
#include
#include
#include
#include
#include
#include
//标识地图大小
#define ROW_MAP 10 //地图的行
#define COL_MAP 20 //地图的列
#define SUCCESS_SCORE 10//通关分数
enum Direction //蛇行走的方向
{
R, //右
L, //左
U, //上
D //下
}Direction;
enum State
{
ERROR_SELF, //咬到自己
ERROR_WALL, //撞到墙
NORMAL, //正常状态
SUCCESS //通关
}State;
typedef struct Snake
{
size_t x; //行
size_t y; //列
struct Snake* next;
}Snake, *pSnake;
void StartGame();
void RunGame();
void EndGame();
#endif
2.Snake.c
#include "Snake.h"
pSnake head = NULL; //定义蛇头指针
pSnake Food = NULL; //定义食物指针
int sleeptime = 500;//间隔时间,用来控制速度
int Score = 0; //总分
int everyScore = 1; //每步得分
//定义游戏中用到的符号
const char food = '#';
const char snake = '*';
void Pos(int x, int y) //控制输出光标
{
COORD pos; //pos为结构体
pos.X = x; //控制列
pos.Y = y; //控制行
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), pos);//读取标准输出句柄来控制光标为pos
}
void Face()
{
system("color 0C");
printf("*******************************************************\n");
printf("* Welcome to Snake Game! *\n");
printf("* *\n");
printf("* ->开始游戏请按 enter键 *\n");
printf("* ->退出游戏请按 esc键 *\n");
printf("* ->暂停游戏请按 space键 *\n");
printf("* ->通过上下左右键来控制蛇的移动 *\n");
printf("* ->通过F1键减速 F2键加速 *\n");
printf("*******************************************************\n");
}
void Map() //初始化地图
{
int i = 0;
for(i = 0; i
{
Pos(i, 0);
printf("■");
Pos(i, ROW_MAP-1);
printf("■");
}
for(i = 0; i
{
Pos(0, i);
printf("■");
Pos(COL_MAP-2, i);
printf("■");
}
}
void PrintSnake() //打印蛇
{
pSnake cur = head;
while(cur)
{
Pos(cur->y, cur->x);
printf("%c", snake);
cur = cur->next;
}
}
void InitSnake() //初始化蛇身
{
int initNum = 3;
int i = 0;
pSnake cur;
head = (pSnake)malloc(sizeof(Snake));
head->x = 5;
head->y = 10;
head->next = NULL;
cur = head;
for(i = 1; i < initNum; i++)
{
pSnake newNode = (pSnake)malloc(sizeof(Snake));
newNode->x = 5+i;
newNode->y = 10;
newNode->next = NULL;
cur->next = newNode;
cur = cur->next;
}
PrintSnake();
}
void CreateFood() //在地图上随机产生一个食物
{
pSnake cur = head;
Food = (pSnake)malloc(sizeof(Snake));
//产生x~y的随机数 k=rand()%(Y-X+1)+X;
srand((unsigned)time(NULL));
Food->x = rand()%(ROW_MAP-2 - 1 + 1)+1;
Food->y = rand()%(COL_MAP-3 - 2 + 1)+2;
Food->next = NULL;
while(cur) //检查食物是否与蛇身重合
{
if(cur->x == Food->x && cur->y == Food->y)
{
free(Food);
Food = NULL;
CreateFood();
return;
}
cur = cur->next;
}
Pos(Food->y, Food->x);
printf("%c", food);
}
void StartGame() //游戏开始的所有设置
{
Face();
system("pause");
if(GetAsyncKeyState(VK_RETURN))
{
system("cls");
Pos(COL_MAP+5, 1);
printf("当前分数/通关分数:");
Pos(COL_MAP+20, 1);
printf("%d/%d", Score, SUCCESS_SCORE);
Pos(COL_MAP+5, 2);
printf("当前分每步得分:");
Pos(COL_MAP+20, 2);
printf("%d", everyScore);
Pos(COL_MAP+5, 3);
printf("\n");
Pos(COL_MAP+5, 4);
printf("速度越快 得分越高哦!!\n");
Map();
InitSnake();
CreateFood();
}
else if(GetAsyncKeyState(VK_ESCAPE))
{
exit(0);
}
}
int IsCrossWall() //判断是否碰到墙
{
if(head->x <= 0 || head->x >= ROW_MAP-1
||head->y <= 1 || head->y >= COL_MAP-2)
{
State = ERROR_WALL;
return 0;
}
return 1;
}
int IsEatSelf(pSnake newHead) //判断是否咬到自己
{
pSnake cur = head;
assert(newHead);
while(cur)
{
if(cur->x == newHead->x && cur->y == newHead->y)
{
State = ERROR_SELF;
return 0;
}
cur = cur->next;
}
return 1;
}
int IsFood(pSnake pos) //判断该位置是不是食物
{
assert(pos);
if(pos->x == Food->x && pos->y == Food->y)
{
return 1;
}
return 0;
}
void SnakeMove() //蛇移动一次
{
pSnake newHead = NULL;
newHead = (pSnake)malloc(sizeof(Snake));
if(Direction == R)
{
newHead->x = head->x;
newHead->y = head->y+1;
newHead->next = head;
}
else if(Direction == L)
{
newHead->x = head->x;
newHead->y = head->y-1;
newHead->next = head;
}
else if(Direction == U)
{
newHead->x = head->x-1;
newHead->y = head->y;
newHead->next = head;
}
else if(Direction == D)
{
newHead->x = head->x+1;
newHead->y = head->y;
newHead->next = head;
}
if(IsFood(newHead))
{
head = newHead;
PrintSnake();
CreateFood();
Score += everyScore;
Pos(COL_MAP+20, 1);
printf("%d/%d", Score, SUCCESS_SCORE);
if(Score >= SUCCESS_SCORE)
{
State = SUCCESS;
}
}
else
{
if(IsCrossWall() && IsEatSelf(newHead))
{
pSnake cur = NULL;
head = newHead;
cur = head;
//删除蛇尾并打印
while(cur->next->next != NULL)
{
Pos(cur->y, cur->x);
printf("%c", snake);
cur = cur->next;
}
Pos(cur->y, cur->x);
printf("%c", snake);
Pos(cur->next->y, cur->next->x);
printf(" "); //打印空格来覆盖频幕上的蛇尾
free(cur->next);
cur->next = NULL;
}
else
{
free(newHead);
newHead = NULL;
}
}
}
void Pause()
{
while(1)
{
Sleep(sleeptime);
if(GetAsyncKeyState(VK_SPACE))
{
break;
}
}
}
void ControlSnake() //用键盘控制游戏
{
if(GetAsyncKeyState(VK_UP) && Direction!=D)
{
Direction = U;
}
else if(GetAsyncKeyState(VK_DOWN) && Direction!=U)
{
Direction = D;
}
else if(GetAsyncKeyState(VK_LEFT) && Direction!=R)
{
Direction = L;
}
else if(GetAsyncKeyState(VK_RIGHT) && Direction!=L)
{
Direction = R;
}
else if(GetAsyncKeyState(VK_F1))
{
if(sleeptime != 500)
{
sleeptime = 500;
everyScore = 1;
Pos(COL_MAP+20, 2);
printf("%d", everyScore);
}
}
else if(GetAsyncKeyState(VK_F2))
{
if(sleeptime != 300)
{
sleeptime = 300;
everyScore = 2;
Pos(COL_MAP+20, 2);
printf("%d", everyScore);
}
}
else if(GetAsyncKeyState(VK_SPACE))
{
Pause();
}
else if(GetAsyncKeyState(VK_ESCAPE))
{
exit(0);
}
}
void StateGame() //判断游戏失败或成功
{
if(State == ERROR_SELF)
{
system("cls");
printf("很遗憾,蛇咬到自己,游戏失败!\n");
}
else if(State == ERROR_WALL)
{
system("cls");
printf("很遗憾,蛇碰到墙壁,游戏失败!\n");
}
else if(State == SUCCESS)
{
system("cls");
printf("恭喜您,已通关!!!\n");
}
}
void RunGame()
{
Direction = R; //蛇初始行走方向为右
State = NORMAL;//游戏初始为正常状态
while(1)
{
ControlSnake();
SnakeMove();
if(State != NORMAL)
{
StateGame();
break;
}
Sleep(sleeptime);
}
}
void EndGame() //释放链表并恢复默认值
{
pSnake cur = head;
while(cur)
{
pSnake del = cur;
cur = cur->next;
free(del);
del = NULL;
}
head = NULL;
if(Food != NULL)
{
free(Food);
Food = NULL;
}
Score = 0;
everyScore = 1;
sleeptime = 500;
}
3.Test.c
#include "Snake.h"
int main()
{
while(1)
{
StartGame();
RunGame();
EndGame();
}
system("pause");
return 0;
}
五、运行界面展示
1.欢迎界面
2.游戏界面
小小的c语言项目,用来练手,仅供参考哦!!
谢谢阅读,如有问题,欢迎提出。
更多有趣的经典小游戏实现专题,分享给大家:
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。
-
C语言代码实现推箱子小游戏
2018-10-18 22:25:34本程序通过VS2013编译器编译通过,用C语言实现推箱子小游戏,分为纯控制台的方式实现和EasyX图形库的方式实现,实现思路清晰,代码比较容易看懂 -
C语言2048小游戏源代码
2017-08-02 15:35:06可以学习2048的算法 -
C语言小游戏——贪吃蛇
2022-03-06 17:06:38游戏界面2.游戏说明3.程序源代码 1.游戏界面 2.游戏说明 贪吃蛇游戏按键说明: 按方向键上下左右,可以实现蛇移动方向的改变: 3.程序源代码 代码如下(示例): #include <stdio.h> #include <stdlib.h&... -
C语言小游戏代码。合集
2011-12-22 17:43:18很多小游戏。。。 代码 贪吃蛇、俄罗斯方块等6个小游戏代码。 可以运行 -
五子棋小游戏C语言源代码
2018-06-11 14:32:29一个简易的、用C语言写的五子棋游戏源代码,仅供参考! -
C语言实现的简单打飞机小游戏
2019-06-23 18:34:00用C语言实现的最简单的 打飞机小游戏, -
C语言编程游戏代码
2021-05-25 08:24:59C语言编程的小游戏,助你提高对计算机语言的掌握。#include #include #include #include #include #define L 1#define LX 15#define LY 4static struct BLOCK{int x0,y0,x1,y1,x2,y2,x3,y3;int color,next;intb[]={{0... -
codeblocks写的c语言小游戏源代码
2017-12-21 23:35:13贪吃蛇游戏源代码,codeblocks写的c语言小游戏源代码,可以运行。 -
飞机大战C语言游戏代码
2018-08-09 11:19:46给广大爱好游戏开发的爱好者,属于私人资源,现在拿来共享,只为获得点积分 -
几个C语言小游戏源代码
2011-03-26 20:59:25几个C语言小游戏源代码,如俄罗斯方块、贪吃蛇、五子棋等的源代码 -
推箱子小游戏c语言代码
2021-04-07 09:30:15推箱子,c语言源代码,小游戏c语言源代码,适合初学者的c语言小游戏源代码 推箱子,c语言源代码,小游戏c语言源代码,适合初学者的c语言小游戏源代码 -
【c语言】小游戏程序——弹跳小球
2018-09-04 15:46:56现在说一下其中一个最简单的小程序:弹跳小球 ———————————————LINE———————————————— 首先我们知道,在窗口的坐标系原点是在窗口左上角的,如图所示 然后我们如果想在这个坐标... -
小游戏代码开发
2018-09-08 11:44:34这是用c语言编写的小游戏,可以硬键盘上的wasd来控制游戏中的上下左右 -
2048小游戏C语言实现代码
2021-01-20 06:03:50本文实例为大家分享了C语言实现2048游戏的具体代码,供大家参考,具体内容如下 大一时学c语言写的,写的不好但当时感觉还行。 环境运行 vc6.0 ,cpp文件。 基本上是c写的,但是改变字体颜色,在控制台移动光标等等... -
贪吃蛇游戏C语言代码 VS2015
2020-03-31 19:16:30经典游戏贪吃蛇游戏的C语言代码,平台使用VS2015,采用easyx图形库,代码为自己手写,绝对可用,压缩包无解压密码,感兴趣的朋友可以下载玩一玩