c语言游戏小型程序代码,C语言小游戏源码「建议收藏」

c语言游戏小型程序代码,C语言小游戏源码「建议收藏」在此提供C语言小游戏源码,包括扫雷游戏,贪吃蛇游戏,时钟等。运行时只要把红色部分改为自己电脑上TC目录的BGI分目录即可。//扫雷游戏#include#include#include#defineLEFTPRESS0xff01#defineLEFTCLICK0xff10#defineLEFTDRAG0xff19#defineMOUSEMOVE0xff08struct{int…

大家好,又见面了,我是你们的朋友全栈君。

在此提供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();

}

本源码整理于互联网

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/144588.html原文链接:https://javaforall.cn

【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛

【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...

(0)


相关推荐

发表回复

您的电子邮箱地址不会被公开。

关注全栈程序员社区公众号