forked from youpeng88/tower_game
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplatform_menu.py
More file actions
132 lines (111 loc) · 4.46 KB
/
platform_menu.py
File metadata and controls
132 lines (111 loc) · 4.46 KB
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
# -*- coding: utf-8 -*-
"""
Created on Tue Jan 19 14:53:39 2016
"""
# -*- coding: utf-8 -*-
"""
Created on Tue Jan 19 12:21:14 2016
@author: pengyou modified on example menu
"""
#! /usr/bin/python
## \file example_simple.py
# \brief A (very simple) example of using the menu system
# \author Scott Barlow
# \date 2009
# \version 1.0.0
#
# An example script to create a window and explore some of the features of the
# menu class I've created. This script just creates a very simple menu for
# users that just want to see a plain and simply menu. This could be made even
# more simple, but I keep some features I deem "essential" (such as
# non-blocking code and only updating the portion of the screen that changed).
#
#
# Copyright 2009 Scott Barlow
#
# This program 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.
#
# This program 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 this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
# MA 02110-1301, USA or see <http://www.gnu.org/licenses/>.
#
#
# Changelog
# V1.0.0 - Initial Release
# V1.0.1 - No change to this file
# V1.0.2 - No change to this file
# V1.0.3 - No change to this file
#
#-------------------------------------------------------------------------------
#---[ Imports ]-----------------------------------------------------------------
#-------------------------------------------------------------------------------
import sys, pygame
from menu import *
## ---[ main ]------------------------------------------------------------------
# This function runs the entire screen and contains the main while loop
#
def main(screen):
# Create 3 diffrent menus. One of them is only text, another one is only
# images, and a third is -gasp- a mix of images and text buttons! To
# understand the input factors, see the menu file
menu = cMenu(50, 50, 20, 5, 'vertical', 100, screen,
[('PC', 1, None),
('Mac', 2, None),
('Back', 3, None)])
# Center the menu on the draw_surface (the entire screen here)
menu.set_center(True, True)
# Center the menu on the draw_surface (the entire screen here)
menu.set_alignment('center', 'center')
# Create the state variables (make them different so that the user event is
# triggered at the start of the "while 1" loop so that the initial display
# does not wait for user input)
state = 0
prev_state = 1
# rect_list is the list of pygame.Rect's that will tell pygame where to
# update the screen (there is no point in updating the entire screen if only
# a small portion of it changed!)
rect_list = []
# Ignore mouse motion (greatly reduces resources when not needed)
pygame.event.set_blocked(pygame.MOUSEMOTION)
# The main while loop
while 1:
# Check if the state has changed, if it has, then post a user event to
# the queue to force the menu to be shown at least once
if prev_state != state:
pygame.event.post(pygame.event.Event(EVENT_CHANGE_STATE, key = 0))
prev_state = state
# Get the next event
e = pygame.event.wait()
# Update the menu, based on which "state" we are in - When using the menu
# in a more complex program, definitely make the states global variables
# so that you can refer to them by a name
if e.type == pygame.KEYDOWN or e.type == EVENT_CHANGE_STATE:
if state == 0:
rect_list, state = menu.update(e, state)
elif state == 1:
print 'Platform: PC'
state = 0
return 1
elif state == 2:
print 'Platform: Mac'
state = 0
return 2
else:
print 'Back to Main Menu'
state = 0
return 4
# Quit if the user presses the exit button
if e.type == pygame.QUIT:
pygame.quit()
sys.exit()
# Update the screen
pygame.display.update(rect_list)