Tetris/part.cpp

133 lines
1.7 KiB
C++
Raw Normal View History

2014-11-07 20:47:50 +01:00
#include "part.hpp"
part::part()
{
posX = WIDTH / 2 - 1;
posY = -4;
generate();
}
void part::fillData()
{
for (int i = 0; i < 4; i++)
{
for (int j = 0; j < 4; j++)
{
data[i][j] = Shapes[shapeIndex][i][j];
}
}
}
void part::generate()
{
int g;
g = (rand() % 7) * 4;
shapeIndex = (ShapeIndexes)g;
color = g / 4;
fillData();
}
void part::move(int x, int y)
{
if (isValid(x, y))
{
posX += x;
posY += y;
}
}
void part::rotate()
{
int coIndex;
coIndex = shapeIndex;
shapeIndex = ((shapeIndex + 1) % 4 == 0 ? shapeIndex - 3 : shapeIndex + 1);
fillData();
if (!isValid())
{
shapeIndex = coIndex;
fillData();
}
}
bool part::isValid(int xOffset, int yOffset)
{
for (int y = 0; y < 4; y++)
{
for (int x = 0; x < 4; x++)
{
if ( (data[y][x] && (posX + x + xOffset >= WIDTH || posX + xOffset < 0)) || (data[y][x] && (posY + y + yOffset > HEIGHT)) )
{
# ifdef DEBUG
cout << "InValid" << endl;
# endif
return false;
}
}
}
# ifdef DEBUG
cout << "Valid" << endl;
# endif
return true;
}
void part::draw(SDL_Surface *screen)
{
for (int i = 0; i < 4 ;i++)
{
for (int m = 0; m < 4; m++)
{
if (data[i][m])
{
drawQuad(screen, (m + posX) * SIZE, (i + posY) * SIZE, colors[color]);
}
}
}
}
void part::draw ( int startX, int startY, SDL_Surface * screen )
{
for (int i = 0; i < 4 ;i++)
{
for (int m = 0; m < 4; m++)
{
if (data[i][m])
{
drawQuad(screen, (m + posX) * SIZE + startX, (i + posY) * SIZE + startY, colors[color]);
}
}
}
}
int part::getPosX()
{
return posX;
}
int part::getPosY()
{
return posY;
}
int part::getElement(int x, int y)
{
return data[y][x];
}
int part::getColor()
{
return color;
}