Untitled

mail@pastecode.io avatar
unknown
plain_text
14 days ago
3.6 kB
2
Indexable
Never
#include "Game.h"
#include <SFML/Graphics.hpp>
#include <cstdlib>

Game::Game() : window(sf::VideoMode(720, 960), "The Game!"), gameOver(false) {
    tetromino.reset(1 + rand() % 7, rand() % 7, tetromino.figures);

    texture.loadFromFile("images/tiles.png");
    sprite.setTexture(texture);

    font.loadFromFile("Roboto-Regular.ttf");

    scoreText.setFont(font);
    highScoreText.setFont(font);
    gameOverText.setFont(font);
    playAgainText.setFont(font);

    scoreText.setCharacterSize(20);
    highScoreText.setCharacterSize(20);
    gameOverText.setCharacterSize(30);
    playAgainText.setCharacterSize(20);

    scoreText.setFillColor(sf::Color(35, 47, 68));
    highScoreText.setFillColor(sf::Color(35, 47, 68));
    gameOverText.setFillColor(sf::Color::Red);
    playAgainText.setFillColor(sf::Color(35, 47, 68));
}

void Game::run() {
    sf::Clock clock;
    while (window.isOpen()) {
        handleEvents();
float deltaTime = clock.restart().asSeconds();
        if (!gameOver) {
            update();
        }
        render();
    }
}

void Game::handleEvents() {
    sf::Event event;
    while (window.pollEvent(event)) {
        if (event.type == sf::Event::Closed) {
            window.close();
        }
        if (event.type == sf::Event::KeyPressed) {
            switch (event.key.code) {
                case sf::Keyboard::Left:
                    tetromino.move(-1, 0);
                    break;
                case sf::Keyboard::Right:
                    tetromino.move(1, 0);
                    break;
                case sf::Keyboard::Down:
                    tetromino.move(0, 1);
                    break;
                case sf::Keyboard::Up:
                    tetromino.rotate();
                    break;
                case sf::Keyboard::Space:
                    // Placeholder for rotating Tetromino
                    break;
                default:
                    break;
            }
        }
    }
}

void Game::update() {
    if (tetromino.gameOver) {
        gameOver = true;
        field.setGameOver(true);
    }

    tetromino.timer += clock.restart().asSeconds();
    if (tetromino.timer > tetromino.delay) {
        tetromino.move(0, 1);
        if (!tetromino.checkCollision(field.field)) {
            tetromino.move(0, -1);
            field.checkLines(score.getScore());
        }
        tetromino.timer = 0;
    }

    if (field.isGameOver()) {
        gameOver = true;
    }
}

void Game::render() {
    window.clear(sf::Color::White);

    field.draw(sprite, window);
    tetromino.draw(sprite, window);

    scoreText.setString("Score: " + std::to_string(score.getScore()));
    highScoreText.setString("High Score: " + std::to_string(score.getHighScore()));
    scoreText.setPosition(10, 10);
    highScoreText.setPosition(10, 50);

    window.draw(scoreText);
    window.draw(highScoreText);

    if (gameOver) {
        gameOverText.setString("Game Over!");
        playAgainText.setString("Press R to Restart or Q to Quit");
        gameOverText.setPosition(180, 400);
        playAgainText.setPosition(150, 460);

        window.draw(gameOverText);
        window.draw(playAgainText);

        if (sf::Keyboard::isKeyPressed(sf::Keyboard::R)) {
            reset();
        }
        if (sf::Keyboard::isKeyPressed(sf::Keyboard::Q)) {
            window.close();
        }
    }

    window.display();
}

void Game::reset() {
    field.reset();
    tetromino.reset(1 + rand() % 7, rand() % 7, tetromino.figures);
    score.updateScore(0);
    gameOver = false;
}
Leave a Comment