QT实现塔防游戏6

文章展示了如何在C++的Qt环境中构建一个传奇游戏中大招系统的一部分。主要涉及SelectButton4类的定义和使用,以及在Gamescene和Legends类中的集成。Legends类中添加了计时器以实现大招的冷却机制,并定义了不同大招的效果,如攻击范围增加、敌人状态改变等。
摘要由CSDN通过智能技术生成

本以为做完了,心血来潮又加了一些,传奇的大招系统,话不多说直接上代码

新建一个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的绘画函数可视化绝招即可

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值