游戏盒子接球最终版
2013-12-02 16:48
106 查看
有几个小地方做了改进,倒计时调用系统函数GetTickCount()来进行计算的,更加方便,其他还有一些地方做出了优化,大家可以自己对比了看一下。这里还要感谢杨伟大哥提出的宝贵建议。
代码如下:
代码如下:
/////////////////////////////////////////////////// // 程序名称:盒子接球(一个简单的小游戏) // 编译环境:Visual C++ 6.0,EasyX 2013霜降版 // 作 者:圣石 <2464847121@qq.com> // 最后修改:2013-12-1 // 玩法:按方向键控制盒子移动接住小球,总共有 20s 的游戏时间,倒计时为 0 时游戏结束 // #include <graphics.h> #include <conio.h> #include <time.h> #include <stdio.h> // 定义常量 #define NUM 10 #define CMD_LEFT 1 #define CMD_RIGHT 2 #define CMD_QUIT 4 int box_x = 10; int box_y = 420; // 定义球的结构体 struct Ball { int x, y, v; }; // 获取用户控制 int GetCommand() { int c = 0; if (GetAsyncKeyState(VK_LEFT) & 0x8000) c |= CMD_LEFT; if (GetAsyncKeyState(VK_RIGHT) & 0x8000) c |= CMD_RIGHT; if (GetAsyncKeyState(VK_ESCAPE) & 0x8000) c |= CMD_QUIT; return c; } // 倒计时 int Time(int t) { char strsec[10]; int sec = 20 - (GetTickCount() - t) / 1000; itoa(sec, strsec, 10); outtextxy(570, 110, " "); outtextxy(570, 110, strcat(strsec, "s")); return sec; } // 介绍 void menu() { line(449, 0, 449, 480); char runTime[] = "游戏倒计时 : ", receiveBallNum[] = "接到的球的数量:", copyRight[] = "版权所有:圣石", finishWorkDate[] = "完成日期:2012年12月1日", introductiona[] = "按方向键控制盒子移动接住", introductionb[] = "小球,倒计时为0时游戏结束"; settextcolor(GREEN); outtextxy(450, 10, introductiona); outtextxy(450, 30, introductionb); outtextxy(450, 110, runTime); outtextxy(450, 210, receiveBallNum); outtextxy(450, 310, copyRight); outtextxy(450, 410, finishWorkDate); } // 产生随机球 void ballRandom(Ball ball[], int i) { ball[i].x = 16 + 45 * i; ball[i].y = 8 + rand() % 32; ball[i].v = 1 + rand() % 5; } // 画球,并计算得分 void calculateScore(Ball ball[], int &score) { for(int i = 0; i < NUM; i++) { fillcircle(ball[i].x, ball[i].y, 8); if(ball[i].y >= 472) { ballRandom(ball, i); continue; } if(box_x + 8 <= ball[i].x && ball[i].x <= box_x + 72 && ball[i].y >= 412) { score++; ballRandom(ball, i); } } } // 主函数 int main() { // 初始化 initgraph(640, 480); srand(time(NULL)); BeginBatchDraw(); setlinecolor(GREEN); setfillcolor(WHITE); menu(); Ball ball[NUM]; int dx, i, c, score = 0; bool flag = true; for(i=0; i<NUM; i++) { ballRandom(ball, i); } int t = GetTickCount(); char strScore[10], str[] = "your score:"; // 游戏主循环 while(flag) { dx = 0; // 显示得分 char strScore[10]; itoa(score, strScore, 10); outtextxy(570, 210, strScore); // 画球,并计算得分 calculateScore(ball, score); // 画盒子 fillrectangle(box_x, box_y, box_x+80, box_y+60); FlushBatchDraw(); // 获取用户控制命令 c = GetCommand(); if (c & CMD_LEFT) dx = -10; if (c & CMD_RIGHT) dx = 10; if (c & CMD_QUIT) flag = false; if (!Time(t)) flag = false; // 延时 Sleep(25); // 擦除游戏区 clearrectangle(0, 0, 448, 480); // 计算球的新坐标 for(i = 0; i < NUM; i++) { ball[i].y += ball[i].v; } // 移动盒子 box_x += dx; if(box_x < 0) box_x = 0; if(box_x > 368) box_x = 368; } // 清空键盘缓冲区 FlushConsoleInputBuffer(GetStdHandle(STD_INPUT_HANDLE)); // 输出游戏结果 itoa(score, strScore, 10); outtextxy(222, 240, strcat(str, strScore)); outtextxy(220, 300, "按任意键退出"); EndBatchDraw(); // 按任意键退出 getch(); closegraph(); return 0; }
相关文章推荐
- 游戏盒子接球
- 仿腾讯盒子游戏(新手练习)
- The Seventh Hunan Collegiate Programming Contest 盒子游戏
- PS4/XBOX迎来全新劲敌!游戏盒子市场前瞻
- 盒子游戏
- Scratch儿童项目式编程--接球游戏 Scratch children program programming - catching a ball
- 有一个投篮游戏。球场有p个篮筐,编号为0,1...,p-1。每个篮筐下有个袋子,每个袋子最多装一个篮球。有n个篮球,每个球编号xi 。规则是将数字为xi 的篮球投到xi 除p的余数为编号的袋里。若袋里已有篮球则球弹出游戏结束输出i,否则重复至所有球都投完。输出-1。问游戏最终的输出是什么?
- 畅游无限游戏盒子(二)--推箱子
- 畅游无限游戏盒子(五)--adruino远程控灯
- 游戏盒子--总结
- 盒子游戏(湖南省第七届大学生计算机程序设计竞赛)
- VUE+WebPack游戏设计:实现盒子动画和键盘特效
- Cocos2D iOS之旅:如何写一个敲地鼠游戏(三):素材最终解决方法
- 1104: 盒子游戏(简单博弈)
- E - 盒子游戏
- 畅游无限游戏盒子(三)--贪吃蛇
- SDUT 3004-盒子游戏(博弈)
- 数独游戏--判断现有数字是否有解--不管是否最终有解Valid Sudoku
- 盒子游戏
- AYITACM第三周周赛 E - 盒子游戏