概述
采用分文件来进行书写,便于后续的维护和改良,首先声明自己需要用到的函数,准备好球类、木板类。
#pragma once
#include<easyx.h>
//木板类
class Board
{
public:
Board(int x,int y,int width,int height,int speed,COLORREF color);
int x;
int y;
int width;
int height;
int speed;
COLORREF color;
};
//球类
class Ball
{
public:
Ball(int circle,int vx,int vy,COLORREF color );
int circle;
int x=300;
int y=300;
int vx;
int vy;
COLORREF color;
};
//画砖块
void drawbrick();
//画木板
void drawboard0(Board &board);
//画球
void drawball(Ball &ball);
//碰撞检测
void collsion(Ball &ball,Board &board);
//砖块的样本,往数组里装随机数,随机显示不同的砖块
void sample();
接下来,把声明好的函数进行一一定义,如下所示:
#include"brick.h"
#include<time.h>
#include<easyx.h>
using namespace std;
int space = 50;
int brickarry[4][14];
void sample()
{
srand((unsigned)time(NULL));
for (int i = 0; i < 4; i++)
for (int j = 0; j < 14; j++)
{
brickarry[i][j] = rand() % 4 + 1;
}
}
//画砖块
void drawbrick()
{
IMAGE img01, img02, img03, img04;
//导入砖块素材
loadimage(&img01, L"01.jpeg", space, space);
loadimage(&img02, L"02.jpeg", space, space);
loadimage(&img03, L"03.jpeg", space, space);
loadimage(&img04, L"04.jpeg", space, space);
for (int i = 0; i < 4; i++)
for (int j = 0; j < 14; j++)
{
switch (brickarry[i][j])
{
case 1:
putimage(j * space, i * space, &img01);
break;
case 2:
putimage(j * space, i * space, &img02);
break;
case 3:
putimage(j * space, i * space, &img03);
break;
case 4:
putimage(j * space, i * space, &img04);
break;
default:
break;
}
}
}
//画球
void drawball(Ball &ball)
{
ball.x += ball.vx;
ball.y += ball.vy;
setfillcolor(ball.color);
fillcircle(ball.x, ball.y, ball.circle);
}
Ball::Ball(int circle, int vx, int vy, COLORREF color)
{
this->circle = circle;
this->vx = vx;
this->vy = vy;
this->color = color;
}
//碰撞检测
void collsion(Ball& ball, Board& board)
{
if (ball.x - ball.circle < 0 || ball.x + ball.circle >700)
ball.vx = -ball.vx;
if (ball.y + ball.circle == 750-board.height&&ball.x<board.x+board.width&&ball.x>board.x)
ball.vy = -ball.vy;
if (ball.y < 5 * space&&ball.y>ball.circle)
{
if ((ball.y - ball.circle) % space == 0&& brickarry[ball.y / space - 1][ball.x / space ] != 0)
{
ball.vy = -ball.vy;
brickarry[ball.y / space-1][ball.x / space] = 0;
}
}
if (ball.y < 4 * space && ball.y>ball.circle)
{
if ((ball.x + ball.circle) % space == 0 && ball.x + ball.circle < 650)
brickarry[ball.y / space][ball.x / space + 1] = 0;
if ((ball.x - ball.circle) % space == 0)
brickarry[ball.y / space][ball.x / space - 1] = 0;
}
//如果木板没有接到球,则游戏结束
if (ball.y > 740)
{
settextcolor(RED);
outtextxy(300, 370, L"游戏结束");
}
}
//传类实现画木板
Board::Board(int x, int y, int width, int height, int speed, COLORREF color)
{
this->x = x;
this->y = y;
this->width = width;
this->height = height;
this->speed = speed;
this->color = color;
}
//画木板
void drawboard0(Board &board)
{
//扫描按键,根据按下的键进行移动木板
if (GetAsyncKeyState('A') || GetAsyncKeyState(VK_LEFT) && board.x > 0)
{
board.x -= board.speed;
}
if (GetAsyncKeyState('D') || GetAsyncKeyState(VK_RIGHT) && (board.x + board.width) < 700)
{
board.x += board.speed;
}
setfillcolor(board.color);
fillrectangle(board.x, board.y, board.x + board.width, 750);
}
最后,在主函数main中,调用各个子涵数就可以了
#include<iostream>
#include<time.h>
#include<easyx.h>
#include<conio.h>
#include"brick.h"
using namespace std;
int main()
{
initgraph(700, 750);
Board board(300, 730, 100,20, 4, WHITE);//创建木板
Ball ball(10, 2, 2, YELLOW);
//BeginBatchDraw()、FlushBatchDraw()、EndBatchDraw()用于防闪烁
BeginBatchDraw();
sample();
while (1)
{
cleardevice(); //清除显示,刷新砖块显示
drawbrick();
drawball(ball);
collsion(ball, board);
drawboard0(board);
FlushBatchDraw();
}
EndBatchDraw();
_getch();
closegraph();
return 0;
}
最后
以上就是忧虑蜜蜂为你收集整理的c++实现打砖块游戏——童年回忆的全部内容,希望文章能够帮你解决c++实现打砖块游戏——童年回忆所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复