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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
|
from pygame.mouse import get_pressed
import Player
import Ghost
from Direction import DIRECTION
import settings as Settings
import map as Map
import pygame
import math
class Game():
def __init__(self):
self.settings = Settings.settings
def init(self):
# Initialize Pygame
pygame.init()
# Set the dimensions of the window
screen = pygame.display.set_mode(
(Settings.settings.width, Settings.settings.height))
# Sprite sheet for pacman
sprite_sheet = pygame.image.load(
'../assets/pacman_left_sprite.png').convert_alpha()
player = Player.Player(sprite_sheet)
blinky = Ghost.Ghost("red", 75, 75)
pinky = Ghost.Ghost("pink", 27 * 30, 30 * 30 + 15)
inky = Ghost.Ghost("orange", 75, 30 * 30 + 15)
clyde = Ghost.Ghost("cyan", 27 * 30 + 15, 75)
# Set the pacman velocity
dx = 0
dy = 0
# counter used to cycle through pacman sprite animation
counter = 0
clock = pygame.time.Clock()
maze = Map.Map()
# length of the map grid size
grid_x = Settings.settings.width // len(maze.maze[0])
grid_y = Settings.settings.height // len(maze.maze)
print(grid_x, grid_y)
# Checks collision with walls
# checks if the current position of pacman is either a dot, big dot or free
def is_valid(x, y):
is_dot = maze.maze[y][x] == Map.D
is_big_dot = maze.maze[y][x] == Map.BD
is_free = maze.maze[y][x] == 0
if is_dot or is_big_dot:
maze.maze[y][x] = 0
return (is_dot or is_free or is_big_dot)
# checks collision with pacman and obstacles returns false if there is a collision and true otherwise
def check_collision(dx, dy):
direct_x = [1, 0, -1, 0, 1, 1, -1, -1]
direct_y = [0, 1, 0, -1, -1, 1, -1, 1]
for i in range(len(direct_x)):
nx = (player.x + dx) + direct_x[i] * 14
ny = (player.y + dy) + direct_y[i] * 14
x = nx // grid_x
y = ny // grid_y
if not is_valid(x, y):
return False
return True
# Main game loop
running = True
while running:
# setting game fps
clock.tick(Settings.settings.fps)
# counter logic for cycling between pacman different sprites
if counter < 19:
counter += 1
else:
counter = 0
screen.fill((0, 0, 0)) # Clear the screen
# Temporary values for delta_x and delta_y in the position of pacman
tx = dx
ty = dy
# Handling events
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
elif event.type == pygame.KEYDOWN:
# Move the circle based on the pressed key
if event.key == pygame.K_w:
player.direction = DIRECTION.UP
ty = -player.speed
tx = 0 # Necssarry to move only horizontal or vertical
elif event.key == pygame.K_s:
player.direction = DIRECTION.DOWN
ty = player.speed
tx = 0 # Necssarry to move only horizontal or vertical
elif event.key == pygame.K_a:
player.direction = DIRECTION.LEFT
tx = -player.speed
ty = 0 # Necssarry to move only horizontal or vertical
elif event.key == pygame.K_d:
player.direction = DIRECTION.RIGHT
tx = player.speed
ty = 0 # Necssarry to move only horizontal or vertical
keys = pygame.key.get_pressed()
if keys[pygame.K_w]:
ty = -player.speed
tx = 0
elif keys[pygame.K_s]:
ty = player.speed
tx = 0
elif keys[pygame.K_a]:
tx = -player.speed
ty = 0
elif keys[pygame.K_d]:
tx = player.speed
ty = 0
# if tx and ty doesn't lead to colliding change the current dx and dy to them and other wise
# let pacman move in his previous direction
if check_collision(tx, ty):
dx = tx
dy = ty
if dx < 0:
player.direction = DIRECTION.LEFT
elif dx > 0:
player.direction = DIRECTION.RIGHT
elif dy < 0:
player.direction = DIRECTION.UP
elif dy > 0:
player.direction = DIRECTION.DOWN
if check_collision(dx, dy):
player.x += dx
player.y += dy
blinky.move(maze.maze, (player.x, player.y))
pinky.move(maze.maze, (player.x, player.y))
inky.move(maze.maze, (player.x, player.y))
clyde.move(maze.maze, (player.x, player.y))
maze.draw_map(screen)
player.draw(screen, counter)
blinky.draw(screen)
pinky.draw(screen)
inky.draw(screen)
clyde.draw(screen)
# Update the screen
pygame.display.flip()
# Quit Pygame
pygame.quit()
|