问题描述
几个月前,我开始使用C ++研究QT,以及如何开发用于大学目的的游戏。 我有一款太空射击游戏,玩家是Qgraphics物品,可以不断播放动画(移动,闲置,更换武器...)以及敌人。
移动系统没问题,玩家可以在地图上自由移动,控件也很流畅,可以做出应有的响应。
拍摄系统还可以!玩家可以创建其他直线移动并消失(从场景中删除)的对象,如果发生碰撞,它们也会碰撞,或者到达场景边界时消失,敌人也可以射击。
按下键时,玩家立即响应任何命令,问题出在敌人身上。
玩家具有周期性的功能,可以在地图的上边界中创建敌人,并且它们会直接向下朝地图的下边界,如果没有被击落,它们会像子弹一样消失。
但是过了一段时间,他们放慢了很多速度,当我关闭允许他们射击的功能时,如果他们这样做会变得更糟,游戏开始变得非常落后,那是不应该发生的,因为对我来说是“只是一堆2D图片在屏幕上移动”。我认为这是有问题的代码,有人可以帮助我吗?这真令人讨厌,在2到3个月里,我无法修复它,或者不知道为什么会这样。
Enemy.h
#define ENEMY_H
#include <QObject>
#include <QGraphicspixmapItem>
#include <QGraphicsItem>
#include <QTimer>
#include <Qpixmap>
#include <QMediaPlayer>
#include <QPainter>
//works similiar to player controlled entity,but this one is controlled by algorithms
class Enemy: public QObject,public QGraphicsItem
{
Q_OBJECT
public:
explicit Enemy(QObject *parent=0);
void move();
void shoot();
private slots:
void nextFrame();
void enemiesInrange();//peiodically checks if there is any enemies in it's front,if there is it will shoot periodically
private:
void paint(QPainter *painter,const qstyleOptionGraphicsItem *option,QWidget *widget);
QRectF boundingRect() const;
QTimer *timer;//for load the next frame of the animation
QTimer *timer2;//for move
QTimer *timer3;//for look for enemies
QTimer *timer4;//for shoot in enemies
Qpixmap *spriteImage;
int currentFrame;
int currentSequence;
QMediaPlayer *bullet_sound;
};
#endif // ENEMY_H
enemy.c
#include "enemy.h"
#include "player.h"
#include <QBitmap>
#include "bullet.h"
#include "game.h"
#include "stdlib.h"
extern Game *game;
using namespace std;
Enemy::Enemy(QObject *parent):
QObject(parent),QGraphicsItem()
{
spriteImage = new Qpixmap(":/images/img/hizack spritesheet.png");
int random_number = rand()% 1200;
setPos(random_number,0);
currentFrame=0;
currentSequence=210;
timer = new QTimer();
connect(timer,&QTimer::timeout,this,&Enemy::nextFrame);
timer->start(100);
timer2 = new QTimer();
connect(timer2,&Enemy::move);
timer2->start(1);
timer3 = new QTimer();
connect(timer3,&Enemy::enemiesInrange);
timer3->start(1000);
}
void Enemy::move()
{
currentSequence = 210;
setPos(x(),y()+0.25);
if(pos().y() + spriteImage->height() < 0 || pos().y() >= 800)
{
game->scene->removeItem(this);
delete this;
return;
}
if(pos().x() >= 600 || pos().x() == 0)
{
game->scene->removeItem(this);
delete this;
return;
}
}
void Enemy::shoot()
{
if(currentSequence == 210)
{
Bullet * bullet = new Bullet(0,currentSequence,20,2);
bullet->setPos(this->pos().x()-20,this->pos().y()+50);
scene()->addItem(bullet);
}
else if(currentSequence == 70)
{
Bullet * bullet = new Bullet(0,30,2);
bullet->setPos(this->pos().x()-20,this->pos().y()-80);
scene()->addItem(bullet);
}
else if(currentSequence == 140)
{
Bullet * bullet = new Bullet(0,2);
bullet->setPos(this->pos().x()+20,this->pos().y()-50 );
scene()->addItem(bullet);
}
else if(currentSequence == 0)
{
Bullet * bullet = new Bullet(0,2);
bullet->setPos(this->pos().x()-50,this->pos().y()-50 );
scene()->addItem(bullet);
}
}
void Enemy::enemiesInrange()//fires player if him is at range
{
qInfo() << "Items in scene: " << scene()->items().size();
if(game->player1->pos().y() <= this->pos().y()+300 )
{
if(game->player1->pos().x() <= this->pos().x()+100 && game->player1->pos().x()>= this->pos().x()-100)
shoot();
}
}
void Enemy::paint(QPainter *painter,QWidget *widget)
{
painter->drawpixmap(-35,-35,*spriteImage,currentFrame,70,70);
Q_UNUSED(option);
Q_UNUSED(widget);
}
QRectF Enemy::boundingRect() const
{
return QRectF(-35,70);
}
void Enemy::nextFrame()
{
currentFrame += 70;
if (currentFrame >= 210)
currentFrame = 0;
this->update(-35,70);
}
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)