Fork Copy #include #include #include #include #include #include #include #include const int SCREEN_WIDTH = 800; const int SCREEN_HEIGHT = 600; const int ROAD_WIDTH = 400; const int CAR_WIDTH = 50; const int CAR_HEIGHT = 80; const int OBSTACLE_WIDTH = 50; const int OBSTACLE_HEIGHT = 50; const int SCROLL_SPEED = 3; class Car { public: sf::Sprite sprite; Car(sf::Texture& texture) { sprite.setTexture(texture); sprite.setPosition(SCREEN_WIDTH / 2 - CAR_WIDTH / 2, SCREEN_HEIGHT - CAR_HEIGHT - 20); sprite.setScale(CAR_WIDTH / texture.getSize().x, CAR_HEIGHT / texture.getSize().y); } void moveLeft() { if (sprite.getPosition().x > (SCREEN_WIDTH - ROAD_WIDTH) / 2) { sprite.move(-10, 0); } } void moveRight() { if (sprite.getPosition().x < (SCREEN_WIDTH - ROAD_WIDTH) / 2 + ROAD_WIDTH - CAR_WIDTH) { sprite.move(10, 0); } } sf::FloatRect getBounds() const { return sprite.getGlobalBounds(); } }; class Obstacle { public: sf::Sprite sprite; bool passed; Obstacle(sf::Texture& texture, float xPos) : passed(false) { sprite.setTexture(texture); sprite.setPosition(xPos, -OBSTACLE_HEIGHT); sprite.setScale(OBSTACLE_WIDTH / texture.getSize().x, OBSTACLE_HEIGHT / texture.getSize().y); } void update() { sprite.move(0, SCROLL_SPEED); } bool isOffScreen() const { return sprite.getPosition().y > SCREEN_HEIGHT; } sf::FloatRect getBounds() const { return sprite.getGlobalBounds(); } }; int main() { srand(static_cast(time(NULL))); // Tạo cửa sổ sf::RenderWindow window(sf::VideoMode(SCREEN_WIDTH, SCREEN_HEIGHT), "Simple Racing Game"); window.setFramerateLimit(60); // Load textures sf::Texture carTexture, obstacleTexture; if (!carTexture.loadFromFile("C: /SCS/Test/x64/Debug/car.png") || !obstacleTexture.loadFromFile("C: /SCS/Test/x64/Debug/obstacle.png")) { std::cerr << "Failed to load textures!" << std::endl; return -1; } // Load font sf::Font font; if (!font.loadFromFile("C: /SCS/Test/x64/Debug/arial.ttf")) { std::cerr << "Failed to load font!" << std::endl; return -1; } // Tạo đối tượng Car playerCar(carTexture); std::vector obstacles; int score = 0; sf::Clock obstacleClock; // Text hiển thị điểm sf::Text scoreText; scoreText.setFont(font); scoreText.setCharacterSize(24); scoreText.setFillColor(sf::Color::White); scoreText.setPosition(10, 10); // Vòng lặp game chính while (window.isOpen()) { sf::Event event; while (window.pollEvent(event)) { if (event.type == sf::Event::Closed) { window.close(); } // Xử lý input if (event.type == sf::Event::KeyPressed) { if (event.key.code == sf::Keyboard::Left) { playerCar.moveLeft(); } else if (event.key.code == sf::Keyboard::Right) { playerCar.moveRight(); } else if (event.key.code == sf::Keyboard::Escape) { window.close(); } } } // Tạo vật cản mới if (obstacleClock.getElapsedTime().asSeconds() > 1.5f) { int roadLeft = (SCREEN_WIDTH - ROAD_WIDTH) / 2; float obstacleX = roadLeft + (rand() % (ROAD_WIDTH - OBSTACLE_WIDTH)); obstacles.emplace_back(obstacleTexture, obstacleX); obstacleClock.restart(); } // Cập nhật vật cản for (auto& obstacle : obstacles) { obstacle.update(); // Kiểm tra va chạm if (!obstacle.passed && playerCar.getBounds().intersects(obstacle.getBounds())) { std::cout << "Game Over! Score: " << score << std::endl; window.close(); } // Tăng điểm khi vượt qua if (!obstacle.passed && obstacle.sprite.getPosition().y > playerCar.sprite.getPosition().y + CAR_HEIGHT) { obstacle.passed = true; score++; } } // Xóa vật cản đã ra khỏi màn hình obstacles.erase(std::remove_if(obstacles.begin(), obstacles.end(), [](const Obstacle& o) { return o.isOffScreen(); }), obstacles.end()); // Cập nhật điểm std::ostringstream ss; ss << "Score: " << score; scoreText.setString(ss.str()); // Vẽ window.clear(sf::Color(0x33, 0x33, 0x33)); // Vẽ đường đua sf::RectangleShape road(sf::Vector2f(ROAD_WIDTH, SCREEN_HEIGHT)); road.setPosition((SCREEN_WIDTH - ROAD_WIDTH) / 2, 0); road.setFillColor(sf::Color(0x80, 0x80, 0x80)); window.draw(road); // Vẽ vạch kẻ đường sf::RectangleShape line(sf::Vector2f(10, 20)); line.setFillColor(sf::Color::White); for (int i = 0; i < SCREEN_HEIGHT; i += 40) { line.setPosition(SCREEN_WIDTH / 2 - 5, i); window.draw(line); } // Vẽ vật cản for (const auto& obstacle : obstacles) { window.draw(obstacle.sprite); } // Vẽ xe window.draw(playerCar.sprite); // Vẽ điểm window.draw(scoreText); window.display(); } return 0; }