-
Notifications
You must be signed in to change notification settings - Fork 0
/
menuscene.py
134 lines (115 loc) · 4.36 KB
/
menuscene.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
#!/usr/bin/python
# -*- coding: utf-8 -*-
"""This module contains the game scene 'MenuScene'."""
import gtk
import gtk.gdk as gdk
import gobject
import cairo
from pnode import *
from objects import *
from uicomponents import *
from effects import *
from motions import *
class MenuScene(Node):
"""The game scene that display the main menu from game start."""
def __init__(self, parent, style, audio, key_down, key_up, on_game_start):
"""Create and put the components."""
super(MenuScene, self).__init__(parent, style)
# Receive class AudioManager, funtion key_up() and key_down()
# from class Game to play music and check player's input
self.audio = audio
self.key_up = key_up
self.key_down = key_down
# Function on_game_start() is called if the player selected the menu
self.on_game_start = on_game_start
# The selection menu component
self.cursor = Bomb(parent=self, style={})
self.sel = Selections(
parent=self,
style={
'left': '25%',
'top': '50%',
'width': '50%',
'height': '30%'
},
font='MS Gothic',
labels=(u'Story Mode', u'Free Game'),
color=(1, 1, 1, 0.7),
bgcolor=(0.3, 0.3, 0.7, 0.7),
cursor=self.cursor
)
self.add_node(self.sel)
self.cursor.count()
# The title (a Label)
title = Label(
parent=self,
style={
'top': '20%',
'left': '15%',
'right': '15%'
},
text=u'ボンバー缶',
font='MS Gothic bold',
color=(1, 1, 0.3, 1),
)
self.add_node(title)
# A smoke effect displayed just for the demonstration
# of the particle system
particle = ParticleEffect(self,
{
'width': '50%',
'height': '50%',
'align': 'center',
'vertical-align': 'bottom'
},
size=10, size_deviation=2, v_size=3,
color=(0.8, 0.4, 0, 0.5), v_color=(-0.5, -0.5, 0, -0.25),
center=(0.5, 0.8),
velocity=(0, -0.3), velocity_deviation=(0.2, 0.1),
lifetime=2.0, initial_amount=100)
self.add_node(particle)
particle.add_action('action', particle.update_action,
duration=1, loop=True, update=True)
# The background image
self.texture = {}
self.texture['bgimg'] = \
cairo.ImageSurface.create_from_png('menu_bg.png')
def on_update(self, cr):
"""Simply display the background image
(centered and touching the window from outside).
Display of other GUI components are handled in each individual class.
"""
scale_width = self.width / float(self.texture['bgimg'].get_width())
scale_height = self.height / float(self.texture['bgimg'].get_height())
if scale_width < scale_height:
scale = scale_height
else:
scale = scale_width
new_width = self.texture['bgimg'].get_width() * scale
new_height = self.texture['bgimg'].get_height() * scale
x = (self.width - new_width) / 2
y = (self.height - new_height) / 2
cr = cairo.Context(self.surface)
cr.scale(scale, scale)
cr.set_source_rgb(0, 0, 0)
cr.paint()
cr.set_source_surface(self.texture['bgimg'], x, y)
cr.paint_with_alpha(0.7)
def on_tick(self, interval):
"""In each tick, check player's input to move up / down in the menu
or select a menu item.
"""
if self.key_up('Up'):
self.audio.play('select.wav')
self.sel.select_up()
elif self.key_up('Down'):
self.audio.play('select.wav')
self.sel.select_down()
# A menu item has been selected
if self.key_up('space'):
if self.sel.selected == 0:
# Start story mode
self.on_game_start(0)
elif self.sel.selected == 1:
# Start free game
self.on_game_start(1)