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 for event in pg.event.get():
48 if event.type == pg.QUIT:
49 self.done = True
50 elif event.type == pg.KEYDOWN:
51 self.keys = pg.key.get_pressed()
52 self.toggle_show_fps(event.key)
53 elif event.type == pg.KEYUP:
54 self.keys = pg.key.get_pressed()
55 self.state.get_event(event)
56
57 def toggle_show_fps(self, key):
58 if key == pg.K_F5:
59 self.show_fps = not self.show_fps
60 if not self.show_fps:
61 pg.display.set_caption(self.caption)
62
63 def main(self):
64 """Main loop for entire program"""
65 while not self.done:
66 self.event_loop()
67 self.update()
68 pg.display.update()
69 self.clock.tick(self.fps)
70 if self.show_fps:
71 fps = self.clock.get_fps()
72 with_fps = "{} - {:.2f} FPS".format(self.caption, fps)
73 pg.display.set_caption(with_fps)
74
75
76class _State(object):
77 """Base class for all game states"""
78 def __init__(self):
79 self.start_time = 0.0
80 self.current_time = 0.0
81 self.done = False
82 self.quit = False
83 self.next = None
84 self.previous = None
85 self.persist = {}
86
87 def get_event(self, event):
88 pass
89
90 def startup(self, current_time, persistant):
91 self.persist = persistant
92 self.start_time = current_time
93
94 def cleanup(self):
95 self.done = False
96 return self.persist
97
98 def update(self, surface, keys, current_time):
99 pass
100
101
102def load_all_gfx(directory, colorkey=(255,0,255), accept=('.png', 'jpg', 'bmp')):
103 graphics = {}
104 for pic in os.listdir(directory):
105 name, ext = os.path.splitext(pic)
106 if ext.lower() in accept:
107 img = pg.image.load(os.path.join(directory, pic))
108 if img.get_alpha():
109 img = img.convert_alpha()
110 else:
111 img = img.convert()
112 img.set_colorkey(colorkey)
113 graphics[name] = img
114 return graphics
115
116
117def load_all_music(directory, accept=('.wav', '.mp3', '.ogg', '.mdi')):
118 songs = {}
119 for song in os.listdir(directory):
120 name, ext = os.path.splitext(song)
121 if ext.lower() in accept:
122 songs[name] = os.path.join(directory, song)
123 return songs
124
125
126def load_all_fonts(directory, accept=('.ttf')):
127 return load_all_music(directory, accept)
128
129
130def load_all_sfx(directory, accept=('.wav','.mp3','.ogg','.mdi')):
131 effects = {}
132 for fx in os.listdir(directory):
133 name, ext = os.path.splitext(fx)
134 if ext.lower() in accept:
135 effects[name] = pg.mixer.Sound(os.path.join(directory, fx))
136 return effects
137
138
139def get_image(x, y, width, height, sprite_sheet):
140 """Extracts image from sprite sheet"""
141 image = pg.Surface([width, height])
142 rect = image.get_rect()
143
144 image.blit(sprite_sheet, (0, 0), (x, y, width, height))
145 image.set_colorkey(c.BLACK)
146
147 return image
148
149
150
151
152
153
154