diff --git a/resources/graphics/press_space_to_start.png b/resources/graphics/press_space_to_start.png new file mode 100644 index 0000000..e3a96bb Binary files /dev/null and b/resources/graphics/press_space_to_start.png differ diff --git a/resources/graphics/restart-level-inactive.png b/resources/graphics/restart_level-inactive.png similarity index 100% rename from resources/graphics/restart-level-inactive.png rename to resources/graphics/restart_level-inactive.png diff --git a/resources/graphics/restart-level-selected.png b/resources/graphics/restart_level-selected.png similarity index 100% rename from resources/graphics/restart-level-selected.png rename to resources/graphics/restart_level-selected.png diff --git a/robotgame/game.py b/robotgame/game.py index 2dfdc53..a19f3fb 100644 --- a/robotgame/game.py +++ b/robotgame/game.py @@ -30,6 +30,7 @@ import pygame import jukebox import level import main_menu +import game_menu class Game(object): """Create an object to handle the game.""" @@ -52,8 +53,9 @@ class Game(object): self.running = False def load(self): - self.objs.append(main_menu.MainMenu(self, os.path.abspath(os.path.join( - "resources", "graphics", "main_menu.png")))) + graphics_dir = os.path.abspath(os.path.join("resources", "graphics")) + + self.objs.append(main_menu.MainMenu(self, graphics_dir)) self.level = None self.jukebox = jukebox.Jukebox( os.path.abspath(os.path.join("resources", "music")), diff --git a/robotgame/game_menu.py b/robotgame/game_menu.py new file mode 100644 index 0000000..3a7445e --- /dev/null +++ b/robotgame/game_menu.py @@ -0,0 +1,84 @@ +# This file is part of ROBOTGAME +# +# ROBOTGAME is free software: you can redistribute it and/or modify it under the +# terms of the GNU General Public License as published by the Free Software +# Foundation, either version 3 of the License, or (at your option) any later +# version. +# +# ROBOTGAME is distributed in the hope that it will be useful, but WITHOUT ANY +# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR +# A PARTICULAR PURPOSE. See the GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License along with +# ROBOTGAME. If not, see . +# +# ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' +# +# game_menu.py +# -------------------- +# date created : Tue Aug 7 2012 +# copyright : (C) 2012 Sakse Dalum +# maintained by : Sakse Dalum + +""" +The in-game menu. +""" + +import os +import pygame + +class GameMenu(object): + def __init__(self, game, img_dir, active=False, selection=0): + self.__dict__.update(locals()) + + self.menu_order = ['restart_level', 'quit'] + + self.load() + + def load(self): + screen_size = self.game.window.get_size() + + for item in self.menu_order: + setattr(self, '%s_imgs' % item, [ + pygame.image.load(os.path.join(self.img_dir, + '%s-%s.png' % (item, end))) + for end in ['inactive', 'selected']]) + img_size = getattr(self, '%s_imgs' % item)[0].get_size() + factors = (float(img_size[0]) / 1920, float(img_size[1]) / 1280) + + setattr(self, '%s_imgs' % item, [ + pygame.transform.smoothscale( + img, + (int(screen_size[0]*factors[0]), + int(screen_size[1]*factors[1]))) + for img in getattr(self, '%s_imgs' % item)]) + + def update(self, e, t, dt): + for event in e: + if event.type == pygame.KEYDOWN: + if event.key == pygame.K_ESCAPE: + self.active = not self.active + if self.active: + if event.key == pygame.K_SPACE: + if self.menu_order[self.selection] == 'restart_level': + pass + if self.menu_order[self.selection] == 'quit': + self.game.stop() + if event.key == pygame.K_UP: + self.selection = max(self.selection - 1, 0) + if event.key == pygame.K_DOWN: + self.selection = min(self.selection + 1, + len(self.menu_order) - 1) + + def draw(self, window): + if self.active: + screen_size = self.game.window.get_size() + + for i in range(len(self.menu_order)): + s = i == self.selection + img = getattr(self, '%s_imgs' % self.menu_order[i])[s] + window.blit(img, + (int((screen_size[0] - img.get_size()[0]) / 2), + int(screen_size[1] / 2) + - (int(screen_size[1]*0.13) + * (len(self.menu_order) / 2 - i)))) diff --git a/robotgame/level.py b/robotgame/level.py index 363a209..c8c1be0 100644 --- a/robotgame/level.py +++ b/robotgame/level.py @@ -26,4 +26,4 @@ A generic level. class Level(object): def __init__(self, level_name): - pass + self.__dict__.update(locals()) diff --git a/robotgame/main_menu.py b/robotgame/main_menu.py index c931411..125c991 100644 --- a/robotgame/main_menu.py +++ b/robotgame/main_menu.py @@ -24,26 +24,51 @@ The main menu. """ +import os import pygame + import level +import game_menu class MainMenu(object): - def __init__(self, game, background_img_path): + def __init__(self, game, img_dir): self.__dict__.update(locals()) self.load() def load(self): - self.background_img = pygame.image.load(self.background_img_path) + self.background_img = pygame.image.load(os.path.join(self.img_dir, + 'main_menu.png')) self.background_img = pygame.transform.smoothscale( self.background_img, self.game.window.get_size()) + self.space_img = pygame.image.load(os.path.join( + self.img_dir, + 'press_space_to_start.png')) + + screen_size = self.game.window.get_size() + img_size = self.space_img.get_size() + factors = (float(img_size[0]) / 1920, float(img_size[1]) / 1280) + + self.space_img = pygame.transform.smoothscale( + self.space_img, + (int(screen_size[0]*factors[0]), + int(screen_size[1]*factors[1]))) + def update(self, e, t, dt): for event in e: if event.type == pygame.KEYDOWN: if event.key == pygame.K_SPACE: self.game.goto_level(level.Level("landing")) self.game.objs.remove(self) + self.game.objs.append(game_menu.GameMenu(self.game, + self.img_dir)) + if event.key == pygame.K_ESCAPE: + self.game.stop() def draw(self, window): window.blit(self.background_img, (0, 0)) + + screen_size = self.game.window.get_size() + window.blit(self.space_img, + (0, screen_size[1] - self.space_img.get_size()[1]))