I meant this is a figurative sense, not as C++ code. "spaceship[0]" is the first spaceship you create in your loop. "spaceship[9]" is the last one. I agree with ado130 - you really need to learn a little more about C++. QVector is the Qt version of std:: vector, and if you don't know what that is either, then you need to learn some more. It's hard to help you with example code if you don't understand the code we are providing.
Remove the QTimer stuff from the Spaceship class. Here is your game.[h, cpp] with some added code to help you get started:
// game.h
#ifndef GAME_H
#define GAME_H
#include <QGraphicsView>
#include <QGraphicsScene>
#include <QVector>
class Spaceship;
{
Q_OBJECT; // <<< Absolutely need this
public:
// constructor
// public method
void start();
private slots:
void onSpaceshipTimeout();
// Attributes should ALWAYS be private or protected, never public
// If you need access to them from outside the Game class, then provide
// Game class methods to get or set their values.
private:
//Map* map; TODO
QVector < Spaceship
* > spaceships;
int direction;
};
#endif // GAME_H
// game.h
#ifndef GAME_H
#define GAME_H
#include <QGraphicsView>
#include <QGraphicsScene>
#include <QVector>
class Spaceship;
class QTimer;
class Game: public QGraphicsView
{
Q_OBJECT; // <<< Absolutely need this
public:
// constructor
Game(QWidget* parent=NULL);
// public method
void start();
private slots:
void onSpaceshipTimeout();
// Attributes should ALWAYS be private or protected, never public
// If you need access to them from outside the Game class, then provide
// Game class methods to get or set their values.
private:
QGraphicsScene * scene;
//Map* map; TODO
QString Score;
QString Life;
QTimer * pSpaceshipTimer;
QVector < Spaceship * > spaceships;
int direction;
};
#endif // GAME_H
To copy to clipboard, switch view to plain text mode
#include "game.h"
#include "player.h"
#include <QTimer>
#include "spaceship.h"
, direction( 1 )
{
// set screen
setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
setFixedSize(1024,800);
// set scene
scene->setSceneRect(0,0,1000,800);
setScene(scene);
// set player
Player * player = new Player();
// add player to scene
scene->addItem(player);
// add enemy to scene
for(int lol = 0; lol < 10; lol++)
{
Spaceship * spaceship = new Spaceship(x, 10);
scene->addItem(spaceship);
x = x + 100;
// add a copy of the spaceship POINTER to the vector for use later
spaceships.push_back( spaceship );
}
pSpaceshipTimer
= new QTimer( this );
connect( pSpaceshipTimer, SIGNAL( timeout() ), this, SLOT( onSpaceshipTimeout() ) );
}
void Game::start()
{
pSpaceshipTimer->start( 500 ); // 50 is too short.
}
void Game::onSpaceshipTimeout()
{
// Note that "direction" has the values 1 or -1 to indicate movement to the right or left, respectively
int nShips = spaceships.size();
if ( direction > 0 )
{
if ( spaceships[ nShips - 1 ]->x() >= 950 )
direction = -1;
}
else
{
if ( spaceships[ 0 ]->x() <= 50 )
direction = 1;
}
int distance = 10 * direction;
for ( nShip = 0; nShip < nShips; ++nShip )
{
Spaceship * spaceship = spaceships[ nShip ];
spaceship->setX( spaceship->x() + distance );
}
}
#include "game.h"
#include "player.h"
#include <QTimer>
#include "spaceship.h"
Game::Game(QWidget *parent)
: QGraphicsView( parent ) // <<< NEED THIS
, direction( 1 )
{
// set screen
setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
setFixedSize(1024,800);
// set scene
scene = new QGraphicsScene();
scene->setSceneRect(0,0,1000,800);
setScene(scene);
// set player
Player * player = new Player();
// add player to scene
scene->addItem(player);
// add enemy to scene
for(int lol = 0; lol < 10; lol++)
{
Spaceship * spaceship = new Spaceship(x, 10);
scene->addItem(spaceship);
x = x + 100;
// add a copy of the spaceship POINTER to the vector for use later
spaceships.push_back( spaceship );
}
pSpaceshipTimer = new QTimer( this );
connect( pSpaceshipTimer, SIGNAL( timeout() ), this, SLOT( onSpaceshipTimeout() ) );
}
void Game::start()
{
pSpaceshipTimer->start( 500 ); // 50 is too short.
}
void Game::onSpaceshipTimeout()
{
// Note that "direction" has the values 1 or -1 to indicate movement to the right or left, respectively
int nShips = spaceships.size();
if ( direction > 0 )
{
if ( spaceships[ nShips - 1 ]->x() >= 950 )
direction = -1;
}
else
{
if ( spaceships[ 0 ]->x() <= 50 )
direction = 1;
}
int distance = 10 * direction;
for ( nShip = 0; nShip < nShips; ++nShip )
{
Spaceship * spaceship = spaceships[ nShip ];
spaceship->setX( spaceship->x() + distance );
}
}
To copy to clipboard, switch view to plain text mode
Not compiled or tested so there may be typos or bugs.
Bookmarks