Answer the question
In order to leave comments, you need to log in
Qt game error (C++) Crash, how to solve?
Created a game, got an error. Error code :
The program has unexpectedly finished.
The process ended forcefully.
build-SpaceRangers-Desktop_Qt_5_15_1_MSVC2015_64bit-Debug\debug\SpaceRangers.exe crashed.
Game code :
View.h
#pragma once
#include <QGraphicsView>
#include <QGraphicsScene>
#include <QObject>
#include "player.h"
#include "virus.h"
#include "laserbullet.h"
class View :public QGraphicsView
{
public:
View();
void view_elements();
QList<QGraphicsItem*> Viruses;
void viruspodMoving(QGraphicsItem *virus);
void deleteVirus(QGraphicsItem *virus);
public slots:
void addVirus();
void virusMov();
void PlayerAttack();
private:
QGraphicsScene mainScene;
Player pl;
Virus vir;
LaserBullet lb;
View *v;
QTimer *timer; // Таймер
QTimer *createVirus;
QTimer *vm;
Virus *vc;
};
#include "view.h"
#include "player.h"
#include <QObject>
#include <QTimer>
#include <QtGlobal>
View::View()
{
setWindowTitle("Space Rangers");
setRenderHint(QPainter::Antialiasing);
setFrameStyle(0);
setSceneRect(0, 0, 600,700);
setFixedSize(600, 700);
mainScene.setItemIndexMethod(QGraphicsScene::NoIndex);
setScene(&mainScene);
connect(&pl, &Player::IS_IT_Attack, this, &View::PlayerAttack);
timer = new QTimer(this);
connect(timer, &QTimer::timeout, &pl, &Player::actions);
timer->start(1000 / 50);
view_elements();
vm = new QTimer(this);
connect(vm, &QTimer::timeout, this, &View::virusMov);
createVirus = new QTimer(this);
connect(createVirus, &QTimer::timeout, this, &View::addVirus);
createVirus->start(30000);
}
void View::view_elements(){
pl.setPos(205, 400);
mainScene.addItem(&pl);
}
void View::addVirus(){
Virus *virus = new Virus();
vc = virus;
mainScene.addItem(virus);
virus->x = ((int)rand()/(420));
virus->setPos(vir.x, 0);
Viruses.append(virus);
viruspodMoving(virus);
}
void View::deleteVirus(QGraphicsItem *virus){
foreach(QGraphicsItem *v, Viruses){
if(v == virus){
mainScene.removeItem(v);
Viruses.removeOne(v);
delete vc;
vir.y = 0;
}
}
createVirus->start(30000);
}
void View::viruspodMoving(QGraphicsItem *virus){
if(vir.y < 671){
emit vm->start(100);
}
if(vir.y >= 670){
deleteVirus(virus);
}
}
void View::virusMov(){
if(vir.y < 671){
vir.y = vir.y + 4;
vc->setPos(vir.x, vir.y);
viruspodMoving(vc);
}
}
void View::PlayerAttack(){
LaserBullet *bullet = new LaserBullet();
LaserBullet *bullet2 = new LaserBullet();
lb.x = pl.x + 41;
lb.y = pl.y - 32;
lb.x2 = pl.x + 25;
lb.y2 = pl.y - 32;
bullet->setPos(lb.x, lb.y);
mainScene.addItem(bullet);
bullet2->setPos(lb.x2, lb.y2);
mainScene.addItem(bullet2);
}
#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;
int x = 0;
int y = 0;
signals:
void checkVirus(QGraphicsItem *item);
private:
void paint(QPainter *painter, const QStyleOptionGraphicsItem *option,QWidget *widget) override;
QRectF boundingRect() const override;
QPixmap *VirusImage;
};
+
#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);
}
Answer the question
In order to leave comments, you need to log in
Run in debugger. Somewhere you access address zero, or go beyond the array boundary, or divide by 0. So by the sheet of code you will not understand what the problem is. Most likely they forgot to create an object somewhere.
The debugger will show the specific position where the error occurred. There already, seeing which variable is incorrect, you need to look at the logic around it.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question