Qt打砖块游戏


概述

程序由自定义的 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
  • 12
    点赞
  • 51
    收藏
    觉得还不错? 一键收藏
  • 6
    评论
砖块是一款经典的游戏,通过qt.4可以很容易地实现这个游戏。下面是一个简单的打砖块游戏示例: 首先,我们需要一个主窗口,可以使用QMainWindow类来实现。在主窗口中,我们需要绘制游戏区域和球、板、砖块游戏元素。可以使用QPainter类来进行绘制。 接着,我们需要定义游戏逻辑。游戏逻辑包括球的运动轨迹、板的移动、砖块的消除等。当球碰到砖块时,砖块会消失,并且玩家得分增加。当球碰到板时,球的运动方向会改变。如果球碰到边界,游戏失败。 最后,我们需要添加交互逻辑。玩家可以通过键盘控制板的移动。可以使用QKeyEvent类来实现键盘事件的处理。 下面是一个简单的示例代码,仅供参考: ```cpp #include <QtGui> class MainWindow : public QMainWindow { Q_OBJECT public: MainWindow(QWidget *parent = 0); protected: void paintEvent(QPaintEvent *event); void keyPressEvent(QKeyEvent *event); private: int ball_x, ball_y; int ball_dx, ball_dy; int board_x, board_y; int board_dx; int brick[5][10]; int score; void initGame(); void updateGame(); }; MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) { setFixedSize(400, 400); setWindowTitle("打砖块"); initGame(); QTimer *timer = new QTimer(this); connect(timer, SIGNAL(timeout()), this, SLOT(updateGame())); timer->start(20); } void MainWindow::paintEvent(QPaintEvent *event) { QPainter painter(this); // 绘制游戏区域 painter.setPen(Qt::black); painter.drawRect(0, 0, 400, 400); // 绘制球 painter.setBrush(Qt::red); painter.drawEllipse(ball_x, ball_y, 10, 10); // 绘制板 painter.setBrush(Qt::blue); painter.drawRect(board_x, board_y, 60, 10); // 绘制砖块 for (int i = 0; i < 5; i++) { for (int j = 0; j < 10; j++) { if (brick[i][j] > 0) { QRect rect(j*40, i*20, 40, 20); painter.setBrush(Qt::green); painter.drawRect(rect); } } } // 绘制得分 painter.setPen(Qt::red); painter.drawText(10, 390, QString("Score: %1").arg(score)); } void MainWindow::keyPressEvent(QKeyEvent *event) { if (event->key() == Qt::Key_Left) { board_dx = -5; } else if (event->key() == Qt::Key_Right) { board_dx = 5; } } void MainWindow::initGame() { ball_x = 190; ball_y = 290; ball_dx = 5; ball_dy = -5; board_x = 170; board_y = 380; board_dx = 0; for (int i = 0; i < 5; i++) { for (int j = 0; j < 10; j++) { brick[i][j] = 1; } } score = 0; } void MainWindow::updateGame() { // 移动球 ball_x += ball_dx; ball_y += ball_dy; // 碰到边界 if (ball_x < 0 || ball_x > 390) { ball_dx = -ball_dx; } if (ball_y < 0) { ball_dy = -ball_dy; } if (ball_y > 390) { initGame(); return; } // 碰到板 QRect rect(board_x, board_y, 60, 10); if (rect.intersects(QRect(ball_x, ball_y, 10, 10))) { ball_dy = -ball_dy; } // 碰到砖块 int row = ball_y / 20; int col = ball_x / 40; if (row >= 0 && row < 5 && col >= 0 && col < 10 && brick[row][col] > 0) { brick[row][col] = 0; ball_dy = -ball_dy; score += 10; } // 移动板 board_x += board_dx; if (board_x < 0 || board_x > 340) { board_dx = 0; } update(); } int main(int argc, char *argv[]) { QApplication app(argc, argv); MainWindow window; window.show(); return app.exec(); } ``` 在上面的代码中,我们使用了QTimer类来定时更新游戏状态,使用QKeyEvent类来处理键盘事件,使用QPainter类来进行游戏元素的绘制。通过以上的代码,我们就可以打造出一个简单的打砖块游戏了。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值