Untitled
unknown
plain_text
21 days ago
1.7 kB
2
Indexable
Never
#include "Tetromino.h" #include <cstdlib> #include <ctime> Tetromino::Tetromino() { srand(time(0)); reset(1 + rand() % 7, rand() % 7, figures); } void Tetromino::reset(int color, int type, const int figures[7][4]) { this->colorNum = color; this->nextColorNum = 1 + rand() % 7; this->nextTemo = rand() % 7; this->timer = 0; this->delay = 0.3; this->gameOver = false; int offset = rand() % 6; setFigure(type, offset); offset = rand() % 6; setFigure(nextTemo, offset); } void Tetromino::setFigure(int type, int offset) { for(int i = 0; i < 4; i++) { a[i].y = figures[type][i] % 2; a[i].x = figures[type][i] / 2 + offset; } } bool Tetromino::checkCollision(int field[23][10]) const { for (int i = 0; i < 4; i++) { if (a[i].x < 0 || a[i].x >= 10 || a[i].y >= 23 || field[a[i].y][a[i].x]) return false; } return true; } void Tetromino::rotate() { Point p = a[1]; for (int i = 0; i < 4; i++) { int x = a[i].y - p.y; int y = a[i].x - p.x; a[i].x = p.x - x; a[i].y = p.y + y; } if (!checkCollision()) { for (int i = 0; i < 4; i++) a[i] = b[i]; } } void Tetromino::move(int dx, int dy) { for (int i = 0; i < 4; i++) { b[i] = a[i]; a[i].x += dx; a[i].y += dy; } if (!checkCollision()) { for (int i = 0; i < 4; i++) a[i] = b[i]; } } void Tetromino::draw(sf::Sprite &sprite, sf::RenderWindow &window) const { for (int i = 0; i < 4; i++) { sprite.setTextureRect(sf::IntRect(colorNum * 18, 0, 18, 18)); sprite.setPosition(a[i].x * 36, a[i].y * 36); sprite.move(36, 36); // Offset window.draw(sprite); } }
Leave a Comment