-
Notifications
You must be signed in to change notification settings - Fork 0
/
pacman.py
64 lines (56 loc) · 2.47 KB
/
pacman.py
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
import pygame
from defs import *
class Pacman:
def __init__(self, game_display, maze, s1, s2, s3, s4):
self.game_display = game_display
self.maze = maze
self.radius = (BLOCK_SIZE - 15.4)
self.x = X_OFFSET + BLOCK_SIZE + self.radius
self.y = Y_OFFSET + BLOCK_SIZE + self.radius
self.direction = Direction.right
self.direction_request = Direction.right
self.image = pygame.image.load("png/PacmanSprites.png")
self.selection = pygame.Rect(s1, s2, s3, s4)
def draw(self):
self.game_display.blit(self.image, (self.x-GHOST_RADIUS, self.y-GHOST_RADIUS), self.selection)
self.move()
pac_x = self.x
pac_y = self.y
pac_rad = self.radius
self.maze.is_eaten(pac_x, pac_y, pac_rad)
def move(self):
possible_directions = self.get_possible_directions()
for i in possible_directions:
if i == self.direction_request:
self.direction = self.direction_request
self.move_common()
def move_common(self):
new_x = self.x
new_y = self.y
if self.direction == Direction.left:
new_x = self.x - STEP
if self.direction == Direction.right:
new_x = self.x + STEP
if self.direction == Direction.up:
new_y = self.y - STEP
if self.direction == Direction.down:
new_y = self.y + STEP
if (False == self.maze.is_colliding(new_x, new_y, self.radius, self.direction)):
self.x = new_x
self.y = new_y
else:
self.x = self.x
self.y = self.y
def get_possible_directions(self):
possible_directions = []
if (False == self.maze.is_colliding(self.x - STEP, self.y, self.radius, Direction.left)):
possible_directions.append(Direction.left)
if (False == self.maze.is_colliding(self.x + STEP, self.y, self.radius, Direction.right)):
possible_directions.append(Direction.right)
if (False == self.maze.is_colliding(self.x, self.y - STEP, self.radius, Direction.up)):
possible_directions.append(Direction.up)
if (False == self.maze.is_colliding(self.x, self.y + STEP, self.radius, Direction.down)):
possible_directions.append(Direction.down)
return possible_directions
def set_direction_request(self, direction):
self.direction_request = direction