Added main menu and game menu.
This commit is contained in:
84
robotgame/game_menu.py
Normal file
84
robotgame/game_menu.py
Normal 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))))
|
||||
Reference in New Issue
Block a user