当前位置: 首页 > news >正文

某旅行社网站建设规划书学做app软件在哪里学

某旅行社网站建设规划书,学做app软件在哪里学,中国导航电子地图,wordpress文章详细页贪吃蛇小游戏 一.项目介绍**[贪吃蛇项目地址](https://gitee.com/strandingzy/QT/tree/zyy/snake)**界面一#xff1a;游戏大厅界面二#xff1a;关卡选择界面界面三#xff1a;游戏界面 二.项目实现2.1 游戏大厅2.2关卡选择界面2.3 游戏房间2.3.1 封装贪吃蛇数据结构2.3.2 … 贪吃蛇小游戏 一.项目介绍**[贪吃蛇项目地址](https://gitee.com/strandingzy/QT/tree/zyy/snake)**界面一游戏大厅界面二关卡选择界面界面三游戏界面 二.项目实现2.1 游戏大厅2.2关卡选择界面2.3 游戏房间2.3.1 封装贪吃蛇数据结构2.3.2 初始化游戏房间界面2.3.3 蛇的移动2.3.4 初始化贪吃蛇生体和食物节点2.3.5 实现定时器的超时槽函数2.3.6 实现各个方向的移动2.3.7 重写绘图事件函数进行渲染2.3.8 检查是否自己会撞到自己2.3.9 设置游戏开始和游戏暂停按钮2.3.10 设置游戏开始和游戏暂停按钮2.3.11 获取历史战绩 代码的总体文件gamehall.hgameroom.hgameselect.hgamehall.cppgameroom.cppgameselect.cpp 一.项目介绍 贪吃蛇游戏是⼀款休闲益智类游戏。它通过控制蛇头方向吃食物从而使得蛇变得越来越长。在本游戏中设置了上下左右四个⽅向键来控制蛇的移动方向。⻝物的产⽣是随机⽣成的当蛇每吃⼀次⻝物就会增加⼀节身体同时游戏积分也会相应的加⼀。在本游戏的设计中蛇的⾝体会越吃越长身体越长对应的难度就越⼤因为⼀旦蛇头和⾝体相交游戏就会结束。 贪吃蛇项目地址 界面一游戏大厅 当用户点击 “开始游戏” 按钮之后就会进⼊到关卡选择界面。 界面二关卡选择界面 在关卡选择界面上设置了三个游戏模式按钮分别是简单模式、正常模式、困难模式⼀个 “历史战绩” 按钮⼀个返回游戏大厅界⾯的按钮。 界面三游戏界面 在游戏界⾯如果想要开始游戏⾸先点击 “开始” 按钮此时蛇就会移动并且还有背景⾳效。如果想要暂停游戏那么点击 “暂停” 按钮即可。当我们在游戏时可以通过右边控制区域的上下左右⽅向键来控制蛇的移动。当蛇每吃⼀次⻝物时伴随有吃⻝物的⾳效蛇⾝会增加⼀节⻓度并且分数 积分也会相应的加⼀。最后在控制区域的右下⻆布局了⼀个 “退出” 按钮当点击 “退出” 按钮时就会有⼀个弹窗提⽰如下 如果我们点击 “Ok” 则会退出到游戏关卡选择界⾯此时我们可以重新选择游戏模式。如果点击“Cancel” 则不会退出游戏我们还是处在当前游戏房间界⾯。 二.项目实现 2.1 游戏大厅 游戏大厅界面比较简单 只有⼀个背景图和⼀个按钮 背景图片的渲染通过QT的绘图事件完成 void GameHall::paintEvent(QPaintEvent *event) {//实例化画家对象QPainter painter(this);//实例化绘图设备QPixmap pix(:res/game_hall.png);//绘画painter.drawPixmap(0,0,this-width(),this-height(),pix);}按钮的响应通过QT的信号槽机制完成 GameHall::GameHall(QWidget *parent): QWidget(parent), ui(new Ui::GameHall) {ui-setupUi(this);this-setFixedSize(1500,1000);//设置窗口大小this-setWindowTitle(QString(贪吃蛇游戏));this-setWindowIcon(QIcon(:res/ico.png));//设置窗口图标//QFont font(华文行楷,20);QPushButton * strbutton new QPushButton(this);strbutton-setText(开始游戏);strbutton-setFont(font);strbutton-move(650,700);strbutton-setShortcut(QKeySequence(Qt::Key_Space));strbutton-setStyleSheet(QPushButton{border:0px});//给按钮设置样式 去除边框GameSelect * gameSelect new GameSelect;connect(strbutton,QPushButton::clicked,[](){this-close();//当前窗口关闭gameSelect-setGeometry(this-geometry());//设置第二个窗口于当前窗口一致gameSelect-show();//新的窗口打开QSound::play(:res/clicked.wav);}); } 这里的Qfound类使用需要在.pro 的文件中加上 QT core gui multimedia这里使用strbutton-setShortcut(QKeySequence(Qt::Key_Space));给按钮添加了快捷键使用strbutton-setStyleSheet(QPushButton{border:0px});去除按钮边框样式通过槽函数跳转到新的窗口 在槽函数中使用了Lambda 表达式 通过QSound创建音频对象使点击按钮有声音 2.2关卡选择界面 关卡选择界⾯包含⼀个背景图和五个按钮背景图的绘制和游戏⼤厅背景图绘制⼀样同样使用的是Qt中的绘图事件下面依次介绍。 背景图片的渲染通过QT的绘图事件完成 void GameHall::paintEvent(QPaintEvent *event) {//实例化画家对象QPainter painter(this);//实例化绘图设备QPixmap pix(:res/game_hall.png);//绘画painter.drawPixmap(0,0,this-width(),this-height(),pix);}按钮的响应通过QT的信号和槽机制完成 #include gameselect.h #includegameroom.h #includeQIcon #includeQPushButton #includegamehall.h #includeQSound #includeQPainterGameSelect::GameSelect(QWidget *parent) : QWidget(parent) {this-setFixedSize(1500,1000);//设置窗口大小this-setWindowIcon(QIcon(:res/ico.png));this-setWindowTitle(游戏关卡选择);QPushButton* backBtn new QPushButton(this);//返回按钮backBtn-move(1400,900);backBtn-setIcon(QIcon(:res/back.png));backBtn-setShortcut(QKeySequence(Qt::Key_Escape));connect(backBtn,QPushButton::clicked,[](){this-close();GameHall * gameHall new GameHall;gameHall-show();QSound::play(:res/clicked.wav);});QFont font(华文行楷,20);gameroom*gameRoom new gameroom;QPushButton* simpleBtn new QPushButton(this);simpleBtn-move(650,300);simpleBtn-setFont(font);simpleBtn-setText(简单模式);connect(simpleBtn,QPushButton::clicked,[](){this-close();gameRoom-setGeometry(this-geometry());gameRoom-show();});QPushButton*normalBtn new QPushButton(this);normalBtn-move(650,400);normalBtn-setFont(font);normalBtn-setText(正常模式);connect(normalBtn,QPushButton::clicked,[](){this-close();gameRoom-setGeometry(this-geometry());gameRoom-show();});QPushButton*hardBtn new QPushButton(this);hardBtn-move(650,500);hardBtn-setFont(font);hardBtn-setText(困难模式);connect(hardBtn,QPushButton::clicked,[]{this-close();gameRoom-setGeometry(this-geometry());gameRoom-show();});QPushButton*hisBtn new QPushButton(this);hisBtn-move(650,600);hisBtn-setFont(font);hisBtn-setText(历史战绩); }void GameSelect::paintEvent(QPaintEvent *event) {QPainter painter(this);//实例化画家对象QPixmap pix(:res/game_select.png);painter.drawPixmap(0,0,this-width(),this-height(),pix); } connect(hardBtn,QPushButton::clicked,[]{ this-close(); gameRoom-setGeometry(this-geometry()); gameRoom-show(); }); 通过关联信号槽使得可以选择游戏模式首先把当前窗口关闭然后显示新的窗口在设置与当前窗口的大小一样 2.3 游戏房间 游戏房间界面包含下面几个部分 背景的绘制蛇的绘制蛇的移动判断蛇是否会撞到自己积分的累加和绘制 怎么让蛇动起来 我们可以⽤⼀个链表表示贪吃蛇⼀个小方块表示蛇的⼀个节点 我们设置蛇的默认⻓度为3向上移动的逻辑就是在蛇的上方加⼊⼀个小方块 然后把最后⼀个小⽅块删除即可需要用到定时器Qtimer 每100 - 200ms 重新渲染 怎么判断蛇有没有吃到食物 判断蛇头和食物的坐标是否相交 怎么控制蛇的移动 借助QT的实践机制实现 重写keyPressEvent即可 在函数中监控想要的键盘事件即可通过绘制四个按钮使⽤信号和槽的机制控制蛇的上、下、左、右移动方向 2.3.1 封装贪吃蛇数据结构 #ifndef GAMEROOM_H #define GAMEROOM_H#include QWidget #includeQSoundenum class SnakeDirect {UP0,DOWN,LEFT,RIGHT };class gameroom : public QWidget {Q_OBJECT public:explicit gameroom(QWidget *parent nullptr);//重写绘图事件void paintEvent(QPaintEvent *event);void moveUp();void moveDown();void moveLeft();void moveRight();bool checkFail();void createNewFood();void setTimeout(int timeout){moveTimetimeout;}private:const int KSnakeNodeWidth 20;const int KSnakeNodeHeight 20;const int KDefaultTimeout 200;//移动速度QListQRectF snakeList;//表示链表QRectF foodRect;//食物SnakeDirect moveDirect SnakeDirect::UP;//默认移动方向QTimer *timer;//定时器bool isGameStart false;//游戏的开始QSound *sound;int moveTime KDefaultTimeout; };#endif // GAMEROOM_H 2.3.2 初始化游戏房间界面 设置窗口大小、标题、图标等 gameroom::gameroom(QWidget *parent) : QWidget(parent) {this-setFixedSize(1500,1000);this-setWindowIcon(QIcon(:res/ico.png));this-setWindowTitle(游戏房间); }2.3.3 蛇的移动 蛇的移动方向为上、下、左、右。通过在游戏房间中布局四个按钮来控制蛇的移动方向。 · 注意 这里贪吃蛇不允许直接掉头 比如当前是向上的 不能直接修改为向下。 QPushButton*up new QPushButton(this);QPushButton*down new QPushButton(this);QPushButton*left new QPushButton(this);QPushButton*right new QPushButton(this);up-move(1320,750);down-move(1320,900);left-move(1220,825);right-move(1420,825);up-setText(↑); // up-setShortcut(QKeySequence(Qt::Key_W));down-setText(↓); // down-setShortcut(QKeySequence(Qt::Key_S));left-setText(←); // left-setShortcut(QKeySequence(Qt::Key_A));right-setText(→); // left-setShortcut(QKeySequence(Qt::Key_D));up-setStyleSheet(QPushButton{border:0px});down-setStyleSheet(QPushButton{border:0px});left-setStyleSheet(QPushButton{border:0px});right-setStyleSheet(QPushButton{border:0px});QFont ft(宋体,36);up-setFont(ft);down-setFont(ft);right-setFont(ft);left-setFont(ft);connect(up,QPushButton::clicked,[](){if(moveDirect! SnakeDirect::DOWN){moveDirect SnakeDirect::UP;}});connect(down,QPushButton::clicked,[](){if(moveDirect! SnakeDirect::UP){moveDirect SnakeDirect::DOWN;}});connect(left,QPushButton::clicked,[](){if(moveDirect! SnakeDirect::LEFT){moveDirect SnakeDirect::RIGHT;}});connect(right,QPushButton::clicked,[](){if(moveDirect! SnakeDirect::RIGHT){moveDirect SnakeDirect::LEFT;}}); 2.3.4 初始化贪吃蛇生体和食物节点 GameRoom::GameRoom(QWidget *parent) :QWidget(parent),ui(new Ui::Widget){ snakeList.push_back(QRectF(this-width()*0.5,this-height()*0.5,KSnakeNodeWidth,KSnakeNodeHeight));moveUp();moveUp();moveUp();//创建食物createNewFood();}moveUp() 的功能是将蛇向上移动⼀次 即在上⽅新增⼀个节点 但不删除尾部节点。 createNewFood() ⽅法的功能是随机创建⼀个⻝物节点 void gameroom::createNewFood() {foodRect QRectF(qrand()%(1500/KSnakeNodeWidth)*KSnakeNodeWidth,qrand()%(this-height()/KSnakeNodeHeight)*KSnakeNodeHeight,KSnakeNodeWidth,KSnakeNodeHeight); } 2.3.5 实现定时器的超时槽函数 定时器的是为了实现每隔⼀段时间能处理移动的逻辑并且更新绘图事件。 ⾸先 需要判断蛇头和⻝物节点坐标是否相交 如果相交 需要创建新的⻝物节点 并且需要更新蛇的⻓度 所以 cnt 需要 1 如果不相交 那么直接处理蛇的移动即可。 根据蛇移动方向 moveDirect 来处理蛇的移动 处理方法是在前方加⼀个 并且删除后方节点重新触发绘图事件 更新渲染 timer new QTimer(this);connect(timer,QTimer::timeout,[]{int cnt 1 ;if(snakeList.front().intersects(foodRect)){//intersects判断会不会相交createNewFood();cnt;QSound::play(:res/eatfood.wav);}while (cnt--) {switch (moveDirect) {case SnakeDirect::UP:moveUp();break;case SnakeDirect::DOWN:moveDown();break;case SnakeDirect::LEFT:moveLeft();break;case SnakeDirect::RIGHT:moveRight();break;}}snakeList.pop_back();//删除最后一个节点update();}); 2.3.6 实现各个方向的移动 各个方向的移动主要在于更新矩形节点的坐标 要注意的是⼀定要处理边界的情况 当边界不够存储⼀个新的节点 我们需要处理穿墙逻辑。 实现向上移动 void gameroom::moveUp() {QPointF leftTop;//左上角坐标QPointF rightBottom;//右下角坐标auto snakeNode snakeList.front();//头int headx snakeNode.x();int heady snakeNode.y();if(heady 0){//穿墙了leftTop QPointF(headx,this-height()-KSnakeNodeHeight);}else {leftTop QPointF(headx,heady- KSnakeNodeHeight);}rightBottom leftTop QPointF(KSnakeNodeWidth,KSnakeNodeHeight);snakeList.push_front(QRectF(leftTop,rightBottom));//更新到链表中 }实现向下移动 void gameroom::moveDown() {QPointF leftTop;//左上角坐标QPointF rightBottom;//右下角坐标auto snakeNode snakeList.front();int headx snakeNode.x();int heady snakeNode.y();if(heady this-height()){leftTop QPointF(headx,0);}else {leftTop snakeNode.bottomLeft();//获取左下角做标}rightBottom leftTop QPointF(KSnakeNodeWidth,KSnakeNodeHeight);snakeList.push_front(QRectF(leftTop,rightBottom));}实现向左移动 void gameroom::moveLeft() {QPointF leftTop;//左上角坐标QPointF rightBottom;//右下角坐标auto snakeNode snakeList.front();int headx snakeNode.x();int heady snakeNode.y();if(headx 0){leftTop QPointF(1500 - KSnakeNodeWidth,heady);}else {leftTop QPointF(headx - KSnakeNodeWidth,heady);}rightBottom leftTop QPointF(KSnakeNodeWidth,KSnakeNodeHeight);snakeList.push_front(QRectF(leftTop,rightBottom)); } 实现向右移动 void gameroom::moveRight() {QPointF leftTop;//左上角坐标QPointF rightBottom;//右下角坐标auto snakeNode snakeList.front();int headx snakeNode.x();int heady snakeNode.y();if(heady KSnakeNodeWidth 1200){leftTop QPointF(0,heady);}else {leftTop snakeNode.bottomRight();}rightBottom leftTop QPointF(KSnakeNodeWidth,KSnakeNodeHeight);snakeList.push_front(QRectF(leftTop,rightBottom)); }2.3.7 重写绘图事件函数进行渲染 重写基类的 paintEvent() 方法进行渲染 渲染背景图渲染蛇头渲染蛇身体渲染蛇尾巴渲染右边游戏控制区域渲染⻝物节点渲染当前分数游戏结束渲染 game over void gameroom::paintEvent(QPaintEvent *event) {QPainter painter(this);//实例画家对象QPixmap pix;pix.load(:res/game_room.png);painter.drawPixmap(0,0,1200,1400,pix);pix.load(:res/bg1.png);painter.drawPixmap(1200,0,300,1100,pix);//绘制蛇//头if(moveDirect SnakeDirect::UP){pix.load(:res/up.png);}else if (moveDirect SnakeDirect::DOWN) {pix.load(:res/down.png);}else if (moveDirect SnakeDirect::LEFT) {pix.load(:res/left.png);}else if(moveDirect SnakeDirect::RIGHT){pix.load(:res/right.png);}auto snakHead snakeList.front();painter.drawPixmap(snakHead.x(),snakHead.y(),snakHead.width(),snakHead.height(),pix);//身体pix.load(:res/Bd.png);for (int i 1;i snakeList.size()-1;i) {auto node snakeList.at(i); //at 获取每一个节点painter.drawPixmap(node.x(),node.y(),node.width(),node.height(),pix);}//尾巴auto snakeTail snakeList.back();painter.drawPixmap(snakeTail.x(),snakeTail.y(),snakeTail.width(),snakeTail.height(),pix);//食物pix.load(:res/food.png);painter.drawPixmap(foodRect.x(),foodRect.y(),foodRect.width(),foodRect.height(),pix);//分数pix.load(:res/sorce_bg.png);painter.drawPixmap(this-width()*0.85,this-height()*0.1,90,40,pix);QPen pen;pen.setColor(Qt::black);QFont font(方正舒体,22);painter.setFont(font);painter.setPen(pen);painter.drawText(this-width()*0.9,this-height()*0.06,QString(%1).arg(snakeList.size()));//往文件中写分数int c snakeList.size();QFile file(C:/Users/zyy/Desktop/1.txt);if(file.open(QIODevice::WriteOnly| QIODevice::Text)){QTextStream out(file);int num c;outnum ;file.close();}//绘制有戏失败的效果if(checkFail()){pen.setColor(Qt::red);painter.setPen(pen);QFont font(方正舒体,22);painter.setFont(font);painter.drawText(this-width()*0.45,this-height()*0.5,QString(GAME OVER!));timer-stop();QSound::play(:res/gameover.wav);sound-stop();} }2.3.8 检查是否自己会撞到自己 bool gameroom::checkFail() {for(int i 0; isnakeList.size();i){for(int ji1; jsnakeList.size();j){if(snakeList.at(i)snakeList.at(j)){return true;}}}return false; }2.3.9 设置游戏开始和游戏暂停按钮 QPushButton * strBtn new QPushButton(this);QPushButton * stopBtn new QPushButton(this);strBtn-move(1270,100);stopBtn-move(1270,170);strBtn-setText(开始);strBtn-setShortcut(QKeySequence(Qt::Key_CQt::CTRL));stopBtn-setText(暂停);stopBtn-setShortcut(QKeySequence(Qt::Key_VQt::CTRL));QFont font(楷体,20);strBtn-setFont(font);stopBtn-setFont(font);connect(strBtn,QPushButton::clicked,[]{sound new QSound(:res/Trepak.wav);sound-play();sound-setLoops(-1);//一直循环播放isGameStart true;timer-start(moveTime);});connect(stopBtn,QPushButton::clicked,[]{isGameStart false;timer-stop();sound-stop();});2.3.10 设置游戏开始和游戏暂停按钮 当我们点击退出游戏按钮时当前游戏房间窗⼝不会⽴即退出而是会弹窗提示提示我们是否要退出游戏效果如下图示 这个弹窗提示我们是通过 Qt 中的消息盒子来实现的具体实现过程如下 //退出游戏QPushButton*ExitBtn new QPushButton(this);ExitBtn-setText(退出游戏);ExitBtn-move(1240,400);ExitBtn-setFont(font);QMessageBox *msg new QMessageBox(this);QPushButton *ok new QPushButton(ok);QPushButton *canel new QPushButton(canel);msg-addButton(ok,QMessageBox::AcceptRole);msg-addButton(canel,QMessageBox::RejectRole);msg-setWindowTitle(退出游戏);msg-setText(确认退出游戏吗);connect(ExitBtn,QPushButton::clicked,[](){msg-show();msg-exec();//时间轮询QSound::play(res/clicked.wav);GameSelect *select new GameSelect;if(msg-clickedButton()ok){this-close();select-show();}else {msg-close();}}); }2.3.11 获取历史战绩 对于历史战绩的获取我们是通过 Qt 中的读写文件操作来实现的。具体实现过程如下 写文件 向文件中写入蛇的长度 int c snakeList.size();QFile file(C:/Users/zyy/Desktop/1.txt);if(file.open(QIODevice::WriteOnly| QIODevice::Text)){QTextStream out(file);int num c;outnum ;file.close();}读文件读取写入文件中蛇的长度 int c snakeList.size();QFile file(C:/Users/zyy/Desktop/1.txt);if(file.open(QIODevice::WriteOnly| QIODevice::Text)){QTextStream out(file);int num c;outnum ;file.close();}代码的总体文件 gamehall.h #ifndef GAMEHALL_H #define GAMEHALL_H#include QWidgetQT_BEGIN_NAMESPACE namespace Ui { class GameHall; } QT_END_NAMESPACEclass GameHall : public QWidget {Q_OBJECTpublic:GameHall(QWidget *parent nullptr);~GameHall();//从写绘图事件void paintEvent(QPaintEvent *event);private:Ui::GameHall *ui; }; #endif // GAMEHALL_H gameroom.h #ifndef GAMEROOM_H #define GAMEROOM_H#include QWidget #includeQSoundenum class SnakeDirect {UP0,DOWN,LEFT,RIGHT };class gameroom : public QWidget {Q_OBJECT public:explicit gameroom(QWidget *parent nullptr);//重写绘图事件void paintEvent(QPaintEvent *event);void moveUp();void moveDown();void moveLeft();void moveRight();bool checkFail();void createNewFood();void setTimeout(int timeout){moveTimetimeout;}private:const int KSnakeNodeWidth 20;const int KSnakeNodeHeight 20;const int KDefaultTimeout 200;//移动速度QListQRectF snakeList;//表示链表QRectF foodRect;//食物SnakeDirect moveDirect SnakeDirect::UP;//默认移动方向QTimer *timer;//定时器bool isGameStart false;//游戏的开始QSound *sound;int moveTime KDefaultTimeout; };#endif // GAMEROOM_H gameselect.h #ifndef GAMESELECT_H #define GAMESELECT_H#include QWidgetclass GameSelect : public QWidget {Q_OBJECT public:explicit GameSelect(QWidget *parent nullptr);//绘图事件void paintEvent(QPaintEvent *event); signals:};#endif // GAMESELECT_H gamehall.cpp #include gamehall.h #include ui_gamehall.h #includeQPainter #includeQIcon #includeQFont #includeQPushButton #includegameselect.h #includeQSoundGameHall::GameHall(QWidget *parent): QWidget(parent), ui(new Ui::GameHall) {ui-setupUi(this);this-setFixedSize(1500,1000);//设置窗口大小this-setWindowTitle(QString(贪吃蛇游戏));this-setWindowIcon(QIcon(:res/ico.png));//设置窗口图标//QFont font(华文行楷,20);QPushButton * strbutton new QPushButton(this);strbutton-setText(开始游戏);strbutton-setFont(font);strbutton-move(650,700);strbutton-setShortcut(QKeySequence(Qt::Key_Space));strbutton-setStyleSheet(QPushButton{border:0px});//给按钮设置样式 去除边框GameSelect * gameSelect new GameSelect;connect(strbutton,QPushButton::clicked,[](){this-close();//当前窗口关闭gameSelect-setGeometry(this-geometry());//设置第二个窗口于当前窗口一致gameSelect-show();//新的窗口打开QSound::play(:res/clicked.wav);}); }GameHall::~GameHall() {delete ui;}void GameHall::paintEvent(QPaintEvent *event) {//实例化画家对象QPainter painter(this);//实例化绘图设备QPixmap pix(:res/game_hall.png);//绘画painter.drawPixmap(0,0,this-width(),this-height(),pix);} gameroom.cpp #include gameroom.h #includeQPainter #includeQPixmap #includeQIcon #includeQTimer #includeQPushButton #includeQMessageBox #includeQFile #includeQTextStream #includegameselect.hgameroom::gameroom(QWidget *parent) : QWidget(parent) {this-setFixedSize(1500,1000);this-setWindowIcon(QIcon(:res/ico.png));this-setWindowTitle(游戏房间);//初始化贪吃蛇snakeList.push_back(QRectF(this-width()*0.5,this-height()*0.5,KSnakeNodeWidth,KSnakeNodeHeight));moveUp();moveUp();moveUp();//创建食物createNewFood();timer new QTimer(this);connect(timer,QTimer::timeout,[]{int cnt 1 ;if(snakeList.front().intersects(foodRect)){//intersects判断会不会相交createNewFood();cnt;QSound::play(:res/eatfood.wav);}while (cnt--) {switch (moveDirect) {case SnakeDirect::UP:moveUp();break;case SnakeDirect::DOWN:moveDown();break;case SnakeDirect::LEFT:moveLeft();break;case SnakeDirect::RIGHT:moveRight();break;}}snakeList.pop_back();//删除最后一个节点update();});//开始暂停QPushButton * strBtn new QPushButton(this);QPushButton * stopBtn new QPushButton(this);strBtn-move(1270,100);stopBtn-move(1270,170);strBtn-setText(开始);strBtn-setShortcut(QKeySequence(Qt::Key_CQt::CTRL));stopBtn-setText(暂停);stopBtn-setShortcut(QKeySequence(Qt::Key_VQt::CTRL));QFont font(楷体,20);strBtn-setFont(font);stopBtn-setFont(font);connect(strBtn,QPushButton::clicked,[]{sound new QSound(:res/Trepak.wav);sound-play();sound-setLoops(-1);//一直循环播放isGameStart true;timer-start(moveTime);});connect(stopBtn,QPushButton::clicked,[]{isGameStart false;timer-stop();sound-stop();});//方向控制QPushButton*up new QPushButton(this);QPushButton*down new QPushButton(this);QPushButton*left new QPushButton(this);QPushButton*right new QPushButton(this);up-move(1320,750);down-move(1320,900);left-move(1220,825);right-move(1420,825);up-setText(↑);up-setShortcut(QKeySequence(Qt::Key_W));down-setText(↓);down-setShortcut(QKeySequence(Qt::Key_S));left-setText(←);left-setShortcut(QKeySequence(Qt::Key_A));right-setText(→);left-setShortcut(QKeySequence(Qt::Key_D));up-setStyleSheet(QPushButton{border:0px});down-setStyleSheet(QPushButton{border:0px});left-setStyleSheet(QPushButton{border:0px});right-setStyleSheet(QPushButton{border:0px});QFont ft(宋体,36);up-setFont(ft);down-setFont(ft);right-setFont(ft);left-setFont(ft);connect(up,QPushButton::clicked,[](){if(moveDirect! SnakeDirect::DOWN){moveDirect SnakeDirect::UP;}});connect(down,QPushButton::clicked,[](){if(moveDirect! SnakeDirect::UP){moveDirect SnakeDirect::DOWN;}});connect(left,QPushButton::clicked,[](){if(moveDirect! SnakeDirect::RIGHT){moveDirect SnakeDirect::LEFT;}});connect(right,QPushButton::clicked,[](){if(moveDirect! SnakeDirect::LEFT){moveDirect SnakeDirect::RIGHT;}});//退出游戏QPushButton*ExitBtn new QPushButton(this);ExitBtn-setText(退出游戏);ExitBtn-move(1240,400);ExitBtn-setFont(font);QMessageBox *msg new QMessageBox(this);QPushButton *ok new QPushButton(ok);QPushButton *canel new QPushButton(canel);msg-addButton(ok,QMessageBox::AcceptRole);msg-addButton(canel,QMessageBox::RejectRole);msg-setWindowTitle(退出游戏);msg-setText(确认退出游戏吗);connect(ExitBtn,QPushButton::clicked,[](){msg-show();msg-exec();//时间轮询QSound::play(res/clicked.wav);GameSelect *select new GameSelect;if(msg-clickedButton()ok){this-close();select-show();}else {msg-close();}}); }void gameroom::paintEvent(QPaintEvent *event) {QPainter painter(this);//实例画家对象QPixmap pix;pix.load(:res/game_room.png);painter.drawPixmap(0,0,1200,1400,pix);pix.load(:res/bg1.png);painter.drawPixmap(1200,0,300,1100,pix);//绘制蛇//头if(moveDirect SnakeDirect::UP){pix.load(:res/up.png);}else if (moveDirect SnakeDirect::DOWN) {pix.load(:res/down.png);}else if (moveDirect SnakeDirect::LEFT) {pix.load(:res/left.png);}else if(moveDirect SnakeDirect::RIGHT){pix.load(:res/right.png);}auto snakHead snakeList.front();painter.drawPixmap(snakHead.x(),snakHead.y(),snakHead.width(),snakHead.height(),pix);//身体pix.load(:res/Bd.png);for (int i 1;i snakeList.size()-1;i) {auto node snakeList.at(i); //at 获取每一个节点painter.drawPixmap(node.x(),node.y(),node.width(),node.height(),pix);}//尾巴auto snakeTail snakeList.back();painter.drawPixmap(snakeTail.x(),snakeTail.y(),snakeTail.width(),snakeTail.height(),pix);//食物pix.load(:res/food.png);painter.drawPixmap(foodRect.x(),foodRect.y(),foodRect.width(),foodRect.height(),pix);//分数pix.load(:res/sorce_bg.png);painter.drawPixmap(this-width()*0.85,this-height()*0.1,90,40,pix);QPen pen;pen.setColor(Qt::black);QFont font(方正舒体,22);painter.setFont(font);painter.setPen(pen);painter.drawText(this-width()*0.9,this-height()*0.06,QString(%1).arg(snakeList.size()));//往文件中写分数int c snakeList.size();QFile file(C:/Users/zyy/Desktop/1.txt);if(file.open(QIODevice::WriteOnly| QIODevice::Text)){QTextStream out(file);int num c;outnum ;file.close();}//绘制有戏失败的效果if(checkFail()){pen.setColor(Qt::red);painter.setPen(pen);QFont font(方正舒体,22);painter.setFont(font);painter.drawText(this-width()*0.45,this-height()*0.5,QString(GAME OVER!));timer-stop();QSound::play(:res/gameover.wav);sound-stop();} }void gameroom::moveUp() {QPointF leftTop;//左上角坐标QPointF rightBottom;//右下角坐标auto snakeNode snakeList.front();//头int headx snakeNode.x();int heady snakeNode.y();if(heady 0){//穿墙了leftTop QPointF(headx,this-height()-KSnakeNodeHeight);}else {leftTop QPointF(headx,heady- KSnakeNodeHeight);}rightBottom leftTop QPointF(KSnakeNodeWidth,KSnakeNodeHeight);snakeList.push_front(QRectF(leftTop,rightBottom));//更新到链表中 }void gameroom::moveDown() {QPointF leftTop;//左上角坐标QPointF rightBottom;//右下角坐标auto snakeNode snakeList.front();int headx snakeNode.x();int heady snakeNode.y();if(heady this-height()){leftTop QPointF(headx,0);}else {leftTop snakeNode.bottomLeft();//获取左下角做标}rightBottom leftTop QPointF(KSnakeNodeWidth,KSnakeNodeHeight);snakeList.push_front(QRectF(leftTop,rightBottom));}void gameroom::moveLeft() {QPointF leftTop;//左上角坐标QPointF rightBottom;//右下角坐标auto snakeNode snakeList.front();int headx snakeNode.x();int heady snakeNode.y();if(headx 0){leftTop QPointF(1500 - KSnakeNodeWidth,heady);}else {leftTop QPointF(headx - KSnakeNodeWidth,heady);}rightBottom leftTop QPointF(KSnakeNodeWidth,KSnakeNodeHeight);snakeList.push_front(QRectF(leftTop,rightBottom)); }void gameroom::moveRight() {QPointF leftTop;//左上角坐标QPointF rightBottom;//右下角坐标auto snakeNode snakeList.front();int headx snakeNode.x();int heady snakeNode.y();if(heady KSnakeNodeWidth 1200){leftTop QPointF(0,heady);}else {leftTop snakeNode.bottomRight();}rightBottom leftTop QPointF(KSnakeNodeWidth,KSnakeNodeHeight);snakeList.push_front(QRectF(leftTop,rightBottom)); }bool gameroom::checkFail() {for(int i 0; isnakeList.size();i){for(int ji1; jsnakeList.size();j){if(snakeList.at(i)snakeList.at(j)){return true;}}}return false; }void gameroom::createNewFood() {foodRect QRectF(qrand()%(1500/KSnakeNodeWidth)*KSnakeNodeWidth,qrand()%(this-height()/KSnakeNodeHeight)*KSnakeNodeHeight,KSnakeNodeWidth,KSnakeNodeHeight); } gameselect.cpp #include gameselect.h #includegameroom.h #includeQIcon #includeQPushButton #includegamehall.h #includeQSound #includeQPainter #includeQTextEdit #includeQFile #includeQTextStreamGameSelect::GameSelect(QWidget *parent) : QWidget(parent) {this-setFixedSize(1500,1000);//设置窗口大小this-setWindowIcon(QIcon(:res/ico.png));this-setWindowTitle(游戏关卡选择);QPushButton* backBtn new QPushButton(this);//返回按钮backBtn-move(1400,900);backBtn-setIcon(QIcon(:res/back.png));backBtn-setShortcut(QKeySequence(Qt::Key_Escape));connect(backBtn,QPushButton::clicked,[](){this-close();GameHall * gameHall new GameHall;gameHall-show();QSound::play(:res/clicked.wav);});QFont font(华文行楷,20);gameroom*gameRoom new gameroom;QPushButton* simpleBtn new QPushButton(this);simpleBtn-move(650,300);simpleBtn-setFont(font);simpleBtn-setText(简单模式);connect(simpleBtn,QPushButton::clicked,[](){this-close();gameRoom-setGeometry(this-geometry());gameRoom-show();gameRoom-setTimeout(300);});QPushButton*normalBtn new QPushButton(this);normalBtn-move(650,400);normalBtn-setFont(font);normalBtn-setText(正常模式);connect(normalBtn,QPushButton::clicked,[](){this-close();gameRoom-setGeometry(this-geometry());gameRoom-show();gameRoom-setTimeout(200);});QPushButton*hardBtn new QPushButton(this);hardBtn-move(650,500);hardBtn-setFont(font);hardBtn-setText(困难模式);connect(hardBtn,QPushButton::clicked,[]{this-close();gameRoom-setGeometry(this-geometry());gameRoom-show();gameRoom-setTimeout(300);});QPushButton*hisBtn new QPushButton(this);hisBtn-move(650,600);hisBtn-setFont(font);hisBtn-setText(历史战绩);connect(hisBtn,QPushButton::clicked,[](){QWidget *widget new QWidget;widget-setWindowTitle(历史战绩);widget-setFixedSize(500,300);QTextEdit *edit new QTextEdit(widget);edit-setFont(font);edit-setFixedSize(500,300);QFile file(C:/Users/zyy/Desktop/1.txt);file.open(QIODevice::ReadOnly);//读取内容QTextStream in(file);int data in.readLine().toInt();//读取一行的数据edit-append(得分为:);edit-append(QString::number(data));//写入文本中widget-show();});}void GameSelect::paintEvent(QPaintEvent *event) {QPainter painter(this);//实例化画家对象QPixmap pix(:res/game_select.png);painter.drawPixmap(0,0,this-width(),this-height(),pix); }
http://www.hkea.cn/news/14519900/

相关文章:

  • 形容网站页面做的好的词语东莞常平有哪些好玩的地方
  • 个人电脑做网站服务器网站vs做的网站排版错位
  • 工具类网站怎么优化seo湖南广厦建设工程有限公司网站
  • 用wordpress建站要不要用宝塔什么是seo搜索优化
  • 自己做的网站网页错位怎样在网站做两份简历
  • 网站备案网站简介建设网站需要有什么特色
  • 广告公司运作模式网站的优化怎么做
  • 哪些网站是做婴童辅食招商的优化搜狐的培训
  • 陕西省交通建设公司网站学校校园网站建设实践选题背景
  • wordpress 403网站建设丶seo优化
  • 房产网站如何做做公司网站的公
  • 广州网络推广建站三种分销渠道
  • 做网站宣传费用记什么科目如何建造自己的网站
  • 做一个网站的详细教学郑州seo顾问
  • 湖州网站开发区火炬手网站建设技术参数
  • 注册公司网站如何注册做网站电子版报价模板
  • 海曙网站建设哪家好网站风格变化
  • 赚钱的网站平台如何找外包的销售团队
  • 网站建设教育seo怎么优化效果更好
  • 石家庄做标书的网站上海营销咨询公司
  • 网站代搭建维护wordpress免费教育机构主题
  • 一台云服务器做多个网站建设银行打印回单网站
  • 工信部网站备案号查询h5″wordpress
  • 北京 公司网站 备案中 开通访问网页开发流程图
  • 室内设计师网站十大网站两个人看的视频在线观看
  • 漯河建网站做网站优化两年遇到的SEO常态
  • 电子商务网站建设内容wordpress 上传图片不显示
  • 怎么制作ppt幻灯片长沙seo管理
  • 网站建设 入门知识ai效果图制作教程
  • 小米网站 用什么做的全国工程信息查询平台