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

相关文章

基于Qt Qml实现时间轴组件

《基于QtQml实现时间轴组件》时间轴组件是现代用户界面中常见的元素,用于按时间顺序展示事件,本文主要为大家详细介绍了如何使用Qml实现一个简单的时间轴组件,需要的可以参考下... 目录写在前面效果图组件概述实现细节1. 组件结构2. 属性定义3. 数据模型4. 事件项的添加和排序5. 事件项的渲染如何使用

基于Qt开发一个简单的OFD阅读器

《基于Qt开发一个简单的OFD阅读器》这篇文章主要为大家详细介绍了如何使用Qt框架开发一个功能强大且性能优异的OFD阅读器,文中的示例代码讲解详细,有需要的小伙伴可以参考一下... 目录摘要引言一、OFD文件格式解析二、文档结构解析三、页面渲染四、用户交互五、性能优化六、示例代码七、未来发展方向八、结论摘要

python与QT联合的详细步骤记录

《python与QT联合的详细步骤记录》:本文主要介绍python与QT联合的详细步骤,文章还展示了如何在Python中调用QT的.ui文件来实现GUI界面,并介绍了多窗口的应用,文中通过代码介绍... 目录一、文章简介二、安装pyqt5三、GUI页面设计四、python的使用python文件创建pytho

QT实现TCP客户端自动连接

《QT实现TCP客户端自动连接》这篇文章主要为大家详细介绍了QT中一个TCP客户端自动连接的测试模型,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 目录版本 1:没有取消按钮 测试效果测试代码版本 2:有取消按钮测试效果测试代码版本 1:没有取消按钮 测试效果缺陷:无法手动停

基于Qt实现系统主题感知功能

《基于Qt实现系统主题感知功能》在现代桌面应用程序开发中,系统主题感知是一项重要的功能,它使得应用程序能够根据用户的系统主题设置(如深色模式或浅色模式)自动调整其外观,Qt作为一个跨平台的C++图形用... 目录【正文开始】一、使用效果二、系统主题感知助手类(SystemThemeHelper)三、实现细节

Qt实现文件的压缩和解压缩操作

《Qt实现文件的压缩和解压缩操作》这篇文章主要为大家详细介绍了如何使用Qt库中的QZipReader和QZipWriter实现文件的压缩和解压缩功能,文中的示例代码简洁易懂,需要的可以参考一下... 目录一、实现方式二、具体步骤1、在.pro文件中添加模块gui-private2、通过QObject方式创建

Qt QWidget实现图片旋转动画

《QtQWidget实现图片旋转动画》这篇文章主要为大家详细介绍了如何使用了Qt和QWidget实现图片旋转动画效果,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 一、效果展示二、源码分享本例程通过QGraphicsView实现svg格式图片旋转。.hpjavascript

嵌入式QT开发:构建高效智能的嵌入式系统

摘要: 本文深入探讨了嵌入式 QT 相关的各个方面。从 QT 框架的基础架构和核心概念出发,详细阐述了其在嵌入式环境中的优势与特点。文中分析了嵌入式 QT 的开发环境搭建过程,包括交叉编译工具链的配置等关键步骤。进一步探讨了嵌入式 QT 的界面设计与开发,涵盖了从基本控件的使用到复杂界面布局的构建。同时也深入研究了信号与槽机制在嵌入式系统中的应用,以及嵌入式 QT 与硬件设备的交互,包括输入输出设

【QT】基础入门学习

文章目录 浅析Qt应用程序的主函数使用qDebug()函数常用快捷键Qt 编码风格信号槽连接模型实现方案 信号和槽的工作机制Qt对象树机制 浅析Qt应用程序的主函数 #include "mywindow.h"#include <QApplication>// 程序的入口int main(int argc, char *argv[]){// argc是命令行参数个数,argv是

Python QT实现A-star寻路算法

目录 1、界面使用方法 2、注意事项 3、补充说明 用Qt5搭建一个图形化测试寻路算法的测试环境。 1、界面使用方法 设定起点: 鼠标左键双击,设定红色的起点。左键双击设定起点,用红色标记。 设定终点: 鼠标右键双击,设定蓝色的终点。右键双击设定终点,用蓝色标记。 设置障碍点: 鼠标左键或者右键按着不放,拖动可以设置黑色的障碍点。按住左键或右键并拖动,设置一系列黑色障碍点