-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame.cpp
60 lines (43 loc) · 1.9 KB
/
game.cpp
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 "game.h"
Game::Game() : m_isGameRunning(false)
{
m_pGameTimer = std::make_unique<QTimer>(this);
connect(m_pGameTimer.get(), &QTimer::timeout, this, &Game::gameLoop);
}
void Game::initialize()
{
/*------------------------------------------- WINDOW INITIALIZE [START]------------------------------------------------------ */
m_pGamePlayWindow = std::make_unique<QMainWindow>();
m_pGamePlayWindow->setStyleSheet("QMainWindow { background-image: url('../../Assets/background.png'); background-position: center; background-repeat: no-repeat; }");
m_pGamePlayWindow->setFixedSize(WINDOW_WIDTH, WINDOW_HEIGHT); // Set window size
m_pGamePlayWindow->show();
/*------------------------------------------- WINDOW INITIALIZE [END]------------------------------------------------------ */
/*------------------------------------------- ENTITY INITIALIZE [START]------------------------------------------------------ */
m_pBird = std::make_unique<Bird> (m_pGamePlayWindow.get(), BIRD_X_POS, BIRD_Y_POS); // deleted when game is over.
m_pPillar = std::make_unique<Pillar>(m_pGamePlayWindow.get());
/*------------------------------------------- ENTITY INITIALIZE [END]------------------------------------------------------ */
run();
}
void Game::run()
{
m_isGameRunning = true;
int frameInterval = 16; // Approximately 60 FPS (1000ms / 60 ≈ 16.67ms)
m_pGameTimer->start(frameInterval); // Starts the timer
}
void Game::pause()
{
m_isGameRunning = false;
m_pGameTimer->stop(); // Stops the timer
}
void Game::end()
{
m_isGameRunning = false;
m_pGameTimer->stop(); // Stops the timer
}
void Game::gameLoop()
{
if (!m_isGameRunning) return;
// Handle collisions, score updates, etc.
// Render the updated frame (typically handled by Qt's event loop)
m_pGamePlayWindow->update(); // This would trigger a repaint of the window
}