Files
PyGame-Pacman/actors/pacman.py

86 lines
2.8 KiB
Python

from .enums import PlayerDirection, Colors
from maze import Maze
import pygame
import math
class ActorPacman:
def __init__(self, scr, center, start_location = (0, 0), maze = None):
self.screen = scr
self.direction = PlayerDirection.DirectionRight
self.speed = 2
self.x, self.y = center
self.radius = 12
self.mouth_angle_deg = 45
self.min_mouth_deg = 5
self.max_mouth_deg = 45
self.animate_speed = 2
self.mouth_closing = False
self.location = list(start_location)
self.maze = maze
def update(self):
self.animate()
def animate(self):
if self.mouth_closing:
self.mouth_angle_deg -= self.animate_speed
if self.mouth_angle_deg <= self.min_mouth_deg:
self.mouth_angle_deg = self.min_mouth_deg
self.mouth_closing = False
else:
self.mouth_angle_deg += self.animate_speed
if self.mouth_angle_deg >= self.max_mouth_deg:
self.mouth_angle_deg = self.max_mouth_deg
self.mouth_closing = True
#self.x = max(self.radius, min(self.x, 400 - self.radius))
#self.y = max(self.radius, min(self.y, 400 - self.radius))
def draw(self):
mouth_angle = math.radians(self.mouth_angle_deg)
center = (self.x, self.y)
rotation_offset = math.radians(self.direction.value)
start_angle = mouth_angle / 2 + rotation_offset
end_angle = 2 * math.pi - mouth_angle / 2 + rotation_offset
points = [center]
angle = start_angle
while angle <= end_angle:
x = center[0] + self.radius * math.cos(angle)
y = center[1] + self.radius * math.sin(angle)
points.append((x, y))
angle += math.radians(1)
pygame.draw.polygon(self.screen, Colors.Yellow.value, points)
def move_right(self):
for _ in range(16):
self.x += self.speed
self.location[0] += 1
if self.location[0] > 28:
self.location[0] = 0
self.x = 16
def move_left(self):
for _ in range(16):
self.x -= self.speed
self.location[0] -= 1
if self.location[0] < 0:
self.location[0] = 28
self.x = (32 * 29) - 16
def move_up(self):
for _ in range(16):
self.y -= self.speed
self.location[1] -= 1
if self.location[1] < 0:
self.location[1] = 32
self.y = (32 * 30) + 48
def move_down(self):
for _ in range(16):
self.y += self.speed
self.location[1] += 1
if self.location[1] > 32:
self.location[1] = 0
self.y = 48
def set_direction(self, dir: PlayerDirection):
self.direction = dir