-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.cpp
163 lines (128 loc) · 2.88 KB
/
main.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
/*
* main.cpp
* snake
*
* Created by Mathieu Allaire on 11-01-23.
* Copyright 2011 mathieuallaire.ca. All rights reserved.
*
*/
#include <iostream>
#include <list>
#include <SFML/Graphics.hpp>
#include "block.h"
#include "snake.h"
#include "fruit.h"
using namespace std;
int main()
{
// Window
sf::RenderWindow app(sf::VideoMode(750, 625), "Snake 1.0 par Mathieu Allaire", sf::Style::Close);
app.SetFramerateLimit(12);
// The snake himself
Snake snake(app);
// All the blocks that build up the snake
list<Block> blocks;
list<Block>::iterator it;
// Direction of the snake
enum direction { UP, RIGHT, DOWN, LEFT };
int direction = RIGHT;
// The fruit himself
Fruit fruit(app);
// Tells if we need to respawn a fruit
bool mustSpawnFruit = true;
// Coords of the fruit
int fruitX;
int fruitY;
while(app.IsOpened())
{
app.Clear();
/**
* Process events
**/
sf::Event Event;
while (app.GetEvent(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::Key::Right))
{
direction = RIGHT;
}
else if((Event.Type == sf::Event::KeyPressed) && (Event.Key.Code == sf::Key::Left))
{
direction = LEFT;
}
else if((Event.Type == sf::Event::KeyPressed) && (Event.Key.Code == sf::Key::Up))
{
direction = UP;
}
else if((Event.Type == sf::Event::KeyPressed) && (Event.Key.Code == sf::Key::Down))
{
direction = DOWN;
}
}
/**
* Spawn a fruit if necessary
**/
if(mustSpawnFruit)
{
bool safe = true;
// We make sure that it doesn't spawn on the snake
do
{
fruitX = (rand() % 30) * 25;
fruitY = (rand() % 25) * 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);
snake.move(direction);
snake.draw();
/**
* Detects if the snake hits the bounds of the board
**/
if(snake.getX() >= 750 || snake.getX() < 0 || snake.getY() >= 625 || snake.getY() < 0)
{
app.Close();
}
/**
* Detects if the snake hits a fruit
**/
if(snake.getX() == fruit.getX() && snake.getY() == fruit.getY())
{
mustSpawnFruit = true;
snake.grow();
}
/**
* Detects if the snake hits himself
**/
blocks = snake.getBlocks();
it = blocks.begin();
it++; // Since we know the last inserted block is the same position as the snake's head, we skip it
for(it; it != blocks.end(); it++)
{
if(it->getX() == snake.getX() && it->getY() == snake.getY())
{
app.Close();
}
}
app.Display();
}
return EXIT_SUCCESS;
}