#include <graphics.h>
#include <conio.h>
#include <time.h>
#include <stdio.h>
/* 【自学去】网站收集 http://www.zixue7.com */
// 定义常量
#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;
}
没有合适的资源?快使用搜索试试~ 我知道了~
c语言盒子接球游戏源码.zip
![preview](https://csdnimg.cn/release/downloadcmsfe/public/img/white-bg.ca8570fa.png)
共6个文件
plg:1个
dsp:1个
opt:1个
![preview-icon](https://csdnimg.cn/release/downloadcmsfe/public/img/scale.ab9e0183.png)
需积分: 0 0 下载量 121 浏览量
2024-05-27
21:40:23
上传
评论
收藏 11KB ZIP 举报
温馨提示
c语言盒子接球游戏源码.zip
资源推荐
资源详情
资源评论
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![7z](https://img-home.csdnimg.cn/images/20210720083312.png)
![txt](https://img-home.csdnimg.cn/images/20210720083642.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![csv](https://img-home.csdnimg.cn/images/20210720083646.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![exe](https://img-home.csdnimg.cn/images/20210720083343.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![7z](https://img-home.csdnimg.cn/images/20210720083312.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
收起资源包目录
![package](https://csdnimg.cn/release/downloadcmsfe/public/img/package.f3fc750b.png)
![folder](https://csdnimg.cn/release/downloadcmsfe/public/img/folder.005fa2e5.png)
![folder](https://csdnimg.cn/release/downloadcmsfe/public/img/folder.005fa2e5.png)
![folder](https://csdnimg.cn/release/downloadcmsfe/public/img/folder.005fa2e5.png)
![folder](https://csdnimg.cn/release/downloadcmsfe/public/img/folder.005fa2e5.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
共 6 条
- 1
资源评论
![avatar-default](https://csdnimg.cn/release/downloadcmsfe/public/img/lazyLogo2.1882d7f4.png)
![avatar](https://profile-avatar.csdnimg.cn/90c03f491ea04d22a033c96fb6c57531_xlfhuahua.jpg!1)
xlfhuahua
- 粉丝: 460
- 资源: 21
上传资源 快速赚钱
我的内容管理 展开
我的资源 快来上传第一个资源
我的收益
登录查看自己的收益我的积分 登录查看自己的积分
我的C币 登录后查看C币余额
我的收藏
我的下载
下载帮助
![voice](https://csdnimg.cn/release/downloadcmsfe/public/img/voice.245cc511.png)
![center-task](https://csdnimg.cn/release/downloadcmsfe/public/img/center-task.c2eda91a.png)
最新资源
- tensorflow-2.8.2-cp310-cp310-win-amd64.whl
- tensorflow-2.8.1-cp39-cp39-win-amd64.whl
- 基于Cadence-All...PCB信号完整性分析与设计-巫玲.pdf
- tensorflow-gpu-2.8.2-cp310-cp310-win-amd64.whl
- 基于Springboot的大学生租房系统(有报告) Javaee项目,springboot项目
- 视频打赏观看视频系统源码.zip
- 基于Cadence-Sigrity板级的电热协同仿真-封云鹏.pdf
- 基于Springboot的会员制医疗预约服务管理信息系统(有报告) Javaee项目,springboot项目
- MySQL忘记root密码解决方案
- thinkphp云赏修复版视频打赏V8.5网站源码.zip
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
![feedback](https://img-home.csdnimg.cn/images/20220527035711.png)
![feedback](https://img-home.csdnimg.cn/images/20220527035711.png)
![feedback-tip](https://img-home.csdnimg.cn/images/20220527035111.png)
安全验证
文档复制为VIP权益,开通VIP直接复制
![dialog-icon](https://csdnimg.cn/release/downloadcmsfe/public/img/green-success.6a4acb44.png)