c++实现打砖块游戏——童年回忆

 采用分文件来进行书写,便于后续的维护和改良,首先声明自己需要用到的函数,准备好球类、木板类。

#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;
	
}

  • 1
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值