data/tools.py (view raw)
1__author__ = 'justinarmstrong'
2
3import os
4import pygame as pg
5import constants as c
6
7class Control(object):
8 """
9 Control class for entire project. Contains the game loop, and contains
10 the event_loop which passes events to States as needed. Logic for flipping
11 states is also found here.
12 """
13 def __init__(self, caption):
14 self.screen = pg.display.get_surface()
15 self.done = False
16 self.clock = pg.time.Clock()
17 self.caption = caption
18 self.fps = 60
19 self.show_fps = False
20 self.current_time = 0.0
21 self.keys = pg.key.get_pressed()
22 self.state_dict = {}
23 self.state_name = None
24 self.state = None
25
26 def setup_states(self, state_dict, start_state):
27 self.state_dict = state_dict
28 self.state_name = start_state
29 self.state = self.state_dict[self.state_name]
30
31 def update(self):
32 self.current_time = pg.time.get_ticks()
33 if self.state.quit:
34 self.done = True
35 elif self.state.done:
36 self.flip_state()
37 self.state.update(self.screen, self.keys, self.current_time)
38
39 def flip_state(self):
40 previous, self.state_name = self.state_name, self.state.next
41 persist = self.state.cleanup()
42 self.state = self.state_dict[self.state_name]
43 self.state.startup(self.current_time, persist)
44 self.state.previous = previous
45
46 def event_loop(self):
47 self.events = pg.event.get()
48
49 for event in self.events:
50 if event.type == pg.QUIT:
51 self.done = True
52 elif event.type == pg.KEYDOWN:
53 self.keys = pg.key.get_pressed()
54 self.toggle_show_fps(event.key)
55 elif event.type == pg.KEYUP:
56 self.keys = pg.key.get_pressed()
57 self.state.get_event(event)
58
59 def toggle_show_fps(self, key):
60 if key == pg.K_F5:
61 self.show_fps = not self.show_fps
62 if not self.show_fps:
63 pg.display.set_caption(self.caption)
64
65 def main(self):
66 """Main loop for entire program"""
67 while not self.done:
68 self.event_loop()
69 self.update()
70 pg.display.update()
71 self.clock.tick(self.fps)
72 if self.show_fps:
73 fps = self.clock.get_fps()
74 with_fps = "{} - {:.2f} FPS".format(self.caption, fps)
75 pg.display.set_caption(with_fps)
76
77
78class _State(object):
79 """Base class for all game states"""
80 def __init__(self, name):
81 self.start_time = 0.0
82 self.current_time = 0.0
83 self.done = False
84 self.quit = False
85 self.next = None
86 self.previous = None
87 self.persist = {}
88 self.name = name
89
90 def get_event(self, event):
91 pass
92
93 def startup(self, current_time, persistant):
94 self.persist = persistant
95 self.start_time = current_time
96
97 def cleanup(self):
98 self.done = False
99 return self.persist
100
101 def update(self, surface, keys, current_time):
102 pass
103
104
105def load_all_gfx(directory, colorkey=(255,0,255), accept=('.png', 'jpg', 'bmp')):
106 graphics = {}
107 for pic in os.listdir(directory):
108 name, ext = os.path.splitext(pic)
109 if ext.lower() in accept:
110 img = pg.image.load(os.path.join(directory, pic))
111 if img.get_alpha():
112 img = img.convert_alpha()
113 else:
114 img = img.convert()
115 img.set_colorkey(colorkey)
116 graphics[name] = img
117 return graphics
118
119
120def load_all_music(directory, accept=('.wav', '.mp3', '.ogg', '.mdi')):
121 songs = {}
122 for song in os.listdir(directory):
123 name, ext = os.path.splitext(song)
124 if ext.lower() in accept:
125 songs[name] = os.path.join(directory, song)
126 return songs
127
128
129def load_all_fonts(directory, accept=('.ttf')):
130 return load_all_music(directory, accept)
131
132
133def load_all_sfx(directory, accept=('.wav','.mp3','.ogg','.mdi')):
134 effects = {}
135 for fx in os.listdir(directory):
136 name, ext = os.path.splitext(fx)
137 if ext.lower() in accept:
138 effects[name] = pg.mixer.Sound(os.path.join(directory, fx))
139 return effects
140
141
142def get_image(x, y, width, height, sprite_sheet):
143 """Extracts image from sprite sheet"""
144 image = pg.Surface([width, height])
145 rect = image.get_rect()
146
147 image.blit(sprite_sheet, (0, 0), (x, y, width, height))
148 image.set_colorkey(c.BLACK)
149
150 return image
151
152
153def create_game_data_dict():
154 """Create a dictionary of persistant values the player
155 carries between states"""
156 data_dict = {'last location': None,
157 'last state': None,
158 'last direction': 'up',
159 'town start pos': [12, 49],
160 'castle start pos': [12, 26],
161 'king item': ['500 gold']}
162
163 return data_dict
164
165
166
167
168
169
170