1 #include "../include/Snake.h"
3 Snake::Snake() : m_direction(0), m_SnakeLength(4), m_Score(0), m_IsGameOver(false)
10 RenderWindow window( VideoMode(
WIDTH,
HEIGHT ),
"Snake Game!" );
15 texture1.loadFromFile(
"images/Snake_Img/white.png" );
16 texture2.loadFromFile(
"images/Snake_Img/red.png" );
18 Sprite sprite1( texture1 );
19 Sprite sprite2( texture2 );
118 if( Keyboard::isKeyPressed( Keyboard::Left ) )
123 if( Keyboard::isKeyPressed( Keyboard::Right ) )
128 if( Keyboard::isKeyPressed( Keyboard::Up ) )
133 if( Keyboard::isKeyPressed( Keyboard::Down ) )
144 for(
int i = 0; i <
N; i++ )
146 for(
int j = 0; j <
M; j++ )
148 sprite1.setPosition( ( i *
SIZE ), ( j *
SIZE ) );
149 window.draw( sprite1 );
156 window.draw( sprite2 );
160 window.draw( sprite2 );
172 while( window.isOpen() )
175 const float time = clock.getElapsedTime().asSeconds();
181 while( window.pollEvent( event ) )
183 if( event.type == Event::Closed )
void GameOverScreen(RenderWindow &window)
Display a game over screen at the end of the game alone with user's score. Currently,...
int m_direction
Direction of the snake.
const int WIDTH
Define width of the window.
virtual int CalculateScore()
Pure virtual function inherited from GameGenerics.h Each game calculate scores differently and theref...
int m_Score
Track game score.
void MoveSnake()
Move Snake across the grid per Tick.
const int N
Define length with 30 squares.
void PlaySnake(RenderWindow &window, Sprite &sprite1, Sprite &sprite2)
Start the game.
void GameOverLogic()
Whenever snake tries to eat itself game is over.
void KeyControls()
Allow user to control snake by using arrow keys.
void BoundaryControl()
If snake cross the boundary of the window make it reappear on the opposite side of the window.
SnakeStrct m_MySnake[100]
Holds x and y cordinates of the fruit.
void InitializeVars()
Create Sprites. Load the Images and set the Sprites. Call PlaySnake() function to start the game.
int m_SnakeLength
Length of the snake.
void MoveToThisDirection()
Set the direction to move the snake. 0 indicates down. 3 indicates up. 1 indicates left....
Snake()
Default Constructor. initialize variables.
const int HEIGHT
Define height of the window.
void GrowUponEat()
Whenever snake eats a fruit make it grow by 1 square.
const int SIZE
Define space between each square.
void DrawSprites(RenderWindow &window, Sprite &sprite1, Sprite &sprite2)
Draw sprites on the window.
bool m_IsGameOver
Track when game is over or not.
Fruit m_MyFruit
Holds x and y cordinates of the snake when it move across the grid.
const int M
Define height with 30 squares.