a-robots-conundrum/arobotsconundrum/game_menu.py

91 lines
3.5 KiB
Python

# This file is part of A Robot's Conundrum.
#
# A Robot's Conundrum 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.
#
# A Robot's Conundrum 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
# A Robot's Conundrum. 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 = ['restart_level', 'quit']
self.load()
def load(self):
screen_size = self.game.window.get_size()
for item in self.menu:
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 toggle_menu(self):
self.game.level.paused = self.active = not self.active
def update(self, e, t, dt):
for event in e:
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_ESCAPE:
self.toggle_menu()
if self.active:
if event.key in [pygame.K_SPACE, pygame.K_RETURN]:
if self.menu[self.selection] == 'restart_level':
#self.game.level.restart()
self.game.restart_level()
self.toggle_menu()
if self.menu[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) - 1)
def draw(self, window):
if self.active:
screen_size = self.game.window.get_size()
for i in range(len(self.menu)):
s = i == self.selection
img = getattr(self, '%s_imgs' % self.menu[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) / 2 - i))))