1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
|
#include <QApplication>
#include <QGraphicsScene>
#include <QGraphicsRectItem>
#include <QGraphicsView>
#include "Player.h"
#include <QTimer>
#include "Enemies.h"
#include "Game.h"
#include "Health.h"
#include "EnemyHealth.h"
extern int enemyNum;
Game::Game(QWidget *parent){
//create scene
scene = new QGraphicsScene(); //creates scene
scene->setSceneRect(0,0,800,600); //sets size of scene so it is not infinite (scrollbar)
setScene(scene); //scene pointer created in header file
setFixedSize(800,600); //sets size of view, not scene
setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
player = new Player(); //player pointer created in header file
//set size and location of player
player->setRect(0,0,100,100);
//make rectangle focusable
player->setFlag(QGraphicsItem::ItemIsFocusable);
player->setFocus(); //sets the rectangle to be focused on
//add rectangle item to scene
scene->addItem(player);
player->setPos(((scene->width()/2) - player->rect().width() / 2), scene->height() - (10 + player->rect().height()));
//sets starting location to the middle of the screen (x), plus half the width of the square to make it centered, and the bottom of the screen, plus a bit to make it entirely visible
//create score
score = new Score();
scene->addItem(score);
health = new Health();
health->setPos(health->x(), health->y()+50);
scene->addItem(health);
enemyHealth = new EnemyHealth(); //this is offending line
enemyHealth->setPos(enemyHealth->x()+600, enemyHealth->y()+50);
scene->addItem(enemyHealth);
//spawn enemies
//QTimer *timer = new QTimer();
//QObject::connect(timer,SIGNAL(timeout()),player,SLOT(spawn())); //every 2s (2000ms), send timeout signal and run spawn function in player class
//timer->start(2000);
if(enemyNum < 1){
Player bo;
bo.spawn();
enemyNum++;
}
show();
}
|