forked from arowayasmeen/Arcade
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsnakeMain.cpp
288 lines (215 loc) · 5.57 KB
/
snakeMain.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
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
#include <iostream>
#include <list>
#include <SFML/Graphics.hpp>
#include <Windows.h>
#include <string>
#include <sstream>
#include <SFML/Audio.hpp>
#include "block.h"
#include "snake.h"
#include "fruit.h"
#include "snakeMain.h"
using namespace std;
int snakeMain ::snakef()
{
// Window
sf::RenderWindow app(sf::VideoMode(800, 500), "POKE SNEK!", sf::Style::Close);
app.setFramerateLimit(7);
//background
sf::Texture back;
if (back.loadFromFile("ResourceSnake/back2.png") == -1) {
return 0;
}
sf::RectangleShape background;
background.setPosition(0, 0);
background.setSize(sf::Vector2f(800, 500));
background.setTexture(&back);
//Font and text
sf::Font font;
if (font.loadFromFile("ResourceSnake/a.ttf") == 0) {
cout << "loading error" << endl;
}
sf::Text title;
title.setFont(font);
title.setCharacterSize(30);
title.setPosition(300, 250);
sf::Text title2;
title2.setFont(font);
title2.setCharacterSize(30);
title2.setPosition(280, 200);
title2.setString("GAME OVER");
sf::Text title3;
title3.setFont(font);
title3.setCharacterSize(25);
title3.setPosition(210, 400);
title3.setString("press any key to exit");
//// The snake - pickachu heads
Snake snake(app);//initialises first two pickachu heads
int score = 0;//initial score set to 0
// Building up the entire snake - trail of pickachu heads
list<Block> blocks;// A list containing the position of each pickachu head
list<Block>::iterator it;
// Direction of the snake
enum direction { UP, RIGHT, DOWN, LEFT };
int direction = RIGHT;
//// The fruit - Pokeball
Fruit fruit(app);
// Tells if we need to respawn pokeball
bool mustSpawnFruit = true;
// Coords of the fruit
int fruitX;
int fruitY;
//Sound
sf::SoundBuffer startGame;
if (startGame.loadFromFile("ResourceSnake/start.wav") == 0)cout << "error" << endl;
sf::Sound start;
start.setBuffer(startGame);
start.play();
sf::SoundBuffer pickPokeball;
if (pickPokeball.loadFromFile("ResourceSnake/Pickup_pokeball.wav") == 0)cout << "error" << endl;
sf::Sound pickup;
pickup.setBuffer(pickPokeball);
sf::SoundBuffer endGame;
if (endGame.loadFromFile("ResourceSnake/dead.wav") == 0)cout << "error" << endl;
sf::Sound end;
end.setBuffer(endGame);
while (app.isOpen())
{
app.clear();
sf::Event Event;
while (app.pollEvent(Event))
{
if (Event.type == sf::Event::Closed)
{
app.close();
}
// Determine the direction of the snake
if ((Event.type == sf::Event::KeyPressed) && (Event.key.code == sf::Keyboard::Right))
{
direction = RIGHT;
}
else if ((Event.type == sf::Event::KeyPressed) && (Event.key.code == sf::Keyboard::Left))
{
direction = LEFT;
}
else if ((Event.type == sf::Event::KeyPressed) && (Event.key.code == sf::Keyboard::Up))
{
direction = UP;
}
else if ((Event.type == sf::Event::KeyPressed) && (Event.key.code == sf::Keyboard::Down))
{
direction = DOWN;
}
}
app.draw(background);
//Spawn pokeball if necessary
if (mustSpawnFruit)
{
bool safe = true;
// We make sure that it doesn't spawn on the snake
do
{
fruitX = (rand() % 20) * 25;
fruitY = (rand() % 20) * 25;
blocks = snake.getBlocks();
for (it = blocks.begin(); it != blocks.end(); it++)
{
if (it->getX() == fruitX && it->getY() == fruitY)
{
safe = false;
break;
}
else
{
safe = true;
}
}
} while (safe != true);
mustSpawnFruit = false;
}
fruit.spawn(fruitX, fruitY);//Draws pokeball on screen
snake.move(direction);//sets new position for snake
snake.draw();
//checks bounds
if (snake.getX() >= 800 || snake.getX() < 0 || snake.getY() >= 500 || snake.getY() < 0)
{
bool play = true;
end.play();//sound
while (play) {
sf::Event event;
while (app.pollEvent(event)) {
if (event.type == sf::Event::Closed)
{
play = false;
app.close();
}
if ((event.type == sf::Event::KeyPressed))
{
play = false;
app.close();
}
}
//printing score
string scr;
ostringstream ss;
ss << "SCORE: ";
ss << score;
scr = ss.str();
title.setString(scr);
app.draw(title);
app.draw(title2);
app.draw(title3);
app.display();
}
}
//Check to see if it hits pokeball
if (snake.getX() == fruit.getX() && snake.getY() == fruit.getY())
{
mustSpawnFruit = true;
score++;
snake.grow();
pickup.play();
}
//Check to see if snake hits itself
blocks = snake.getBlocks();
it = blocks.begin();
it++; // not required to check for first Pikachu head
for (it; it != blocks.end(); it++)
{
if (it->getX() == snake.getX() && it->getY() == snake.getY())
{
bool play = true;
end.play();
while (play) {
sf::Event event;
while (app.pollEvent(event)) {
if (event.type == sf::Event::Closed)
{
play = false;
app.close();
}
if ((event.type == sf::Event::KeyPressed))
{
play = false;
app.close();
}
}
//printing score
string scr;
ostringstream ss;
ss << "SCORE: ";
ss << score;
scr = ss.str();
title.setString(scr);
title.setString(scr);
app.draw(title);
app.draw(title2);
app.draw(title3);
app.display();
}
}
}
app.display();
}
return 0;
}