#include "Game.h" #include "Score.h" #include #include #include #include #include #include #include Game::Game(int &argc, char **argv, int flags) : QApplication(argc, argv, flags) , scene(nullptr) , view(nullptr) , score(nullptr) { } Game::~Game() { delete scene; delete view; } int Game::run() { // Init the random seed qsrand(QTime::currentTime().msec()); // An invisible object that manages all the items scene = new QGraphicsScene(); // Any visible item to place in the scene view = new QGraphicsView(scene); view->resize(800, 600); // Set a black background and add a nebula over it view->setBackgroundBrush(QBrush(Qt::black, Qt::SolidPattern)); // scene->addItem( new QGraphicsPixmapItem(QPixmap(":/Resources/Background.png")) ); // The scene has infinite size, but we want it has the same size than the view // This stops the weird behavior of the autoscroll feature of the view being smaller than the // scene, because the scene auto-increases to include the bullets being moved scene->setSceneRect( view->rect() ); // Disable scrollbars because they are not needed view->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff); view->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff); // A label to show the player score score = new Score(tr("Score"), 0, Qt::blue); score->setPos(0, 0); scene->addItem(score); // Load the graphic resources // svgRenderer = new QSvgRenderer(QString(":/Resources/Graphics.svg"), this); // Create the player control /* Player* player = new Player(); player->setSharedRenderer(svgRenderer); scene->addItem(player); player->setInitialPos(); */ /* // Launch an enemy periodically QTimer timer; connect(&timer, &QTimer::timeout, this, &Game::launchEnemy); timer.start(2500); */ // Play background music // playBackgroundMusic("BackgroundMusic.mp3"); // Show the view and enter in application's event loop view->show(); return exec(); } void Game::playBackgroundMusic(const QString& audioFilename) { QMediaPlaylist *playlist = new QMediaPlaylist(); playlist->setPlaybackMode(QMediaPlaylist::Loop); #ifdef Q_OS_OSX playlist->addMedia(QUrl::fromLocalFile(Game::applicationDirPath() + "/Resources/" + audioFilename)); #else playlist->addMedia(QUrl("qrc:/Resources/" + audioFilename)); #endif QMediaPlayer* mediaPlayer = new QMediaPlayer(this); mediaPlayer->setPlaylist(playlist); mediaPlayer->setVolume(75); // 75% mediaPlayer->play(); } void Game::launchEnemy() { /* Enemy* enemy = new Enemy(this); scene->addItem(enemy); enemy->setInitialPos(); */ }