在这里插入代码片
void MainWindow::paintEvent(QPaintEvent *)
{
if(option_flag)
{
QPixmap level1(":/image/level1.png");
QPixmap level2(":/image/level2.png");
QPixmap level3(":/image/level3.png");
QPixmap level4(":/image/map4.jpg");
QPixmap img = level4.scaled(720,420);
QPainter levelpainter(this);
levelpainter.drawPixmap(0,0,level1);
levelpainter.drawPixmap(720,0,level2);
levelpainter.drawPixmap(0,420,level3);
levelpainter.drawPixmap(720,420,img);
return;
}
if(start_flag)
{
QPixmap start_picture(":/image/screen.jpg");
QPainter startpainter(this);
QPixmap img=start_picture.scaled(1440,840);
startpainter.drawPixmap(0,0,img);
this->startButton ->show();
this->optionButton ->show();
this->helpButton ->show();
this->exitButton ->show();
if(restart_flag)
{
m_waves=0;
m_playerGold=1000;
m_wavesInfo.clear();
m_towerPositionsList.clear();
m_towersList.clear();
m_wayPointsList.clear();
m_enemyList.clear();
m_bulletList.clear();
preLoadWavesInfo(1);
loadTowerPositions123();
//loadTowerPosition4();
addWayPoints123();
//addWayPoints4();
m_gameEnded=0;
m_gameWin=0;
m_playerHp=1;
}
return;
}
if (m_gameEnded || m_gameWin)
{
this->pauseButton->hide();
this->exitButton_1->hide();
this->replay->hide();
this->screenShotButton->hide();
this->damageskill->hide();
this->slowdown->hide();
QPainter painter(this);
QPixmap pix;
if(m_gameWin)
{
pix.load(":/image/25.jpg");
painter.drawPixmap(0,0,1440,840,pix);
}
if(m_gameEnded)
{
pix.load(":/image/fail.jpg");
painter.drawPixmap(0,0,1440,840,pix);
}
return ;
}
QPixmap cachePix;
if(_level!=4){
cachePix.load(":/image/Bg.png");
}
else{
cachePix.load(":/image/Bg2.png");
}
QPainter cachePainter(&cachePix);
foreach (const Tower *tower, m_towersList)
tower->draw(&cachePainter);
//draw the towers
foreach (const WayPoint *wayPoint, m_wayPointsList)
wayPoint->draw(&cachePainter);
//draw the waypoints
foreach (const Enemy *enemy, m_enemyList)
enemy->draw(&cachePainter);
//draw the enemies
foreach (const Bullet *bullet, m_bulletList)
bullet->draw(&cachePainter);
//draw the bullets
QPainter painter(this);
painter.drawPixmap(0, 0, cachePix);
QPixmap Pix1;
QPixmap Pix2;
if(flag==1)
{
if(m_playerGold<220)
Pix1=QPixmap(":/image/tower1_nopayment.png");
else
Pix1=QPixmap(":/image/tower1_payment.png");
if(m_playerGold<260)
Pix2=QPixmap(":/image/tower2_nopayment.png");
else
Pix2=QPixmap(":/image/tower2_payment.png");
QPainter mypainter(this);
int x=temp.rx();
int y=temp.ry();
mypainter.drawPixmap(x-110,y-120,Pix1);
mypainter.drawPixmap(x+50,y-120,Pix2);
}
else if(flag==2)
{
if(m_playerGold<100)
Pix1=QPixmap(":/image/noupgrade.png");
else
Pix1=QPixmap(":/image/upgrade.png");
Pix2=QPixmap(":/image/sell.png");
QPainter mypainter(this);
int x=temp.rx();
int y=temp.ry();
mypainter.drawPixmap(x-90,y-90,Pix1);
mypainter.drawPixmap(x+30,y-90,Pix2);
}
else
{
foreach (const TowerPosition tower, m_towerPositionsList)
{
if(tower.containPoint(currentPos) && !tower.hasTower())
{
QPoint Temp=tower.centerPos();
painter.save();
painter.setPen(QPen(Qt::green, 5));
painter.drawEllipse(Temp, 40, 15);
painter.restore();
break;
}
}
}
QPixmap coinbar=QPixmap(":/image/bar.png");
QPainter mybar(this);
mybar.drawPixmap(740,785,coinbar);
drawPlayerGold(&mybar);
drawHP(&mybar);
drawWave(&mybar);
}
void MainWindow::mousePressEvent(QMouseEvent *event)
{
QPoint mypoint;
QPoint pressPos = event->pos();
auto it = m_towerPositionsList.begin();//auto 自动匹配类型
if(option_flag)
{
if(pressPos.rx()>=0&&pressPos.rx()<=720&&pressPos.ry()>=0&&pressPos.ry()<=420)
{
preLoadWavesInfo(1);
this->con=false;
_level=1;
}
if(pressPos.rx()>720&&pressPos.rx()<=1440&&pressPos.ry()>=0&&pressPos.ry()<=420)
{
preLoadWavesInfo(2);
this->con=false;
_level=2;
}
if(pressPos.rx()>=0&&pressPos.rx()<=720&&pressPos.ry()>420&&pressPos.ry()<=840)
{
preLoadWavesInfo(3);
this->con=false;
_level=3;
}
if(pressPos.rx()>720&&pressPos.rx()<=1440&&pressPos.ry()>420&&pressPos.ry()<=840)
{
preLoadWavesInfo(4);
this->con=true;
_level=4;
this->m_wayPointsList.clear();
this->m_towerPositionsList.clear();
this->loadTowerPositions123();
this->addWayPoints123();
}
option_flag=0;
gameStart();
return;
}
if(flag==1)
{
it+=times;
mypoint.setX(pressPos.rx()+110-40);
mypoint.setY(pressPos.ry()+120-40);
if(it->containPoint(mypoint))
{
Tower *tower = new Tower(QString::fromLocal8Bit("weapon1"),it->centerPos(), this);
if(m_playerGold<tower->getcost())
{
update();
return;
}
m_playerGold -= tower->getcost();
m_towersList.push_back(tower);
flag=0;
m_audioPlayer->playSound(TowerPlaceSound);
it->setHasTower();
update();
return;
}
mypoint.setX(pressPos.rx()-50-40);
mypoint.setY(pressPos.ry()+120-40);
if(it->containPoint(mypoint))
{
Tower *tower = new Tower(QString::fromLocal8Bit("weapon2"),it->centerPos(), this);
if(m_playerGold<tower->getcost())
{
update();
return;
}
m_playerGold -= tower->getcost();
m_towersList.push_back(tower);
flag=0;
m_audioPlayer->playSound(TowerPlaceSound);
it->setHasTower();
update();
return;
}
flag=0;
update();
return;
}
if(flag==2)
{
it+=times;
mypoint.setX(pressPos.rx()+110-40);
mypoint.setY(pressPos.ry()+120-40);
if(it->containPoint(mypoint))
{
if(m_playerGold<100)
{
update();
return;
}
foreach(Tower * tower,m_towersList)
{
if(it->centerPos()==tower->m_pos)
{
tower->m_damage+=5;
}
}
m_playerGold -= 100;
flag=0;
update();
return;
}
mypoint.setX(pressPos.rx()-50-40);
mypoint.setY(pressPos.ry()+120-40);
if(it->containPoint(mypoint))
{
foreach(Tower * tower,m_towersList)
{
if(it->centerPos()==tower->m_pos)
{
tower->getRemoved();
it->m_hasTower=false;
}
}
m_playerGold+=100;
flag=0;
update();
return;
}
flag=0;
update();
return;
}
times=0;
while (it != m_towerPositionsList.end())
{
if (it->containPoint(pressPos) && !it->hasTower())
{
flag=1;
temp=it->centerPos();
break;
}
if (it->containPoint(pressPos) && it->hasTower())
{
flag=2;
temp=it->centerPos();
break;
}
++times;
++it;
}
}
我的小游戏(代码)
最新推荐文章于 2024-07-08 16:02:16 发布