-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
177 lines (143 loc) · 4.6 KB
/
Copy pathmain.cpp
File metadata and controls
177 lines (143 loc) · 4.6 KB
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
#include <SFML/Graphics.hpp>
#include <iostream>
#include <MazeGenerator.h>
#include <Player.h>
#include <sstream>
#include <CoinManager.h>
std::string FloatToString(float d)
{
std::stringstream ss;
ss << d;
std::string s = ss.str();
std::string r = "";
int db = 0;
while (s[db] != '.'){
db++;
}
int i = 0;
while (i < db + 2){
r += s[i];
i++;
}
return r;
}
std::string IntToString(int d)
{
std::stringstream ss;
ss << d;
std::string s = ss.str();
return s;
}
int main()
{
sf::RenderWindow window(sf::VideoMode(1024, 720), "Pac-Man");
MazeGenerator mazeGenerator(25,19,window);
CoinManager coinManager(25,19);
Player player(mazeGenerator);
sf::Clock clock;
sf::Time dt;
sf::Time time;
sf::Text displayTime, displayCoin, menuText;
sf::Font font;
if (!font.loadFromFile("arial.ttf"))
{
//"Font error"
}
menuText.setFont(font);
menuText.setCharacterSize(40);
enum State {Menu, Game, Score};
State state = Menu;
int frameC = 0;
int score = 0;
while (window.isOpen())
{
sf::Event event;
while (window.pollEvent(event))
{
if (event.type == sf::Event::Closed)
window.close();
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Left)){
player.SetDir(4);
}
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Right)){
player.SetDir(3);
}
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Up)){
player.SetDir(1);
}
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Down)){
player.SetDir(2);
}
/*if (sf::Keyboard::isKeyPressed(sf::Keyboard::Space)){
player.SetDir(0);
}*/
/* if (sf::Mouse::isButtonPressed(sf::Mouse::Right)){
std::cout<<sf::Mouse::getPosition(window).x<<std::endl;
std::cout<<sf::Mouse::getPosition(window).y<<std::endl;
}
*/
}
window.clear();
//GAMESTATE
if (state == Game){
frameC++;
dt = clock.getElapsedTime();
time += clock.getElapsedTime();
clock.restart();
if (frameC > 10){
frameC = 0;
displayTime.setString("Time: " + FloatToString(time.asSeconds()));
}
displayCoin.setString("Coins: " + IntToString(score));
window.draw(displayCoin);
window.draw(displayTime);
player.MovePlayer(dt);
if (mazeGenerator.CollectCoin(player.GetMX(), player.GetMY())){
score++;
}
mazeGenerator.DrawMaze(window);
player.DrawPlayer(window);
if (player.GetWin()){
state = Score;
}
}
//GAMESTATE END
//MENUSTATE
if (state == Menu){
menuText.setString("Play");
menuText.setPosition(1024/2 - 40, 768/2 - 80);
window.draw(menuText);
if (sf::Mouse::isButtonPressed(sf::Mouse::Left) && sf::Mouse::getPosition(window).x > 1024 / 2 - 40 && sf::Mouse::getPosition(window).x < 1024 / 2 + 50 &&
sf::Mouse::getPosition(window).y > 768 / 2 - 80 && sf::Mouse::getPosition(window).y < 768 / 2){
state = Game;
mazeGenerator.Generate();
mazeGenerator.DrawConsole();
player.SetMaze(mazeGenerator);
player.SetBasic();
score = 0;
time = time.Zero;
displayTime.setFont(font);
displayTime.setCharacterSize(20);
displayTime.setColor(sf::Color::White);
displayTime.setPosition(300, 20);
displayCoin.setFont(font);
displayCoin.setCharacterSize(20);
displayCoin.setColor(sf::Color::White);
displayCoin.setPosition(100, 20);
clock.restart();
}
}
//SCORESTATE
if (state == Score){
displayTime.setString("Your score: " + IntToString(score * 5 - (int)(time.asSeconds() * 2)));
displayTime.setPosition(1024/2 - 80, 768/2 - 80);
displayCoin.setString("Press Space to return to the Main menu or ESC to exit the game");
displayCoin.setPosition(1024/2 - 280, 768/2 - 200);
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Space)) state = Menu;
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Escape)) window.close();
window.draw(displayCoin);
window.draw(displayTime);
}
window.display();
}
}