Qt打砖块游戏

本文介绍了使用C++和Qt框架开发打砖块游戏的实现细节,包括Ball类代表小球,Brick类表示砖块,Paddle类作为划浆,Game类控制游戏逻辑,以及GameWindow类作为主窗口。重点讨论了小球的移动速度、方向调整、碰撞检测以及窗口缩放时的精度问题。
摘要由CSDN通过智能技术生成


概述

程序由自定义的 5 个类构成:
     • Ball 类代表小球,包含小球所在矩形于窗格 (frame) 中的位置,另外,小球还包含变量来表示小球的颜色、运动速度等属性。

      • Brick 类代表砖块,砖块同样包含尺寸、方位、颜色这些信息。

     • Paddle 类代表划浆,划浆包含移动步长、方位、颜色这些信息。
     • Game 类代表游戏窗格,用来直接控制小球的运行,划浆的移动,及砖块的绘制。
    • GameWindow 类代表程序的主窗口,通过菜单和工具栏的动作来间接控制游戏的运行与停止、小球的运行速度等。

小球 Ball

ball.h

#ifndef _BALL_H_
#define _BALL_H_

#include <QRect>
#include <QColor>

class Ball
{
public:
    Ball( qreal, qreal, qreal, QColor = Qt::green );
    ~Ball();
    
    void move();
    QRectF rect() const;

    void setShape( const QRectF & );
    void setColor( const QColor & );
    void setDirX( qreal );
    void setDirY( qreal );
    void setSpeed( qreal );
    
    QRectF getShape() const;
    QColor getColor() const;
    qreal getDirX() const;
    qreal getDirY() const;
    qreal getSpeed() const;
    
private:
    QRectF *shape;
    QColor color;

    qreal dirX;
    qreal dirY;
    qreal speed;
};

#endif 

        shape 用来指向包含小球位置和尺寸信息的 QRectF 对象,这里使用 QRectF 而不是 QRect,

是因为 QRectF 中使用 qreal 变量来保存矩形的位置和尺寸信息,而QRect 使用 int 类型变量来保

存信息,因此,使用 QRectF 能跟精确地保存小球的位置和尺寸信息,尤其是当对程序窗口进行缩放

时,这种效果更明显,其中,qreal 类型相当于 double 类型。

        color 变量用来包含小球的颜色。此外,使用 qreal 类型dirX 和 dirY 变量来分别表示小球在水

平方向和垂直方向的运动方向。setShape 函数用来对 shape 变量进行新的设置,而 getShape 函

数用来返回QRectF 对象,包含 shape 变量的副本。当窗口需要进行绘制时,这时窗口就需要使用 

getShape 函数来得知小球的大小和方位,这样才能正确绘制小球。

        setDirX 和 setDirY 函数用来设置小球的移动方向,特别时当小球与窗口边缘、划浆、砖块进行

碰撞时,就需要改变小球的移动方向。

ball.cpp

#include "ball.h"

Ball::Ball( qreal x, qreal y, qreal radius, QColor ballColor )
{
    dirX = 1.0;
    dirY = -1.0;
    speed = 1.0;
    
    shape = new QRectF( x, y, radius, radius );
    color = ballColor;
}

Ball::~Ball()
{
    delete shape;
}

void Ball::setShape( const QRectF &newShape )
{
    shape->setRect( newShape.left(), newShape.top(),
		    newShape.width(), newShape.height() );
}

void Ball::setColor( const QColor &newColor )
{
    color = newColor;
}

void Ball::setDirX( qreal newDirX )
{
    dirX = newDirX;
}

void Ball::setDirY( qreal newDirY )
{
    dirY = newDirY;
}

QRectF Ball::getShape() const
{
    return QRectF( shape->left(), shape->top(),
		  shape->width(), shape->height() );
}

QColor Ball::getColor() const
{
    return color;
}

qreal Ball::getDirX() const
{
    return dirX;
}

qreal Ball::getDirY() const
{
    return dirY;
}

void Ball::move()
{
    shape->setLeft( shape->left() + dirX );
    shape->setRight( shape->right() + dirX );
    shape->setTop( shape->top() + dirY );
    shape->setBottom( shape->bottom() + dirY );
}

void Ball::setSpeed( qreal newSpeed )
{
    speed = newSpeed;
    setDirX( speed );
    setDirY( -speed );
}

qreal Ball::getSpeed() const
{
    return speed;
}

setSpeed 函数用来设置小球的移动速度,move 函数则用来使小球移动,需要注意的是,调用 QRectF 

函数的 setLeft 函数设置小球的左边缘时,小球的宽度也会随着改变,

为防止小球的尺寸发生改变,需要使用 setRight 函数进行同样的设置,这样才能保持小球的宽度不变。

砖块 Brick

brick.h

#ifndef _BRICK_H_
#define _BRICK_H_

#include <QRect>
#include <QColor>

class Brick
{
public:
    Brick( qreal, qreal, qreal, qreal, QColor = Qt::red );
    ~Brick();

    void setShape( const QRectF & );
    void setColor( const QColor & );

    QRectF getShape() const;
    QColor getColor() const;
    
    
private:
    QRectF *shape;
    QColor color;
};

#endif 

brick.cpp

#include "brick.h"

Brick::Brick( qreal x, qreal y, qreal width, qreal height, QColor brickColor )
{
    shape = new QRectF( x, y, width, height );
    color = brickColor;
}

Brick::~Brick()
{
    delete shape;
}


void Brick::setShape( const QRectF &newShape )
{
    shape->setRect( newShape.left(), newShape.top(),
		    newShape.width(), newShape.height() );
}

QRectF Brick::getShape() const
{
    return QRectF( shape->left(), shape->top(),
		  shape->width(), shape->height() );
}

void Brick::setColor( const QColor &newColor )
{
    color = newColor;
}

QColor Brick::getColor() const
{
    return color;
}
划浆 Paddle

paddle.h

#ifndef _PADDLE_H_
#define _PADDLE_H_

#include <QRect>
#include <QColor> 

class Paddle
{
public:
    Paddle( qreal, qreal, q
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值