编写吃c语言程序步骤,自己做的一个C语言小游戏——吃金子

本周数据结构课程设计,哥挑了个简单点的题目,用C语言编写了一个图形界面的吃金子小游戏。

基本想法是:

定义了一个数组,通过数据映射到屏幕的坐标。

然后通过一些画图函数,画出淘金者(红色圆)和金子(黄色圆)。

通过键盘的上下左右键控制淘金者的上下左右移动。

每隔六秒钟,游戏界面会刷一次,此时金子会更换位置。

如果你是高手,一分钟吃到了20个那你就Win,如果你碰到墙壁,Game Over。

此游戏超挫,属于版本一,有兴趣的下下来玩玩也不错,当然没有那些Flash做的Beautiful。呵呵。

下面是源代码和一些注释:

/*

Version: v1.0

Author:xufeiyang

DateTime:2010.12.13

IDE:Turbo C 2.0

*/

/************************************/

#include "graphics.h" /*头文件*/

#include "time.h"

#include "stdlib.h"

#include "bios.h"

#include "dos.h"

#include "stdio.h"

#define ESC 0x11b /*键盘扫描码*/

#define UP 0x4800

#define DOWN 0x5000

#define LEFT 0x4b00

#define F1 0x3b00

#define RIGHT 0x4d00

#define YES 0x1579

#define NO 0x316e

#define RESTART 0x1372

/****************************************/

time_t start, end;

int diff;

int oldtime;

int goldnum = 0;

int Bord[20][20]=

{

2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,

2,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,2,

2,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,2,

2,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,2,

2,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,2,

2,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,2,

2,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,2,

2,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,2,

2,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,2,

2,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,2,

2,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,2,

2,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,2,

2,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,2,

2,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,2,

2,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,2,

2,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,2,

2,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,2,

2,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,2,

2,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,2,

2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2

};

typedef struct {        /* hunger */

int cirx;

int ciry;

int cirradius;

}Hunger, Gold;

Hunger hunger;

Gold gold;

int oldGoldx;

int oldGoldy;

void initBord();

void drawHunger();

void updateHunger();

void drawGold();

void updateGold();

void randomBean();

void printSum();

void delSum();

int justify();

void moveDown();

void moveUp();

void moveRight();

void moveLeft();

void winGame();

void loseGame();

int main()

{

int x, y;

int key = 0;

int result;

time_t st;

hunger.cirradius = 9;    /*初始化淘金者和金子的半径*/

gold.cirradius = 5;

initBord();

while(1)

{

end = time(NULL);

while(!kbhit() || !(key = bioskey(0)))

{

end = time(NULL);

diff = difftime(end, start);

if(diff % 6 == 0)

{

updateGold();

randomBean();

break;

}

}

if(key)

{

switch(key)

{

case DOWN:

moveDown();

break;

case UP:

moveUp();

break;

case LEFT:

moveLeft();

break;

case RIGHT:

moveRight();

break;

case ESC:

sleep(2);

closegraph();

exit(1);

case RESTART:

closegraph();

initBord();

break;

} /* switch */

delSum();

printSum();

key = 0;

}    /* if */

/*        if((59 - diff)%4 == 0)

{

updateGold();

randomBean();

}

*/

result = justify();

if(result == 1)

{

winGame();

break;

}

else if(result == 0)

{

loseGame();

break;

}

else

continue;

}

sleep(2);

closegraph();

return 0;

}

/*********************************************/

void initBord()

{

int gd = VGA, errorcode;

int gm = VGAHI;

int x, y;

srand((unsigned int)time(NULL));

initgraph(&gd, &gm,"");

errorcode = graphresult();

if(errorcode < 0)

{

printf("Graphics error:%s/n", grapherrormsg(errorcode));

printf("Press any key to halt:");

exit(1);

}

cleardevice();

start = time(NULL);

do

{

x = 1 + rand()%18;

y = 1 + rand()%18;

}while(Bord[x][y] == 2);

setbkcolor(9);

hunger.cirx = x;

hunger.ciry = y;

drawHunger();

printSum();

randomBean();

setcolor(BLUE);

moveto(39, 39);

lineto(39, 439);

linerel(400, 0);

linerel(0, -400);

linerel(-400, 0);

moveto(59, 59);

lineto(59, 419);

linerel(360, 0);

linerel(0, -360);

linerel(-360, 0);

moveto(449, 39);

lineto(589, 39);

linerel(0, 400);

linerel(-140, 0);

linerel(0, -400);

}

void winGame()

{

setcolor(RED);

setfillstyle(SOLID_FILL, RED);

outtextxy(getmaxx()/2+20, getmaxy()/2,"Y O U  W I N ! ");

}

void loseGame()

{

setcolor(RED);

setfillstyle(SOLID_FILL, RED);

outtextxy(getmaxx()/2+20, getmaxy()/2," GAME OVER !");

}

int justify()

{

if(hunger.ciry >= 19 || hunger.ciry <= 0

|| hunger.cirx >= 19 || hunger.cirx <= 0

|| (diff > 60 && goldnum < 30))

{

cleardevice();

return 0;    /*撞墙者死*/

}

else if(diff <= 60 && goldnum >= 30)

{

cleardevice();

return 1;

}

else

return 2;

}

void drawHunger()

{

Bord[hunger.cirx][hunger.ciry] = 3;

setcolor(RED);

circle(69 + (hunger.cirx-1) * 20, 69 + (hunger.ciry - 1) * 20, hunger.cirradius);

setfillstyle(SOLID_FILL, RED);

floodfill(69+(hunger.cirx-1)*20-1, 69+(hunger.ciry-1)*20-1,RED);

}

void updateHunger()

{

Bord[hunger.cirx][hunger.ciry] = 1;

setcolor(0);

circle(69 + (hunger.cirx-1) * 20, 69 + (hunger.ciry - 1) * 20, hunger.cirradius);

setfillstyle(SOLID_FILL, 0);

floodfill(69+(hunger.cirx-1)*20-1, 69+(hunger.ciry-1)*20-1,0);

}

void randomBean()

{

int x, y;

do

{

x = 1 + rand() % 18;

y = 1 + rand() % 18;

}while(Bord[x][y] == 3);

gold.cirx = x;

gold.ciry = y;

drawGold();

}

void drawGold()

{

oldGoldx = gold.cirx;

oldGoldy = gold.ciry;

Bord[gold.cirx][gold.ciry] = 0;

setcolor(YELLOW);

circle(69 + (gold.cirx - 1) * 20, 69 + (gold.ciry - 1) * 20, gold.cirradius);

setfillstyle(SOLID_FILL, YELLOW);

floodfill(69+(gold.cirx-1)*20-2, 69+(gold.ciry-1)*20-2,YELLOW);

}

void updateGold()

{

Bord[gold.cirx][gold.ciry] = 1;

setcolor(0);

circle(69 + (gold.cirx - 1) * 20, 69 + (gold.ciry - 1) * 20, gold.cirradius);

setfillstyle(SOLID_FILL, 0);

floodfill(69+(gold.cirx-1)*20-2, 69+(gold.ciry-1)*20-2, 0);

}

void printSum()

{

char c[10], a[10];

memset(c,0x00, sizeof(c));

memset(a, 0x00, sizeof(a));

oldtime = 60-diff;

sprintf(c,"%d", goldnum);

sprintf(a,"%d", 60-diff);

/*    itoa(goldnum, c, sizeof(c));

itoa(60-diff,a,sizeof(a));

*/

setcolor(11);

outtextxy(480,120,"60S Eat 30");

outtextxy(453,140,">>EAT BEAN GAME<

outtextxy(510,160,c);

outtextxy(480,200,"SECOND LEFT ");

setcolor(4);

outtextxy(500,220,a);

}

void delSum()

{

char c[10], a[10];

memset(c, 0x00, sizeof(c));

memset(a, 0x00, sizeof(a));

sprintf(c,"%d", goldnum - 1);

sprintf(a,"%d", oldtime);

setcolor(0);

outtextxy(510,160,c);

setcolor(0);

outtextxy(500,220,a);

}

void moveDown()

{

updateHunger();

hunger.ciry ++;

if(Bord[hunger.cirx][hunger.ciry] == 0)

{

goldnum++;

updateGold();

randomBean();

}

drawHunger();

}

void moveUp()

{

updateHunger();

hunger.ciry --;

if(Bord[hunger.cirx][hunger.ciry] == 0)

{

goldnum++;

updateGold();

randomBean();

}

drawHunger();

}

void moveRight()

{

updateHunger();

hunger.cirx++;

if(Bord[hunger.cirx][hunger.ciry] == 0)

{

goldnum++;

updateGold();

randomBean();

}

drawHunger();

}

void moveLeft()

{

updateHunger();

hunger.cirx --;

if(Bord[hunger.cirx][hunger.ciry] == 0)

{

goldnum++;

updateGold();

randomBean();

}

drawHunger();

}

俺直接定义成全局函数的,没有用参数传递,估计兄弟姐妹们看了会有点晕。呵呵,下次做之前一定要想好优化一下。

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值