Untitled
#include <array> #include <chrono> #include <ctime> #include <SFML/Graphics.hpp> #include <iostream> #include "Global.h" #include "DrawText.h" #include "Pacman.h" #include "Ghost.h" #include "GhostManager.h" #include "ConvertSketch.h" #include "DrawMap.h" #include "MapCollision.h" int main() { //Is the game won? bool game_won = 0; //Used to make the game framerate-independent. unsigned lag = 0; unsigned char level = 0; //Similar to lag, used to make the game framerate-independent. //std::chrono::time_point<std::chrono::steady_clock> previous_time; //It's not exactly like the map from the original Pac-Man game, but it's close enough. //std::array<std::string, MAP_HEIGHT> map_sketch = { // " ################### ", // " #........#........# ", // " #o##.###.#.###.##o# ", // " #.................# ", // " #.##.#.#####.#.##.# ", // " #....#...#...#....# ", // " ####.### # ###.#### ", // " #....# 0 #.# ", // "#####.# ##=## #.#####", // " . #123# . ", // "#####.# ##### #.#####", // " #.# #.# ", // " ####.# ##### #.#### ", // " #........#........# ", // " #.##.###.#.###.##.# ", // " #o.#.....P.....#.o# ", // " ##.#.#.#####.#.#.## ", // " #....#...#...#....# ", // " #.######.#.######.# ", // " #.................# ", // " ################### " //}; std::array<std::string, MAP_HEIGHT> map_sketch; freopen("map.txt","r",stdin); for (int i = 0; i < MAP_HEIGHT; ++i) { std::string s; std::cin >> s; std::cout << s << "\n"; map_sketch.at(i).assign(s); } std::array<std::array<Cell, MAP_HEIGHT>, MAP_WIDTH> map; //Initial ghost positions. std::array<Position, 4> ghost_positions; //SFML thing. Stores events, I think. sf::Event event; sf::RenderWindow window(sf::VideoMode(CELL_SIZE * MAP_WIDTH * SCREEN_RESIZE, (FONT_HEIGHT + CELL_SIZE * MAP_HEIGHT) * SCREEN_RESIZE), "Pac-Man", sf::Style::Close); //Resizing the window. window.setView(sf::View(sf::FloatRect(0, 0, CELL_SIZE * MAP_WIDTH, FONT_HEIGHT + CELL_SIZE * MAP_HEIGHT))); GhostManager ghost_manager; Pacman pacman; //Generating a random seed. srand(static_cast<unsigned>(time(0))); map = convert_sketch(map_sketch, ghost_positions, pacman, 0); ghost_manager.reset(level, ghost_positions); //Get the current time and store it in a variable. auto previous_time = std::chrono::steady_clock::now(); while (1 == window.isOpen()) { //Here we're calculating the lag. unsigned delta_time = std::chrono::duration_cast<std::chrono::microseconds>(std::chrono::steady_clock::now() - previous_time).count(); lag += delta_time; previous_time += std::chrono::microseconds(delta_time); //While the lag exceeds the maximum allowed frame duration. while (FRAME_DURATION <= lag) { //We decrease the lag. lag -= FRAME_DURATION; while (1 == window.pollEvent(event)) { switch (event.type) { case sf::Event::Closed: { //Making sure the player can close the window. window.close(); } } } if (0 == game_won && 0 == pacman.get_dead()) { game_won = 1; pacman.update(level, map); ghost_manager.update(level, map, pacman); //We're checking every cell in the map. for (const std::array<Cell, MAP_HEIGHT>& column : map) { for (const Cell& cell : column) { if (Cell::Pellet == cell) //And if at least one of them has a pellet. { game_won = 0; //The game is not yet won. break; } } if (0 == game_won) { break; } } if (1 == game_won) { pacman.set_animation_timer(0); } } else if (1 == sf::Keyboard::isKeyPressed(sf::Keyboard::Return)) //Restarting the game. { game_won = 0; if (1 == pacman.get_dead()) { level = 0; } else { //After each win we reduce the duration of attack waves and energizers. level++; } map = convert_sketch(map_sketch, ghost_positions, pacman, 0); ghost_manager.reset(level, ghost_positions); pacman.reset(); } //I don't think anything needs to be explained here. if (FRAME_DURATION > lag) { window.clear(); if (sf::Mouse::isButtonPressed(sf::Mouse::Button::Left)) { sf::Vector2i mousePos = sf::Mouse::getPosition(window); int row = std::floor(mousePos.y / SCREEN_RESIZE / CELL_SIZE); int col = std::floor(mousePos.x / SCREEN_RESIZE / CELL_SIZE); if (row < 0 || row >= MAP_HEIGHT || col < 0 || col >= MAP_WIDTH) continue; std::string newRow = map_sketch.at(row); std::cout <<mousePos.x<<" "<<mousePos.y<<" "<< row << " " << col << " "<<newRow << "\n";// << newRow << "\n"; newRow[col] = '#'; map_sketch.at(row).assign(newRow); map = convert_sketch(map_sketch, ghost_positions, pacman); } if (sf::Mouse::isButtonPressed(sf::Mouse::Button::Right)) { sf::Vector2i mousePos = sf::Mouse::getPosition(window); int row = std::floor(mousePos.y / SCREEN_RESIZE / CELL_SIZE); int col = std::floor(mousePos.x / SCREEN_RESIZE / CELL_SIZE); if (row < 0 || row >= MAP_HEIGHT || col < 0 || col >= MAP_WIDTH) continue; std::string newRow = map_sketch.at(row); std::cout <<mousePos.x<<" "<<mousePos.y<<" "<< row << " " << col << " "<<newRow << "\n";// << newRow << "\n"; newRow[col] = '*'; map_sketch.at(row).assign(newRow); map = convert_sketch(map_sketch, ghost_positions, pacman); } if (0 == game_won && 0 == pacman.get_dead()) { draw_map(map, window); ghost_manager.draw(GHOST_FLASH_START >= pacman.get_energizer_timer(), window); draw_text(0, 0, CELL_SIZE * MAP_HEIGHT, "Level: " + std::to_string(1 + level), window); } pacman.draw(game_won, window); if (1 == pacman.get_animation_over()) { if (1 == game_won) { draw_text(1, 0, 0, "Next level!", window); } else { draw_text(1, 0, 0, "Game over", window); } } window.display(); } } } }
Leave a Comment