#include "gamemap.h"
#include <QScrollBar>
#include <QTimer>
#include <QDebug>
#include <QGraphicsProxyWidget>
#include "plantscard.h"
#include "gamemedia.h"
#include <QVector>
#include "cooldowndark.h"
#include <QGraphicsPixmapItem>
#include <QMouseEvent>
#include <QGraphicsPixmapItem>
#include "plants.h"
#include "zombies.h"
#include "smallcar.h"
#include <QOpenGLWidget>
#include "spade.h"
#include <QtGlobal>
#include <QPropertyAnimation>
#include <QList>
gameMap::gameMap(QGraphicsView *parent) : QGraphicsView(parent)
{
//测试
// QOpenGLWidget* openGLWidget = new QOpenGLWidget;
// this->setViewport(openGLWidget);
this->setWindowTitle("Plants VS Zombies");
this->gameScene = new QGraphicsScene;
this->backgroundItem = new QGraphicsPixmapItem;
//隐藏指针
//this->setCursor(Qt::BlankCursor);
//添加草地背景
this->gameScene->setSceneRect(QRectF(0,0,1400,600));
this->backgroundItem->setPixmap(QPixmap(":/res/backGround/gameBackground.jpg"));
this->gameScene->addItem(this->backgroundItem);
this->setScene(this->gameScene);
this->setFixedSize(800,602);
//隐藏水平拖动条
this->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
this->horizontalScrollBar()->setValue(288);
//显示阳光label
this->showSun = new QLabel;
this->showSun->setText(QString::number(gameMap::SunshineNum));
this->showSun->setStyleSheet("QLabel {background-color: transparent;}");
this->showSun->setFont(QFont("Microsoft YaHei",10));
this->showSun->resize(50,50);
this->showSun->setAlignment(Qt::AlignHCenter);
QTimer * updateSun = new QTimer(this);
connect(updateSun,&QTimer::timeout,this,&gameMap::updateSun);
updateSun->start(100);
//显示初始站立僵尸
this->showBeginStandZombies();
this->showChoosePlantsWidget();
this->gameBeginMove();
//测试代码用 编译删除-------------------------------------------
//this->downChooseCard->start(3);
//------------------------------------------------------------
//强制刷新
this->setViewportUpdateMode(QGraphicsView::FullViewportUpdate);
}
void gameMap::setMouse()
{
QGraphicsPixmapItem * item = new QGraphicsPixmapItem;
item->setPixmap(QPixmap(":/res/mouse/mouse.png"));
this->gameScene->addItem(item);
item->setZValue(60);
QTimer * timer = new QTimer;
connect(timer,&QTimer::timeout,this,[=](){
item->setPos(Plants::mousePosX - 20,Plants::mousePosY + 29);
});
timer->start(6);
}
void gameMap::showChoosePlantsWidget()
{
this->downChooseCard = new QTimer(this);
//创建选卡窗口
this->chooseCardWidget = new QGraphicsPixmapItem(QPixmap(":/res/backGround/chooseCardBar.png"));
this->chooseCardWidget->setZValue(1);
//this->chooseCardWidget->setPos(230,-3);
this->chooseCardWidget->setPos(230,-89);
this->gameScene->addItem(chooseCardWidget);
showSunLabel = this->gameScene->addWidget(this->showSun);
showSunLabel->setPos(243,60);
showSunLabel->hide();
showSunLabel->setZValue(2);
//连接向下移动选择卡牌窗口
connect(this->downChooseCard,&QTimer::timeout,this,[=](){
this->chooseCardWidget->setPos(chooseCardWidget->x(),chooseCardWidget->y() + 1);
if(this->chooseCardWidget->y() >= -3)
{
this->downChooseCard->stop();
this->showSunLabel->show();
//显示植物卡牌
this->doShowPlantsCard();
}
});
}
void gameMap::updateSun()
{
//检测阳光是否发生变化
if(this->showSun->text().toInt() == gameMap::SunshineNum)
{
return;
}
this->showSun->setText(QString::number(gameMap::SunshineNum));
}
void gameMap::gameBeginMove()
{
this->leftMove = new QTimer(this);
this->rightMove = new QTimer(this);
this->horizontalScrollBar()->setValue(0);
//播放开头音乐
this->beginChooseCardMedia = new gameMedia(":/res/Media/gameBeginChooseCard.wav");
connect(this->rightMove,&QTimer::timeout,this,[=](){
this->horizontalScrollBar()->setValue(this->horizontalScrollBar()->value() + 1);
if(this->horizontalScrollBar()->value() >= 600)
{
this->rightMove->stop();
//等待两秒后 镜头向左移动
QTimer::singleShot(2300,[=](){
//镜头向左移动
this->leftMove->start(2);
});
}
});
connect(this->leftMove,&QTimer::timeout,this,[=](){
this->horizontalScrollBar()->setValue(this->horizontalScrollBar()->value() - 1);
if(this->horizontalScrollBar()->value() <= 217)
{
this->leftMove->stop();
//出现选择植物框
this->downChooseCard->start(2);
//显示小推车
this->setSmallCar();
//显示准备开始游戏
this->showRedReady();
//鼠标指针
//this->setMouse();
//清理开头站立僵尸
this->deleteBeginZombie();
//设置铲子
this->setSpade();
//设置自然阳光
QTimer * setSunTimer = new QTimer(this);
connect(setSunTimer,&QTimer::timeout,this,&gameMap::setSunshine);
setSunTimer->start(13000);
//开始生成僵尸
this->outZombies();
}
});
//开局等待两秒后 开始移动镜头
QTimer::singleShot(2300,[=](){
this->rightMove->start(2);
});
}
void gameMap::doShowPlantsCard()
{
int id = 1;
this->cards = {&Id_1,&Id_2,&Id_3,&Id_4,&Id_5,&Id_6};
for(QVector<PlantsCard**>::iterator it = cards.begin();it!=cards.end();it++)
{
*(*it) = new PlantsCard(id,248 + (53 * id),6);
this->gameScene->addItem((*(*it)));
id ++;
//将半透明阴影item 发送至scene游戏场景中
connect(*(*it),&PlantsCard::sendNewItem,this,&gameMap::getNewItem);
//连接 花费阳光信号槽
connect(*(*it),&PlantsCard::spendSunshine,this,&gameMap::doSpendSunshine);
}
//将阳光传递给每张卡
// connect(this,&gameMap::sendSunshine,this,[=](int num){
// for(QVector<PlantsCard**>::iterator it = cards.begin();it!=cards.end();it++)
// {
// (*(*it))->getSunshine(num);
// }
// });
// QTimer * send = new QTimer;
// connect(send,&QTimer::timeout,this,[=](){
// emit this->sendSunshine(gameMap::SunshineNum);
// });
// send->start(10);
}
void gameMap::getNewItem(QGraphicsObject * item)
{
this->scene()->addItem(item);
}
void gameMap::doSpendSunshine(int num)
{
gameMap::SunshineNum -= num;
}
void gameMap::showRedReady()
{
this->showReadyGame = new QGraphicsPixmapItem;
//音效
gameMedia * ready = new gameMedia(":/res/Media/ReadySetPlant.wav",this);
this->showReadyGame->setPixmap(QPixmap(":/res/backGround/gameBeginRed/gameBegin-Ready.png"));
this->showReadyGame->setPos(500,260);
//逐渐停止最开始选卡音乐
this->beginChooseCardMedia->stopMedia(true);
//计时器切换图片
QTimer::singleShot(600,[=](){
this->showReadyGame->setPixmap(QPixmap(":/res/backGround/gameBeginRed/gameBegin-Set.png"));
this->showReadyGame->setPos(530,260);
});
QTimer::singleShot(1200,[=](){
this->showReadyGame->setPixmap(QPixmap(":/res/backGround/gameBeginRed/gameBegin-Plant.png"));
this->showReadyGame->setPos(436,260);
});
QTimer::singleShot(2000,[=](){
this->showReadyGame->setPos(0,-300);
//播放白天音乐
gameMedia * dayMusic = new gameMedia(":/res/Media/pvzDayMusic.wav");
//设置循环播放
dayMusic->setForeverPlay(":/res/Media/pvzDayMusic.wav");
});
//添加至场景中5
this->gameScene->addItem(this->showReadyGame);
}
void gameMap::mouseMoveEvent(QMouseEvent *event)
{
//将鼠标坐标存储至Plants公共成员变量
P
没有合适的资源?快使用搜索试试~ 我知道了~
温馨提示
1.项目代码均经过功能验证ok,确保稳定可靠运行。欢迎下载体验!下载完使用问题请私信沟通。 2.主要针对各个计算机相关专业,包括计算机科学、信息安全、数据科学与大数据技术、人工智能、通信、物联网等领域的在校学生、专业教师、企业员工。 3.项目具有丰富的拓展空间,不仅可作为入门进阶,也可直接作为毕设、课程设计、大作业、初期项目立项演示等用途。 4.当然也鼓励大家基于此进行二次开发。在使用过程中,如有问题或建议,请及时沟通。 5.期待你能在项目中找到乐趣和灵感,也欢迎你的分享和反馈! 【资源说明】 C++开发基于QT实现的植物大战僵尸小游戏源码.zipC++开发基于QT实现的植物大战僵尸小游戏源码.zipC++开发基于QT实现的植物大战僵尸小游戏源码.zipC++开发基于QT实现的植物大战僵尸小游戏源码.zipC++开发基于QT实现的植物大战僵尸小游戏源码.zipC++开发基于QT实现的植物大战僵尸小游戏源码.zipC++开发基于QT实现的植物大战僵尸小游戏源码.zipC++开发基于QT实现的植物大战僵尸小游戏源码.zipC++开发基于QT实现的植物大战僵尸小游戏源码.zipC++开发基于QT实现的植物大战僵尸小游戏源码.zipC++开发基于QT实现的植物大战僵尸小游戏源码.zip C++开发基于QT实现的植物大战僵尸小游戏源码.zipC++开发基于QT实现的植物大战僵尸小游戏源码.zip
资源推荐
资源详情
资源评论
收起资源包目录
C++开发基于QT实现的植物大战僵尸小游戏源码.zip (36个子文件)
code_project
cooldowndark.h 618B
gamemap.h 2KB
peas.cpp 2KB
plantscard.h 1KB
zombie.h 206B
zombies.h 2KB
zombies.cpp 5KB
mainwidget.cpp 163B
plantthread.h 1KB
gamemap.cpp 12KB
sumshine.cpp 88B
mainwidget.h 223B
plantthread.cpp 536B
gamemedia.cpp 2KB
plants.h 2KB
shootbox.h 730B
main.cpp 208B
peas.h 606B
sunshine.h 1KB
spade.h 667B
PVZ.pro 2KB
PVZ.pro.user 41KB
gamemedia.h 660B
cooldowndark.cpp 1KB
setplants.cpp 2KB
sumshine.h 216B
plants.cpp 10KB
smallcar.h 670B
spade.cpp 2KB
res.qrc 14KB
smallcar.cpp 2KB
plantscard.cpp 4KB
zombie.cpp 82B
setplants.h 372B
sunshine.cpp 3KB
shootbox.cpp 2KB
共 36 条
- 1
资源评论
北航程序员小C
- 粉丝: 2222
- 资源: 1823
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功