本以为做完了,心血来潮又加了一些,传奇的大招系统,话不多说直接上代码
新建一个select button4类
#ifndef SELECTBUTTON3_H
#define SELECTBUTTON3_H
#include <QPainter>
#include <QPoint>
#include <QSize>
#include"gamescene.h"
#include"legends.h"
class Gamescene;
class Legends;
class SelectButton4
{
public:
SelectButton4();
SelectButton4(QPoint pos,Gamescene *game,QString path=":/image/star.png");
~SelectButton4();
//绘画button
void draw(QPainter * painter)const;
//移除button2
void getRemoved();
//得到该button2的传奇
Legends * getLegend();
//设置该button2的传奇
void setLegend(Legends * legend);
//判断点击点是否在button2的内部
bool containPos(QPoint pos);
//得到button2的左上点
QPoint getPos();
//图片固定大小
static const QSize myfixedSize;
protected:
//指向游戏界面的指针
Gamescene *mygame;
//我的传奇
Legends *mylegend;
//坐标点
QPoint mypos;
//图片地址
QString mypath;
};
#endif // SELECTBUTTON4_H
#include "selectbutton4.h"
const QSize SelectButton4::myfixedSize(90,90);
//构造函数
SelectButton4::SelectButton4(QPoint pos,Gamescene *game,QString path):
mygame(game),
mypos(pos),
mypath(path)
{
}
//析构函数
SelectButton4::~SelectButton4()
{
mygame=NULL;
mylegend=NULL;
}
//得到坐标
QPoint SelectButton4::getPos()
{
return mypos;
}
//移除该button
void SelectButton4::getRemoved()
{
mygame->removeButton4(this);
}
//设置此处的传奇
void SelectButton4::setLegend(Legends *Legend)
{
mylegend=Legend;
}
//绘画函数
void SelectButton4::draw(QPainter *painter) const
{
painter->save();
painter->drawPixmap(mypos.x(),mypos.y(),myfixedSize.width(),myfixedSize.height(),mypath);
painter->restore();
}
//存在选择框
bool SelectButton4::containPos(QPoint pos)
{
bool xInHere=(pos.x()>mypos.x() && pos.x()<mypos.x()+myfixedSize.width());
bool yInHere=(pos.y()>mypos.y() && pos.y()<mypos.y()+myfixedSize.height());
return xInHere && yInHere;
}
在gamescene中连接,修改鼠标事件
在legends类中增加大招
首先添加计时器
/构造函数中
myabilityTimer=new QTimer(this);
connect(myabilityTimer,SIGNAL(timeout()),this,SLOT(onCountdown()));
mytime=15;
/其他函数中实现
//计时器开始计时
void Legends::beginCountdown()
{
if(myabilityTimer->isActive()==false)
{
myabilityTimer->start(1000);
}
}
//计时器正在计时
void Legends::onCountdown()
{
mytime-=1;
if(mytime==0)
{
myabilityTimer->stop();
}
}
传奇大招函数
//释放技能
void Legends:: doAbility()
{
std::cout<<"doability"<<std::endl;
switch (mynum)
{
case 1 :{
beginCountdown();
if(myabilityTimer->isActive()==true)
{
Legend[mynum].myattackRange*=2;
}
}break;
case 2 :{
QList<Enemy * > enemyList=mygame->getEnemyList();
foreach(Enemy * enemy,enemyList)
enemy->boomed();
}break;
case 3 :{
mygame->awardMoney(300);
}break;
case 4 :{
beginCountdown();
if(myabilityTimer->isActive()==true)
{
mychooseEnemy->lowSpeed();
}
}break;
case 5 :{
beginCountdown();
if(myabilityTimer->isActive()==true)
{
Legend[mynum].mydamage *=2;
}
}break;
}
}
ok,增加功能完成,在gamescene的绘画函数可视化绝招即可