Retro_Gameboy
The purpose of this project is to create a GameBoy with classic games.
Arkanoid.cpp
Go to the documentation of this file.
1 #include <iostream>
2 #include <string>
3 #include "../include/Arkanoid.h"
4 
5 Arkanoid::Arkanoid() : n( 0 ), blocksRemain( 0 ), remainingLives( 8 ), isGameOver( false )
6 {
8 }
9 
11 {
12  RenderWindow window( VideoMode( LENGTH, WIDTH ), "Arkanoid Game!" );
13  window.setFramerateLimit( 60 );
14 
15  Texture texture1;
16  Texture texture2;
17  Texture texture3;
18  Texture texture4;
19  Texture texture5;
20 
21  texture1.loadFromFile( IMAGEPATH+"block01.png" );
22  texture2.loadFromFile( IMAGEPATH+"background.jpg" );
23  texture3.loadFromFile( IMAGEPATH+"ball.png" );
24  texture4.loadFromFile( IMAGEPATH+"paddle.png" );
25  texture5.loadFromFile( IMAGEPATH+"Heart.png" );
26 
27  Sprite s_Background( texture2 );
28  Sprite s_Ball( texture3 );
29  Sprite s_Paddle( texture4 );
30  Sprite s_Lives( texture5 );
31 
32  s_Paddle.setPosition( 300, 440 );
33  s_Ball.setPosition( 300, 300 );
34 
35  Sprite block[1000];
36  Sprite lives[3];
37 
38  for( int i = 0; i < 3; i++ )
39  {
40  lives[i].setTexture( texture5 );
41  lives[i].setPosition( i * 24, 0 );
42  }
43 
44  for( int i = 1; i <= 10; i++)
45  {
46  for( int j = 1; j <= 10; j++)
47  {
48  block[n].setTexture( texture1 );
49  block[n].setPosition( i * 43, j * 20 );
50  n++;
51  }
52  }
53 
54  blocksRemain = n;
55 
56  PlayArkanoid( window, s_Background, s_Ball, s_Paddle, block, lives, texture1 );
57 }
58 
59 void Arkanoid::CheckCollisions( float& xAxis, float& xPos, float& yAxis, float& yPos, Sprite* block, const bool isVertical = false )
60 {
61  for ( int i = 0; i < n; i++ )
62  {
63  if ( FloatRect( xAxis + 3, yAxis + 3, 6, 6).intersects( block[i].getGlobalBounds() ) )
64  {
65  block[i].setPosition( -100, 0 );
66  blocksRemain--;
67 
68  if( isVertical == true )
69  {
70  yPos = -yPos;
71  }
72  else
73  {
74  xPos = -xPos;
75  }
76 
77  }
78  }
79 }
80 
81 void Arkanoid::SetBoundaries( float& xAxis, float& xPos, float& yAxis, float& yPos, Sprite* lives )
82 {
83  if ( ( xAxis < 0 ) || ( xAxis > LENGTH ) )
84  {
85  xPos = -xPos;
86  }
87 
88  if ( ( yAxis < 0 ) || ( yAxis > WIDTH ) )
89  {
90  yPos = -yPos;
91  }
92 
93  if( yAxis == WIDTH )
94  {
96  SetLives( lives );
97  }
98 
99 }
100 
101 void Arkanoid::SetControls( Sprite& s_Paddle )
102 {
103  if ( Keyboard::isKeyPressed( Keyboard::Right ) )
104  {
105  s_Paddle.move( 6, 0 );
106  }
107 
108  if ( Keyboard::isKeyPressed( Keyboard::Left ) )
109  {
110  s_Paddle.move( -6, 0 );
111  }
112 }
113 
114 void Arkanoid::SetLives( Sprite* lives )
115 {
116  if( remainingLives == 6 )
117  {
118  lives[2].setPosition( -100, 0 );
119  }
120  else if( remainingLives == 4 )
121  {
122  lives[1].setPosition( -100, 0 );
123  }
124  else if( remainingLives == 2 )
125  {
126  lives[0].setPosition( -100, 0 );
127  }
128  else if( remainingLives == 0 )
129  {
130  isGameOver = true;
131  }
132 }
133 
135 {
136  int score = 0;
137  const int eliminatedBlocks = ( n - blocksRemain );
138  const int eliminatedBlocksScore = ( eliminatedBlocks * 100 );
139 
140  int heartBonus = 0;
141 
142  if( remainingLives == 6 )
143  {
144  heartBonus = 6000;
145  }
146  else if( remainingLives == 4 )
147  {
148  heartBonus = 4000;
149  }
150  else if( remainingLives == 2 )
151  {
152  heartBonus = 2000;
153  }
154  else if( remainingLives == 0 )
155  {
156  heartBonus = 0;
157  }
158 
159  score = ( eliminatedBlocksScore + heartBonus );
160 
161  return score;
162 }
163 
164 void Arkanoid::PlayArkanoid( RenderWindow& window, Sprite& s_Background, Sprite& s_Ball, Sprite& s_Paddle, Sprite* block, Sprite* lives, Texture& texture1 )
165 {
166 
167  srand( time( 0 ) );
168  float xPos = 6;
169  float yPos = 5;
170  float xAxis = 300;
171  float yAxis = 300;
172 
173  while ( window.isOpen() )
174  {
175  Event event;
176  while (window.pollEvent( event ) )
177  {
178  if ( event.type == Event::Closed )
179  {
180  window.close();
181  }
182 
183  }
184 
185  if( isGameOver != true )
186  {
187 
188  xAxis += xPos;
189  CheckCollisions( xAxis, xPos, yAxis, yPos, block, false);
190 
191  yAxis += yPos;
192  CheckCollisions( xAxis, xPos, yAxis, yPos, block, true);
193 
194  SetBoundaries( xAxis, xPos, yAxis, yPos, lives );
195 
196  SetControls( s_Paddle );
197 
198  // Make ball bounce on the Paddle.
199  if ( FloatRect( xAxis, yAxis, 12, 12 ).intersects( s_Paddle.getGlobalBounds() ) )
200  {
201  yPos = -( rand() % 5 + 2 );
202  }
203 
204  // Make the ball moving.
205  s_Ball.setPosition( xAxis, yAxis );
206 
207  // Draw Sprites
208  window.clear();
209  window.draw( s_Background );
210  window.draw( s_Ball );
211  window.draw( s_Paddle );
212 
213  for ( int i = 0; i < n; i++)
214  {
215  if( i < 3)
216  {
217  window.draw( lives[i] );
218  }
219  window.draw( block[i] );
220  }
221 
222  window.display();
223 
224  }
225  else
226  {
227  //Draw Sprites
228  GameOverScreen( window );
229  }
230 
231  }
232 
233 }
int blocksRemain
Remaining titls.
Definition: Arkanoid.h:42
virtual int CalculateScore()
Pure virtual function inherited from GameGenerics.h Each game calculate scores differently and theref...
Definition: Arkanoid.cpp:134
const std::string IMAGEPATH
Define path where image are located.
Definition: Arkanoid.h:36
Arkanoid()
Default Constructor. Calls the InitializeVars() function.
Definition: Arkanoid.cpp:5
const int WIDTH
Define the Width of the window.
Definition: Arkanoid.h:33
int n
The number of tiles in the block. Currently, it has 100 tiles.
Definition: Arkanoid.h:39
void SetBoundaries(float &xAxis, float &xPos, float &yAxis, float &yPos, Sprite *lives)
Set the boundaries so ball will remian inside the window. Also, whenever the ball hits outside the pa...
Definition: Arkanoid.cpp:81
void InitializeVars()
Create Sprites. Load the Images and set the Sprites. Creates a block of titles with 100 tiles....
Definition: Arkanoid.cpp:10
bool isGameOver
Set true when user lost all the chances. This indicate time to display game over screen.
Definition: Arkanoid.h:52
int remainingLives
Player has 3 hearts and once it ran out game will be over. Remaining lives are multiple of 2....
Definition: Arkanoid.h:48
const int LENGTH
Define the length of the window. If you want to change this you might need to change the background i...
Definition: Arkanoid.h:30
void SetControls(Sprite &s_Paddle)
Set the paddle to move right and left by pressing right and left arrow keys.
Definition: Arkanoid.cpp:101
void PlayArkanoid(RenderWindow &window, Sprite &s_Background, Sprite &s_Ball, Sprite &s_Paddle, Sprite *block, Sprite *lives, Texture &texture1)
Start the game and call other helper functions.
Definition: Arkanoid.cpp:164
void CheckCollisions(float &xAxis, float &xPos, float &yAxis, float &yPos, Sprite *block, const bool isVertical)
This will check collisions with the tiles and the ball. Everytime the ball hit a tile,...
Definition: Arkanoid.cpp:59
void SetLives(Sprite *lives)
Remove a heart whenever user miss the ball and it hit outside the paddle.
Definition: Arkanoid.cpp:114
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