-
Notifications
You must be signed in to change notification settings - Fork 1
/
snake project.txt
437 lines (348 loc) · 10.2 KB
/
snake project.txt
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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
#include <SFML/Graphics.hpp>
#include <SFML/Window.hpp>
#include <ctime>
#include <iostream>
#include <sstream>
using namespace std;
using namespace sf;
Font font; //logo and in-game font
int score=-1;
struct borders // defines arena walls (simple linked list)
{
RectangleShape rect;
struct borders *next;
FloatRect boundingBox;
};
struct borders* btemp=NULL;
struct snake //snake is a doubly linked list
{
float x; // direction of movement along x axis
float y; // direction of movement along y axis
struct snake *next;
struct snake *prev;
RectangleShape rectangle;
FloatRect boundingBox;
};
struct snake *top=NULL; //tail of snake
struct snake *last=NULL; //head of snake
struct snake* temp1=NULL;
struct movement // queue to store direction of movement
{
float a,b;
struct movement* next;
};
struct movement* first=NULL; //top of the queue
struct movement* temp=NULL;
void construct_borders()
{
struct borders *rect1= new borders;
struct borders *rect2= new borders;
struct borders *rect3= new borders;
struct borders *rect4= new borders;
rect1->rect.setSize(Vector2f(20,410));
rect2->rect.setSize(Vector2f(470,10));
rect3->rect.setSize(Vector2f(470,10));
rect4->rect.setSize(Vector2f(20,410));
rect1->rect.setFillColor(Color::White);rect2->rect.setFillColor(Color::White);rect3->rect.setFillColor(Color::White);rect4->rect.setFillColor(Color::White);
rect1->rect.setPosition(10,70);rect2->rect.setPosition(10,70);rect3->rect.setPosition(10,470);rect4->rect.setPosition(470,70);
rect1->next=rect2;
rect2->next=rect3;
rect3->next=rect4;
rect4->next=NULL;
btemp=rect1;
}
void insert(RenderWindow *window, bool *status, Text &text) //make the snake grow
{
*status=false;
struct snake *p= new snake;
p->rectangle.setSize(Vector2f(10,10));
p->rectangle.setOutlineThickness(-1);
p->rectangle.setOutlineColor(Color::Black);
if (top==NULL)
{
p->rectangle.setFillColor(Color::Green);
p->rectangle.setPosition(240,240);
top=p;
last=p;
p->next=NULL;
p->prev=NULL;
p->x=0; p->y=-10; //initial direction of movement
temp1=p;
window->clear(Color::Black); window->draw(p->rectangle); window->display();
}
else
{
p->rectangle.setFillColor(Color(0,140,0));
p->next=temp1;
p->prev=last;
p->x=temp1->x; //follow the direction of previous node
p->y=temp1->y;
top=p;
temp1->prev=p;
Vector2f position=temp1->rectangle.getPosition();
position.y=((-temp1->y)+position.y); //set the position with respect to previous node's direction of movement and position
position.x=((-temp1->x)+position.x);
p->rectangle.setPosition(position);
window->clear(Color::Black);
struct snake* temp3;
temp3=last; // after insertion of new node the snake is redrawn node by node
do{
window->draw(temp3->rectangle);
temp3=temp3->prev;
}while(temp3!=last);
struct borders *t=btemp; // re-draws walls
do {
window->draw(t->rect);
t=t->next;
}while(t!=NULL);
window->draw(text); //re-draw score
window->display();
temp1=p;
}
}
void update_score(Text &score1)
{
score++;
ostringstream ss; //conversion from int to string for display on window
ss << score;
string str = ss.str();
score1.setFont(font);
score1.setCharacterSize(40);
score1.setPosition(200,10);
score1.setColor(sf::Color::White);
score1.setString(str);
}
void insert_movement(float a, float b) // queue node is created to save every keystroke
{
if ((last->x!=-a)||(last->y!=-b)) //check to prevent snake head from moving in the opposite direct and crashing into itself
{
struct movement* n=new movement;
n->a=a; n->b=b;
n->next=NULL;
if (first==NULL)
{
temp=n;
first=n;
}
else
{
temp->next=n;
temp=n;
}
}
}
void remove_movement() // queue node is removed after snake head has turned
{
struct movement* temp2=first;
struct movement* temp3=first->next;
if(first->next!=NULL)
{
first->next=temp3->next;
first=temp3;
free(temp2);
}
else
{
free(first);
first=NULL;
}
}
Vector2f generate_random() //generate random positions for food
{
if(score==0)
srand(time(NULL)); //seeding the random function only once
float x= (31+rand() % 428);
float y= (81+rand() % 383);
Vector2f position;
position.x=x; position.y=y;
CircleShape dummy(4);
dummy.setPosition(position);
FloatRect bb=dummy.getGlobalBounds();
struct snake* temp3=last; //check if food position is not occupied by snake
do{
temp3->boundingBox=temp3->rectangle.getGlobalBounds();
if(temp3->boundingBox.intersects(bb))
position=generate_random();
temp3=temp3->prev;
}while(temp3!=last);
return position;
}
CircleShape food(4);
void generate_food()
{
food.setPosition(generate_random());
food.setFillColor(Color::Yellow);
}
void draw_all(RenderWindow *window,Text *score2,Text *score3)
{
struct borders *t=btemp; // draw walls
do {
window->draw(t->rect);
t=t->next;
}while(t!=NULL);
window->draw(food);
window->draw(*score3); //draw "score"
window->draw(*score2); //draw score(integers)
struct snake *temp5=last; //draw and move each snake node
do
{
temp5->rectangle.move(temp5->x,temp5->y);
window->draw(temp5->rectangle);
temp5=temp5->prev;
}while(temp5!=last);
}
bool collision_check(bool *status)
{
FloatRect boundingBox1=last->rectangle.getGlobalBounds(); // snake head's bounding box
FloatRect boundingBox2=food.getGlobalBounds(); //food's bounding box
if (boundingBox2.intersects(boundingBox1))
{generate_food(); *status=true;}
struct snake *temp5=last->prev;
do
{
temp5->boundingBox=temp5->rectangle.getGlobalBounds(); //check if snake has crashed into itself
if (temp5->boundingBox.intersects(boundingBox1))
return true;
temp5=temp5->prev;
}while(temp5!=last);
struct borders *t=btemp;
do {
t->boundingBox=t->rect.getGlobalBounds(); //check if snake has crashed into a wall
if (t->boundingBox.intersects(boundingBox1))
return true;
t=t->next;
}while(t!=NULL);
}
void moving(RenderWindow *window) //movement thread. makes sure snake nodes turn only after its previous node has turned
{
construct_borders();
Text text;
text.setFont(font);
text.setString("Game Over");
text.setCharacterSize(40);
text.setPosition(275,10);
text.setColor(Color(255,0,51));
Text mytext;
mytext.setFont(font);
mytext.setString("Score: ");
mytext.setCharacterSize(40);
mytext.setPosition(20,10);
Text score2;
update_score(score2);
bool collision=false; //collision turns true when snake collides with itself/wall. status remains false
bool status=false; //status turns true when snake collides with food. collision remains false
insert(window,&status,mytext); //intialize snake
insert(window,&status,mytext);
insert(window,&status,mytext);
insert(window,&status,mytext);
generate_food();
while(window->isOpen())
{
struct snake *temp4=top;
struct snake *previous=temp4->next;
if(first!=NULL)
{
last->x=first->a; // assign head of snake movement direction corresponding to first node of queue.
last->y=first->b;
remove_movement();
}
window->clear(Color::Black);
draw_all(window,&score2,&mytext);
window->display();
collision=collision_check(&status);
if (status==true)
{
insert(window,&status,mytext);
update_score(score2);
}
if (collision==true)
{
window->draw(text);
window->display();
break;
}
sleep(seconds(0.1f));
do {
temp4->x=previous->x;
temp4->y=previous->y;
temp4=temp4->next;
previous=previous->next;
}while(temp4!=last);
}
}
void title_screen(RenderWindow *window)
{
Font font1;
if (!font.loadFromFile("brokenstick.ttf"))
cout<<"blahblah error"<<endl;
if (!font1.loadFromFile("BRLNSR.ttf"))
cout<<"blahblah error"<<endl;
Texture logo;
if (!logo.loadFromFile("k11766153.png"))
cout<<"blahblah error"<<endl;
Sprite sprite;
sprite.setTexture(logo);
sprite.setPosition(165,30);
sprite.setColor(Color(0,240,0));
Text snake1;
snake1.setFont(font);
snake1.setString("Snake");
snake1.setCharacterSize(50);
snake1.setColor(Color(0,230,0));
snake1.setPosition(185,164);
Text text;
text.setFont(font1);
text.setString("Created By: ");
text.setCharacterSize(30);
text.setColor(Color(255,255,51));
text.setStyle(Text::Bold);
text.setPosition(175,230);
Text text2;
text2.setFont(font1);
text2.setString("Ayesha Waheed-040\nDaniya Asim-041\nKomal Mariam-046\nMaryam Atiq-076");
text2.setCharacterSize(24);
text2.setColor(Color(0,255,255));
text2.setPosition(157,270);
window->clear(Color::Black);
window->draw(sprite);
window->draw(snake1);
window->draw(text);
window->draw(text2);
window->display();
sleep(seconds(4));
}
void main()
{
RenderWindow window(VideoMode(500,500),"Snake Game");
window.setVerticalSyncEnabled(true); //application runs at same frequency as monitor (60 frames/second)
window.setKeyRepeatEnabled(false); //halts generation of multiple events when a key is pressed once and held
title_screen(&window);
window.setActive(false); // main thread releases command of window
Thread thread(&moving,&window); //transferring command to second moving thread
thread.launch();
Event event;
while (window.isOpen())
{
while(window.pollEvent(event))
{
switch (event.type)
{
case Event::Closed:
window.close();
break;
case Event::KeyPressed:
{
if(Keyboard::isKeyPressed(Keyboard::Left))
insert_movement(-10,0);
if(Keyboard::isKeyPressed(Keyboard::Right))
insert_movement(10,0);
if(Keyboard::isKeyPressed(Keyboard::Up))
insert_movement(0,-10);
if(Keyboard::isKeyPressed(Keyboard::Down))
insert_movement(0,10);
break;
}
}
}
}
}