-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgame_state.py
More file actions
72 lines (53 loc) · 2 KB
/
Copy pathgame_state.py
File metadata and controls
72 lines (53 loc) · 2 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
###############################################################################
#
# game_state.py
#
###############################################################################
from common import *
import level_parser, rect
#------------------------------------------------------------------------------
#
# State class
#
# This class holds the entire state of the game, including the
# rects for the agents, the level and all of its attributes,
# and agent attributes.
#
#------------------------------------------------------------------------------
class State:
#------------------------------------------------------------------------------
# __init__
#------------------------------------------------------------------------------
def __init__(self, filename=None):
# Create a level object
if filename:
self.level = level_parser.Level(filename)
else:
self.level = level_parser.Level()
# Create data structures to keep track of ghost attributes
self.ghost_rect = {}
self.ghost_mode = {}
self.ghost_timer = {}
self.current_ghost_direction = {}
self.next_ghost_direction = {}
# Initialize attributes for Pac-Man
self.pacman_lives = 3
self.score = 0
self.level_number = 0
# Initially, all of the agents are immobile
self.pacman_immobile = True
self.ghost_immobile = [True] * GHOSTS
#------------------------------------------------------------------------------
# load_level()
#------------------------------------------------------------------------------
def load_level(self, filename, tile_size):
"Loads the level that corresponds to the given filename and initializes the rects"
# Parse the level file
self.level.parse_file(filename)
# Create a rect for Pac-Man
self.pacman_rect = rect.Rect(self.level.pacman_home, (tile_size, tile_size))
# Total number of pellets originally for this level
self.total_pellets = self.level.num_pellets
# Create the ghost rects
for i in range(GHOSTS):
self.ghost_rect[i] = rect.Rect(self.level.ghost_home[i], (tile_size, tile_size))