基于QT多关卡的塔防游戏
基于QT多关卡的塔防游戏
多关卡多怪兽,多防御塔与多子弹类型,对于界面无美化,主要实现其功能,Boss尺寸是故意调大的,对于其路线方向没有进行处理(每个图片朝向不同,除非所有怪兽朝向相同),纯QT实现。
参考模板
参考了优快云地址https://blog.youkuaiyun.com/satanzw/article/details/10418063
核心实现思想
为了偷懒,一个地图多关卡,多怪兽,地图可以设置背景(实现多背景),怪兽可以修改其属性,填充背景等等,包括塔防子弹,其实都是一个类,但是将接口开放,在其上层可扩展控制
运行图片
代码
代码不算难,有基本框架理解了就非常容易。这个就不付太多了
// 怪兽类 实现多样式
void Enemy::SetPixmap()
{
m_maxHp =60;
m_currentHp = 60;
m_walkingSpeed= 2;
m_sprite = QPixmap(":/image/gui2.png");
}
void Enemy::SetMaxEmery()
{
m_maxHp =100;
m_currentHp = 100;
m_walkingSpeed= 1;
m_sprite = QPixmap(":/image/gui3.png");
}
//子弹类的部分代码
void Bullet::SetPixmap()
{
m_sprite = QPixmap(":/image/leaf.png");
}
void Bullet::draw(QPainter *painter) const
{
painter->drawPixmap(m_currentPos, m_sprite);
}
void Bullet::move()
{
// 100毫秒内击中敌人
static const int duration = 100;
QPropertyAnimation *animation = new QPropertyAnimation(this, "m_currentPos");
animation->setDuration(duration);
animation->setStartValue(m_startPos);
animation->setEndValue(m_targetPos);
connect(animation, SIGNAL(finished()), this, SLOT(hitTarget()));
animation->start();
}
void Bullet::SetMaxBullet(int a)
{
if(a==0)
m_sprite = QPixmap(":/image/pull.png");
else {
m_sprite = QPixmap(":/image/pull2.png");
}
}
//塔的升级删除等操作
void MainWitndow::UpTower()
{
int c = ui->UpBox->currentIndex();
int b;
m_towersList.at(c)->GetCount(b);
if(b >=3)
{
QMessageBox message(QMessageBox::Warning,"Error","Connot up Current Tower!", QMessageBox::Yes , NULL);
message.exec();
return;
}
m_myGold -= TowerCost*0.3;
m_towersList.at(c)->SetKill();
}
void MainWindow::DeleteTower()
{
m_myGold += TowerCost*0.7;
int c = ui->UpBox->currentIndex();
ui->UpBox->removeItem(c);
auto it = m_towerDeleteList.begin();
int i = 0;
while (it != m_towerDeleteList.end())
{
if(i==c)
{
QPoint t = it->centerPos();
for(auto ite = m_towerPositionsList.begin();ite!=m_towerPositionsList.end();ite++)
{
if(t == ite->centerPos())
{ ite->DeleteTower();
it->DeleteTower();
}
}
}
it++;
i++;
}
m_towerDeleteList.removeAt(c);
m_towersList.removeOne(m_towersList.at(c));
update();
}
void MainWindow::GBTower()
{
int c = ui->UpBox->currentIndex();
int a = ui->comboBox->currentIndex();
int b ;
m_towersList.at(c)->GetCount(b);
if(b <3||b==4)
{
QMessageBox message(QMessageBox::Warning,"Error","Connot Change Current Tower!", QMessageBox::Yes , NULL);
message.exec();
return;
}
if(a == 0)
m_myGold -= TowerCost*0.7;
if(a==1)
m_myGold -= TowerCost*3;
m_towersList.at(c)->SetPixmap(a);
}
void MainWindow::awardGold(int gold)
{
m_myGold += gold;
update();
}