本周数据结构课程设计,哥挑了个简单点的题目,用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();

}

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

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

  1. 用原生javascript做的一个打地鼠的小游戏

    学习javascript也有一段时间了,一直以来分享的都是一些概念型的知识,今天有空做了一个打地鼠的小游戏,来跟大家分享一下,大家也可以下载来增加一些生活的乐趣,下面P出代码:首先是HTML部分代码: ...

  2. 用html5做了一个打气球小游戏

    最近初试html5,用canvas和audio做了一个打气球小游戏. demo放在SAE,网址是:[url]http://auzll.sinaapp.com/balloon/[/url] 效果如下: ...

  3. android游戏编程之从零开始_纯C语言程序员写的编程新手入门基础小游戏之最炫酷推箱子...

    很多编程爱好者都编写过推箱子游戏编程吧,最近有好些朋友看见我以前的推箱子程序后, 问我是怎么做的.我一直想把这个程序的整个过程写一份详细的东西,与各位编程爱好者分享,一直没空.正好现在放假了,而且离回 ...

  4. 用AspectJ做的一个回合格斗小游戏

    由于Spring2的AOP部分作了比较大的调整,很多地方的使用引入了AspectJ中的内容,为了完成<深入Spring 2:轻量级J2EE开发框架原理与实践>中Spring AOP应用一章 ...

  5. 用JS做了一个贪吃蛇小游戏,求顶贴..

    这个周JS课程也快结束了,昨天下午琢磨着做一个小游戏,于是,这个小东东就这么诞生了,目前还存在两个小BUG,欢迎各位高手指正.... 实现的效果有: W,A,S,D,经及小键盘方向键控制方向; 头碰到 ...

  6. xcode怎么执行c语言程序,使用Xcode实现第一个C语言程序——Hello world

    最近一直使用Xcode学习OC,Swift,并开发iOS应用.闲来无趣,想在Mac上写几个C程序.以前在Windows中,我们常常使用VC++,Visual Studio,等等C或C++的IDE,可是 ...

  7. js做的一个猜数字小游戏

    <!DOCTYPE html> <html lang="en"> <head><meta charset="UTF-8" ...

  8. c语言设置一个选择数字的程序,C语言编一个数字益智小游戏

    程序功能及运行情况 设计的程序是一个数字益智游戏,旨在培养小朋友玩家的数学思维,提高玩家的数学能力.游戏共设有四个不同的小游戏,分别是一位数四则运算.两位数四则运算.找最值游戏.排序游戏.程序能实现产 ...

  9. DSB算法C语言程序,单片机中使用DSB温度传感器C语言程序.doc

    单片机中使用DSB温度传感器C语言程序 单片机中使用DS18B20温度传感器C语言程序(参考1) /************************************************** ...

  10. 贪吃蛇c++语言程序,利用C/C++实现较完整贪吃蛇游戏

    记得在大一时刚学习c/c++语言,学到一半突然想用这门语言做一些小游戏出来,首先想到的便是贪吃蛇.于是本人利用空余时间写出了这么一个简单的小游戏. 由于当时的我还没有能力构造出用户界面,故直接使用do ...

最新文章

  1. 两个提高javascript执行效率的简易代码分享给大家
  2. FlexboxLayout使用(Google官方实现流式布局控件)
  3. 文章内容页调用所属栏目地址的标签
  4. 2019ICPC(银川) - Function!(数论+数学分块)
  5. python基础总结--- 列表、内置函数(4-5)
  6. Android Studio出现Failed to open zip file. Gradle's dependency cache may be corrupt问题的解决
  7. SharePoint v3:忘掉模拟用户Impersonate,SPSecurity.RunWithElevatedPrivileges来了
  8. numpy库中的mat和array使用小结
  9. [javascript]实现登陆界面拖动窗口
  10. c++ string
  11. Java学习笔记(六)数据的操作(增、删、改的操作)
  12. 图:活动现场双屏管理系统V3-多线程抽奖版软件,完美升级收工!历时3个月,艰辛坎坷...
  13. 记忆测试系统c语言,c语言重点回忆
  14. Vue之echarts圆饼图详解
  15. 如何使Tello无人机能够通过Python进行条形码扫描?
  16. linux版围棋软件,LEELA围棋下载
  17. [导入]2008大型历史正剧《朱元璋》更新第46集[完结]
  18. 通用的产品功能设计方法
  19. linux 微信不能发图片,微信回应发原图泄露位置信息​;元旦起 AI 造假音视频不得随意发布...
  20. 写一些生活的琐事(纯属发泄)

热门文章

  1. ios浏览器微信支付回调页面_iOS微信支付结果页面返回原程序按钮
  2. Node.js:Dotenv从`.env` 文件加载环境变量的库
  3. gentoo 下Local time zone must be set--see zic manual page解决办法[原创]
  4. python数据分析简历_帮粉丝推荐简历|Python数据分析师
  5. 图片转ascii字符画C语言,将图片转为ASCII字符画
  6. “蔚来杯“2022牛客暑期多校训练营5-A Don‘t Starve
  7. 淘码手机验证码平台——唯一可以批量接收验证码及批量解封账号的平台
  8. 万邦淘宝/天猫按关键字搜索淘宝商品 API 返回值
  9. 大学生面试:教你用“一切细节”征服HR
  10. 高通modem启动过程_高通8953启动流程【转】