-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGame.cpp
119 lines (96 loc) · 2.86 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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
/*!
* \file Game.cpp
* \author Ekinox <[email protected]>
*/
#include "Game.h"
#include <SFML/Graphics.hpp>
#include "Mario.h"
#include "config.h"
/*!
* \param Window the window on which render the game
* \param GameMap the map to play
* \brief The default constructor
*
* <b>BE CAREFUL :</b> Do not delete the window/game map before the
* Game object, otherwise the Game object won't be valid !
*/
Game::Game(sf::RenderWindow &Window, Map &GameMap) :
myWindow(&Window), myMap(&GameMap)
{
}
/*!
* \brief The method to call to make the game play
*/
void Game::Run()
{
// Set the frame rate
myWindow->SetFramerateLimit(framerate);
// Initialize Mario
Mario mario =
Mario(myMap->GetMarioPosX(), myMap->GetMarioPosY(), *myMap, 3);
// Main loop
bool Stop = false;
while (!Stop)
{
// Event managing
sf::Event Event;
while (myWindow->GetEvent(Event))
{
switch (Event.Type)
{
case sf::Event::Closed:
return;
case sf::Event::KeyPressed:
switch (Event.Key.Code)
{
case sf::Key::Escape:
return;
case sf::Key::Left:
mario.GoLeft();
break;
case sf::Key::Right:
mario.GoRight();
break;
case sf::Key::Up:
mario.Jump();
break;
case sf::Key::R:
mario.Run();
break;
default:
break;
}
break;
case sf::Event::KeyReleased:
switch (Event.Key.Code)
{
case sf::Key::Left:
mario.StopGoingLeft();
break;
case sf::Key::Right:
mario.StopGoingRight();
break;
case sf::Key::R:
mario.StopRunning();
break;
default:
break;
}
default:
break;
}
}
// Update the map and all which is on it
mario.Update();
// If Mario lost, close
if (mario.Lost())
return;
// Update the view
myWindow->SetView(mario.GetView());
// Redraw the screen
myWindow->Clear(BACKGROUND_COLOR);
myWindow->Draw(*myMap);
myWindow->Draw(mario);
myWindow->Display();
}
}