-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
79 lines (67 loc) · 2.68 KB
/
main.cpp
File metadata and controls
79 lines (67 loc) · 2.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#include <SFML/Graphics.hpp>
#include "PacMan.hpp"
#include "Ghost.hpp"
#include "Level.hpp"
#include "GameUI.hpp"
#include "WorldObjects.hpp"
#include "Constants.hpp"
#include <iostream>
#include <vector>
#include <cstdlib>
#include <ctime>
#include <cmath>
int main() {
Level level;
GameUI gameUI;
WorldObjects worldObjects(Constants::CELL_SIZE, level.getWalls());
sf::RenderWindow window(sf::VideoMode(Constants::WINDOW_WIDTH * Constants::CELL_SIZE, Constants::WINDOW_HEIGHT * Constants::CELL_SIZE), "Pac-Man with Ghosts");
const float cellSize = Constants::CELL_SIZE;
sf::Vector2i pacManSpawnCell = level.getPacManSpawn();
sf::Vector2f pacManSpawn(pacManSpawnCell.x * cellSize + cellSize / 2.0f, pacManSpawnCell.y * cellSize + cellSize / 2.0f);
std::cout << "Pac-Man spawn position: (" << pacManSpawn.x << ", " << pacManSpawn.y << ")\n";
PacMan pacman(pacManSpawn.x, pacManSpawn.y);
std::vector<Ghost> ghosts;
for (const auto& ghostSpawnCell : level.getGhostSpawns()) {
sf::Vector2f ghostSpawn(ghostSpawnCell.x * cellSize + cellSize / 2.0f, ghostSpawnCell.y * cellSize + cellSize / 2.0f);
std::cout << "Ghost spawn position: (" << ghostSpawn.x << ", " << ghostSpawn.y << ")\n";
ghosts.emplace_back(ghostSpawn.x, ghostSpawn.y);
}
int score = 0;
bool isPaused = false;
sf::Clock clock;
while (window.isOpen()) {
sf::Event event;
while (window.pollEvent(event)) {
if (event.type == sf::Event::Closed) {
window.close();
}
if (event.type == sf::Event::MouseButtonPressed && event.mouseButton.button == sf::Mouse::Left) {
sf::Vector2i mousePosition = sf::Mouse::getPosition(window);
if (gameUI.isMenuButtonClicked(mousePosition)) {
isPaused = !isPaused;
}
}
}
if (!isPaused) {
pacman.handleInput();
float deltaTime = clock.restart().asSeconds();
pacman.update(deltaTime, level.getWalls());
for (auto& ghost : ghosts) {
ghost.update(deltaTime, pacman.getPosition(), level.getWalls()); // Передача координат Pac-Man
}
worldObjects.update(pacman.getPosition(), score);
} else {
clock.restart(); // Чтобы время не шло на паузе
}
window.clear();
level.render(window);
worldObjects.render(window);
gameUI.render(window, score, isPaused);
pacman.render(window);
for (auto& ghost : ghosts) {
ghost.render(window);
}
window.display();
}
return 0;
}