Untitled
unknown
plain_text
a year ago
6.0 kB
12
Indexable
#include "Game.h"
Game::Game() : window(sf::VideoMode(720, 960), "Tetris"), timer(0), delay(0.3), gameOver(false) {
init();
}
void Game::init() {
t1.loadFromFile("images/tiles.png");
t2.loadFromFile("images/background.png");
t3.loadFromFile("images/frame.png");
font.loadFromFile("Roboto-Regular.ttf");
s.setTexture(t1);
frame.setTexture(t3);
background.setSize(sf::Vector2f(720, 960));
background.setFillColor(sf::Color(200, 220, 255, 180));
playfieldBoundary.setSize(sf::Vector2f(180, 360));
playfieldBoundary.setFillColor(sf::Color(200, 220, 255));
playfieldBoundary.setOutlineThickness();
playfieldBoundary.setOutlineColor(sf::Color(35, 47, 68));
playfieldBoundary.setPosition(20, 20);
nextTempoBoundary.setSize(sf::Vector2f(200, 160));
nextTempoBoundary.setFillColor(sf::Color(200, 220, 255));
nextTempoBoundary.setOutlineThickness(4);
nextTempoBoundary.setOutlineColor(sf::Color(35, 47, 68));
nextTempoBoundary.setPosition(20, 420);
scoreBoundary.setSize(sf::Vector2f(120, 60));
scoreBoundary.setFillColor(sf::Color(200, 220, 255));
scoreBoundary.setOutlineThickness(4);
scoreBoundary.setOutlineColor(sf::Color(35, 47, 68));
scoreBoundary.setPosition(420, 20);
highScoreBoundary.setSize(sf::Vector2f(120, 60));
highScoreBoundary.setFillColor(sf::Color(200, 220, 255));
highScoreBoundary.setOutlineThickness(4);
highScoreBoundary.setOutlineColor(sf::Color(35, 47, 68));
highScoreBoundary.setPosition(420, 100);
gameOverText.setFont(font);
gameOverText.setString("Game Over!");
gameOverText.setCharacterSize(48);
gameOverText.setFillColor(sf::Color(35, 47, 68));
gameOverText.setPosition(200, 400);
playAgainText.setFont(font);
playAgainText.setString("Press R to Restart");
playAgainText.setCharacterSize(32);
playAgainText.setFillColor(sf::Color(35, 47, 68));
playAgainText.setPosition(150, 460);
reset();
}
void Game::reset() {
gameOver = false;
timer = 0;
delay = 0.3;
score.update(0);
field.clear();
tetromino.generateNext();
}
void Game::processEvents() {
sf::Event event;
while (window.pollEvent(event)) {
if (event.type == sf::Event::Closed) {
window.close();
}
if (event.type == sf::Event::KeyPressed) {
if (event.key.code == sf::Keyboard::Up) {
tetromino.rotate();
if (!tetromino.checkCollision(field.field)) {
for (int i = 0; i < 4; i++) {
tetromino.a[i] = tetromino.b[i];
}
}
} else if (event.key.code == sf::Keyboard::Left) {
tetromino.move(-1);
if (!tetromino.checkCollision(field.field)) {
for (int i = 0; i < 4; i++) {
tetromino.a[i] = tetromino.b[i];
}
}
} else if (event.key.code == sf::Keyboard::Right) {
tetromino.move(1);
if (!tetromino.checkCollision(field.field)) {
for (int i = 0; i < 4; i++) {
tetromino.a[i] = tetromino.b[i];
}
}
} else if (event.key.code == sf::Keyboard::Down) {
tetromino.drop();
} else if (event.key.code == sf::Keyboard::R && gameOver) {
reset();
}
}
}
}
void Game::update() {
float time = clock.restart().asSeconds();
timer += time;
if (timer > delay) {
tetromino.drop();
if (!tetromino.checkCollision(field.field)) {
for (int i = 0; i < 4; i++) {
tetromino.a[i] = tetromino.b[i];
}
field.placeTetromino(tetromino);
field.checkLines(score.score);
tetromino.generateNext();
if (!tetromino.checkCollision(field.field)) {
gameOver = true;
}
}
timer = 0;
}
}
void Game::render() {
window.clear();
window.draw(background);
window.draw(playfieldBoundary);
window.draw(nextTempoBoundary);
window.draw(scoreBoundary);
window.draw(highScoreBoundary);
for (int i = 0; i < 23; i++) {
for (int j = 0; j < 10; j++) {
if (field.field[i][j] > 0) {
s.setTextureRect(sf::IntRect(field.field[i][j] * 18, 0, 18, 18));
s.setPosition(j * 18, i * 18);
s.move(28, 31);
window.draw(s);
}
}
}
for (int i = 0; i < 4; i++) {
s.setTextureRect(sf::IntRect(tetromino.colorNum * 18, 0, 18, 18));
s.setPosition(tetromino.a[i].x * 18, tetromino.a[i].y * 18);
s.move(28, 31);
window.draw(s);
}
score.draw(window, font);
if (gameOver) {
window.draw(gameOverText);
window.draw(playAgainText);
}
window.display();
}
void Game::run() {
while (window.isOpen()) {
processEvents();
if (!gameOver) {
update();
}
render();
}
}
```
### 9. `main.cpp`
This file will contain the `main` function that initializes and runs the game.
```cpp
#include "Game.h"
int main() {
Game game;
game.run();
return 0;
}
```
### Project Structure
```
/Project
|-- Tetromino.h
|-- Tetromino.cpp
|-- Field.h
|-- Field.cpp
|-- Score.h
|-- Score.cpp
|-- Game.h
|-- Game.cpp
|-- main.cpp
|-- images/
|-- tiles.png
|-- background.png
|-- frame.png
|-- Roboto-Regular.ttf
```
### Compilation
To compile the project, you can use a command like:
```bash
g++ -std=c++17 main.cpp Game.cpp Tetromino.cpp Field.cpp Score.cpp -o tetris -lsfml-graphics -lsfml-window -lsfml-system
```
This structure allows each component of your Tetris game to be organized separately, making it easier to manage and extend the project.Editor is loading...
Leave a Comment