QT-贪吃小游戏

2024-01-19 12:44
文章标签 qt 小游戏 贪吃

本文主要是介绍QT-贪吃小游戏,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

QT-贪吃小游戏

  • 一、演示效果
  • 二、关键程序
  • 三、下载链接


一、演示效果

在这里插入图片描述

二、关键程序

#include "Snake.h"
#include "Food.h"
#include "Stone.h"
#include "Mushroom.h"
#include "Ai.h"
#include "Game.h"
#include "Util.h"
#include "SnakeUnit.h"
#include <QGraphicsScene>
#include <QGraphicsRectItem>
#include <QTimer>
#include <QDebug>
#include <typeinfo.h>
#include <stdlib.h>
#include <QDesktopWidget>extern Game *g;
QGraphicsScene *sc;
QTimer *timer;Snake::Snake()
{}Snake::Snake(QGraphicsScene *s, QString nam, int l)
{sc = s;length = l;size = 6;alive = false;direction = "right";score = 1;count = 0;    speed = size+2;name = nam;type = "me";//snake bodyint startX = Util::screenWidth()/2;int startY = Util::screenHeight()/2;color = Util::randomSnakeColor();pic = ":/images/snakeUnit"+QString::number(color)+".png";for(int i=0; i<length; i++){SnakeUnit *e = new SnakeUnit(name, this);if(i==0){e->setPixmap(QPixmap(":/images/snakeUnit"+QString::number(color)+"Head.png"));e->setTransformOriginPoint(e->pixmap().width()/2, e->pixmap().height()/2);}else{           e->setPixmap(QPixmap(pic));e->setZValue(-100);            }e->setPos(startX-i*size, startY);s->addItem(e);body.append(e);       }body[0]->setRotation(90);//boundaryboundary = new QGraphicsEllipseItem(0, 0, 100, 100);boundary->setPen(QPen(Qt::transparent));boundary->setZValue(-1);s->addItem(boundary);//snake nameinfo = new QGraphicsTextItem();info->setFont(QFont("calibri", 9));info->setPlainText(name);info->setDefaultTextColor(Qt::white);info->setPos(startX+10, startY+10);s->addItem(info);//move timertimer = new QTimer();connect(timer, SIGNAL(timeout()), this, SLOT(move()));timer->start(100);
}void Snake::move()
{if(g->snake->alive == true){//move snakefor(int i = body.size()-1; i>=0; i--){//bodyif(i != 0){body[i]->setX(body[i-1]->x());body[i]->setY(body[i-1]->y());               }//headelse{//move according to directionif(direction == "right"){body[0]->setX(body[0]->x()+speed);}else if(direction == "left"){body[0]->setX(body[0]->x()-speed);}else if(direction == "up"){body[0]->setY(body[0]->y()-speed);}else if(direction == "down"){body[0]->setY(body[0]->y()+speed);}}}//move boundaryboundary->setX(body[0]->x()-boundary->rect().width()/2);boundary->setY(body[0]->y()-boundary->rect().height()/2);//move snake nameinfo->setX(body[0]->x()+10);info->setY(body[0]->y()+10);//acc according to ai typeif(type == "normal"){//change direction randomly with low attack levelchangeRandomDirection(1);}else if(type == "chipku"){avoidThreat();//change direction randomly according to attack levelchangeRandomDirection(g->attackLevel);}else if(type == "courage"){//move away from threat if presentavoidThreat();//change direction randomly with low attack levelchangeRandomDirection(1);}else if(type == "paytu"){avoidThreat();//eat nearby foodQList<QGraphicsItem *> food_items = boundary->collidingItems();for(int i=0; i<food_items.size(); i++){if(typeid(*(food_items[i])) == typeid(Food) || (typeid(*(food_items[i])) == typeid(Mushroom))){//if food has minimum life of 2 secondsFood *f = (Food*)food_items[i];if(f->life > 1){QString d = Util::giveDirection(body[0]->x(), body[0]->y(), f->x(), f->y());if(d != Util::oppositeDirection(direction)){changeDirection(d);qDebug()<<"Food";}}}}}//check collssionQList<QGraphicsItem *> colliding_items = body[0]->collidingItems();for(int i=0; i<colliding_items.size(); i++){//foodif(typeid(*(colliding_items[i])) == typeid(Food)){body[0]->scene()->removeItem(colliding_items[i]);delete colliding_items[i];                count+=2;//update length at each 10 scoreif(count > 10){score++;count = 0;g->updateScore();//append one unitSnakeUnit *e = new SnakeUnit(name, this);e->setPixmap(QPixmap(pic));e->setPos(-100,-100);body[0]->scene()->addItem(e);body.append(e);}}// Mushroomelse if(typeid(*(colliding_items[i])) == typeid(Mushroom)){g->scene->removeItem(colliding_items[i]);delete colliding_items[i];count+=5;g->updateScore();}//stoneelse if(typeid(*(colliding_items[i])) == typeid(Stone)){               destroy();break;}//other snakeelse if(typeid(*(colliding_items[i])) == typeid(SnakeUnit) && ((SnakeUnit*)colliding_items[i])->parent != this){qDebug()<<"Collission " + name + " : " + ((SnakeUnit*)colliding_items[i])->name;destroy();break;}}//check screen-boundsif(body[0]->x() > sc->width()) body[0]->setX(0);else if(body[0]->x() < 0) body[0]->setX(sc->width());else if(body[0]->y() < 0) body[0]->setY(sc->height());else if(body[0]->y() > sc->height()) body[0]->setY(0);}
}void Snake::destroy(){//remove yourself and turn into cloudsfor(int i=0; i<body.size(); i++){SnakeUnit *s = body[i];new Food(sc, 1, 1, s->x(), s->y());g->scene->removeItem(s); //remove body from scene}g->scene->removeItem(info); //remove info from scenealive = false;g->snakes.removeOne(this);Util::removeReservedName(this->name);Util::removeReservedColor(this->color);g->scene->removeItem(this->boundary);//delete ai from memoryif(type == "ai"){        delete this;}//add new snakeg->generateAi(1);
}void Snake::changeDirection(QString dir){if(dir=="right" && direction != "left"){direction = "right";body[0]->setRotation(0);body[0]->setRotation(90);}else if(dir=="left" && direction != "right"){direction = "left";body[0]->setRotation(0);body[0]->setRotation(-90);}else if(dir=="up" && direction != "down"){direction = "up";body[0]->setRotation(0);}else if(dir=="down" && direction != "up"){direction = "down";body[0]->setRotation(0);body[0]->setRotation(180);}
}void Snake::changeRandomDirection(int attackLevel){if(Util::random(0,10) % 2 == 0){//change directionint r = Util::random(0,3+attackLevel);if(r==0 && direction != "left"){changeDirection("right");}else if(r==1 && direction != "right"){changeDirection("left");}else if(r==2 && direction != "down"){changeDirection("up");}else if(r==3 && direction != "up"){changeDirection("down");}//move towards the playerelse if(r>3){QString d = Util::giveDirection(body[0]->x(), body[0]->y(), g->snake->body[0]->x(), g->snake->body[0]->y());if(direction != Util::oppositeDirection(d)) changeDirection(d);}}
}void Snake::avoidThreat(){bool threat = false;int threatPointX, threatPointY;QList<QGraphicsItem *> boundary_items = boundary->collidingItems();for(int i=0; i<boundary_items.size(); i++){//if its other's boundary or bodyif(typeid(*(boundary_items[i])) == typeid(QGraphicsEllipseItem) || (typeid(*(boundary_items[i])) == typeid(SnakeUnit)) || (typeid(*(boundary_items[i])) == typeid(Stone))){threat = true;threatPointX = (boundary_items[i])->x();threatPointY = (boundary_items[i])->y();}}if(threat == true){QString d = Util::giveDirection(body[0]->x(), body[0]->y(), threatPointX, threatPointY);if(d != Util::oppositeDirection(direction)){changeDirection(Util::oppositeDirection(d));qDebug()<<"Threat kiled";}}
}

三、下载链接

https://download.csdn.net/download/u013083044/88758860

这篇关于QT-贪吃小游戏的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/622465

相关文章

VS配置好Qt环境之后但无法打开ui界面的问题解决

《VS配置好Qt环境之后但无法打开ui界面的问题解决》本文主要介绍了VS配置好Qt环境之后但无法打开ui界面的问题解决,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要... 目UKeLvb录找到Qt安装目录中designer.UKeLvBexe的路径找到vs中的解决方案资源

Qt之QMessageBox的具体使用

《Qt之QMessageBox的具体使用》本文介绍Qt中QMessageBox类的使用,用于弹出提示、警告、错误等模态对话框,具有一定的参考价值,感兴趣的可以了解一下... 目录1.引言2.简单介绍3.常见函数4.按钮类型(QMessage::StandardButton)5.分步骤实现弹窗6.总结1.引言

Qt中Qfile类的使用

《Qt中Qfile类的使用》很多应用程序都具备操作文件的能力,包括对文件进行写入和读取,创建和删除文件,本文主要介绍了Qt中Qfile类的使用,具有一定的参考价值,感兴趣的可以了解一下... 目录1.引言2.QFile文件操作3.演示示例3.1实验一3.2实验二【演示 QFile 读写二进制文件的过程】4.

Qt实现网络数据解析的方法总结

《Qt实现网络数据解析的方法总结》在Qt中解析网络数据通常涉及接收原始字节流,并将其转换为有意义的应用层数据,这篇文章为大家介绍了详细步骤和示例,感兴趣的小伙伴可以了解下... 目录1. 网络数据接收2. 缓冲区管理(处理粘包/拆包)3. 常见数据格式解析3.1 jsON解析3.2 XML解析3.3 自定义

C++如何通过Qt反射机制实现数据类序列化

《C++如何通过Qt反射机制实现数据类序列化》在C++工程中经常需要使用数据类,并对数据类进行存储、打印、调试等操作,所以本文就来聊聊C++如何通过Qt反射机制实现数据类序列化吧... 目录设计预期设计思路代码实现使用方法在 C++ 工程中经常需要使用数据类,并对数据类进行存储、打印、调试等操作。由于数据类

Qt中QGroupBox控件的实现

《Qt中QGroupBox控件的实现》QGroupBox是Qt框架中一个非常有用的控件,它主要用于组织和管理一组相关的控件,本文主要介绍了Qt中QGroupBox控件的实现,具有一定的参考价值,感兴趣... 目录引言一、基本属性二、常用方法2.1 构造函数 2.2 设置标题2.3 设置复选框模式2.4 是否

QT进行CSV文件初始化与读写操作

《QT进行CSV文件初始化与读写操作》这篇文章主要为大家详细介绍了在QT环境中如何进行CSV文件的初始化、写入和读取操作,本文为大家整理了相关的操作的多种方法,希望对大家有所帮助... 目录前言一、CSV文件初始化二、CSV写入三、CSV读取四、QT 逐行读取csv文件五、Qt如何将数据保存成CSV文件前言

Qt中QUndoView控件的具体使用

《Qt中QUndoView控件的具体使用》QUndoView是Qt框架中用于可视化显示QUndoStack内容的控件,本文主要介绍了Qt中QUndoView控件的具体使用,具有一定的参考价值,感兴趣的... 目录引言一、QUndoView 的用途二、工作原理三、 如何与 QUnDOStack 配合使用四、自

Qt spdlog日志模块的使用详解

《Qtspdlog日志模块的使用详解》在Qt应用程序开发中,良好的日志系统至关重要,本文将介绍如何使用spdlog1.5.0创建满足以下要求的日志系统,感兴趣的朋友一起看看吧... 目录版本摘要例子logmanager.cpp文件main.cpp文件版本spdlog版本:1.5.0采用1.5.0版本主要

Qt 中 isHidden 和 isVisible 的区别与使用小结

《Qt中isHidden和isVisible的区别与使用小结》Qt中的isHidden()和isVisible()方法都用于查询组件显示或隐藏状态,然而,它们有很大的区别,了解它们对于正确操... 目录1. 基础概念2. 区别清见3. 实际案例4. 注意事项5. 总结1. 基础概念Qt 中的 isHidd