Untitled
unknown
plain_text
a year ago
3.3 kB
15
Indexable
#include "Game.h"
#include <iostream>
Game::Game() : window(sf::VideoMode(720, 960), "The Game!"), timer(0), delay(0.3f), gameOver(false) {
// Load textures and fonts
t1.loadFromFile("images/tiles.png");
t2.loadFromFile("images/background.png");
t3.loadFromFile("images/frame.png");
font.loadFromFile("Roboto-Regular.ttf");
s.setTexture(t1);
s.setScale(2, 2);
frame.setTexture(t3);
background.setSize(sf::Vector2f(720, 960));
background.setFillColor(sf::Color(200, 220, 255, 180));
// Initialize text objects
// ... (Initialize all your texts here, similar to how you did in main)
resetGame();
}
void Game::resetGame() {
std::fill(&field[0][0], &field[0][0] + 23 * 10, 0);
currentTetromino.generateNewTetromino();
nextTetromino.generateNewTetromino(); // Khởi tạo tetromino tiếp theo
score.reset();
gameOver = false;
timer = 0;
}
void Game::run() {
while (window.isOpen()) {
processEvents();
update();
render();
}
}
void Game::processEvents() {
sf::Event event;
while (window.pollEvent(event)) {
if (event.type == sf::Event::Closed)
window.close();
if (gameOver && event.type == sf::Event::KeyPressed && event.key.code == sf::Keyboard::Space) {
resetGame();
}
// Handle other events like movement, rotation, etc.
}
}
void Game::update() {
if (gameOver) return;
float time = clock.getElapsedTime().asSeconds();
clock.restart();
timer += time;
// Handle game logic (movement, collision, line clearing)
// ...
if (timer > delay) {
// Di chuyển tetromino xuống dưới
// Kiểm tra va chạm, đặt tetromino, tạo tetromino mới, v.v.
if (/* Nếu currentTetromino chạm đáy hoặc va chạm */) {
for (const auto& p : currentTetromino.getBlockPositions()) {
field[p.y][p.x] = currentTetromino.getColorNum();
}
currentTetromino = nextTetromino; // Đổi tetromino tiếp theo thành tetromino hiện tại
nextTetromino.generateNewTetromino(); // Tạo tetromino tiếp theo mới
// Kiểm tra xem có hàng nào đầy không
checkLines();
}
timer = 0;
}
if (score.getScore() > score.getHighScore()) {
score.setHighScore(score.getScore());
}
}
void Game::render() {
window.clear(sf::Color::White);
window.draw(background);
// Vẽ tetromino hiện tại
for (const auto& p : currentTetromino.getBlockPositions()) {
s.setTextureRect(sf::IntRect(currentTetromino.getColorNum() * 18, 0, 18, 18));
s.setPosition(p.x * 36, p.y * 36);
s.move(28, 31); // Dịch chuyển một chút để vẽ khung hình
window.draw(s);
}
// Vẽ tetromino tiếp theo
for (const auto& p : nextTetromino.getBlockPositions()) {
s.setTextureRect(sf::IntRect(nextTetromino.getColorNum() * 18, 0, 18, 18));
s.setPosition(p.x * 36, p.y * 36);
s.move(350, 80); // Chỉnh sửa vị trí để vẽ trong khu vực "Next"
window.draw(s);
}
// Vẽ các phần tử khác của game: khung, text, score, v.v.
// ...
window.display();
}Editor is loading...
Leave a Comment