all repos — Legends-RPG @ a452513181f1eef80107430ce479b678dc2fa21c

A fantasy mini-RPG built with Python and Pygame.

Added a simple title screen.
Justin Armstrong justinmeister@gmail.com
Fri, 30 May 2014 10:16:19 -0700
commit

a452513181f1eef80107430ce479b678dc2fa21c

parent

f0f531c2662a5962b0ee222c6b6e5373ac3ed6aa

M data/constants.pydata/constants.py

@@ -82,4 +82,10 @@ FIRE = 'fire'

PUNCH = 'punch' POWERUP = 'powerup' -TRANSITION_SPEED = 25 +TRANSITION_SPEED = 35 + +#LEVEL STATES + +NORMAL = 'normal' +TRANSITION_IN = 'transition in' +TRANSITION_OUT = 'transition out'
M data/main.pydata/main.py

@@ -1,5 +1,4 @@

-from data.states.main_menu import main_menu -from data.states import shop, levels, battle +from data.states import shop, levels, battle, main_menu from . import setup, tools from . import constants as c
A data/states/main_menu.py

@@ -0,0 +1,92 @@

+import pygame as pg +from .. import setup, tools, tilerender +from .. import constants as c + +class Menu(tools._State): + def __init__(self): + super(Menu, self).__init__() + self.music = setup.MUSIC['kings_theme'] + pg.mixer.music.load(self.music) + pg.mixer.music.set_volume(.4) + pg.mixer.music.play(-1) + self.next = c.OVERWORLD + self.tmx_map = setup.TMX['title'] + self.renderer = tilerender.Renderer(self.tmx_map) + self.map_image = self.renderer.make_2x_map() + self.map_rect = self.map_image.get_rect() + self.viewport = self.make_viewport(self.map_image) + self.level_surface = pg.Surface(self.map_rect.size) + self.title_box = setup.GFX['title_box'] + self.title_rect = self.title_box.get_rect() + self.title_rect.midbottom = self.viewport.midbottom + self.title_rect.y -= 30 + self.game_data = tools.create_game_data_dict() + self.state_dict = self.make_state_dict() + self.name = c.MAIN_MENU + self.state = c.TRANSITION_IN + self.alpha = 255 + self.transition_surface = pg.Surface(setup.SCREEN_RECT.size) + self.transition_surface.fill(c.BLACK_BLUE) + self.transition_surface.set_alpha(self.alpha) + + def make_viewport(self, map_image): + """ + Create the viewport to view the level through. + """ + map_rect = map_image.get_rect() + return setup.SCREEN.get_rect(bottomright=map_rect.bottomright) + + def make_state_dict(self): + """ + Make the dictionary of state methods for the level. + """ + state_dict = {c.TRANSITION_IN: self.transition_in, + c.TRANSITION_OUT: self.transition_out, + c.NORMAL: self.normal_update} + + return state_dict + + def update(self, surface, *args): + """ + Update scene. + """ + update_level = self.state_dict[self.state] + update_level() + self.draw_level(surface) + + def draw_level(self, surface): + """ + Blit tmx map and title box onto screen. + """ + self.level_surface.blit(self.map_image, self.viewport, self.viewport) + self.level_surface.blit(self.title_box, self.title_rect) + surface.blit(self.level_surface, (0,0), self.viewport) + surface.blit(self.transition_surface, (0,0)) + + def get_event(self, event): + if event.type == pg.KEYDOWN: + self.state = c.TRANSITION_OUT + + def transition_in(self): + """ + Transition into scene with a fade. + """ + self.transition_surface.set_alpha(self.alpha) + self.alpha -= c.TRANSITION_SPEED + if self.alpha <= 0: + self.alpha = 0 + self.state = c.NORMAL + + + def transition_out(self): + """ + Transition out of scene with a fade. + """ + self.transition_surface.set_alpha(self.alpha) + self.alpha += c.TRANSITION_SPEED + if self.alpha >= 255: + self.game_data['last state'] = self.name + self.done = True + + def normal_update(self): + pass
D data/states/main_menu/__init__.py

@@ -1,1 +0,0 @@

-__author__ = 'justinarmstrong'
D data/states/main_menu/main_menu.py

@@ -1,28 +0,0 @@

-import pygame as pg -from ... import setup, tools -from ... import constants as c - -class Menu(tools._State): - def __init__(self): - super(Menu, self).__init__() - self.next = c.OVERWORLD - self.surface = setup.SCREEN - self.rect = self.surface.get_rect() - text = 'Arrows for navigation, space for input, Enter/Return for menu' - self.font = pg.font.Font(setup.FONTS[c.MAIN_FONT], 22) - self.rendered_text = self.font.render(text, 1, c.BLACK) - self.text_rect = self.rendered_text.get_rect() - self.text_rect.center = self.rect.center - self.game_data = tools.create_game_data_dict() - self.name = c.MAIN_MENU - - - def update(self, surface, keys, current_time): - self.current_time = current_time - surface.fill(c.WHITE) - surface.blit(self.rendered_text, self.text_rect) - - def get_event(self, event): - if event.type == pg.KEYDOWN: - self.game_data['last state'] = self.name - self.done = True
A resources/tmx/title.tmx

@@ -0,0 +1,19 @@

+<?xml version="1.0" encoding="UTF-8"?> +<map version="1.0" orientation="orthogonal" width="25" height="19" tilewidth="16" tileheight="16"> + <tileset firstgid="1" name="tileset2" tilewidth="16" tileheight="16"> + <image source="../graphics/tileset2.png" width="160" height="180"/> + </tileset> + <tileset firstgid="111" name="tileset1" tilewidth="16" tileheight="16"> + <image source="../graphics/tileset1.png" width="160" height="80"/> + </tileset> + <layer name="Tile Layer 1" width="25" height="19"> + <data encoding="base64" compression="zlib"> + eJyTZGBgkBzFo3gUj+JRPIpHMc0wAJGoLmQ= + </data> + </layer> + <layer name="Tile Layer 2" width="25" height="19"> + <data encoding="base64" compression="zlib"> + eJy1kzsOwDAMQrn/yOzDdqklhNypgBTFyvDwLwBA9MT3TNFnhD8FPuVu1aH5p+u48k/3ybmNPl15p32Wv3v75ftXy/S5p/kap+tYZnvu2ifKW1o+kxZf40avaHFrty6flC5m84+4d0rKegD8hDUX + </data> + </layer> +</map>