CODE for tetris game
CODE for tetris game
#include <SFML/Graphics.hpp>
#include <vector>
#include <iostream>
#include <cstdlib>
#include <ctime>
int tetrominoShapes[7][4] = {
{2, 3, 4, 5} // O shape
};
class Tetris {
private:
bool gameOver;
RenderWindow window;
vector<vector<int>> grid;
RectangleShape block;
int currentShape;
int rotation;
Color colors[7] = {
Color::Cyan, // I
Color::Red, // Z
Color::Green, // S
Color::Yellow, // T
Color::Blue, // L
Color::Magenta, // J
Color::White // O
};
Font font;
Text scoreText;
public:
Tetris() {
block.setFillColor(Color::White);
spawnNewPiece();
fallTime = 0.0f;
gameOver = false;
if (!font.loadFromFile("FORTE.ttf")) {
exit(1);
scoreText.setFont(font);
scoreText.setCharacterSize(24);
scoreText.setFillColor(Color::White);
scoreText.setPosition(10, 10);
updateScoreText();
void updateScoreText() {
void spawnNewPiece() {
bool checkCollision() {
// Check if the current piece collides with the walls or other blocks
return true;
return false;
void rotatePiece() {
rotation = (rotation + 1) % 4;
x += dx;
y += dy;
if (checkCollision()) {
x -= dx;
y -= dy;
placePiece();
spawnNewPiece();
void placePiece() {
// Debugging output
cout << "Placing block at: (" << px << ", " << py << ")" << endl;
clearFullLines();
cout << "Game over: Block at top row" << endl; // Debugging output
gameOver = true;
return;
}
}
void clearFullLines() {
if (grid[i][j] == 0) {
fullLine = false;
break;
if (fullLine) {
updateScoreText();
void draw() {
window.clear();
if (grid[i][j] != 0) {
block.setFillColor(colors[grid[i][j] - 1]);
window.draw(block);
if (!gameOver)
block.setFillColor(colors[currentShape]);
window.draw(block);
else
Text gameOverText;
gameOverText.setFont(font);
gameOverText.setCharacterSize(36);
gameOverText.setFillColor(Color::Red);
gameOverText.setString("Game Over");
window.draw(scoreText);
window.display();
void run() {
while (window.isOpen()) {
Event event;
while (window.pollEvent(event)) {
if (event.type == Event::Closed)
window.close();
if (event.key.code == Keyboard::Left)
if (event.key.code == Keyboard::Right)
if (event.key.code == Keyboard::Down)
if (event.key.code == Keyboard::Up)
if (!gameOver)
else
cout << "Game Over: No further actions allowed." << endl; // Debugging output
draw();
};
int main() {
Tetris game;
game.run();
return 0;