1 Star 0 Fork 1

jxva/基于P2P模式的联机雷霆战机

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
playsence.cpp 18.51 KB
一键复制 编辑 原始数据 按行查看 历史
Cverse 提交于 2023-03-07 10:42 . 添加项目文件。
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700
#include "playsence.h"
#include "udpclient.h"
#include<thread>
#include<iostream>
using namespace std;
void PlaySence::Socket()
{
char ip[] = "47.113.197.15";
cli=new UDPClient(ip,54321);
gamestart = true;
function<void(void)> send = [&]() {
while (true) {
point s(m_hero.m_X, m_hero.m_Y);
cli->Send(s);
cli->SendData();
Sleep(40);
}
};
std::thread ths(send);
//持续收数据
while (true) {
cli->ReceData();
point r = cli->Rece();
m_fri.m_X = r.x;
m_fri.m_Y = r.y;
m_fri.setposition(r.x, r.y);
Sleep(40);
}
ths.join();
}
PlaySence::PlaySence(QWidget *parent) : QMainWindow(parent)
{
gamestart = false;
this->setFixedSize(GAME_WIDTH,GAME_HEIGHT);
this->setWindowTitle(GAME_TITLE);
//定时器设置
m_Timer.setInterval(GAME_RATE);
this->playGame();
m_recorder=0;
m_midrecorder=0;
//播放movie
movie = new QMovie(this);
movie->setFileName(WARINGMOVIE);
movielab = new QLabel(this);
movielab->setMovie(movie);
movielab->move(100,300);
movielab->resize(600,90);
movielab->hide();
//显示分数
score="0";
scorelab = new QLabel(this);
QPalette pal;
pal.setColor(QPalette::WindowText,Qt::white);
QFont font;
font.setFamily("华文新魏");
font.setPointSize(20);
scorelab->setFont(font);
scorelab->setPalette(pal);
scorelab->resize(100,50);
scorelab->move(10,100);
scorelab->setText("分数");
//友军血量
m_fri.m_boold = 3;
m_hero.m_boold = 3;
plane1lab = new QLabel(this);
plane2lab = new QLabel(this);
plane1lab->setFont(font);
plane1lab->setPalette(pal);
plane2lab->setFont(font);
plane2lab->setPalette(pal);
plane1lab->resize(160,50);
plane1lab->move(10,160);
plane2lab->resize(160,50);
plane2lab->move(10,220);
plane1lab->setText("我的血量:");
plane2lab->setText("友军血量:");
m_hero.wudi=true;
m_fri.wudi=true;
//boss血量
bosslab = new QLabel(this);
bosslab->setFont(font);
bosslab->setPalette(pal);
bosslab->resize(160,50);
bosslab->move(10,280);
bosslab->setText("boss血量:");
bosslab->hide();
bossblood="100";
//初始化血量
bossplane.m_boold=100;
//boss战机初始化
bossplane.m_Free=true;
//初始化关卡时间信息
Mid=false;
End=false;
Start=false;
//
QTimer::singleShot(5000,this,[=](){
Start=true;
});
QTimer::singleShot(10000,this,[=](){
Mid=true;
});
QTimer::singleShot(15000,this,[=](){
Start=false;
Mid=false;
movielab->show();
movie->start();
QSound::play(WARNINGMUSIC);
});
QTimer::singleShot(18000,this,[=](){
Mid=true;
End=true;
movie->stop();
movielab->hide();
});
}
void PlaySence::playGame()
{
m_Timer.start();
QSound::play(BACKGROUND);
connect(&m_Timer,&QTimer::timeout,[=](){
if((m_hero.m_boold>0||m_fri.m_boold>0)&& gamestart)
{
if(Start)
{
//敌军出场
enemyToScene();
}
//地图滚动
updatePosition();
//重新更新图片
update();
//碰撞检测
collisionDetection();
//中等低级出场
if(Mid)
{
midenemytosence();
}
if(End)
{
bossplane.m_Free=false;
bosslab->show();
}
}
else
{
}
});
}
void PlaySence::updatePosition()
{
m_map.mapPosition();
//发射子弹
if(m_hero.m_boold>0)
m_hero.shoot();
if(m_fri.m_boold>0)
m_fri.shoot();
for(int i=0;i<BULLET_NUM;i++)
{
//若子弹状态为非空闲则更新子弹位置
if(!m_hero.m_bullet[i].m_Free)
{
m_hero.m_bullet[i].updatePosition();
}
}
//友军子弹
for(int i=0;i<BULLET_NUM;i++)
{
if(!m_fri.m_bullet[i].m_Free)
{
m_fri.m_bullet[i].updatePosition();
}
}
//敌军坐标计算
for(int i=0;i<ENEMY_NUM;i++)
{
if(!m_enemys[i].m_Free)
{
m_enemys[i].updatePosition();
}
}
//boss
if(End&&bossplane.m_boold>0)
{
bossplane.movenewposition();
bossplane.shoot();
for(int i=0;i<BULLET_NUM;i++)
{
if(!bossplane.m_bullet[i].m_Free)
{
bossplane.m_bullet[i].updatePositionBoss();
}
}
}
//中等战机坐标计算
for(int i=0;i<ENEMY_NUM;i++)
{
if(!m_midenemys[i].m_Free)
{
m_midenemys[i].updatePosition();
}
}
//计算爆炸播放的图片
for(int i = 0 ; i < BOMB_NUM;i++)
{
if(m_bombs[i].m_Free == false)
{
m_bombs[i].updateInfo();
}
}
}
void PlaySence::paintEvent(QPaintEvent *event)
{
QPainter paint(this);
//地图
paint.drawPixmap(0,m_map.m_map1_posY,m_map.m_map1);
paint.drawPixmap(0,m_map.m_map2_posY,m_map.m_map2);
//我方战机
if(m_hero.m_boold>0)
{
paint.drawPixmap(m_hero.m_X,m_hero.m_Y,m_hero.m_Plane);
//子弹
for(int i=0;i<BULLET_NUM;i++)
{
if(!m_hero.m_bullet[i].m_Free)
{
paint.drawPixmap(m_hero.m_bullet[i].m_X,m_hero.m_bullet[i].m_Y,m_hero.m_bullet[i].m_Bullet);
}
}
}
if(m_fri.m_boold>0)
{
paint.drawPixmap(m_fri.m_X,m_fri.m_Y,m_fri.m_friplane);
//友军子弹
for(int i=0;i<BULLET_NUM;i++)
{
if(!m_fri.m_bullet[i].m_Free)
{
paint.drawPixmap(m_fri.m_bullet[i].m_X,m_fri.m_bullet[i].m_Y,m_fri.m_bullet[i].m_Bullet);
}
}
}
//敌机
for(int i=0;i<ENEMY_NUM;i++)
{
if(!m_enemys[i].m_Free)
{
paint.drawPixmap(m_enemys[i].m_X,m_enemys[i].m_Y,m_enemys[i].m_enemy);
}
}
//中等敌机
for(int i=0;i<ENEMY_NUM;i++)
{
if(!m_midenemys[i].m_Free)
{
paint.drawPixmap(m_midenemys[i].m_X,m_midenemys[i].m_Y,m_midenemys[i].m_midenemy);
}
}
//绘制爆炸图片
for(int i = 0 ; i < BOMB_NUM;i++)
{
if(m_bombs[i].m_Free == false)
{
paint.drawPixmap(m_bombs[i].m_X,m_bombs[i].m_Y,m_bombs[i].m_pixArr[m_bombs[i].m_index]);
}
}
//显示分数
paint.setPen(Qt::white);
paint.setFont(QFont("华文新魏",15));
paint.drawText(120,136,score);
//显示血量
QPixmap pix;
pix.load(PLANE_BLOOD);
if(m_hero.m_boold==3)
{
paint.drawPixmap(180,191,pix);
paint.drawPixmap(220,191,pix);
paint.drawPixmap(260,191,pix);
}
else if(m_hero.m_boold==2)
{
paint.drawPixmap(180,191,pix);
paint.drawPixmap(220,191,pix);
}
else if(m_hero.m_boold==1)
{
paint.drawPixmap(180,191,pix);
}
else
{
paint.drawText(180,191,"阵亡");
}
if(m_fri.m_boold==3)
{
paint.drawPixmap(180,261,pix);
paint.drawPixmap(220,261,pix);
paint.drawPixmap(260,261,pix);
}
else if(m_fri.m_boold==2)
{
paint.drawPixmap(180,261,pix);
paint.drawPixmap(220,261,pix);
}
else if(m_fri.m_boold==1)
{
paint.drawPixmap(180,261,pix);
}
else
{
paint.drawText(180,261,"阵亡");
}
//boss子弹
if(bossplane.m_boold>0&&End)
{
paint.drawPixmap(bossplane.m_X,bossplane.m_Y,bossplane.m_BossPlane);
for(int i=0;i<BULLET_NUM;i++)
{
if(!bossplane.m_bullet[i].m_Free)
{
paint.drawPixmap(bossplane.m_bullet[i].m_X,bossplane.m_bullet[i].m_Y,bossplane.m_bullet[i].m_Bulletboss);
}
}
paint.setPen(Qt::white);
paint.setFont(QFont("华文新魏",15));
paint.drawText(180,320,bossblood);
}
}
void PlaySence::keyPressEvent(QKeyEvent *event)
{
m_hero.m_X=m_hero.m_X-m_hero.m_X%step;
m_hero.m_Y=m_hero.m_Y-m_hero.m_Y%step;
if(event->key()==Qt::Key_Left)
{
m_hero.m_X=(m_hero.m_X-step)<0?0:m_hero.m_X-step;
m_hero.setPosition(m_hero.m_X,m_hero.m_Y);
}
if(event->key()==Qt::Key_Right)
{
m_hero.m_X=(m_hero.m_X+step)>GAME_WIDTH-m_hero.m_Plane.width()?GAME_WIDTH-m_hero.m_Plane.width():m_hero.m_X+step;
m_hero.setPosition(m_hero.m_X,m_hero.m_Y);
}
if(event->key()==Qt::Key_Up)
{
m_hero.m_Y=(m_hero.m_Y-step)<0?0:m_hero.m_Y-step;
m_hero.setPosition(m_hero.m_X,m_hero.m_Y);
}
if(event->key()==Qt::Key_Down)
{
m_hero.m_Y=(m_hero.m_Y+step)>GAME_HEIGHT-m_hero.m_Plane.height()?GAME_HEIGHT-m_hero.m_Plane.height():m_hero.m_Y+step;
m_hero.setPosition(m_hero.m_X,m_hero.m_Y);
}
}
void PlaySence::enemyToScene()
{
m_recorder++;
if(m_recorder<ENEMY_INTERVAL)
{
return;
}
m_recorder=0;
for(int i=0;i<ENEMY_NUM;i++)
{
if(m_enemys[i].m_Free)
{
m_enemys[i].m_Free=false;
m_enemys[i].m_X = rand() % (GAME_WIDTH - m_enemys[i].m_Rect.width());
m_enemys[i].m_Y = -m_enemys[i].m_Rect.height();
break;
}
}
}
void PlaySence::midenemytosence()
{
m_midrecorder++;
if(m_midrecorder<ENEMY_INTERVAL)
{
return;
}
m_midrecorder=0;
for(int i=0;i<ENEMY_NUM;i++)
{
if(m_midenemys[i].m_Free)
{
m_midenemys[i].m_Free=false;
m_midenemys[i].m_X = rand() % (GAME_WIDTH - m_midenemys[i].m_Rect.width());
m_midenemys[i].m_Y = -m_midenemys[i].m_Rect.height();
break;
}
}
}
void PlaySence::collisionDetection()
{
int temp;
//普通战机碰撞判断
for(int i=0;i<ENEMY_NUM;i++)
{
if(m_enemys[i].m_Free)
{
continue;
}
for(int j=0;j<BULLET_NUM;j++)
{
if(m_hero.m_bullet[i].m_Free)
{
continue;
}
if(m_enemys[i].m_Rect.intersects(m_hero.m_bullet[j].m_Rect))
{
m_enemys[i].m_Free = true;
m_hero.m_bullet[j].m_Free = true;
temp = score.toInt();
temp++;
score=QString::number(temp);
//爆炸音效
QSound::play(BAOZHA);
//播放爆炸效果
for(int k = 0 ; k < BOMB_NUM;k++)
{
if(m_bombs[k].m_Free)
{
//爆炸状态设置为非空闲
m_bombs[k].m_Free = false;
//更新坐标
m_bombs[k].m_X = m_enemys[i].m_X;
m_bombs[k].m_Y = m_enemys[i].m_Y;
break;
}
}
}
}
for(int k=0;k<BULLET_NUM;k++)
{
if(m_fri.m_bullet[i].m_Free)
{
continue;
}
if(m_enemys[i].m_Rect.intersects(m_fri.m_bullet[k].m_Rect))
{
m_enemys[i].m_Free = true;
m_fri.m_bullet[k].m_Free = true;
temp = score.toInt();
temp++;
score=QString::number(temp);
//播放爆炸效果
// QSound::play(BAOZHA);
for(int l = 0 ; l < BOMB_NUM;l++)
{
if(m_bombs[l].m_Free)
{
//爆炸状态设置为非空闲
m_bombs[l].m_Free = false;
//更新坐标
m_bombs[l].m_X = m_enemys[i].m_X;
m_bombs[l].m_Y = m_enemys[i].m_Y;
break;
}
}
}
}
if(m_enemys[i].m_Rect.intersects(m_hero.m_Rect)&&!m_enemys[i].m_Free&&m_hero.wudi)
{
m_enemys[i].m_Free=true;
m_hero.m_boold--;
m_hero.wudi=false;
QTimer::singleShot(2000,this,[=](){
m_hero.wudi=true;
});
}
if(m_enemys[i].m_Rect.intersects(m_fri.m_Rect)&&!m_enemys[i].m_Free&&m_fri.wudi)
{
m_enemys[i].m_Free=true;
m_fri.m_boold--;
m_fri.wudi=false;
QTimer::singleShot(2000,this,[=](){
m_fri.wudi=true;
});
}
}
//中等战机判断
for(int i=0;i<ENEMY_NUM;i++)
{
if(m_midenemys[i].m_Free)
{
continue;
}
for(int j=0;j<BULLET_NUM;j++)
{
if(m_hero.m_bullet[i].m_Free)
{
continue;
}
if(m_midenemys[i].m_Rect.intersects(m_hero.m_bullet[j].m_Rect))
{
m_midenemys[i].Blood--;
if(m_midenemys[i].Blood<1)
{
QSound::play(BAOZHA);
m_midenemys[i].m_Free=true;
m_midenemys[i].Blood=10;
temp = score.toInt();
temp+=5;
score=QString::number(temp);
}
m_hero.m_bullet[j].m_Free = true;
//播放爆炸效果
for(int k = 0 ; k < BOMB_NUM;k++)
{
if(m_bombs[k].m_Free)
{
//爆炸状态设置为非空闲
m_bombs[k].m_Free = false;
//更新坐标
m_bombs[k].m_X = m_midenemys[i].m_X;
m_bombs[k].m_Y = m_midenemys[i].m_Y;
break;
}
}
}
}
for(int k=0;k<BULLET_NUM;k++)
{
if(m_fri.m_bullet[i].m_Free)
{
continue;
}
if(m_midenemys[i].m_Rect.intersects(m_fri.m_bullet[k].m_Rect))
{
m_midenemys[i].Blood--;
if(m_midenemys[i].Blood<1)
{
QSound::play(BAOZHA);
m_midenemys[i].m_Free=true;
m_midenemys[i].Blood=10;
temp = score.toInt();
temp+=5;
score=QString::number(temp);
}
m_fri.m_bullet[k].m_Free = true;
//播放爆炸效果
for(int l = 0 ; l < BOMB_NUM;l++)
{
if(m_bombs[l].m_Free)
{
//爆炸状态设置为非空闲
m_bombs[l].m_Free = false;
//更新坐标
m_bombs[l].m_X = m_midenemys[i].m_X;
m_bombs[l].m_Y = m_midenemys[i].m_Y;
break;
}
}
}
}
if(m_midenemys[i].m_Rect.intersects(m_hero.m_Rect)&&!m_midenemys[i].m_Free&&m_hero.wudi)
{
m_midenemys[i].m_Free=true;
m_hero.m_boold--;
m_hero.wudi=false;
temp = plane1.toInt();
temp--;
plane1 = QString::number(temp);
QTimer::singleShot(2000,this,[=](){
m_hero.wudi=true;
});
}
if(m_midenemys[i].m_Rect.intersects(m_fri.m_Rect)&&!m_midenemys[i].m_Free&&m_fri.wudi)
{
m_midenemys[i].m_Free=true;
m_fri.m_boold--;
m_fri.wudi=false;
temp = plane2.toInt();
temp--;
plane2 = QString::number(temp);
QTimer::singleShot(2000,this,[=](){
m_fri.wudi=true;
});
}
}
//boss判断
for(int i=0;i<BULLET_NUM;i++)
{
if(m_hero.m_bullet[i].m_Free)
{
continue;
}
if(bossplane.m_Rect.intersects(m_hero.m_bullet[i].m_Rect)&&End)
{
if(bossplane.m_boold<1)
{
bossplane.m_boold=0;
bossblood=QString::number(bossplane.m_boold);
}
else
{
bossplane.m_boold--;
bossblood=QString::number(bossplane.m_boold);
}
m_hero.m_bullet[i].m_Free = true;
//播放爆炸效果
for(int k = 0 ; k < BOMB_NUM;k++)
{
if(m_bombs[k].m_Free)
{
//爆炸状态设置为非空闲
m_bombs[k].m_Free = false;
//更新坐标
m_bombs[k].m_X = m_midenemys[i].m_X;
m_bombs[k].m_Y = m_midenemys[i].m_Y;
break;
}
}
}
}
for(int i=0;i<BULLET_NUM;i++)
{
if(m_fri.m_bullet[i].m_Free)
{
continue;
}
if(bossplane.m_Rect.intersects(m_fri.m_bullet[i].m_Rect)&&End)
{
if(bossplane.m_boold<1)
{
bossplane.m_boold=0;
bossblood=QString::number(bossplane.m_boold);
}
else
{
bossplane.m_boold--;
bossblood=QString::number(bossplane.m_boold);
}
m_fri.m_bullet[i].m_Free = true;
//播放爆炸效果
for(int l = 0 ; l < BOMB_NUM;l++)
{
if(m_bombs[l].m_Free)
{
//爆炸状态设置为非空闲
m_bombs[l].m_Free = false;
//更新坐标
m_bombs[l].m_X = m_midenemys[i].m_X;
m_bombs[l].m_Y = m_midenemys[i].m_Y;
break;
}
}
}
}
for(int i=0;i<BULLET_NUM;i++)
{
if(bossplane.m_bullet[i].m_Free)
{
return;
}
if(bossplane.m_bullet[i].m_Rect.intersects(m_hero.m_Rect)&&!bossplane.m_bullet[i].m_Free&&m_hero.wudi)
{
bossplane.m_bullet[i].m_Free=true;
m_hero.m_boold--;
m_hero.wudi=false;
QTimer::singleShot(2000,this,[=](){
m_hero.wudi=true;
});
}
if(bossplane.m_bullet[i].m_Rect.intersects(m_fri.m_Rect)&&!bossplane.m_bullet[i].m_Free&&m_fri.wudi)
{
bossplane.m_bullet[i].m_Free=true;
m_fri.m_boold--;
m_fri.wudi=false;
QTimer::singleShot(2000,this,[=](){
m_fri.wudi=true;
});
}
}
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
C
1
https://gitee.com/jxva/VirusCombat.git
git@gitee.com:jxva/VirusCombat.git
jxva
VirusCombat
基于P2P模式的联机雷霆战机
master

搜索帮助