Tetris

 avatar
user_5862267
plain_text
5 months ago
5.4 kB
4
Indexable
int main()
{
    srand(time(0));

    RenderWindow window(VideoMode(320, 480), "The Game!");

    Texture t1, t2, t3;
    t1.loadFromFile("images/tiles.png");
    t2.loadFromFile("images/background.png");
    t3.loadFromFile("images/frame.png");

    Sprite s(t1), background(t2), frame(t3);

    Font font;
    font.loadFromFile("fonts/arial.ttf");

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

    scoreText.setFillColor(Color::Black);
    highScoreText.setFillColor(Color::Black);
    gameOverText.setFillColor(Color::Red);
    playAgainText.setFillColor(Color::Blue);

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

    scoreText.setPosition(200, 20);  // Position on the right side
    highScoreText.setPosition(200, 50);
    gameOverText.setPosition(40, 220);
    playAgainText.setPosition(70, 260);

    int dx = 0;
    bool rotate = 0;
    int colorNum = 1;
    float timer = 0, delay = 0.3;
    int score = 0;
    int highScore = 0;
    bool gameOver = false;

    Clock clock;

    // Initialize the first piece
    resetGame(score, colorNum, timer, delay, gameOver);

    while (window.isOpen())
    {
        float time = clock.getElapsedTime().asSeconds();
        clock.restart();
        timer += time;

        Event e;
        while (window.pollEvent(e))
        {
            if (e.type == Event::Closed)
                window.close();

            if (gameOver)
            {
                if (e.type == Event::KeyPressed && e.key.code == Keyboard::Space)
                {
                    resetGame(score, colorNum, timer, delay, gameOver);
                }
                continue;
            }

            if (e.type == Event::KeyPressed)
                if (e.key.code == Keyboard::Up) rotate = true;
                else if (e.key.code == Keyboard::Left) dx = -1;
                else if (e.key.code == Keyboard::Right) dx = 1;
        }

        if (gameOver)
        {
            window.clear(Color::White);
            window.draw(background);
            window.draw(scoreText);
            window.draw(highScoreText);
            window.draw(gameOverText);
            playAgainText.setString("Press SPACE to Play Again");
            window.draw(playAgainText);
            window.draw(frame);
            window.display();
            continue;
        }

        if (Keyboard::isKeyPressed(Keyboard::Down)) delay = 0.05;

        //// <- Move -> ///
        for (int i = 0; i < 4; i++) { b[i] = a[i]; a[i].x += dx; }
        if (!check()) for (int i = 0; i < 4; i++) a[i] = b[i];

        //////Rotate//////
        if (rotate)
        {
            Point p = a[1]; //center of rotation
            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 (!check()) for (int i = 0; i < 4; i++) a[i] = b[i];
        }

        ///////Tick//////
        if (timer > delay)
        {
            for (int i = 0; i < 4; i++) { b[i] = a[i]; a[i].y += 1; }

            if (!check())
            {
                for (int i = 0; i < 4; i++) field[b[i].y][b[i].x] = colorNum;

                colorNum = 1 + rand() % 7;
                int n = rand() % 7;
                for (int i = 0; i < 4; i++)
                {
                    a[i].x = figures[n][i] % 2;
                    a[i].y = figures[n][i] / 2;
                }

                // Check for game over
                for (int i = 0; i < 4; i++)
                {
                    if (a[i].y < 0)
                    {
                        gameOver = true;
                        break;
                    }
                }
            }

            timer = 0;
        }

        ///////check lines//////////
        int k = M - 1;
        for (int i = M - 1; i > 0; i--)
        {
            int count = 0;
            for (int j = 0; j < N; j++)
            {
                if (field[i][j]) count++;
                field[k][j] = field[i][j];
            }
            if (count < N) 
            {
                k--;
            }
            else
            {
                score += 100;  // Increase score by 100 for each line cleared
            }
        }

        // Update high score
        if (score > highScore) highScore = score;

        dx = 0; rotate = 0; delay = 0.3;

        /////////draw//////////
        window.clear(Color::White);
        window.draw(background);

        for (int i = 0; i < M; i++)
            for (int j = 0; j < N; j++)
            {
                if (field[i][j] == 0) continue;
                s.setTextureRect(IntRect(field[i][j] * 18, 0, 18, 18));
                s.setPosition(j * 18, i * 18);
                s.move(28, 31); //offset
                window.draw(s);
            }

        for (int i = 0; i < 4; i++)
        {
            s.setTextureRect(IntRect(colorNum * 18, 0, 18, 18));
            s.setPosition(a[i].x * 18, a[i].y * 18);
            s.move(28, 31); //offset
            window.draw(s);
        }

        window.draw(scoreText);
        window.draw(highScoreText);
        window.draw(frame);
        window.display();
    }

    return 0;
}
Leave a Comment