blob: 9d937869ae1d965e101c9af5001ffdf36fb863a2 (
plain)
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
|
import Player
from Direction import DIRECTION
import settings as Settings
import map as Map
import pygame
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)
# Set the circle's velocity
dx = 0
dy = 0
# counter used to cycle through pacman sprite animation
counter = 0
clock = pygame.time.Clock()
sprite_width, sprite_height = 32, 32
map = Map.Map()
grid_x = Settings.settings.width // len(map.maze[0])
grid_y = (Settings.settings.height - 50) // len(map.maze)
# Checks collision with walls
def check_collision(dx, dy):
print(grid_x, grid_y)
x = int((player.x + dx) / grid_x)
y = int((player.y + dy) / grid_y)
print(x, y)
print(map.maze[x][y])
is_dot = map.maze[x][y] == Map.D
is_big_dot = map.maze[x][y] == Map.BD
is_free = map.maze[x][y] == 0
return not (is_dot or is_free or is_big_dot)
# 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
# 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
dy = -player.speed
dx = 0 # Necssarry to move only horizontal or vertical
elif event.key == pygame.K_s:
player.direction = DIRECTION.DOWN
dy = player.speed
dx = 0 # Necssarry to move only horizontal or vertical
elif event.key == pygame.K_a:
player.direction = DIRECTION.LEFT
dx = -player.speed
dy = 0 # Necssarry to move only horizontal or vertical
elif event.key == pygame.K_d:
player.direction = DIRECTION.RIGHT
dx = player.speed
dy = 0 # Necssarry to move only horizontal or vertical
# Update the circle's position and checking for collisions
if not check_collision(dx, dy):
player.x += dx
player.y += dy
screen.fill((0, 0, 0)) # Clear the screen
map.draw_map(screen)
player.draw(screen, counter)
# Update the screen
pygame.display.flip()
# Quit Pygame
pygame.quit()
|