Retro_Gameboy
The purpose of this project is to create a GameBoy with classic games.
Snake.cpp
Go to the documentation of this file.
1 #include "../include/Snake.h"
2 
3 Snake::Snake() : m_direction(0), m_SnakeLength(4), m_Score(0), m_IsGameOver(false)
4 {
6 }
7 
9 {
10  RenderWindow window( VideoMode( WIDTH, HEIGHT ), "Snake Game!" );
11 
12  Texture texture1;
13  Texture texture2;
14 
15  texture1.loadFromFile( "images/Snake_Img/white.png" );
16  texture2.loadFromFile( "images/Snake_Img/red.png" );
17 
18  Sprite sprite1( texture1 );
19  Sprite sprite2( texture2 );
20 
21  m_MyFruit.x = 10;
22  m_MyFruit.y = 10;
23 
24  PlaySnake( window, sprite1, sprite2 );
25 }
26 
28 {
29  const int score = m_Score;
30  return score;
31 }
32 
34 {
35  for( int i = m_SnakeLength; i > 0; i-- )
36  {
37  m_MySnake[i].x = m_MySnake[i-1].x;
38  m_MySnake[i].y = m_MySnake[i-1].y;
39  }
40 
42  GrowUponEat();
44  GameOverLogic();
45 
46 }
47 
49 {
50  for( int i = 1; i < m_SnakeLength; i++)
51  {
52  if( ( m_MySnake[0].x == m_MySnake[i].x ) && ( m_MySnake[0].y == m_MySnake[i].y ) )
53  {
54  m_IsGameOver = true;
55  }
56  }
57 }
58 
60 {
61  if( ( m_MySnake[0].x == m_MyFruit.x ) && ( m_MySnake[0].y == m_MyFruit.y ) )
62  {
63  m_SnakeLength++;
64  m_Score += 100;
65  m_MyFruit.x = rand() % N;
66  m_MyFruit.y = rand() % M;
67  }
68 }
69 
71 {
72  if( m_MySnake[0].x > N )
73  {
74  m_MySnake[0].x = 0;
75  }
76 
77  if( m_MySnake[0].x < 0 )
78  {
79  m_MySnake[0].x = N;
80  }
81 
82  if( m_MySnake[0].y > M )
83  {
84  m_MySnake[0].y = 0;
85  }
86 
87  if( m_MySnake[0].y < 0 )
88  {
89  m_MySnake[0].y = M;
90  }
91 }
92 
94 {
95 
96  if( m_direction == 0 )
97  {
98  m_MySnake[0].y += 1;
99  }
100  else if( m_direction == 1 )
101  {
102  m_MySnake[0].x -= 1;
103  }
104  else if( m_direction == 2 )
105  {
106  m_MySnake[0].x += 1;
107  }
108  else
109  {
110  m_MySnake[0].y -= 1;
111  }
112 
113 
114 }
115 
117 {
118  if( Keyboard::isKeyPressed( Keyboard::Left ) )
119  {
120  m_direction = 1;
121  }
122 
123  if( Keyboard::isKeyPressed( Keyboard::Right ) )
124  {
125  m_direction = 2;
126  }
127 
128  if( Keyboard::isKeyPressed( Keyboard::Up ) )
129  {
130  m_direction = 3;
131  }
132 
133  if( Keyboard::isKeyPressed( Keyboard::Down ) )
134  {
135  m_direction = 0;
136  }
137 }
138 
139 void Snake::DrawSprites( RenderWindow& window, Sprite& sprite1, Sprite& sprite2 )
140 {
141  //Draw
142  window.clear();
143 
144  for( int i = 0; i < N; i++ )
145  {
146  for( int j = 0; j < M; j++ )
147  {
148  sprite1.setPosition( ( i * SIZE ), ( j * SIZE ) );
149  window.draw( sprite1 );
150  }
151  }
152 
153  for( int i = 0; i < m_SnakeLength; i++)
154  {
155  sprite2.setPosition( ( m_MySnake[i].x * SIZE ), ( m_MySnake[i].y * SIZE) );
156  window.draw( sprite2 );
157  }
158 
159  sprite2.setPosition( ( m_MyFruit.x * SIZE ), (m_MyFruit.y * SIZE) );
160  window.draw( sprite2 );
161 
162 
163 }
164 
165 void Snake::PlaySnake( RenderWindow& window, Sprite& sprite1, Sprite& sprite2 )
166 {
167  srand( time( 0 ) );
168  Clock clock;
169  float timer = 0;
170  float delay = 0.1;
171 
172  while( window.isOpen() )
173  {
174 
175  const float time = clock.getElapsedTime().asSeconds();
176  clock.restart();
177  timer += time;
178 
179  Event event;
180 
181  while( window.pollEvent( event ) )
182  {
183  if( event.type == Event::Closed )
184  {
185  window.close();
186  }
187  }
188 
189  if( m_IsGameOver != true )
190  {
191  KeyControls();
192 
193  if( timer > delay )
194  {
195  timer = 0;
196  MoveSnake();
197  }
198 
199  DrawSprites( window, sprite1, sprite2 );
200  window.display();
201  }
202  else
203  {
204  //Draw GameOver Screen
205  GameOverScreen( window );
206  }
207 
208 
209  }
210 
211 
212 }
void GameOverScreen(RenderWindow &window)
Display a game over screen at the end of the game alone with user's score. Currently,...
Definition: GameGenerics.cpp:9
int m_direction
Direction of the snake.
Definition: Snake.h:27
const int WIDTH
Define width of the window.
Definition: Snake.h:21
virtual int CalculateScore()
Pure virtual function inherited from GameGenerics.h Each game calculate scores differently and theref...
Definition: Snake.cpp:27
int m_Score
Track game score.
Definition: Snake.h:33
void MoveSnake()
Move Snake across the grid per Tick.
Definition: Snake.cpp:33
const int N
Define length with 30 squares.
Definition: Snake.h:12
void PlaySnake(RenderWindow &window, Sprite &sprite1, Sprite &sprite2)
Start the game.
Definition: Snake.cpp:165
void GameOverLogic()
Whenever snake tries to eat itself game is over.
Definition: Snake.cpp:48
void KeyControls()
Allow user to control snake by using arrow keys.
Definition: Snake.cpp:116
void BoundaryControl()
If snake cross the boundary of the window make it reappear on the opposite side of the window.
Definition: Snake.cpp:70
SnakeStrct m_MySnake[100]
Holds x and y cordinates of the fruit.
Definition: Snake.h:58
void InitializeVars()
Create Sprites. Load the Images and set the Sprites. Call PlaySnake() function to start the game.
Definition: Snake.cpp:8
int m_SnakeLength
Length of the snake.
Definition: Snake.h:30
void MoveToThisDirection()
Set the direction to move the snake. 0 indicates down. 3 indicates up. 1 indicates left....
Definition: Snake.cpp:93
Snake()
Default Constructor. initialize variables.
Definition: Snake.cpp:3
const int HEIGHT
Define height of the window.
Definition: Snake.h:24
void GrowUponEat()
Whenever snake eats a fruit make it grow by 1 square.
Definition: Snake.cpp:59
const int SIZE
Define space between each square.
Definition: Snake.h:18
void DrawSprites(RenderWindow &window, Sprite &sprite1, Sprite &sprite2)
Draw sprites on the window.
Definition: Snake.cpp:139
bool m_IsGameOver
Track when game is over or not.
Definition: Snake.h:36
Fruit m_MyFruit
Holds x and y cordinates of the snake when it move across the grid.
Definition: Snake.h:55
const int M
Define height with 30 squares.
Definition: Snake.h:15
int y
Definition: Snake.h:50
int x
Definition: Snake.h:49