Added main menu and game menu.

This commit is contained in:
Sakse Dalum 2012-08-07 17:26:49 +02:00
parent 06aca4eec9
commit c14c5e248e
7 changed files with 116 additions and 5 deletions

Binary file not shown.

After

Width:  |  Height:  |  Size: 401 KiB

View File

Before

Width:  |  Height:  |  Size: 88 KiB

After

Width:  |  Height:  |  Size: 88 KiB

View File

Before

Width:  |  Height:  |  Size: 73 KiB

After

Width:  |  Height:  |  Size: 73 KiB

View File

@ -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")),

84
robotgame/game_menu.py Normal file
View File

@ -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 <http://www.gnu.org/licenses/>.
#
# ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' '
#
# game_menu.py
# --------------------
# date created : Tue Aug 7 2012
# copyright : (C) 2012 Sakse Dalum
# maintained by : Sakse Dalum <don_s@hongabar.org>
"""
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))))

View File

@ -26,4 +26,4 @@ A generic level.
class Level(object):
def __init__(self, level_name):
pass
self.__dict__.update(locals())

View File

@ -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]))