1 #include "../include/Tetris.h"
6 for(
int row = 0; row <
M; row++ )
8 for(
int col = 0; col <
N; col++ )
15 for(
int i = 0; i < 4; i++ )
28 RenderWindow window( VideoMode( 320, 480 ),
"The Game!" );
34 texture1.loadFromFile(
"images/tiles.png" );
35 texture2.loadFromFile(
"images/background.png");
36 texture3.loadFromFile(
"images/frame.png");
38 Sprite sprite( texture1 );
39 Sprite background( texture2 );
40 Sprite frame( texture3 );
41 sprite.setTextureRect( IntRect( 0, 0, 18, 18 ) );
52 while( window.isOpen() )
55 float time = clock.getElapsedTime().asSeconds();
61 if( Keyboard::isKeyPressed( Keyboard::Down ) )
78 Draw( sprite, background, frame, window, colorNum );
87 while( window.pollEvent( event ) )
89 if( event.type == Event::Closed )
94 if( event.type == Event::KeyPressed )
96 if( event.key.code == Keyboard::Up )
100 else if( event.key.code == Keyboard::Left )
104 else if( event.key.code == Keyboard::Right )
115 for(
int i = 0; i < 4; i++)
117 if( (
a[i].x < 0 ) || (
a[i].x >=
N ) || (
a[i].y >=
M ) )
121 else if(
field[
a[i].y ][
a[i].x ] )
133 for(
int i = 0; i < 4; i++)
141 for(
int i = 0; i < 4; i++ )
155 for(
int i = 0; i < 4; i++)
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;
165 for(
int i = 0; i < 4; i++ )
180 for(
int i = 0; i < 4; i++ )
187 for(
int i = 0; i < 4; i++)
192 colorNum = 1 + rand() % 7;
194 for(
int i = 0; i < 4; i++ )
213 for (
int i =
M-1; i > 0; i--)
216 for (
int j = 0; j <
N; j++ )
218 if (
field[i][j] != 0 )
242 void Tetris::Draw( Sprite& sprite, Sprite& background, Sprite& frame, RenderWindow& window,
const int& colorNum )
245 window.clear( Color::White );
246 window.draw( background );
248 for(
int i = 0; i <
M; i++ )
250 for(
int j = 0; j <
N; j++ )
252 if(
field[i][j] == 0 )
256 sprite.setTextureRect( IntRect(
field[i][j]*18, 0, 18, 18 ) );
257 sprite.setPosition( j * 18, i * 18);
260 sprite.move( 28, 31 );
261 window.draw( sprite );
265 for(
int i = 0; i < 4; i++ )
267 sprite.setTextureRect( IntRect( colorNum*18, 0, 18, 18 ) );
268 sprite.setPosition(
a[i].x * 18,
a[i].y * 18 );
271 sprite.move( 28, 31 );
272 window.draw( sprite );
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 );
283 window.draw( frame );
void MoveTile(const int position=0)
Move shapes horizontally.
Tetris()
Default constructor. Used to intialize arrays.
void Draw(Sprite &sprite, Sprite &background, Sprite &frame, RenderWindow &window, const int &colorNum)
Draw UI and shapes.
const int figures[7][4]
Define 7 x 4 rectangle[ 7 rows, 4 coloumns] This defines the shapes. For example, these are the follo...
float MoveDownTilePerClick(float timer, const float delay, int &colorNum)
Move shapes down for each tick.
void EventHandler(bool &rotate, int &dx, RenderWindow &window)
int field[M][N]
Define a 10 x 20 rectangle. This is use as the background for the game.
static const int N
Define length of a rectangle.
void RotateTile(const bool rotate=false)
Allow user to rotate shapes.
Point a[4]
Use to move shapes.
virtual int CalculateScore()
Pure virtual function inherited from GameGenerics.h Each game calculate scores differently and theref...
bool Check()
Check the boundaries.
void PlayTetris()
Play Tetris Game.
static const int M
Define width of a rectangle.
void CheckLines()
Reduce line upon completion.