Untitled
unknown
plain_text
a year ago
6.7 kB
13
Indexable
int main() {
// Create the main window
sf::RenderWindow window(sf::VideoMode(CELL_SIZE * MAP_WIDTH * SCREEN_RESIZE, (FONT_HEIGHT + CELL_SIZE * MAP_HEIGHT) * SCREEN_RESIZE), "Pac-Man", sf::Style::Close);
window.setView(sf::View(sf::FloatRect(0, 0, CELL_SIZE * MAP_WIDTH, FONT_HEIGHT + CELL_SIZE * MAP_HEIGHT)));
// Create the menu
Menu menu(window.getSize().x, window.getSize().y);
bool inMenu = true;
bool game_won = 0;
unsigned lag = 0;
unsigned char level = 0;
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;
std::array<Position, 4> ghost_positions;
sf::Event event;
GhostManager ghost_manager;
Pacman pacman;
srand(static_cast<unsigned>(time(0)));
map = convert_sketch(map_sketch, ghost_positions, pacman, 0);
ghost_manager.reset(level, ghost_positions);
auto previous_time = std::chrono::steady_clock::now();
while (window.isOpen()) {
while (window.pollEvent(event)) {
if (event.type == sf::Event::Closed) {
window.close();
}
if (inMenu) {
if (event.type == sf::Event::KeyPressed) {
if (event.key.code == sf::Keyboard::Up) {
menu.MoveUp();
} else if (event.key.code == sf::Keyboard::Down) {
menu.MoveDown();
} else if (event.key.code == sf::Keyboard::Return) {
int selectedItem = menu.GetPressedItem();
if (selectedItem == 0) {
inMenu = false; // Start the game
} else if (selectedItem == 1) {
// Show tutorial
std::cout << "Tutorial: Use arrow keys to move Pac-Man. Use the mouse to modify the maze." << std::endl;
} else if (selectedItem == 2) {
window.close(); // Quit the game
}
}
}
} else {
// Handle game events and logic
if (event.type == sf::Event::Closed) {
window.close();
}
}
}
if (!inMenu) {
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 (FRAME_DURATION <= lag) {
lag -= FRAME_DURATION;
while (window.pollEvent(event)) {
switch (event.type) {
case sf::Event::Closed: {
window.close();
break;
}
}
}
if (!game_won && !pacman.get_dead()) {
game_won = 1;
pacman.update(level, map);
ghost_manager.update(level, map, pacman);
for (const std::array<Cell, MAP_HEIGHT>& column : map) {
for (const Cell& cell : column) {
if (Cell::Pellet == cell) {
game_won = 0;
break;
}
}
if (!game_won) break;
}
if (game_won) pacman.set_animation_timer(0);
} else if (sf::Keyboard::isKeyPressed(sf::Keyboard::Return)) {
game_won = 0;
if (pacman.get_dead()) level = 0;
else level++;
map = convert_sketch(map_sketch, ghost_positions, pacman, 0);
ghost_manager.reset(level, ghost_positions);
pacman.reset();
}
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);
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);
newRow[col] = '*';
map_sketch.at(row).assign(newRow);
map = convert_sketch(map_sketch, ghost_positions, pacman);
}
if (!game_won && !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 (pacman.get_animation_over()) {
if (game_won) {
draw_text(1, 0, 0, "Next level!", window);
} else {
draw_text(1, 0, 0, "Game over", window);
}
}
window.display();
}
}
} else {
window.clear();
menu.draw(window);
window.display();
}
}
return 0;
}
Editor is loading...
Leave a Comment