all repos — Legends-RPG @ b2c18e895bb4ebc85ad81e72dc2d57210b3e1da4

A fantasy mini-RPG built with Python and Pygame.

data/tools.py (view raw)

  1__author__ = 'justinarmstrong'
  2
  3import os
  4import pygame as pg
  5
  6class Control(object):
  7    """
  8    Control class for entire project.  Contains the game loop, and contains
  9    the event_loop which passes events to States as needed.  Logic for flipping
 10    states is also found here.
 11    """
 12    def __init__(self, caption):
 13        self.screen = pg.display.get_surface()
 14        self.done = False
 15        self.clock = pg.time.Clock()
 16        self.caption = caption
 17        self.fps = 60
 18        self.show_fps = False
 19        self.current_time = 0.0
 20        self.keys = pg.key.get_pressed()
 21        self.state_dict = {}
 22        self.state_name = None
 23        self.state = None
 24
 25    def setup_states(self, state_dict, start_state):
 26        self.state_dict = state_dict
 27        self.state_name = start_state
 28        self.state = self.state_dict[self.state_name]
 29
 30    def update(self):
 31        self.current_time = pg.time.get_ticks()
 32        if self.state.quit:
 33            self.done = True
 34        elif self.state.done:
 35            self.flip_state()
 36        self.state.update(self.screen, self.keys, self.current_time)
 37
 38    def flip_state(self):
 39        previous, self.state_name = self.state_name, self.state.next
 40        persist = self.state.cleanup()
 41        self.state = self.state_dict[self.state_name]
 42        self.state.startup(self.current_time, persist)
 43        self.state.previous = previous
 44
 45    def event_loop(self):
 46        for event in pg.event.get():
 47            if event.type == pg.QUIT:
 48                self.done = True
 49            elif event.type == pg.KEYDOWN:
 50                self.keys = pg.key.get_pressed()
 51                self.toggle_show_fps(event.key)
 52            elif event.type == pg.KEYUP:
 53                self.keys = pg.key.get_pressed()
 54            self.state.get_event(event)
 55
 56    def toggle_show_fps(self, key):
 57        if key == pg.K_F5:
 58            self.show_fps = not self.show_fps
 59            if not self.show_fps:
 60                pg.display.set_caption(self.caption)
 61
 62    def main(self):
 63        """Main loop for entire program"""
 64        while not self.done:
 65            self.event_loop()
 66            self.update()
 67            pg.display.update()
 68            self.clock.tick(self.fps)
 69            if self.show_fps:
 70                fps = self.clock.get_fps()
 71                with_fps = "{} - {:.2f} FPS".format(self.caption, fps)
 72                pg.display.set_caption(with_fps)
 73
 74class _State(object):
 75    """Base class for all game states"""
 76    def __init__(self):
 77        self.start_time = 0.0
 78        self.current_time = 0.0
 79        self.done = False
 80        self.quit = False
 81        self.next = None
 82        self.previous = None
 83        self.persist = {}
 84
 85    def get_event(self, event):
 86        pass
 87
 88    def startup(self, current_time, persistant):
 89        self.persist = persistant
 90        self.start_time = current_time
 91
 92    def cleanup(self):
 93        self.done = False
 94        return self.persist
 95
 96    def update(self, surface, keys, current_time):
 97        pass
 98
 99
100def load_all_gfx(directory, colorkey=(255,0,255), accept=('.png', 'jpg', 'bmp')):
101    graphics = {}
102    for pic in os.listdir(directory):
103        name, ext = os.path.splitext(pic)
104        if ext.lower() in accept:
105            img = pg.image.load(os.path.join(directory, pic))
106            if img.get_alpha():
107                img = img.convert_alpha()
108            else:
109                img = img.convert()
110                img.set_colorkey(colorkey)
111            graphics[name] = img
112    return graphics
113
114
115def load_all_music(directory, accept=('.wav', '.mp3', '.ogg', '.mdi')):
116    songs = {}
117    for song in os.listdir(directory):
118        name, ext = os.path.splitext(song)
119        if ext.lower() in accept:
120            song[name] = os.path.join(directory, song)
121    return songs
122
123
124def load_all_fonts(directory, accept=('ttf')):
125    return load_all_music(directory, accept)
126
127
128def load_all_sfx(directory, accept=('.wav','.mp3','.ogg','.mdi')):
129    effects = {}
130    for fx in os.listdir(directory):
131        name, ext = os.path.splitext(fx)
132        if ext.lower() in accept:
133            effects[name] = pg.mixer.Sound(os.path.join(directory, fx))
134    return effects
135
136
137
138
139
140