Added main menu and game menu.
This commit is contained in:
parent
06aca4eec9
commit
c14c5e248e
Binary file not shown.
After Width: | Height: | Size: 401 KiB |
Before Width: | Height: | Size: 88 KiB After Width: | Height: | Size: 88 KiB |
Before Width: | Height: | Size: 73 KiB After Width: | Height: | Size: 73 KiB |
|
@ -30,6 +30,7 @@ import pygame
|
||||||
import jukebox
|
import jukebox
|
||||||
import level
|
import level
|
||||||
import main_menu
|
import main_menu
|
||||||
|
import game_menu
|
||||||
|
|
||||||
class Game(object):
|
class Game(object):
|
||||||
"""Create an object to handle the game."""
|
"""Create an object to handle the game."""
|
||||||
|
@ -52,8 +53,9 @@ class Game(object):
|
||||||
self.running = False
|
self.running = False
|
||||||
|
|
||||||
def load(self):
|
def load(self):
|
||||||
self.objs.append(main_menu.MainMenu(self, os.path.abspath(os.path.join(
|
graphics_dir = os.path.abspath(os.path.join("resources", "graphics"))
|
||||||
"resources", "graphics", "main_menu.png"))))
|
|
||||||
|
self.objs.append(main_menu.MainMenu(self, graphics_dir))
|
||||||
self.level = None
|
self.level = None
|
||||||
self.jukebox = jukebox.Jukebox(
|
self.jukebox = jukebox.Jukebox(
|
||||||
os.path.abspath(os.path.join("resources", "music")),
|
os.path.abspath(os.path.join("resources", "music")),
|
||||||
|
|
|
@ -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))))
|
|
@ -26,4 +26,4 @@ A generic level.
|
||||||
|
|
||||||
class Level(object):
|
class Level(object):
|
||||||
def __init__(self, level_name):
|
def __init__(self, level_name):
|
||||||
pass
|
self.__dict__.update(locals())
|
||||||
|
|
|
@ -24,26 +24,51 @@
|
||||||
The main menu.
|
The main menu.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
import os
|
||||||
import pygame
|
import pygame
|
||||||
|
|
||||||
import level
|
import level
|
||||||
|
import game_menu
|
||||||
|
|
||||||
class MainMenu(object):
|
class MainMenu(object):
|
||||||
def __init__(self, game, background_img_path):
|
def __init__(self, game, img_dir):
|
||||||
self.__dict__.update(locals())
|
self.__dict__.update(locals())
|
||||||
|
|
||||||
self.load()
|
self.load()
|
||||||
|
|
||||||
def load(self):
|
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 = pygame.transform.smoothscale(
|
||||||
self.background_img, self.game.window.get_size())
|
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):
|
def update(self, e, t, dt):
|
||||||
for event in e:
|
for event in e:
|
||||||
if event.type == pygame.KEYDOWN:
|
if event.type == pygame.KEYDOWN:
|
||||||
if event.key == pygame.K_SPACE:
|
if event.key == pygame.K_SPACE:
|
||||||
self.game.goto_level(level.Level("landing"))
|
self.game.goto_level(level.Level("landing"))
|
||||||
self.game.objs.remove(self)
|
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):
|
def draw(self, window):
|
||||||
window.blit(self.background_img, (0, 0))
|
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]))
|
||||||
|
|
Loading…
Reference in New Issue