all repos — Legends-RPG @ ce2b2c848151d5a9365aee0fc44c1037638725ed

A fantasy mini-RPG built with Python and Pygame.

data/tools.py (view raw)

  1__author__ = 'justinarmstrong'
  2
  3import os
  4import pygame as pg
  5from . import 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):
 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.game_data = {}
 88
 89    def get_event(self, event):
 90        pass
 91
 92    def startup(self, current_time, game_data):
 93        self.game_data = game_data
 94        self.start_time = current_time
 95
 96    def cleanup(self):
 97        self.done = False
 98        return self.game_data
 99
100    def update(self, surface, keys, current_time):
101        pass
102
103
104def load_all_gfx(directory, colorkey=(255,0,255), accept=('.png', 'jpg', 'bmp')):
105    graphics = {}
106    for pic in os.listdir(directory):
107        name, ext = os.path.splitext(pic)
108        if ext.lower() in accept:
109            img = pg.image.load(os.path.join(directory, pic))
110            if img.get_alpha():
111                img = img.convert_alpha()
112            else:
113                img = img.convert()
114                img.set_colorkey(colorkey)
115            graphics[name] = img
116    return graphics
117
118
119def load_all_music(directory, accept=('.wav', '.mp3', '.ogg', '.mdi')):
120    songs = {}
121    for song in os.listdir(directory):
122        name, ext = os.path.splitext(song)
123        if ext.lower() in accept:
124            songs[name] = os.path.join(directory, song)
125    return songs
126
127
128def load_all_fonts(directory, accept=('.ttf')):
129    return load_all_music(directory, accept)
130
131
132def load_all_sfx(directory, accept=('.wav','.mp3','.ogg','.mdi')):
133    effects = {}
134    for fx in os.listdir(directory):
135        name, ext = os.path.splitext(fx)
136        if ext.lower() in accept:
137            effects[name] = pg.mixer.Sound(os.path.join(directory, fx))
138    return effects
139
140
141def get_image(x, y, width, height, sprite_sheet):
142    """Extracts image from sprite sheet"""
143    image = pg.Surface([width, height])
144    rect = image.get_rect()
145
146    image.blit(sprite_sheet, (0, 0), (x, y, width, height))
147    image.set_colorkey(c.BLACK)
148
149    return image
150
151
152def create_game_data_dict():
153    """Create a dictionary of persistant values the player
154    carries between states"""
155    player_items = {'gold': 600}
156
157    player_health = {'current': 100,
158                     'maximum': 100}
159
160    player_magic = {'current': 100,
161                    'maximum': 100}
162
163    player_stats = {'Health': player_health,
164                    'Level': 1,
165                    'Experience to next level': 100,
166                    'Magic Points': player_magic,
167                    'Attack Points': 10,
168                    'Defense Points': 10}
169
170
171    data_dict = {'last location': None,
172                 'last state': None,
173                 'last direction': 'up',
174                 'town start pos': [12, 49],
175                 'castle start pos': [12, 26],
176                 'house start pos': [12, 13],
177                 'king item': {'gold': 500},
178                 'player inventory': player_items,
179                 'player stats': player_stats
180    }
181
182    return data_dict
183
184
185
186
187
188
189