Всем привет! После компиляции программы выходит ошибка : Starting C:\Qt\build-SpaceRangers-Desktop_Qt_5_15_1_MSVC2015_64bit-Debug\debug\SpaceRangers.exe ...
The program has unexpectedly finished.
The process was ended forcefully. C:\Qt\build-SpaceRangers-Desktop_Qt_5_15_1_MSVC2015_64bit-Debug\debug\SpaceRangers.exe crashed. В коде ошибок нет вроде, но вот исходник:
Player.h :
#pragma once
#include <QObject>
#include <QGraphicsItem>
#include <QTimer>
#include <QPixmap>
#include <QPainter>
#include "virus.h"
class Player: public QObject, public QGraphicsItem
{
Q_OBJECT
public:
Player(QObject *parent = 0);
int pWidth = 80;
int pHeigth = 80;
int x = 205;
int y = 400;
public slots:
void moving();
private:
void paint(QPainter *painter, const QStyleOptionGraphicsItem *option,QWidget *widget) override;
QRectF boundingRect() const override;
QPixmap *PlayerImage; // В данный объект pixmap будет помещен спрайт
Virus *vir;
QTimer t;
};
View.h :
#pragma once
#include <QGraphicsView>
#include <QGraphicsScene>
#include <QObject>
#include "player.h"
#include "virus.h"
class View :public QGraphicsView
{
public:
View();
void view_elements();
public slots:
void addVirus();
void deleteVirus(QGraphicsItem *item);
private:
QGraphicsScene mainScene;
Player pl;
Virus *vir;
View *v;
QTimer *timer; // Таймер
QTimer *createVirus;
};
Virus.h :
#pragma once
#include <QtWidgets>
#include <QGraphicsItem>
class Virus: public QObject, public QGraphicsItem
{
Q_OBJECT
public:
Virus(QObject *parent = 0);
~Virus();
int vWidth = 70;
int vHeigth = 55;
QList<QGraphicsItem*> Viruses;
int x = 205;
int y = 0;
public slots:
void moving();
signals:
void checkVirus(QGraphicsItem *item);
private:
void paint(QPainter *painter, const QStyleOptionGraphicsItem *option,QWidget *widget) override;
QRectF boundingRect() const override;
QPixmap *VirusImage;
};
main.cpp :
#include "view.h"
#include <QtWidgets>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
View v;
v.show();
return a.exec();
}
Player.cpp :
#include "player.h"
#include "virus.h"
#include <QtWidgets>
#include <QGraphicsItem>
#include <QGraphicsScene>
using namespace Qt;
Player::Player(QObject *parent):QObject(parent), QGraphicsItem()
{
PlayerImage = new QPixmap();
PlayerImage->load(":/Player.png");
}
QRectF Player::boundingRect() const{
return QRectF(0, 0, pWidth, pHeigth);
}
void Player::paint(QPainter *painter, const QStyleOptionGraphicsItem *option,QWidget *widget){
Virus vir;
QList<QGraphicsItem*> bad = vir.Viruses;
foreach(QGraphicsItem *v, bad){
if(collidesWithItem(v)){
PlayerImage->load(":/VPlayert.png");
painter->drawPixmap(0, 0, pWidth, pHeigth, *PlayerImage);
update();
}
}
painter->drawPixmap(0, 0, pWidth, pHeigth, *PlayerImage);
Q_UNUSED(option);
Q_UNUSED(widget);
}
void Player::moving()
{
//*******************Обработка нажатий клавиш на клавиатуре*********************************************
if(GetAsyncKeyState(VK_LEFT)){
x = x-4 ;
setPos(x, y);
}
if(GetAsyncKeyState(VK_RIGHT)){
x = x+4;
setPos(x, y);
}
if(GetAsyncKeyState(VK_UP)){
y = y-5;
setPos(x, y);
}
if(GetAsyncKeyState(VK_DOWN)){
y = y+4;
setPos(x, y);
}
if(this->x - 4 < 0){
x = x+4;
this->setX(4);
}
if(this->x + 4 > 420){
x = x-4;
this->setX(416);
}
if(this->y - 5 < 0){
y = y+5;
this->setY(5);
}
if(this->y + 4 > 520){
y = y-4;
this->setY(516);
}
}
View.cpp :
#include "view.h"
#include "player.h"
#include <QObject>
#include <QTimer>
View::View()
{
setWindowTitle("Space Rangers");
setRenderHint(QPainter::Antialiasing);
setFrameStyle(0);
setSceneRect(0, 0, 500,600);
setFixedSize(500, 600);
mainScene.setItemIndexMethod(QGraphicsScene::NoIndex);
setScene(&mainScene);
timer = new QTimer();
connect(timer, &QTimer::timeout, &pl, &Player::moving);
connect(timer, &QTimer::timeout, vir, &Virus::moving);
timer->start(1000 / 50);
view_elements();
connect(createVirus, &QTimer::timeout, this, &View::addVirus);
createVirus->start(40000);
connect(vir, &Virus::checkVirus, this, &View::deleteVirus);
}
void View::view_elements(){
pl.setPos(205, 400);
mainScene.addItem(&pl);
}
void View::addVirus(){
Virus *virus = new Virus();
mainScene.addItem(virus);
virus->x = (qrand() % (251)) * ((qrand()%2 == 1)?1:-1);
virus->setPos(virus->x,0);
virus->setZValue(-1);
virus->Viruses.append(virus);
}
void View::deleteVirus(QGraphicsItem *item){
foreach(QGraphicsItem *virus, vir->Viruses){
if(virus == item){
mainScene.removeItem(virus);
vir->Viruses.removeOne(item);
delete virus;
}
}
}
Virus.cpp
#include "virus.h"
#include "view.h"
Virus::Virus(QObject *parent):QObject(parent), QGraphicsItem()
{
VirusImage = new QPixmap();
VirusImage->load(":/Virus.png");
}
Virus::~Virus(){
}
QRectF Virus::boundingRect() const{
return QRectF(0, 0, vWidth, vHeigth);
}
void Virus::paint(QPainter *painter, const QStyleOptionGraphicsItem *option,QWidget *widget){
painter->drawPixmap(0, 0, vWidth, vHeigth, *VirusImage);
Q_UNUSED(option);
Q_UNUSED(widget);
}
void Virus::moving(){
for(int i = 0; i < 2; i++){
y = y+2;
setPos(x, y);
}
if(y == 525){
QList<QGraphicsItem*> bad = Viruses;
foreach(QGraphicsItem *v, bad){
emit checkVirus(v);
}
}
}
Даже не могу предположить где заключается ошибка.
Буду благодарен за ответ.