Qt 游戏之路(一) 场景搭建 和 战车移动、地图拉伸
场景信息
//info.cpp文件 存储场景信息 int MAPWIDTH = 1200; int MAPHEIGHT = 650;
//info.h文件 存储全局变量 #ifndef INFO_H //全局变量 #define INFO_H #define SCENEWIDTH 1920 #define SCENEHEIGHT 1200 extern int MAPWIDTH; extern int MAPHEIGHT; #endif // INFO_H
游戏客户区 ,由QGraphicsItem、QGraphicsScene、QGraphicsView 图形视图框架搭建
//gamewindow.h #ifndef GAMEWINDOW_H #define GAMEWINDOW_H #include#include #include #include #include #include #include "info.h" #include "player.h" class Player; class GameWindow : public QGraphicsView { Q_OBJECT friend class Player; public: GameWindow(QWidget *parent = 0); void mousePressEvent(QMouseEvent *event); public: QGraphicsScene *scene; //场景 Player *player; bool canArrive(qreal x, qreal y); bool canArrive(QPointF pos); void moveto(qreal x, qreal y); void moveto(QPointF pos); QTimer *timer; //用于移动 protected: void fixClientFor(qreal x, qreal y, bool right, bool down); private slots: void move_use_timer(); }; #endif// GAMEWINDOW_H
实现文件
#include
主窗口
#ifndef MAINWINDOW_H #define MAINWINDOW_H #include#include "gamewindow.h" #include "info.h" class GameWindow; namespace Ui { class MainWindow; } class MainWindow : public QMainWindow { Q_OBJECT public: explicit MainWindow(QWidget *parent = 0); ~MainWindow(); void paintEvent(QPaintEvent *); Ui::MainWindow *ui; GameWindow *client; //视窗 QGraphicsScene *scene; //场景 }; #endif // MAINWINDOW_H
主窗口实现文件
#include "mainwindow.h" #include "ui_mainwindow.h" MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow) { ui->setupUi(this); scene = new QGraphicsScene(this); client = new GameWindow(this); client->scene = scene; client->setScene(scene); client->setFixedHeight(MAPHEIGHT); //视口 client->setFixedWidth(MAPWIDTH); scene->setSceneRect(0, 0, SCENEWIDTH, SCENEHEIGHT); //场景 scene->addPixmap(QPixmap(":/pic/back")); client->show(); client->player->itempix = scene->addPixmap(*client->player->pix); client->player->setPos(MAPWIDTH/2, MAPHEIGHT/2); } MainWindow::~MainWindow() { delete ui; } void MainWindow::paintEvent(QPaintEvent *) { client->setFixedHeight(this->height()); // 重绘时重置大小(窗口大小可能被改变) client->setFixedWidth(this->width()); MAPWIDTH = this->width(); MAPHEIGHT = this->height(); }
资源 :/pic/1.png
资源 :/pic/back.jpg 由于上传大小问题,建议把这个图片改成png格式
玩家类
#ifndef PLAYER_H #define PLAYER_H #include#include #include "info.h" #include "mainwindow.h" class Player : public QWidget { Q_OBJECT public: Player(QWidget * parent = 0); ~Player() {} void setPos(qreal x, qreal y); QPixmap * pix; //角色图组指针 QGraphicsPixmapItem *itempix; QPointF cpos, arrivepos; //角色坐标,到达目标坐标 QTimer *timer; //用于移动 qreal speed; //速度 }; #endif // PLAYER_H
玩家类实现
#include "player.h" //Player Player::Player(QWidget * parent) : QWidget(parent) { timer = 0; speed = 5; pix = new QPixmap(":/pic/1"); } void Player::setPos(qreal x, qreal y) { cpos.setX(x); cpos.setY(y); itempix->setPos(cpos); }
主函数
#include "info.h" #include "mainwindow.h" #includeint main(int argc, char *argv[]) { QApplication a(argc, argv); MainWindow mainw; mainw.setMinimumSize(MAPWIDTH, MAPHEIGHT); mainw.show(); return a.exec(); }