all repos — Legends-RPG @ 68e9a52fe83686831824476d53ff7135dbcec2cf

A fantasy mini-RPG built with Python and Pygame.

data/tools.py (view raw)

  1__author__ = 'justinarmstrong'
  2
  3import os, random
  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.previous = previous
 44        self.state.startup(self.current_time, persist)
 45        self.set_music()
 46
 47    def set_music(self):
 48        """
 49        Set music for the new state.
 50        """
 51        if self.state.music:
 52            pg.mixer.music.load(self.state.music)
 53            pg.mixer.music.set_volume(.4)
 54            pg.mixer.music.play()
 55        else:
 56            pg.mixer.music.stop()
 57
 58    def event_loop(self):
 59        self.events = pg.event.get()
 60
 61        for event in self.events:
 62            if event.type == pg.QUIT:
 63                self.done = True
 64            elif event.type == pg.KEYDOWN:
 65                if event.key == pg.K_ESCAPE:
 66                    self.done = True
 67                self.keys = pg.key.get_pressed()
 68                self.toggle_show_fps(event.key)
 69                self.state.get_event(event)
 70            elif event.type == pg.KEYUP:
 71                self.keys = pg.key.get_pressed()
 72                self.state.get_event(event)
 73
 74    def toggle_show_fps(self, key):
 75        if key == pg.K_F5:
 76            self.show_fps = not self.show_fps
 77            if not self.show_fps:
 78                pg.display.set_caption(self.caption)
 79
 80    def main(self):
 81        """Main loop for entire program"""
 82        while not self.done:
 83            self.event_loop()
 84            self.update()
 85            pg.display.update()
 86            self.clock.tick(self.fps)
 87            if self.show_fps:
 88                fps = self.clock.get_fps()
 89                with_fps = "{} - {:.2f} FPS".format(self.caption, fps)
 90                pg.display.set_caption(with_fps)
 91
 92
 93class _State(object):
 94    """Base class for all game states"""
 95    def __init__(self):
 96        self.start_time = 0.0
 97        self.current_time = 0.0
 98        self.done = False
 99        self.quit = False
100        self.next = None
101        self.previous = None
102        self.game_data = {}
103        self.music = None
104
105    def get_event(self, event):
106        pass
107
108    def startup(self, current_time, game_data):
109        self.game_data = game_data
110        self.start_time = current_time
111
112    def cleanup(self):
113        self.done = False
114        return self.game_data
115
116    def update(self, surface, keys, current_time):
117        pass
118
119
120def load_all_gfx(directory, colorkey=(255,0,255), accept=('.png', 'jpg', 'bmp')):
121    graphics = {}
122    for pic in os.listdir(directory):
123        name, ext = os.path.splitext(pic)
124        if ext.lower() in accept:
125            img = pg.image.load(os.path.join(directory, pic))
126            if img.get_alpha():
127                img = img.convert_alpha()
128            else:
129                img = img.convert()
130                img.set_colorkey(colorkey)
131            graphics[name] = img
132    return graphics
133
134
135def load_all_music(directory, accept=('.wav', '.mp3', '.ogg', '.mdi')):
136    songs = {}
137    for song in os.listdir(directory):
138        name, ext = os.path.splitext(song)
139        if ext.lower() in accept:
140            songs[name] = os.path.join(directory, song)
141    return songs
142
143
144def load_all_fonts(directory, accept=('.ttf')):
145    return load_all_music(directory, accept)
146
147
148def load_all_tmx(directory, accept=('.tmx')):
149    return load_all_music(directory, accept)
150
151
152def load_all_sfx(directory, accept=('.wav','.mp3','.ogg','.mdi')):
153    effects = {}
154    for fx in os.listdir(directory):
155        name, ext = os.path.splitext(fx)
156        if ext.lower() in accept:
157            effects[name] = pg.mixer.Sound(os.path.join(directory, fx))
158    return effects
159
160
161def get_image(x, y, width, height, sprite_sheet):
162    """Extracts image from sprite sheet"""
163    image = pg.Surface([width, height])
164    rect = image.get_rect()
165
166    image.blit(sprite_sheet, (0, 0), (x, y, width, height))
167    image.set_colorkey(c.BLACK)
168
169    return image
170
171def get_tile(x, y, tileset, width=16, height=16, scale=1):
172    """Gets the surface and rect for a tile"""
173    surface = get_image(x, y, width, height, tileset)
174    surface = pg.transform.scale(surface, (int(width*scale), int(height*scale)))
175    rect = surface.get_rect()
176
177    tile_dict = {'surface': surface,
178                 'rect': rect}
179
180    return tile_dict
181
182
183def create_game_data_dict():
184    """Create a dictionary of persistant values the player
185    carries between states"""
186
187    player_items = {'GOLD': dict([('quantity',100),
188                                  ('value',0)]),
189                    'Healing Potion': dict([('quantity',2),
190                                            ('value',15)]),
191                    'Ether Potion': dict([('quantity',1),
192                                          ('value', 15)]),
193                    'Rapier': dict([('quantity', 1),
194                                    ('value', 50),
195                                    ('power', 5)]),
196                    'equipped weapon': 'Rapier',
197                    'equipped armor': []}
198
199    player_health = {'current': 70,
200                     'maximum': 70}
201
202    player_magic = {'current': 70,
203                    'maximum': 70}
204
205    player_stats = {'health': player_health,
206                    'Level': 1,
207                    'experience to next level': 30,
208                    'magic': player_magic,
209                    'attack points': 10,
210                    'Defense Points': 10}
211
212
213    data_dict = {'last location': None,
214                 'last state': None,
215                 'last direction': 'down',
216                 'king item': {'GOLD': dict([('quantity', 100),
217                                             ('value',0)])},
218                 'old man item': {'ELIXIR': dict([('value',1000),
219                                                  ('quantity',1)])},
220                 'player inventory': player_items,
221                 'player stats': player_stats,
222                 'battle counter': random.randint(50, 255),
223                 'treasure1': True,
224                 'treasure2': True,
225                 'treasure3': True,
226                 'treasure4': True,
227                 'treasure5': True,
228                 'brother quest complete': False,
229                 'talked to sick brother': False,
230                 'has brother elixir': False,
231                 'elixir received': False,
232                 'old man gift': '',
233                 'battle type': '',
234                 'crown quest': False,
235                 'delivered crown': False,
236                 'brother item': 'ELIXIR'
237    }
238
239    return data_dict
240
241
242
243
244
245
246