C++实现简易俄罗斯方块小游戏

实现一个简易的俄罗斯方块(Tetris)游戏涉及多个方面,包括图形显示、游戏逻辑、用户输入等。下面是一个简化版本的俄罗斯方块游戏的实现步骤和代码示例。这个实现会使用 C++ 和一个简单的图形库——如 SFML(Simple and Fast Multimedia Library),这是一个非常适合游戏开发的跨平台图形库。

1. 环境准备

-安装 SFML:
你可以从 SFML 官网 下载适合你操作系统的版本。
按照安装说明配置 SFML 库到你的开发环境。

2. 游戏设计

游戏区域: 游戏区域是一个网格,每个网格单元格可以容纳一个方块(积木块)。
方块: 每个方块由不同的形状组成,通常由 4 个小方块构成。
控制: 允许玩家通过键盘控制方块的移动和旋转。

3. 完整实现的代码

1. 主要文件和目录结构
tetris/
├── main.cpp
├── Tetris.h
├── Tetris.cpp
├── Piece.h
├── Piece.cpp
├── Grid.h
├── Grid.cpp
├── resources/
│   └── (SFML资源文件)
└── CMakeLists.txt
2. main.cpp
#include <SFML/Graphics.hpp>
#include "Tetris.h"

int main() {
    sf::RenderWindow window(sf::VideoMode(300, 600), "Tetris");
    Tetris game;

    sf::Clock clock;
    while (window.isOpen()) {
        sf::Time deltaTime = clock.restart();
        sf::Event event;
        while (window.pollEvent(event)) {
            if (event.type == sf::Event::Closed)
                window.close();
        }

        game.update(deltaTime.asSeconds());
        window.clear();
        game.draw(window);
        window.display();
    }

    return 0;
}
3. Tetris.h
#ifndef TETRIS_H
#define TETRIS_H

#include <SFML/Graphics.hpp>
#include "Piece.h"
#include "Grid.h"

class Tetris {
public:
    Tetris();
    void update(float deltaTime);
    void draw(sf::RenderWindow &window);

private:
    sf::RectangleShape blockShape;
    Piece currentPiece;
    Grid grid;
    sf::Clock moveClock;
    sf::Time moveInterval;

    void checkCollisions();
    void rotatePiece();
    void movePiece(int dx);
    void dropPiece();
    void mergePiece();
    void removeFullLines();
    bool isPositionValid(const Piece& piece, int dx, int dy);
};

#endif // TETRIS_H
4. Tetris.cpp
#include "Tetris.h"

Tetris::Tetris() : moveInterval(sf::seconds(0.5f)) {
    blockShape.setSize(sf::Vector2f(30, 30));
    blockShape.setFillColor(sf::Color::Cyan);
    currentPiece = Piece();
    grid = Grid();
}

void Tetris::update(float deltaTime) {
    if (moveClock.getElapsedTime() >= moveInterval) {
        moveClock.restart();
        dropPiece();
    }
    // Handle user input
    if (sf::Keyboard::isKeyPressed(sf::Keyboard::Left)) {
        movePiece(-1);
    } else if (sf::Keyboard::isKeyPressed(sf::Keyboard::Right)) {
        movePiece(1);
    } else if (sf::Keyboard::isKeyPressed(sf::Keyboard::Up)) {
        rotatePiece();
    }
}

void Tetris::draw(sf::RenderWindow &window) {
    // Draw the grid
    grid.draw(window);

    // Draw the current piece
    for (auto &block : currentPiece.getBlocks()) {
        blockShape.setPosition(block);
        window.draw(blockShape);
    }
}

void Tetris::checkCollisions() {
    // Check if the piece collides with the grid or other pieces
    if (!isPositionValid(currentPiece, 0, 0)) {
        mergePiece();
        removeFullLines();
        currentPiece = Piece(); // Create a new piece
    }
}

void Tetris::rotatePiece() {
    currentPiece.rotate();
    if (!isPositionValid(currentPiece, 0, 0)) {
        currentPiece.undoRotation();
    }
}

void Tetris::movePiece(int dx) {
    currentPiece.move(dx * 30, 0);
    if (!isPositionValid(currentPiece, dx * 30, 0)) {
        currentPiece.move(-dx * 30, 0);
    }
}

void Tetris::dropPiece() {
    currentPiece.move(0, 30);
    if (!isPositionValid(currentPiece, 0, 30)) {
        currentPiece.move(0, -30);
        mergePiece();
        removeFullLines();
        currentPiece = Piece(); // Create a new piece
    }
}

void Tetris::mergePiece() {
    grid.mergePiece(currentPiece);
}

void Tetris::removeFullLines() {
    grid.removeFullLines();
}

bool Tetris::isPositionValid(const Piece& piece, int dx, int dy) {
    for (auto &block : piece.getBlocks()) {
        sf::Vector2i pos((int)block.x + dx, (int)block.y + dy);
        if (pos.x < 0 || pos.x >= Grid::WIDTH || pos.y >= Grid::HEIGHT || grid.isOccupied(pos.x, pos.y)) {
            return false;
        }
    }
    return true;
}
5. Piece.h
#ifndef PIECE_H
#define PIECE_H

#include <vector>
#include <SFML/Graphics.hpp>

class Piece {
public:
    Piece();
    std::vector<sf::Vector2f> getBlocks() const;
    void rotate();
    void undoRotation();
    void move(float dx, float dy);

private:
    std::vector<sf::Vector2f> blocks;
    std::vector<sf::Vector2f> originalBlocks;
    void initializePiece();
    void rotateClockwise();
    void rotateCounterClockwise();
};

#endif // PIECE_H
6. Piece.cpp
#include "Piece.h"
#include <algorithm>

Piece::Piece() {
    initializePiece();
}

void Piece::initializePiece() {
    // Initialize with a default shape (e.g., a straight line)
    blocks = {
        {120, 0},
        {150, 0},
        {180, 0},
        {210, 0}
    };
    originalBlocks = blocks;
}

std::vector<sf::Vector2f> Piece::getBlocks() const {
    return blocks;
}

void Piece::rotate() {
    rotateClockwise();
}

void Piece::undoRotation() {
    blocks = originalBlocks;
}

void Piece::move(float dx, float dy) {
    for (auto &block : blocks) {
        block.x += dx;
        block.y += dy;
    }
}

void Piece::rotateClockwise() {
    // Implement rotation logic
}

void Piece::rotateCounterClockwise() {
    // Implement rotation logic
}
7. Grid.h
#ifndef GRID_H
#define GRID_H

#include <vector>
#include <SFML/Graphics.hpp>

class Grid {
public:
    static const int WIDTH = 10;
    static const int HEIGHT = 20;
    Grid();
    void draw(sf::RenderWindow &window);
    bool isOccupied(int x, int y) const;
    void mergePiece(const Piece &piece);
    void removeFullLines();

private:
    std::vector<std::vector<bool>> cells;
    void drawCell(sf::RenderWindow &window, int x, int y);
};

#endif // GRID_H
8. Grid.cpp
#include "Grid.h"

Grid::Grid() {
    cells.resize(HEIGHT, std::vector<bool>(WIDTH, false));
}

void Grid::draw(sf::RenderWindow &window) {
    for (int y = 0; y < HEIGHT; ++y) {
        for (int x = 0; x < WIDTH; ++x) {
            if (cells[y][x]) {
                drawCell(window, x, y);
            }
        }
    }
}

bool Grid::isOccupied(int x, int y) const {
    return cells[y][x];
}

void Grid::mergePiece(const Piece &piece) {
    for (const auto &block : piece.getBlocks()) {
        int x = block.x / 30;
        int y = block.y / 30;
        if (x >= 0 && x < WIDTH && y >= 0 && y < HEIGHT) {
            cells[y][x] = true;
        }
    }
}

void Grid::removeFullLines() {
    for (int y = 0; y < HEIGHT; ++y) {
        bool isFull = true;
        for (int x = 0; x < WIDTH; ++x) {
            if (!cells[y][x]) {
                isFull = false;
                break;
            }
        }
        if (isFull) {
            // Remove the full line
            cells.erase(cells.begin() + y);
            cells.insert(cells.begin(), std::vector<bool>(WIDTH, false));
        }
    }
}

void Grid::drawCell(sf::RenderWindow &window, int x, int y) {
    sf::RectangleShape cell(sf::Vector2f(30, 30));
    cell.setFillColor(sf::Color::Green);
    cell.setPosition(x * 30, y * 30);
    window.draw(cell);
}
9. CMakeLists.txt
cmake_minimum_required(VERSION 3.10)
project(Tetris)

find_package(SFML 2.5 COMPONENTS graphics window system REQUIRED)

add_executable(Tetris main.cpp Tetris.cpp Piece.cpp Grid.cpp)
target_link_libraries(Tetris sfml-graphics sfml-window sfml-system)

4. 运行和测试

  1. 创建构建目录:

    mkdir build
    cd build
    
  2. 生成构建文件:

    cmake ..
    
  3. 编译项目:

    make
    
  4. 运行游戏:

    ./Tetris
    

5. 游戏功能

  • 控制: 使用键盘的箭头键移动和旋转方块。
  • 方块生成: 初始生成一个方块,并在每次掉落后生成新的方块。
  • 碰撞检测: 检测方块是否与边界或其他方块碰撞。
  • 行消除: 检测并删除已填满的行。

以上是一个基础的俄罗斯方块游戏实现,其中还可以进一步扩展功能,如得分系统、游戏结束检测、不同形状的方块等。这些改进将进一步提升游戏的复杂性和可玩性。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值