Retro_Gameboy
The purpose of this project is to create a GameBoy with classic games.
Tetris.cpp
Go to the documentation of this file.
1 #include "../include/Tetris.h"
2 
4 {
5 
6  for( int row = 0; row < M; row++ )
7  {
8  for( int col = 0; col < N; col++ )
9  {
10  field[row][col] = 0;
11  }
12  }
13 
14  // Initialize struct array
15  for( int i = 0; i < 4; i++ )
16  {
17  a[i].x = 0;
18  a[i].y = 0;
19  b[i].x = 0;
20  b[i].y = 0;
21  }
22 
23 }
24 
26 {
27  srand( time(0) );
28  RenderWindow window( VideoMode( 320, 480 ), "The Game!" );
29 
30  // Load Tiles
31  Texture texture1;
32  Texture texture2;
33  Texture texture3;
34  texture1.loadFromFile( "images/tiles.png" );
35  texture2.loadFromFile("images/background.png");
36  texture3.loadFromFile("images/frame.png");
37 
38  Sprite sprite( texture1 );
39  Sprite background( texture2 );
40  Sprite frame( texture3 );
41  sprite.setTextureRect( IntRect( 0, 0, 18, 18 ) );
42 
43  int dx = 0;
44  bool rotate = false;
45  int colorNum = 1;
46  float timer = 0;
47  float delay = 0.3;
48 
49  Clock clock;
50 
51  // Window Form
52  while( window.isOpen() )
53  {
54 
55  float time = clock.getElapsedTime().asSeconds();
56  clock.restart();
57  timer += time;
58 
59  EventHandler( rotate, dx, window );
60 
61  if( Keyboard::isKeyPressed( Keyboard::Down ) )
62  {
63  delay = 0.05;
64  }
65 
66  MoveTile( dx );
67 
68  RotateTile( rotate );
69 
70  timer = MoveDownTilePerClick( timer, delay, colorNum );
71 
72  CheckLines();
73 
74  dx = 0;
75  rotate = false;
76  delay = 0.3;
77 
78  Draw( sprite, background, frame, window, colorNum );
79 
80  }
81 }
82 
83 void Tetris::EventHandler( bool& rotate, int& dx, RenderWindow& window )
84 {
85  Event event;
86 
87  while( window.pollEvent( event ) )
88  {
89  if( event.type == Event::Closed )
90  {
91  window.close();
92  }
93 
94  if( event.type == Event::KeyPressed )
95  {
96  if( event.key.code == Keyboard::Up )
97  {
98  rotate = true;
99  }
100  else if( event.key.code == Keyboard::Left )
101  {
102  dx = -1;
103  }
104  else if( event.key.code == Keyboard::Right )
105  {
106  dx = 1;
107  }
108  }
109  }
110 }
111 
113 {
114 
115  for( int i = 0; i < 4; i++)
116  {
117  if( ( a[i].x < 0 ) || ( a[i].x >= N ) || ( a[i].y >= M ) )
118  {
119  return false;
120  }
121  else if( field[ a[i].y ][ a[i].x ] )
122  {
123  return false;
124  }
125 
126  }
127  return true;
128 }
129 
130 void Tetris::MoveTile( const int position )
131 {
132  // Move
133  for( int i = 0; i < 4; i++)
134  {
135  b[i] = a[i];
136  a[i].x += position;
137  }
138 
139  if( !Check() )
140  {
141  for( int i = 0; i < 4; i++ )
142  {
143  a[i] = b[i];
144  }
145  }
146 }
147 
148 void Tetris::RotateTile( const bool rotate )
149 {
150  // Roatate
151  if( rotate )
152  {
153  // Center of rotation
154  Point point = a[1];
155  for( int i = 0; i < 4; i++)
156  {
157  const int x = a[i].y - point.y;
158  const int y = a[i].x - point.x;
159  a[i].x = point.x - x;
160  a[i].y = point.y + y;
161  }
162 
163  if( !Check() )
164  {
165  for( int i = 0; i < 4; i++ )
166  {
167  a[i] = b[i];
168  }
169  }
170 
171  }
172 
173 }
174 
175 float Tetris::MoveDownTilePerClick( float timer, const float delay, int& colorNum )
176 {
177  // Move 1 down per Tick
178  if( timer > delay )
179  {
180  for( int i = 0; i < 4; i++ )
181  {
182  a[i].y += 1;
183  }
184 
185  if( !Check() )
186  {
187  for( int i = 0; i < 4; i++)
188  {
189  field[ b[i].y ][ b[i].x ] = colorNum;
190  }
191 
192  colorNum = 1 + rand() % 7;
193  int n = rand() % 7;
194  for( int i = 0; i < 4; i++ )
195  {
196  a[i].x = figures[n][i] % 2;
197  a[i].y = figures[n][i] / 2;
198  }
199 
200  }
201 
202  timer = 0;
203  }
204 
205  return timer;
206 }
207 
209 {
210 
211  // Check lines
212  int k = M-1;
213  for (int i = M-1; i > 0; i--)
214  {
215  int count = 0;
216  for ( int j = 0; j < N; j++ )
217  {
218  if ( field[i][j] != 0 )
219  {
220  count++;
221  }
222 
223  field[k][j] = field[i][j];
224 
225  }
226 
227  if ( count < N )
228  {
229  k--;
230  }
231  }
232 
233 }
234 
236 {
237  int score = 0;
238  // Need to be Implemented.
239  return score;
240 }
241 
242 void Tetris::Draw( Sprite& sprite, Sprite& background, Sprite& frame, RenderWindow& window, const int& colorNum )
243  {
244  // Draw
245  window.clear( Color::White );
246  window.draw( background );
247 
248  for( int i = 0; i < M; i++ )
249  {
250  for( int j = 0; j < N; j++ )
251  {
252  if( field[i][j] == 0 )
253  {
254  continue;
255  }
256  sprite.setTextureRect( IntRect( field[i][j]*18, 0, 18, 18 ) );
257  sprite.setPosition( j * 18, i * 18);
258 
259  // Offset
260  sprite.move( 28, 31 );
261  window.draw( sprite );
262  }
263  }
264 
265  for( int i = 0; i < 4; i++ )
266  {
267  sprite.setTextureRect( IntRect( colorNum*18, 0, 18, 18 ) );
268  sprite.setPosition( a[i].x * 18, a[i].y * 18 );
269 
270  // Offset
271  sprite.move( 28, 31 );
272  window.draw( sprite );
273  }
274 
275 
276  Font font;
277  font.loadFromFile( "fonts/arial.ttf" );
278  Text text( "Score:", font, 30 );
279  text.setPosition( window.getSize().x*0.09, window.getSize().y - text.getGlobalBounds().height - text.getGlobalBounds().top );
280  text.setColor( sf::Color::Red );
281  window.draw( text );
282 
283  window.draw( frame );
284  window.display();
285  }
void MoveTile(const int position=0)
Move shapes horizontally.
Definition: Tetris.cpp:130
Tetris()
Default constructor. Used to intialize arrays.
Definition: Tetris.cpp:3
void Draw(Sprite &sprite, Sprite &background, Sprite &frame, RenderWindow &window, const int &colorNum)
Draw UI and shapes.
Definition: Tetris.cpp:242
const int figures[7][4]
Define 7 x 4 rectangle[ 7 rows, 4 coloumns] This defines the shapes. For example, these are the follo...
Definition: Tetris.h:40
float MoveDownTilePerClick(float timer, const float delay, int &colorNum)
Move shapes down for each tick.
Definition: Tetris.cpp:175
void EventHandler(bool &rotate, int &dx, RenderWindow &window)
Definition: Tetris.cpp:83
int field[M][N]
Define a 10 x 20 rectangle. This is use as the background for the game.
Definition: Tetris.h:18
static const int N
Define length of a rectangle.
Definition: Tetris.h:14
void RotateTile(const bool rotate=false)
Allow user to rotate shapes.
Definition: Tetris.cpp:148
Point a[4]
Use to move shapes.
Definition: Tetris.h:59
virtual int CalculateScore()
Pure virtual function inherited from GameGenerics.h Each game calculate scores differently and theref...
Definition: Tetris.cpp:235
bool Check()
Check the boundaries.
Definition: Tetris.cpp:112
void PlayTetris()
Play Tetris Game.
Definition: Tetris.cpp:25
static const int M
Define width of a rectangle.
Definition: Tetris.h:11
Point b[4]
Keep a backup.
Definition: Tetris.h:62
void CheckLines()
Reduce line upon completion.
Definition: Tetris.cpp:208
Denotes x and y points.
Definition: Tetris.h:54