Untitled

mail@pastecode.io avatar
unknown
plain_text
21 days ago
5.5 kB
2
Indexable
Never
#include <SFML/Graphics.hpp>
#include <iostream>
#include "Tetromino.hpp"
#include "Score.hpp"

using namespace sf;

const int playfieldWidth = 180;  // chiều rộng của khung trò chơi
const int playfieldHeight = 360; // chiều cao của khung trò chơi
const int boundaryThickness = 4; // độ dày của khung
const int windowHeight = 960;    // chiều cao cửa sổ
const int windowWidth = 720;     // chiều rộng cửa sổ
const int M = 23;  // số hàng của khung chơi
const int N = 10;  // số cột của khung chơi
const int blockSize = 36;  // kích thước mỗi ô của khung chơi

int field[M][N] = { 0 };  // mảng lưu trạng thái các ô của khung chơi

void resetGame(int& score, int& colorNum, int& nextColorNum, int& nextTemo, float& timer, float& delay, bool& gameOver) {
    for (int i = 0; i < M; i++) {
        for (int j = 0; j < N; j++) field[i][j] = 0;
    }

    score = 0;
    colorNum = 1 + rand() % 7;
    timer = 0;
    delay = 0.3;
    gameOver = false;

    resetTetromino(colorNum, nextColorNum, nextTemo);
}

void drawPlayfieldFrame(RenderWindow& window) {
    RectangleShape frame(Vector2f(playfieldWidth + boundaryThickness * 2, playfieldHeight + boundaryThickness * 2));
    frame.setFillColor(Color(128, 128, 128));
    frame.setPosition(Vector2f(blockSize, blockSize * 3));
    window.draw(frame);

    RectangleShape playfieldBackground(Vector2f(playfieldWidth, playfieldHeight));
    playfieldBackground.setFillColor(Color::Black);
    playfieldBackground.setPosition(Vector2f(blockSize + boundaryThickness, blockSize * 3 + boundaryThickness));
    window.draw(playfieldBackground);
}

void drawGridLines(RenderWindow& window) {
    for (int i = 1; i < N; i++) {
        RectangleShape verticalLine(Vector2f(2, playfieldHeight));
        verticalLine.setFillColor(Color(64, 64, 64));
        verticalLine.setPosition(blockSize + boundaryThickness + i * blockSize, blockSize * 3 + boundaryThickness);
        window.draw(verticalLine);
    }

    for (int i = 1; i < M; i++) {
        RectangleShape horizontalLine(Vector2f(playfieldWidth, 2));
        horizontalLine.setFillColor(Color(64, 64, 64));
        horizontalLine.setPosition(blockSize + boundaryThickness, blockSize * 3 + boundaryThickness + i * blockSize);
        window.draw(horizontalLine);
    }
}

void drawTetromino(RenderWindow& window, Sprite& s) {
    for (int i = 0; i < 4; i++) {
        s.setTextureRect(IntRect(18 * a[i].x, 0, 18, 18));
        s.setPosition(a[i].x * blockSize, a[i].y * blockSize);
        s.move(blockSize, blockSize * 3);
        window.draw(s);
    }
}

void drawField(RenderWindow& window, Sprite& s) {
    for (int i = 0; i < M; i++) {
        for (int j = 0; j < N; j++) {
            if (field[i][j] == 0) continue;
            s.setTextureRect(IntRect(18 * (field[i][j] - 1), 0, 18, 18));
            s.setPosition(j * blockSize, i * blockSize);
            s.move(blockSize, blockSize * 3);
            window.draw(s);
        }
    }
}

void drawNextTetromino(RenderWindow& window, Sprite& s, int nextColorNum) {
    if (checkI()) {
        for (int i = 0; i < 4; i++) {
            s.setTextureRect(IntRect(nextColorNum * 18, 0, 18, 18));
            s.setPosition(c[i].x * blockSize, c[i].y * blockSize);
            s.move(blockSize + blockSize + playfieldWidth * 2 + 1.5 * blockSize - 8, 4 * blockSize + blockSize / 2);
            window.draw(s);
        }
    }
    else if (checkO()) {
        for (int i = 0; i < 4; i++) {
            s.setTextureRect(IntRect(nextColorNum * 18, 0, 18, 18));
            s.setPosition(c[i].x * blockSize, c[i].y * blockSize);
            s.move(blockSize + blockSize + playfieldWidth * 2 + 1.5 * blockSize - 8, 4 * blockSize + blockSize);
            window.draw(s);
        }
    }
    else {
        for (int i = 0; i < 4; i++) {
            s.setTextureRect(IntRect(nextColorNum * 18, 0, 18, 18));
            s.setPosition(c[i].x * blockSize, c[i].y * blockSize);
            s.move(blockSize + blockSize + playfieldWidth * 2 + blockSize - 8, 4 * blockSize + blockSize);
            window.draw(s);
        }
    }
}

int main() {
    srand(static_cast<unsigned>(time(0)));

    RenderWindow window(VideoMode(windowWidth, windowHeight), "Tetris Game");

    Texture t1;
    t1.loadFromFile("images/tiles.png");
    Sprite s(t1);
    s.setScale(2, 2);

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

    Clock clock;
    resetGame(score, colorNum, nextColorNum, nextTemo, 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 (e.type == Event::KeyPressed && e.key.code == Keyboard::Space) {
                resetGame(score, colorNum, nextColorNum, nextTemo, timer, delay, gameOver);
            }
        }

        if (timer > delay) {
            timer = 0;
            // Cập nhật logic cho tetromino ở đây...
        }

        window.clear(Color::White);
        drawPlayfieldFrame(window);
        drawGridLines(window);
        drawField(window, s);
        drawTetromino(window, s);
        drawNextTetromino(window, s, nextColorNum);
        window.display();
    }

    return 0;
}
Leave a Comment