c语言sscanf函数的用法是什么
226
2022-11-27
Qt游戏编程——飞机大战——补充
补充包:
#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
随机数
#ifndef RANDOMIZER_H#define RANDOMIZER_H#include
#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
#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_OBJECTpublic: 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
其他物体(暂时没用,可以添加增加游戏性)
#ifndef UNFLYER_H#define UNFLYER_H#include
#include "unflyer.h"UnFlyer::UnFlyer(QGraphicsItem *parent) : QGraphicsObject(parent) {}UnFlyer::~UnFlyer() {}int UnFlyer::type() const { return UserType + 1;}
一切以下载的源码为主(20多个类,天知道我有没有粘错。。。),欢迎大家补充、改进
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。
发表评论
暂时没有评论,来抢沙发吧~