-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpac_man.py
More file actions
108 lines (95 loc) · 3.45 KB
/
pac_man.py
File metadata and controls
108 lines (95 loc) · 3.45 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
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
"""
Author Paul Brace March 2025
PacMan game developed using arcade
PacMan class and functions
"""
import arcade
from constants import *
import random
# Speed reached at level 6 and above
player_max_speed = 3.66
# Timer used to display packman animation on caught
caught_timer_default = int(FRAME_REFRESH * 1.5)
life_lost = arcade.load_sound('sounds/lifeLost.wav')
pacman_whole = arcade.load_texture('images/pacWhole.png')
pacman_moving = [
arcade.load_texture('images/pacOpenLeft.png'),
arcade.load_texture('images/pacOpenRight.png'),
arcade.load_texture('images/pacOpenUp.png'),
arcade.load_texture('images/pacOpenDown.png')
]
caught_image = [
arcade.load_texture('images/lost1.png'),
arcade.load_texture('images/lost2.png'),
arcade.load_texture('images/lost3.png'),
arcade.load_texture('images/lost4.png'),
arcade.load_texture('images/lost5.png'),
arcade.load_texture('images/lost6.png')
]
class PacMan(arcade.Sprite):
def __init__(self, x, y):
# initialise positioned offset 10 to left to center between bricks
x = x * 20 + 10
y = WINDOW_HEIGHT - (y * 20 + 40)
super().__init__(pacman_whole, 18/20, x, y)
# True when whole image displayed
self.whole = True
self.start_position = (x, y)
self.speed = player_max_speed
self.speed_for_level = player_max_speed
self._caught = False
self.caught_timer = 0
self.frame_count = 0
self.current_direction = HOLD
self.next_direction = HOLD
self.change_direction = False
self.done = False
def set_speed_percent(self, perc):
# Adjust the speed of the player
if 100 >= perc >= 0:
self.speed = player_max_speed * perc / 100
self.speed_for_level = self.speed
def set_caught(self):
# PacMan has been caught so start animation
self._caught = True
self.next_direction = HOLD
life_lost.play(volume=0.15)
self.caught_timer = caught_timer_default
self.texture = pacman_whole
self.whole = True
def caught(self):
return self._caught
def return_to_start(self):
# reposition at start point
self.center_x = self.start_position[0]
self.center_y = self.start_position[1]
self.speed = self.speed_for_level
self.current_direction = HOLD
self.texture = pacman_whole
self.whole = True
self._caught = False
self.done = False
def update(self, delta_time):
#If we have been caught then animate image
if self._caught:
self.caught_timer -= 1
if self.caught_timer <= -6:
self.done = True
elif self.caught_timer % 6 == 0:
self.texture = caught_image[5 - self.caught_timer // 15]
else:
# Set image for current direction
if self.current_direction == HOLD:
self.texture = pacman_whole
else:
self.frame_count += 1
if self.frame_count > 10 or self.change_direction:
self.frame_count = 0
self.change_direction = False
if self.whole:
self.texture = pacman_moving[self.current_direction - 1]
self.whole = False
else:
self.texture = pacman_whole
self.whole = True
super().update(delta_time)