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

相关推荐
ShineWinsu11 分钟前
对于Linux:模版方法类的解析以及socket、TcpSocket的封装
linux·c++·面试·socket·模板方法模式·封装·tcpsocket
hold?fish:palm1 小时前
9 找到字符串中所有字母异位词
c++·算法·leetcode
啦啦啦啦啦zzzz2 小时前
算法:回溯算法
c++·算法·leetcode
Lzg_na3 小时前
订阅发布模块事例
c++
郝学胜-神的一滴3 小时前
[简化版 GAMES 104] 现代游戏引擎 02:拆解现代游戏引擎5+1层级架构,吃透引擎底层核心逻辑
c++·unity·架构·游戏引擎·图形渲染·unreal engine·系统设计
汉克老师3 小时前
2026年CSP-J初赛分数线-----预测
c++·csp-j·小学生·学c++编程
脱胎换骨-军哥3 小时前
C++数据库存储引擎内核开发:B+树索引与MVCC并发控制的完整实现
数据库·c++·b树
小保CPP4 小时前
OpenCV C++将多张图像合并为webp动图
c++·人工智能·opencv·计算机视觉
德迅云安全-上官4 小时前
德迅云安全游戏盾与DDoS高防IP对比:差异、优势及选型指南
tcp/ip·游戏·ddos
乐观勇敢坚强的老彭4 小时前
信奥C++一维数组笔记
开发语言·c++·笔记