qt 坦克大战游戏 GUI绘制

关于本章节中使用的图形绘制类,如QGraphicsView、QGraphicsScene等的详细使用说明请参见我的另一篇文章:

图形绘制QGraphicsView、QGraphicsScene、QGraphicsItem、Qt GUI-CSDN博客

本文将模仿坦克大战游戏,目前只绘制出一辆坦克,并能发射炮弹

源代码下载地址:https://download.csdn.net/download/zhouhui1982/88784330

main.cpp文件中代码

#include <QApplication>

#include <QGraphicsScene>

#include <QGraphicsView>

#include <QGraphicsRectItem>

#include <QKeyEvent>

#include <QTimer>

//坦克类对象,构建坦克主体

class Tank : public QGraphicsRectItem {

public:

Tank() {

// 坦克身体

setRect(0, 0, 30, 50);

setBrush(Qt::blue);

// 炮塔

turret = new QGraphicsRectItem(10, -20, 10, 30);

turret->setParentItem(this);

// 设置炮塔颜色

turret->setBrush(Qt::red);

}

//重写键盘事件,判断上下左右、空格按钮

void keyPressEvent(QKeyEvent *event) override {

// Handle key presses to control the tank

switch (event->key()) {

case Qt::Key_Left:

moveBy(-10, 0);

break;

case Qt::Key_Right:

moveBy(10, 0);

break;

case Qt::Key_Up:

moveBy(0, -10);

break;

case Qt::Key_Down:

moveBy(0, 10);

break;

case Qt::Key_Space: //如果是空格键,发射炮弹

//绘制第一发炮弹

QGraphicsEllipseItem *bulletItem = new QGraphicsEllipseItem(0, 0, 5, 10);

bulletItem->setPos(mapToScene(12, -50));

bulletItem->setBrush(Qt::red);

scene()->addItem(bulletItem);

//记录第一发炮弹位置

QPointF pos = bulletItem->pos();

//每隔500ms,发射下一发炮弹,看着像连续的动画效果

QTimer::singleShot(500, =() {

//移除第一发炮弹

scene()->removeItem(bulletItem);

//绘制第二发炮弹

QGraphicsEllipseItem *bulletItem1 = new QGraphicsEllipseItem(0, 0, 5, 10);

bulletItem1->setPos(pos.x(), pos.y() - 30);

bulletItem1->setBrush(Qt::red);

scene()->addItem(bulletItem1);

QTimer::singleShot(500, =() {

//移除第二发炮弹

scene()->removeItem(bulletItem1);

//绘制第三发炮弹

QGraphicsEllipseItem *bulletItem2 = new QGraphicsEllipseItem(0, 0, 5, 10);

bulletItem2->setPos(pos.x(), pos.y() - 80);

bulletItem2->setBrush(Qt::red);

scene()->addItem(bulletItem2);

QTimer::singleShot(500, =() {

//移除第三发炮弹

scene()->removeItem(bulletItem2);

});

});

});

break;

// default:

// break;

}

}

private:

QGraphicsRectItem *turret; // 炮塔

};

//绘制主画布

class TankGame : public QGraphicsView {

public:

TankGame() {

scene = new QGraphicsScene(this); // 2D 图形 画布

setScene(scene);

//创建tank实例

tank = new Tank();

//加载tank实例到画布

scene->addItem(tank);

//抗锯齿渲染 减少图形边缘的锯齿状效果 使得图形看起来更加清晰和平滑

setRenderHint(QPainter::Antialiasing);

//平滑的像素变换渲染 用于对图片进行变换(如旋转、缩放等)时保持图片的平滑性

setRenderHint(QPainter::SmoothPixmapTransform);

//边界框发生变化时进行更新

setViewportUpdateMode(QGraphicsView::BoundingRectViewportUpdate);

//高质量抗锯齿渲染 与上边的代码类似 但是这个设置更加强调图形的质量而不是性能

setRenderHint(QPainter::HighQualityAntialiasing);

//设置了 画布 的大小为 800x600 像素

setFixedSize(800, 600);

}

//画布的键盘事件,调用tank类中的键盘事件

protected:

void keyPressEvent(QKeyEvent *event) override {

// Forward key events to the tank

tank->keyPressEvent(event);

}

private:

QGraphicsScene *scene; //画布实例

Tank *tank; //坦克实例

};

//主函数

int main(int argc, char *argv\[\])

{

QApplication a(argc, argv);

//声明画布

TankGame tankGame;

//显示画布

tankGame.show();

return a.exec();

}

tank.pro文件中代码

QT -= core gui

greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

CONFIG += c++11

The following define makes your compiler emit warnings if you use

any Qt feature that has been marked deprecated (the exact warnings

depend on your compiler). Please consult the documentation of the

deprecated API in order to know how to port your code away from it.

DEFINES += QT_DEPRECATED_WARNINGS

You can also make your code fail to compile if it uses deprecated APIs.

In order to do so, uncomment the following line.

You can also select to disable deprecated APIs only up to a certain version of Qt.

#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0

SOURCES += \

main.cpp

Default rules for deployment.

qnx: target.path = /tmp/$${TARGET}/bin

else: unix:!android: target.path = /opt/$${TARGET}/bin

!isEmpty(target.path): INSTALLS += target

相关推荐
博客18007 小时前
酷宝的使用方法,超好用的免费界面库,C++、MFC可用
c++·mfc·界面库·库来帮·酷宝
郝学胜_神的一滴9 小时前
CMake 026:属性体系精讲、四大作用域全解 & 实战代码落地
c++·cmake
nujnewnehc11 小时前
不会 py, 用 ai 写了个游戏辅助的感受
人工智能·游戏
众少成多积小致巨1 天前
JNI (Java Native Interface) 技术手册中文参考指南
android·java·c++
jump_jump1 天前
为了重玩金庸群侠传,我研究了一下 Ruffle 怎么复活 Flash
游戏·rust·github
xcyxiner2 天前
DicomViewer (dcmtk读取dcm文件)5
qt
XIAOHEZIcode2 天前
Linux系统鼠标偏移常见原因以及修复方案
linux·运维·游戏
xcyxiner3 天前
DicomViewer (后台线程处理文件)4
qt
Aloys_Code3 天前
逆向一个被遗忘的DVD游戏格式:从DES加密到Rust模拟器
游戏·模拟器·retroarch·复古游戏·native32·sunplus·赤刃·钢铁风暴
xcyxiner3 天前
DicomViewer (添加模型类)3
qt