all repos — Legends-RPG @ 2ada8efcd445f4fdc94020fcb7f03a34dca285af

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
 74
 75class _State(object):
 76    """Base class for all game states"""
 77    def __init__(self):
 78        self.start_time = 0.0
 79        self.current_time = 0.0
 80        self.done = False
 81        self.quit = False
 82        self.next = None
 83        self.previous = None
 84        self.persist = {}
 85
 86    def get_event(self, event):
 87        pass
 88
 89    def startup(self, current_time, persistant):
 90        self.persist = persistant
 91        self.start_time = current_time
 92
 93    def cleanup(self):
 94        self.done = False
 95        return self.persist
 96
 97    def update(self, surface, keys, current_time):
 98        pass
 99
100
101def load_all_gfx(directory, colorkey=(255,0,255), accept=('.png', 'jpg', 'bmp')):
102    graphics = {}
103    for pic in os.listdir(directory):
104        name, ext = os.path.splitext(pic)
105        if ext.lower() in accept:
106            img = pg.image.load(os.path.join(directory, pic))
107            if img.get_alpha():
108                img = img.convert_alpha()
109            else:
110                img = img.convert()
111                img.set_colorkey(colorkey)
112            graphics[name] = img
113    return graphics
114
115
116def load_all_music(directory, accept=('.wav', '.mp3', '.ogg', '.mdi')):
117    songs = {}
118    for song in os.listdir(directory):
119        name, ext = os.path.splitext(song)
120        if ext.lower() in accept:
121            songs[name] = os.path.join(directory, song)
122    return songs
123
124
125def load_all_fonts(directory, accept=('.ttf')):
126    return load_all_music(directory, accept)
127
128
129def load_all_sfx(directory, accept=('.wav','.mp3','.ogg','.mdi')):
130    effects = {}
131    for fx in os.listdir(directory):
132        name, ext = os.path.splitext(fx)
133        if ext.lower() in accept:
134            effects[name] = pg.mixer.Sound(os.path.join(directory, fx))
135    return effects
136
137
138
139
140
141