QT--吃月亮小游戏
Posted intelligencepointer
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了QT--吃月亮小游戏相关的知识,希望对你有一定的参考价值。
QT--吃月亮小游戏
简介:
-
该设计使用了鼠标事件、键盘事件、绘图事件,可通过鼠标或者方向键控制人物移动吃到月亮
代码:
widget.h文件:
#ifndef WIDGET_H #define WIDGET_H #include <QWidget> #include <QPaintEvent> //绘图事件 #include <QMouseEvent> //鼠标 #include <QPoint> #include <QKeyEvent> //键盘 #include <QPainter> #include <QList> #include <QLineEdit> namespace Ui class Widget; class Widget : public QWidget Q_OBJECT public: explicit Widget(QWidget *parent = nullptr); ~Widget(); public: int random_x; //保存月亮随机坐标 int random_y; int direction; //保存键盘事件 int Score = 0; //得分 QString Score_data; //保存得分 bool meet = false; QPainter *painter; QLineEdit *line; void Wall_x(int &); void Wall_y(int &); //遇见墙壁 void meet_s(); //遇到月亮,重画月亮 int Xrandom(); //产生随机数 int Yrandom(); //产生随机数 void Direction(); //控制人物改变方向 void paintEvent(QPaintEvent *event); //绘图 void mousePressEvent(QMouseEvent *event); //鼠标点击 void mouseMoveEvent(QMouseEvent *event); //鼠标拖动 void keyPressEvent(QKeyEvent *event); //键盘事件 private: QPoint point; private: Ui::Widget *ui; ; #endif // WIDGET_H
#include "widget.h" #include "ui_widget.h" #include <QDebug> #include <QPainter> //绘图画家 #include <QTime> #include <QMutableListIterator> Widget::Widget(QWidget *parent) : QWidget(parent), ui(new Ui::Widget) ui->setupUi(this); this->setMinimumSize(500,500); //窗口大小 point.setX(this->width()/2); //初始化人物 在窗口中央 point.setY(this->width()/2); random_x = Xrandom(); //初始化月亮 random_y = Yrandom(); void Widget::mousePressEvent(QMouseEvent *event) //鼠标点击 qDebug() << event->pos() ; point = event->pos(); update(); void Widget:: mouseMoveEvent(QMouseEvent *event) //鼠标拖动 qDebug() <<event->pos(); point = event->pos(); update(); void Widget::Wall_x(int &x) //遇见墙壁 if(x <= 20) x += 50; //遇见就弹回 Score -= 3; else if (x >= this->width()-40) x -= 50; Score -= 3; void Widget::Wall_y(int &y) //遇见墙壁 if(y <= 20) y += 50; Score -= 3; else if (y >= this->height()-40) y -= 50; Score -= 3; void Widget::keyPressEvent(QKeyEvent *event) //键盘事件,控制人物移动 int x = point.x(); int y = point.y(); qDebug() << event->key() ; direction = event->key(); switch(event->key()) case Qt::Key_Up: //上 qDebug() <<"上"; y -= 10; Wall_y(y); break; case Qt::Key_Down: //下 qDebug() <<"下"; y += 10; Wall_y(y); break; case Qt::Key_Left: //左 qDebug() <<"左"; x -= 10; Wall_x(x); break; case Qt::Key_Right: //右 qDebug() <<"右"; x += 10; Wall_x(x); break; default : break; point.setX(x) ; point.setY(y); qDebug() <<x <<":" <<y; int Widget::Xrandom() //产生随机数 int random_q; QTime time; time= QTime::currentTime(); qsrand(time.msec()+time.second()*1000); random_q = qrand() % (this->width()-40); if(random_q < 20) random_q += 20; return random_q; int Widget::Yrandom() //产生随机数 int random_q; QTime time; time= QTime::currentTime(); qsrand(time.msec()+time.second()*1000); random_q = qrand() % (this->height()-40); if(random_q < 20) random_q += 20; return random_q; void Widget::meet_s() //遇到月亮 if((point.x()-random_x < 20 && point.x()-random_x > -20)&& (point.y() - random_y < 20 &&point.y() - random_y > -20) ) meet = true; if(!meet) /* 豆豆 */ QRectF target_Peas(random_x,random_y, 100, 100); QRectF source_Peas(0,0, 400,400); QPixmap pixmap_Peas(":/new/prefix1/peas3.jpg"); //此处使用自己添加的资源文件夹内的图片 painter->drawPixmap(target_Peas, pixmap_Peas, source_Peas); else qDebug() <<"遇见"; random_x = Xrandom(); random_y = Yrandom(); Score++; meet = false; void Widget::Direction() //控制人物改变方向 QRectF target(-10,-10, 40.0, 40.0); QRectF source(0.0, 0.0, 400.0, 400.0); QPixmap pixmap_right(":/new/prefix1/character_right.jpg"); //此处使用自己添加的资源文件夹内的图片 QPixmap pixmap1_left(":/new/prefix1/character_left.jpg"); //人物向左移动是的图片 if(Qt::Key_Up == direction) //上 painter->rotate(-90); painter->drawPixmap(target, pixmap_right, source); else if(Qt::Key_Down == direction) //下 painter->rotate(90); painter->drawPixmap(target, pixmap_right, source); else if(Qt::Key_Left == direction) //左 painter->drawPixmap(target, pixmap1_left, source); else //右 painter->drawPixmap(target, pixmap_right, source); void Widget::paintEvent(QPaintEvent *event) painter = new QPainter(this); /* 画笔 */ QPen pen (Qt::red,10); QPen pen1 (Qt::green,5); painter->setPen(pen1); /* 边框 */ QRectF rectangle(0, 0,this->width(),this->width()); QRectF rectangle1(10, 10, this->width()-20, this->height()-20); /* 背景 */ QRectF target(0,0, this->width(), this->width()); QRectF source(0,0, this->width(), this->width()); QPixmap pixmap_background(":/new/prefix1/background.jpg"); painter->drawPixmap(target, pixmap_background, source); painter->setRenderHint(QPainter::Antialiasing,true); //抗锯齿 /* 画边框 */ painter->drawRect(rectangle); painter->drawRect(rectangle1); /* 得分 */ Score_data = "得分:" + QString::number(Score); painter->drawText(0,0,100,100,Qt::AlignCenter,Score_data); painter->setPen(pen); meet_s(); //绘制月亮 painter->translate(point.x()+10,point.y()+10); //移动画家 Direction(); //画人物 update(); //必须要刷新 Widget::~Widget() delete ui;
以上是关于QT--吃月亮小游戏的主要内容,如果未能解决你的问题,请参考以下文章
Python游戏开发,pygame模块,Python实现经典吃豆豆小游戏