承接上篇:

http://blog.csdn.net/hao_zong_yin/article/details/74540652

补充包:

#ifndef SUPPLY_H
#define SUPPLY_H#include "flyer.h"class Supply : public Flyer
{
public:Supply(qreal w, qreal h, qreal speed, const QPixmaps &pixs, QGraphicsScene *scene, QGraphicsItem *parent = 0):Flyer(w, h, speed, pixs, scene, parent) {}~Supply() {}QRectF boundingRect() const {return m_pixs.at(0).rect();}QPainterPath shape() const {QPainterPath path;path.addRect(boundingRect());return path;}void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) {Q_UNUSED(option);Q_UNUSED(widget);painter->drawPixmap(0, 0, m_pixs.at(0));}void advance(int) {if (!checkPos(DOWN)) {posLost();return;}QPointF pos = scenePos();pos.ry() += m_speed;setPos(pos);}void posLost() {setVisible(false);deleteLater();}void doCollide() {}void fall() {setVisible(false);deleteLater();}
};#endif // SUPPLY_H

补充包——血包

#ifndef BLOODSUPPLY_H
#define BLOODSUPPLY_H#include "supply.h"class BloodSupply : public Supply
{
public:BloodSupply(qreal w, qreal h, qreal speed, const QPixmaps &pixs, QGraphicsScene *scene, QGraphicsItem *parent = 0);~BloodSupply();int name() const;
};#endif // BLOODSUPPLY_H
#include "bloodsupply.h"BloodSupply::BloodSupply(qreal w, qreal h, qreal speed, const QPixmaps &pixs, QGraphicsScene *scene, QGraphicsItem *parent):Supply(w, h, speed, pixs, scene, parent) {}BloodSupply::~BloodSupply() {}int BloodSupply::name() const
{return BLOODSUPPLYNAME;
}

补充包——炸弹包

#ifndef BOMBSUPPLY_H
#define BOMBSUPPLY_H#include "supply.h"class BombSupply : public Supply
{
public:BombSupply(qreal w, qreal h, qreal speed, const QPixmaps &pixs, QGraphicsScene *scene, QGraphicsItem *parent = 0);~BombSupply();int name() const;
};#endif // BOMBSUPPLY_H
#include "bombsupply.h"BombSupply::BombSupply(qreal w, qreal h, qreal speed, const QPixmaps &pixs, QGraphicsScene *scene, QGraphicsItem *parent):Supply(w, h, speed, pixs, scene, parent) {}BombSupply::~BombSupply() {}int BombSupply::name() const {return BOMBSUPPLYNAME;
}

补充包——子弹包

#ifndef BULLETSUPPLY_H
#define BULLETSUPPLY_H#include "supply.h"class BulletSupply : public Supply
{
public:BulletSupply(qreal w, qreal h, qreal speed, const QPixmaps &pixs, QGraphicsScene *scene, QGraphicsItem *parent = 0);~BulletSupply();int name() const;
};#endif // BULLETSUPPLY_H
#include "bulletsupply.h"BulletSupply::BulletSupply(qreal w, qreal h, qreal speed, const QPixmaps &pixs, QGraphicsScene *scene, QGraphicsItem *parent):Supply(w, h, speed, pixs, scene, parent) {}BulletSupply::~BulletSupply() {}int BulletSupply::name() const {return BULLETSUPPLYNAME;
}

炸弹:

#ifndef PLAYERBOMB_H
#define PLAYERBOMB_H#include "bullet.h"
#include "enemybullet.h"class Bomb : public Bullet
{
public:Bomb(qreal angel, qreal w, qreal h, qreal speed, const QPixmaps &pixs, QGraphicsScene *scene, QGraphicsItem *parent = 0);~Bomb();QPainterPath shape() const;QRectF boundingRect() const;void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);int name() const;void advance(int);void posLost();void doCollide();void fall();
};#endif // BOMB_H
#include "bomb.h"Bomb::Bomb(qreal angel, qreal w, qreal h, qreal speed, const QPixmaps &pixs, QGraphicsScene *scene, QGraphicsItem *parent):Bullet(angel, w, h, speed, pixs, scene, parent) {}Bomb::~Bomb() {}QRectF Bomb::boundingRect() const {return m_pixs.at(m_pixpos).rect();
}QPainterPath Bomb::shape() const
{QPainterPath path;path.addRect(boundingRect());return path;
}void Bomb::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{Q_UNUSED(option);Q_UNUSED(widget);painter->drawPixmap(0, 0, m_pixs.at(m_pixpos));
}int Bomb::name() const
{return BOMBNAME;
}void Bomb::advance(int)
{if (!checkPos(UP) || !checkPos(DOWN) || !checkPos(LEFT) || !checkPos(RIGHT)) {posLost();return;}m_step++;if (m_step == BOMBPIXSTEP) {m_step = 0;m_pixpos++;if (m_pixpos == 2) m_pixpos = 0;}QPointF pos = scenePos();pos.rx() -= xSpeed;pos.ry() -= ySpeed;setPos(pos);doCollide();
}void Bomb::posLost() {setVisible(false);deleteLater();
}void Bomb::doCollide()
{foreach (QGraphicsItem *t, collidingItems()) {if (t->type() != UnFlyer::TYPE) {Flyer *flyer = static_cast<Flyer*>(t);switch (flyer->name()) {case ENEMYBULLETNAME:flyer->fall();break;}}}
}void Bomb::fall() {setVisible(false);deleteLater();
}

随机数

#ifndef RANDOMIZER_H
#define RANDOMIZER_H#include <QObject>class Randomizer : public QObject
{Q_OBJECT
public:static int creat(int x);static int creat(int x, int y);
private:explicit Randomizer(QObject *parent = 0);
};#endif // RANDOMIZER_H
#include "randomizer.h"Randomizer::Randomizer(QObject *parent) : QObject(parent) {}int Randomizer::creat(int x)
{return qrand() % x;
}int Randomizer::creat(int x, int y)
{return qrand() % (y - x) + x;
}

菜单

#ifndef MENUWIDGET_H
#define MENUWIDGET_H#include <QDialog>
#include <QStackedWidget>
#include <QVBoxLayout>
#include <QPushButton>class MenuWidget : public QDialog
{Q_OBJECT
public:explicit MenuWidget(bool isRunning, QWidget *parent = 0);
signals:void sig_newGame();void sig_quit();
protected slots:void slt_quit();void slt_newGame();private:QPushButton *m_new;QPushButton *m_back;QPushButton *m_quit;bool states;void initUI();
};#endif // MENUWIDGET_H
#include "menuwidget.h"MenuWidget::MenuWidget(bool isRunning, QWidget *parent) : QDialog(parent), states(isRunning) {setWindowFlags(Qt::FramelessWindowHint);initUI();setMinimumSize(150,300);setMaximumSize(150,300);move(parent->rect().center() - QPoint(150/2,300/2));connect(m_new, SIGNAL(clicked()), this, SLOT(slt_newGame()));connect(m_quit, SIGNAL(clicked()), this ,SLOT(slt_quit()));connect(m_back, SIGNAL(clicked()), this, SLOT(close()));}void MenuWidget::initUI()
{m_new = new QPushButton("New Game");m_back = new QPushButton("Back");m_quit = new QPushButton("Quit");QVBoxLayout *mainLayout = new QVBoxLayout;mainLayout->addStretch();mainLayout->addWidget(m_new);mainLayout->addWidget(m_back);mainLayout->addWidget(m_quit);mainLayout->addStretch();setLayout(mainLayout);if (states) {m_new->setDisabled(true);} else {m_back->setDisabled(true);}
}void MenuWidget::slt_quit()
{emit sig_quit();close();
}void MenuWidget::slt_newGame()
{emit sig_newGame();close();
}

控制台(游戏逻辑, 注释的代码崩了)

#ifndef SPACE_H
#define SPACE_H#include "planefactory.h"
#include "playerPlane.h"
#include "bloodsupply.h"
#include "bombsupply.h"
#include "bulletsupply.h"
#include "randomizer.h"
#include "menuwidget.h"class Space : public QGraphicsView
{Q_OBJECT
public:Space(QWidget *parent = 0);void init();void bloodsupply();void bombsupply();void bulletsupply();void enemys();
protected:void mouseDoubleClickEvent(QMouseEvent *event);
private://QGraphicsTextItem *scoretext, *bloodtext, *bombtext;QGraphicsScene *m_scene;QTimer *m_timer;PlayerPlane *m_player;uint m_scores, m_bloods, m_bombs, m_level, m_step;bool boss;//boss战标识bool isRunning;
signals:void sig_menu();
protected slots:void slt_newGame();void slt_playerDead();void slt_startGame();void slt_pauseGame();void slt_updata();void slt_addscore(int);void slt_menu();//void slt_updateScore(int);//void slt_updateBlood(int);//void slt_updateBomb(int);
};#endif // SPACE_H
#include "space.h"Space::Space(QWidget *parent) : QGraphicsView(parent), boss(false),  isRunning(false) {m_scene = new QGraphicsScene;m_scene->setSceneRect(0, 0, SCENEWIDTH, SCENEHEIGHT);setScene(m_scene);setWindowFlags(Qt::FramelessWindowHint);setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);setCacheMode(QGraphicsView::CacheBackground);setViewportUpdateMode(QGraphicsView::BoundingRectViewportUpdate);setOptimizationFlags(QGraphicsView::DontClipPainter | QGraphicsView::DontSavePainterState);setRenderHint(QPainter::Antialiasing);QPixmap pix(SCENEWIDTH, SCENEHEIGHT);pix.load(background);QPixmap temp = pix.scaled(SCENEWIDTH, SCENEHEIGHT, Qt::KeepAspectRatioByExpanding);setBackgroundBrush(temp);m_timer = new QTimer;connect(m_timer, SIGNAL(timeout()), this, SLOT(slt_updata()));connect(this, SIGNAL(sig_menu()), this, SLOT(slt_menu()));init();
}void Space::init() {
//    scoretext = new QGraphicsTextItem("0");
//    scoretext->setPos(0, 0);
//    m_scene->addItem(scoretext);//    bloodtext = new QGraphicsTextItem("0");
//    bloodtext->setPos(0, 10);
//    m_scene->addItem(bloodtext);//    bombtext = new QGraphicsTextItem("0");
//    bombtext->setPos(0, 20);
//    m_scene->addItem(bombtext);m_scores = 0, m_bloods = PLAYERPLANEBLOOD, m_bombs = 0, m_level = 1, m_step = 0;QPixmaps t;t.append(QPixmap(playerplane0));t.append(QPixmap(playerplane1));t.append(QPixmap(playerplane2));t.append(QPixmap(playerplane3));m_player = new PlayerPlane(PLAYERPLANEBLOOD, PLAYERPLANESIZE, PLAYERPLANESIZE, PLAYERPLANESPEED, t, m_scene);m_player->setFocus();connect(m_player, SIGNAL(sig_fall()), this, SLOT(slt_playerDead()));//connect(m_player, SIGNAL(sig_score(int)), this, SLOT(slt_updateScore(int)));//connect(m_player, SIGNAL(sig_blood(int)), this, SLOT(slt_updateBlood(int)));//connect(m_player, SIGNAL(sig_bomb(int)), this, SLOT(slt_updateBomb(int)));
}void Space::slt_newGame()
{m_scene->clear();init();m_timer->start(50);isRunning = true;
}void Space::slt_playerDead()
{m_timer->stop();isRunning = false;
}void Space::slt_startGame()
{if (isRunning) m_timer->start(50);
}void Space::slt_pauseGame()
{if (isRunning) m_timer->stop();
}void Space::slt_updata()
{m_scene->advance();m_step++;if (m_step % 50 == 0 && !boss) {enemys();}if (m_step % 233 == 0) {bulletsupply();}if (m_step % 666 == 0) {bombsupply();m_step = 0;}if (m_step % 2333 == 0) {bloodsupply();}
}void Space::slt_addscore(int score)
{m_scores += score;if (score == BOSSSCORE) {boss = false;m_level = 1;m_scores = 0;//slt_updateScore(m_scores);}if (m_scores != 0 && m_scores % 50 == 0) {m_level++;if (m_level == 7) m_level = 1;}
}void Space::bloodsupply()
{QPixmaps t;t.append(QPixmap(bloodsupplypix));BloodSupply *bloodsupply = new BloodSupply(BLOODSUPPLYSIZE, BLOODSUPPLYSIZE, BLOODSUPPLYSPEED, t, m_scene);qreal x = Randomizer::creat(SCENEWIDTH - BLOODSUPPLYSIZE);bloodsupply->setPos(x, 0);
}void Space::bombsupply()
{QPixmaps t;t.append(QPixmap(bombsupplypix));BloodSupply *bombsupply = new BloodSupply(BOMBSUPPLYSIZE, BOMBSUPPLYSIZE, BOMBSUPPLYSPEED, t, m_scene);qreal x = Randomizer::creat(SCENEWIDTH - BOMBSUPPLYSIZE);bombsupply->setPos(x, 0);
}void Space::bulletsupply()
{QPixmaps t;t.append(QPixmap(bulletsupplypix));uint x = Randomizer::creat(SCENEWIDTH - BULLETSUPPLYSIZE);BulletSupply *bulletsupply = new BulletSupply(BULLETSUPPLYSIZE, BULLETSUPPLYSIZE, BULLETSUPPLYSPEED, t, m_scene);bulletsupply->setPos(x,0);
}void Space::enemys()
{if (m_level == 6) {PlaneFactory::BossPlanes bossplanes = PlaneFactory::bcreator(1, scene());foreach (Boss *b, bossplanes) {int x = Randomizer::creat(SCENEWIDTH - BOSSSIZE);b->setPos(x, 0);connect(b, SIGNAL(sig_score(int)), this, SLOT(slt_addscore(int)));}boss = true;}else {PlaneFactory::FishPlanes fishplanes = PlaneFactory::fcreator(m_level, scene());foreach (Fish *f, fishplanes) {int z =  Randomizer::creat(6);if (z == 1) {int y = Randomizer::creat(SCENEHEIGHT / 2 - FISHSIZE);f->setPos(0, y);}else if (z == 2) {int y = Randomizer::creat(SCENEHEIGHT / 2 - FISHSIZE);f->setPos(SCENEWIDTH - FISHSIZE, y);}else {int x = Randomizer::creat(SCENEWIDTH - FISHSIZE);f->setPos(x, 0);}connect(f, SIGNAL(sig_score(int)), this, SLOT(slt_addscore(int)));}}
}void Space::slt_menu()
{if (isRunning) {slt_pauseGame();QScopedPointer<MenuWidget> w(new MenuWidget(true, this));connect(w.data(), SIGNAL(sig_newGame()), this, SLOT(slt_newGame()));connect(w.data(), SIGNAL(sig_quit()), this, SLOT(close()));w->setModal(true);w->show();w->exec();slt_startGame();}else {QScopedPointer<MenuWidget> w(new MenuWidget(false, this));connect(w.data(), SIGNAL(sig_newGame()), this, SLOT(slt_newGame()));connect(w.data(), SIGNAL(sig_quit()), this, SLOT(close()));w->setModal(true);w->show();w->exec();}
}void Space::mouseDoubleClickEvent(QMouseEvent *event) {event->accept();emit sig_menu();
}//void Space::slt_updateScore(int score) {
//    QString text = QString::number(score, 10);
//    scoretext->setPlainText(text);
//}//void Space::slt_updateBlood(int blood) {
//    QString text = QString::number(blood, 10);
//    bloodtext->setPlainText(text);
//}//void Space::slt_updateBomb(int bomb) {
//    QString text = QString::number(bomb, 10);
//    bombtext->setPlainText(text);
//}

其他物体(暂时没用,可以添加增加游戏性)

#ifndef UNFLYER_H
#define UNFLYER_H#include <QtWidgets>
#include "global.h"class UnFlyer : public QGraphicsObject {Q_OBJECT
public:UnFlyer(QGraphicsItem *parent = 0);~UnFlyer();enum {TYPE = UserType + 1};int type() const;
};#endif // UNFLYER_H
#include "unflyer.h"UnFlyer::UnFlyer(QGraphicsItem *parent) : QGraphicsObject(parent) {}UnFlyer::~UnFlyer() {}int UnFlyer::type() const {return UserType + 1;
}

一切以下载的源码为主(20多个类,天知道我有没有粘错。。。),欢迎大家补充、改进

Qt游戏编程——飞机大战——补充相关推荐

  1. Qt学习总结——飞机大战小游戏制作

    Qt学习总结--飞机大战小游戏制作 1. 需求分析 这篇文章写于2020年暑假,完成学校实训项目之后,对自己的项目实践做了一个总结,回顾整个项目的制作过程,同时也复习一下Qt的相关知识,总结项目制作过 ...

  2. python飞机大战加背景音乐_python实现飞机大战小游戏 python飞机大战中的音频文件怎么改成MP3...

    怎么样用Python写飞机大战游戏 python开发飞机大战外星人游戏怎么弄双人模式新的一年,哪怕仍是一个人,也要活得像一支队伍,为自己的头脑和心灵招兵买马,不气馁,有召唤,爱自由. 主函数 impo ...

  3. 微信小游戏 demo 飞机大战 代码分析(四)(enemy.js, bullet.js, index.js)

    微信小游戏 demo 飞机大战 代码分析(四)(enemy.js, bullet.js, index.js) 微信小游戏 demo 飞机大战 代码分析(一)(main.js) 微信小游戏 demo 飞 ...

  4. Python小游戏之 - 飞机大战 !

    用Python写的"飞机大战"小游戏 源代码如下: # coding=utf-8 import random import os import pygame# 用一个常量来存储屏幕 ...

  5. python飞机大战资料-Python之游戏开发-飞机大战

    Python之游戏开发-飞机大战 想要代码文件,可以加我微信:nickchen121 #!/usr/bin/env python # coding: utf-8 import pygame impor ...

  6. 安卓小游戏:飞机大战

    安卓小游戏:飞机大战 前言 前面写了十二篇自定义view的博客,说实话写的还是有点无聊了,最近调整了一下,觉得还是要对开发有热情,就写了点小游戏,现在抽时间把博客也写一写,希望读者喜欢. 需求 这里就 ...

  7. 【游戏程序设计】完整二维游戏开发-飞机大战

    学习了前面的许多知识,现在可以真正做个可以玩的游戏了. 之前是用MFC做的飞机大战,但是有许多的问题.这次我们用Win32程序来开发,所有的代码都是自己写成的,可以控制更多的细节.这次的游戏就没有之前 ...

  8. Java Swing 经典小游戏《飞机大战》———— (四)碰撞检测 游戏状态与得分 玩家升级

    前期回顾 Java Swing 经典小游戏<飞机大战>---- (一)获取素材,创建窗口,添加滚动背景,双缓冲 Java Swing 经典小游戏<飞机大战>---- (二)玩家 ...

  9. 自己动手写游戏:飞机大战

    要说微信中最火爆的小游戏是哪款,可能既不是精心打造的3D大作,也不是<植物大战僵尸2>,而是微信5.0刚开启时的<飞机大战>. 就是这样一款铅笔手绘风格的简单到不能再简单的&q ...

  10. 【Python】游戏:飞机大战

    本文主要内容: 复现:Eric Matthes所著的<python编程从入门到实践>一书中,第12章到第14章的<外星人入侵>项目的代码,并成功运行. 将项目的主要文件和代码做 ...

最新文章

  1. 思考如何提高博客的访问量
  2. R使用Iris数据集构建SVM分类器
  3. centos安装mysql wsl_在 Windows Linux 子系统中安装 CentOS
  4. 209计算机考试题库,计算机考试题库:计算机考试模拟练习题(209)
  5. 十年再出发!阿里云智能总裁张建锋演讲全记录
  6. mysql .pdb是什么文件_超详细讲解如何使用 pdb 在服务器上调试代码
  7. DataBseDesign工作笔记002---数据库表设计
  8. SAP License:GR/IR
  9. MarkDown学习指南(一)
  10. 台式机装苹果系统_苹果电脑macbook装windows系统U盘启动的详细方法
  11. linux基础知识复习学习笔记
  12. 【招生目录】 2023年北京交通大学计算机学院博士研究生招生专业目录
  13. 计算机项目教学法探讨,基于项目教学法的非计算机专业计算机教学的设计和探讨...
  14. PEI表面修饰CNTs步骤及原理
  15. 书单 | 带你轻松度假的10本好书!
  16. 网页/前端大作业 html+css 无js 前端三剑客 大一网页大作业 9个页面
  17. 一款轻、快、无广告的杀毒安全软件(火绒5.0)
  18. Makefile编写和使用技巧
  19. selenium之浏览器弹出新窗口并在新窗口操作
  20. 交换机组播风暴_【交换机】交换机如何配置storm-contro风暴控制

热门文章

  1. 连续分配、链接分配和索引分配详解
  2. 收藏几个漂亮的管理后台模板
  3. C语言基础之十进制与二进制转换
  4. Deepin 与 Win10 双系统 Deepin无法启动且没有引导项问题
  5. 2021SC@SDUSC Zxing开源代码(十四)Aztec二维码(三)
  6. 元宇宙iwemeta:《时代》杂志新封面,元宇宙将改变一切
  7. 金源高端IPO被终止:曾拟募资7.5亿 儒杉资产与溧阳产投是股东
  8. 计算机类毕业设计评语导师评语,毕业设计导师评语
  9. python中cnt是什么意思_CNT是什么意思
  10. Retrofit使用小结