在好例子网,分享、交流、成长!
您当前所在位置:首页C/C++ 开发实例C/C++游戏开发 → c++ 游戏反弹球消砖块

c++ 游戏反弹球消砖块

C/C++游戏开发

下载此实例
  • 开发语言:C/C++
  • 实例大小:4.33KB
  • 下载次数:27
  • 浏览次数:758
  • 发布时间:2020-04-07
  • 实例类别:C/C++游戏开发
  • 发 布 人:dingyuanhao3
  • 文件格式:.cpp
  • 所需积分:2
 相关标签:

实例介绍

【实例简介】
【实例截图】
【核心代码】#include <conio.h>
#include <graphics.h>

#define High 480  // 游戏画面尺寸
#define Width 640
#define Brick_num 10 // 砖块个数

// 全局变量
int ball_x,ball_y; // 小球的坐标
int ball_vx,ball_vy; // 小球的速度
int radius;  // 小球的半径
int bar_x,bar_y; // 挡板中心坐标
int bar_high,bar_width;  // 挡板的高度和宽度
int bar_left,bar_right,bar_top,bar_bottom; // 挡板的上下左右位置坐标

int isBrickExisted[Brick_num]; // 每个砖块是否存在,1为存在,0为没有了
int brick_high,brick_width; // 每个砖块的高度和宽度

void startup()  // 数据初始化
{
ball_x = Width/2;
ball_y = High/2;
ball_vx = 1;
ball_vy = 1;
radius = 20;

bar_high = High/20;
bar_width = Width/2;
bar_x = Width/2;
bar_y = High - bar_high/2;
bar_left = bar_x - bar_width/2;
bar_right = bar_x bar_width/2;
bar_top = bar_y - bar_high/2;
bar_bottom = bar_y bar_high/2;

brick_width = Width/Brick_num;
brick_high = High/Brick_num;

int i;
for (i=0;i<Brick_num;i )
isBrickExisted[i] = 1;

initgraph(Width, High);
BeginBatchDraw();
}

void clean()  // 消除画面
{
setcolor(BLACK);
setfillcolor(BLACK);
fillcircle(ball_x, ball_y, radius); // 绘制黑线、黑色填充的圆
bar(bar_left,bar_top,bar_right,bar_bottom); // 绘制黑线、黑色填充的挡板

int i,brick_left,brick_right,brick_top,brick_bottom;
for (i=0;i<Brick_num;i )
{
brick_left = i*brick_width;
brick_right = brick_left brick_width;
brick_top = 0;
brick_bottom = brick_high;
if (!isBrickExisted[i]) // 砖块没有了,绘制黑色
fillrectangle(brick_left,brick_top,brick_right,brick_bottom);
}
}

void show()  // 显示画面
{
setcolor(YELLOW);
setfillcolor(GREEN);
fillcircle(ball_x, ball_y, radius); // 绘制黄线、绿色填充的圆
bar(bar_left,bar_top,bar_right,bar_bottom); // 绘制黄线、绿色填充的挡板

int i,brick_left,brick_right,brick_top,brick_bottom;

for (i=0;i<Brick_num;i )
{
brick_left = i*brick_width;
brick_right = brick_left brick_width;
brick_top = 0;
brick_bottom = brick_high;

if (isBrickExisted[i]) // 砖块存在,绘制砖块
{
setcolor(WHITE);
setfillcolor(RED);
fillrectangle(brick_left,brick_top,brick_right,brick_bottom); // 绘制砖块
}
}

FlushBatchDraw();
// 延时
Sleep(3);
}

void updateWithoutInput()  // 与用户输入无关的更新
{
// 挡板和小圆碰撞,小圆反弹
if ( ( (ball_y radius >= bar_top) && (ball_y radius < bar_bottom-bar_high/3) )
||  ( (ball_y-radius <= bar_bottom) && (ball_y-radius > bar_top-bar_high/3) ) )
if ( (ball_x>=bar_left) && (ball_x<=bar_right) )
ball_vy = -ball_vy;

// 更新小圆坐标
ball_x = ball_x ball_vx;
ball_y = ball_y ball_vy;

// 小圆和边界碰撞
if ((ball_x==radius)||(ball_x==Width-radius))
ball_vx = -ball_vx;
if ((ball_y==radius)||(ball_y==High-radius))
ball_vy = -ball_vy;

// 判断小圆是否和某个砖块碰撞
int i,brick_left,brick_right,brick_bottom;
for (i=0;i<Brick_num;i )
{
if (isBrickExisted[i]) // 砖块存在,才判断
{
brick_left = i*brick_width;
brick_right = brick_left brick_width;
brick_bottom = brick_high;
if ( (ball_y==brick_bottom radius) && (ball_x>=brick_left) && (ball_x<=brick_right) )
{
isBrickExisted[i] = 0;
ball_vy = -ball_vy;
}
}
}
}

void updateWithInput()  // 与用户输入有关的更新
{
char input;
if(kbhit())  // 判断是否有输入
{
input = getch();  // 根据用户的不同输入来移动,不必输入回车
if (input == 'a' && bar_left>0)   
{
bar_x = bar_x-15;  // 位置左移
bar_left = bar_x - bar_width/2;
bar_right = bar_x bar_width/2;
}
if (input == 'd' && bar_right<Width)
{
bar_x = bar_x 15;  // 位置右移
bar_left = bar_x - bar_width/2;
bar_right = bar_x bar_width/2;
}
if (input == 'w' && bar_top>0)   
{
bar_y = bar_y-15;  // 位置左移
bar_top = bar_y - bar_high/2;
bar_bottom = bar_y bar_high/2;
}
if (input == 's' && bar_bottom<High)
{
bar_y = bar_y 15;  // 位置右移
bar_top = bar_y - bar_high/2;
bar_bottom = bar_y bar_high/2;
}
}
}

void gameover()
{
EndBatchDraw();
closegraph();
}

int main()
{
startup();  // 数据初始化
while (1)  //  游戏循环执行
{
clean();  // 把之前绘制的内容取消
updateWithoutInput();  // 与用户输入无关的更新
updateWithInput();     // 与用户输入有关的更新
show();  // 显示新画面
}
gameover();     // 游戏结束、后续处理
return 0;
}

标签:

实例下载地址

c++ 游戏反弹球消砖块

不能下载?内容有错? 点击这里报错 + 投诉 + 提问

好例子网口号:伸出你的我的手 — 分享

网友评论

发表评论

(您的评论需要经过审核才能显示)

查看所有0条评论>>

小贴士

感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。

  • 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
  • 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
  • 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
  • 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。

关于好例子网

本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明

;
报警