当前位置:网站首页>Wild road play QT, episode 31, glass cleaning game
Wild road play QT, episode 31, glass cleaning game
2022-04-21 19:50:00 【Old man ginseng】
https://live.csdn.net/v/199805
Glass cleaning games
This example , Most simplified Qt The logic and ideas of developing small games ,
Graphic elements --》 scene --》 View
QGraphicsItem--》QGraphicsScene--》QGraphicsView
Graphic elements : Little ant , Rag , Background image
Code :
pro File to add :
QT += core gui multimedia
widget.h
#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>
#include <QGraphicsItem>
#include <math.h>
#include <QGraphicsScene>
#include <QPainter>
#include <QStyleOption>
#include <QGraphicsRectItem>
#include <QGraphicsView>
#include <QDebug>
#include <QList>
#include <QTimer>
#include <QMediaPlayer>
#include <QMediaPlaylist>
namespace Ui {
class Widget;
}
class Widget : public QWidget
{
Q_OBJECT
public:
explicit Widget(QWidget *parent = 0);
~Widget();
// Generate ants
void CreateMaYi();
// Collision detection function
void Collison();
private:
Ui::Widget *ui;
QGraphicsView mGameView;// Game view
QGraphicsScene mScene;// Game scenario
QGraphicsPixmapItem mBackGround1;// Background image 1
QGraphicsPixmapItem mMabu;// Rag
// Media playing background music
QMediaPlayer *player;
QMediaPlaylist *playlist;
// Timer
QTimer *mMaYiCreateTimer;// Ants generate timers
QTimer *mCollisonTimer;// collision detection
// Containers
QList<QGraphicsPixmapItem*> mMaYiList;// Ant container , Pretend there are ants
};
#endif // WIDGET_H
widget.cpp:
#include "widget.h"
#include "ui_widget.h"
Widget::Widget(QWidget *parent) :
QWidget(parent),
ui(new Ui::Widget)
{
ui->setupUi(this);
// Graphic elements --》 scene --》 View
// Play background music
player=new QMediaPlayer;
playlist = new QMediaPlaylist;
playlist->setPlaybackMode(QMediaPlaylist::Loop);// By setting Playlist loop Achieve loop play
playlist->addMedia(QUrl("qrc:/img/caboli.mp3"));
player->setPlaylist(playlist);
player->setVolume(100);
player->play();
this->setFixedSize(800,800);// Set the window size
mGameView.setSceneRect(QRect(0,0,800,800));// Set up the view , Rectangle size
mScene.setSceneRect(QRect(0,0,800,800));// Setting scene , Rectangle size
mBackGround1.setPixmap(QPixmap(":/img/img/boli.jpg"));// Set the background image
mMabu.setPixmap(QPixmap(":/img/img/mabu.png"));// Set up a rag
mMabu.setFlag(QGraphicsItem::ItemIsMovable);// Mouse drag support
mMabu.setPos(500,500);// Set the position of the rag
mMabu.setZValue(1);// Set the rag as the upper layer
// Add picture elements to the scene
mScene.addItem(&mBackGround1);// Add background image 1
mScene.addItem(&mMabu);// Add a rag picture
// Set the view scene
mGameView.setScene(&mScene);
// Set the parent of the view to window
mGameView.setParent(this);
// Show view
mGameView.show();
// Start the ant creation timer
mMaYiCreateTimer = new QTimer(this);
mMaYiCreateTimer->start(2000);
connect(mMaYiCreateTimer,&QTimer::timeout,this,&Widget::CreateMaYi);
// Start the collision detection timer
mCollisonTimer = new QTimer(this);
mCollisonTimer->start(100);
connect(mCollisonTimer,&QTimer::timeout,this,&Widget::Collison);
}
Widget::~Widget()
{
delete ui;
}
void Widget::CreateMaYi()// Generate ants
{
QGraphicsPixmapItem *mMayi = new QGraphicsPixmapItem;// Ant
QPixmap pixmap(":/img/img/mayi.png");
mMayi->setPixmap(pixmap);
int randX = qrand()%(800-pixmap.width());// Randomly generated ant starting point X Location
int randY = qrand()%(800-pixmap.height());// Randomly generated ant starting point Y Location
mMayi->setPos(QPoint(randX,randY));
// Add to scene
mScene.addItem(mMayi);
// Add to manager
mMaYiList.append(mMayi);
}
void Widget::Collison()// Collision detection function
{
// Traverse the ant container
for(int i=0; i < mMaYiList.size(); i++ )
{
if(mMabu.collidesWithItem(mMaYiList[i]))
{
// Remove scene
mScene.removeItem(mMaYiList[i]);
// Remove Manager
mMaYiList.removeOne(mMaYiList[i]);
qDebug()<<" Collision ";
}
}
}
Pictures of resources used :



版权声明
本文为[Old man ginseng]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204211943337110.html
边栏推荐
- Building / building projects on the command line using cmake
- Building / Qt 5 and QT 6 compatibility using cmake
- R语言数据分析从入门到高级:(八)数据清洗技巧之数据格式转换(包含宽数据与长数据之间的转换)
- 【玩转Lighthouse】使用腾讯云轻量实现微信支付业务
- Modifying the root password of MySQL
- 界面组件Telerik UI for WPF入门指南 - 颜色主题生成器
- Results of the 21st day at home
- 艾尔登法环“无法加载保存数据”解决方法
- Xinguan is merciless, human beings have feelings, Xinlong agriculture ensures people's livelihood and jointly fights the epidemic -- condolences to the front line of fighting the epidemic, love the el
- juc-Queue接口以及其实现类
猜你喜欢

SAP PS section 12 network cost plan

Installing MySQL 8 on Linux centos7 (simple and practical)

SAP PS 第12节 网络成本计划

Interesting souls are the same. It takes only 2 steps to slide the computer off

Solutions informatiques pour les entreprises manufacturières haut de gamme, maintenance prédictive de l'équipement, des données et du système de la plate - forme de commerce électronique industriel

Introduction to applet project files

照片删除了怎么恢复?4个方案,这才是官方指南

数商云:剖析企业采购管理的现状,推进企业采购模式优化升级

SVPWM模块为什么会出现扇区判断错误?

MYSQL输入密码后闪退的解决方法
随机推荐
vtkjs介绍
使用CMake构建/导入目标
Interesting souls are the same. It takes only 2 steps to slide the computer off
How to judge whether the nbit bit of int type value is 1 or 0
GBase 8a设置 group_concat_max_len 参数后报错解决方案
Installing MySQL 8 on Linux centos7 (simple and practical)
[ctf.show.reverse] 1024杯 抽象语言
iMeta | EndNote调整完美引文格式教程(视频)
Dolphin DB vscode plug-in tutorial
Xinguan is merciless, human beings have feelings, Xinlong agriculture ensures people's livelihood and jointly fights the epidemic -- condolences to the front line of fighting the epidemic, love the el
05. Prototype mode
杰理之使用硬件定时器来模拟中断请求【篇】
leetcode541. Reverse string II
ParaView Glance 启动报错
Redis基础
HW-新员工考试-遍历
内置注解讲解
Use of complex function in MATLAB
SAP PS 第12节 网络成本计划
杰理之VDDIO_SYSVDD_DCDC14系统电压配置说明【篇】