120 lines
1.9 KiB
C++
120 lines
1.9 KiB
C++
#include "Tetris.h"
|
|
|
|
Tetris::Tetris()
|
|
{
|
|
paint_device().resize(
|
|
Size(
|
|
m_Width + 6,
|
|
m_Height
|
|
)
|
|
);
|
|
|
|
m_GameField.resize(
|
|
m_Width,
|
|
m_Height
|
|
);
|
|
|
|
m_Figure =
|
|
new IBlock(
|
|
Point(5,1)
|
|
);
|
|
|
|
track_key(KEY_LEFT);
|
|
track_key(KEY_RIGHT);
|
|
track_key(KEY_DOWN);
|
|
track_key(' ');
|
|
}
|
|
Tetris::~Tetris()
|
|
{
|
|
delete m_Figure;
|
|
}
|
|
bool Tetris::end() const
|
|
{
|
|
return m_End;
|
|
}
|
|
void Tetris::render(
|
|
PaintDevice& paintDevice
|
|
)
|
|
{
|
|
m_GameField.render(
|
|
paintDevice
|
|
);
|
|
|
|
if (m_Figure)
|
|
{
|
|
m_Figure->render(
|
|
paintDevice
|
|
);
|
|
}
|
|
|
|
Vector2 scorePos(m_Width + 1, 2);
|
|
|
|
std::wstring text = L"SCORE: " + std::to_wstring(m_Score);
|
|
|
|
for (size_t i = 0; i < text.size(); i++)
|
|
{
|
|
paintDevice.set_char(
|
|
Vector2(scorePos.x() + i, scorePos.y()),
|
|
text[i]
|
|
);
|
|
}
|
|
}
|
|
void Tetris::update(int dt)
|
|
{
|
|
m_Figure->backup();
|
|
|
|
m_Figure->update(dt);
|
|
|
|
if (m_GameField.has_collision(*m_Figure))
|
|
{
|
|
m_Figure->restore();
|
|
|
|
m_GameField.merge(*m_Figure);
|
|
|
|
int lines = m_GameField.clear_lines();
|
|
|
|
if (lines > 0)
|
|
{
|
|
m_Score += lines * 100;
|
|
}
|
|
|
|
delete m_Figure;
|
|
|
|
m_Figure =
|
|
new IBlock(Point(5,1));
|
|
}
|
|
}
|
|
void Tetris::on_button_press(
|
|
int button
|
|
)
|
|
{
|
|
m_Figure->backup();
|
|
|
|
switch (button)
|
|
{
|
|
case KEY_LEFT:
|
|
m_Figure->move_left();
|
|
break;
|
|
|
|
case KEY_RIGHT:
|
|
m_Figure->move_right();
|
|
break;
|
|
|
|
case KEY_DOWN:
|
|
m_Figure->boost();
|
|
break;
|
|
|
|
case ' ':
|
|
m_Figure->rotate();
|
|
break;
|
|
}
|
|
|
|
if (
|
|
m_GameField.has_collision(
|
|
*m_Figure
|
|
)
|
|
)
|
|
{
|
|
m_Figure->restore();
|
|
}
|
|
} |