-
Notifications
You must be signed in to change notification settings - Fork 0
/
Game.cc
96 lines (87 loc) · 1.78 KB
/
Game.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#include "Game.h"
Game::Game(int width, int height, int brick_sz):
m_width(width),m_height(height),m_brick_sz(brick_sz),
m_bricks(std::vector<pBrick>()),m_stopped(false),
m_movingLeft(false), m_movingRight(false)
{
srand (time(NULL));
newPiece();
}
void Game::newPiece()
{
Tetrimino_t pieceType=(Tetrimino_t)(rand()%(Z_TETRIMINO+1));
m_piece=std::make_shared<Tetrimino>(4*m_brick_sz,0,m_brick_sz,pieceType);
}
void Game::show(SDL_Renderer* rndr)
{
for(std::vector<pBrick>::iterator it=m_bricks.begin(); it!=m_bricks.end(); ++it)
{
(*it)->show( rndr );
}
if(m_movingLeft)
{
m_piece->move(-m_brick_sz,0);
if(isCollision())
{
m_piece->move(m_brick_sz,0);
}
}
if(m_movingRight)
{
m_piece->move(m_brick_sz,0);
if(isCollision())
{
m_piece->move(-m_brick_sz,0);
}
}
m_piece->show( rndr );
}
bool Game::isCollision()
{
pBrick* newBricks=m_piece->getBricks();
for(int i = 0; i<TETRIMINO_SIZE; ++i)
{
pBrick current=newBricks[i];
SDL_Point pos=current->getPos();
if(pos.x<0 || pos.x+m_brick_sz>m_width || pos.y+m_brick_sz>m_height)
{
return true;
}
else
{
for(std::vector<pBrick>::iterator it=m_bricks.begin(); it!=m_bricks.end(); ++it)
{
SDL_Point fixedPos=(*it)->getPos();
if(pos.x==fixedPos.x && pos.y==fixedPos.y)
{
return true;
}
}
}
}
return false;
}
void Game::step()
{
if(!m_stopped)
{
m_piece->move(0,m_brick_sz);
if(isCollision())
{
m_piece->move(0,-m_brick_sz);
pBrick* newBricks=m_piece->getBricks();
for(int i = 0; i<TETRIMINO_SIZE; ++i)
{
pBrick current=newBricks[i];
m_bricks.push_back(current);
}
newPiece();
if(isCollision())
{
SDL_Log("Game over!");
m_stopped=true;
}
//SDL_Log("m_bricks has %lu elements\n",m_bricks.size());
}
}
}