-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmenu.py
More file actions
214 lines (179 loc) · 7.49 KB
/
menu.py
File metadata and controls
214 lines (179 loc) · 7.49 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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
# coding=utf-8
import os
import pygame
import pygameMenu
import engine
from config import GAME_CFG
from config import GAME_ENV
from config import PLAYER_CFG
from config import PlayerControls
# -----------------------------------------------------------------------------
# Constants and global variables
# -----------------------------------------------------------------------------
ABOUT = ['GC2\'s Snake',
'Author: @woergi',
pygameMenu.locals.TEXT_NEWLINE,
'Email: office@gc2.at']
COLOR_BLACK = (0, 0, 0)
COLOR_WHITE = (255, 255, 255)
COLOR_BACKGROUND = (74, 74, 74)
FPS = 60.0
MENU_BACKGROUND_COLOR = (37, 133, 47)
MENU_FONT = pygameMenu.font.FONT_BEBAS
WINDOW_SIZE = (1024, 768)
PLAYERS = 1
SPEED = 6
MAX_PLAYERS = 4
player_controls = []
for i in range(0, MAX_PLAYERS):
player_controls.append(PlayerControls())
input_ctrl_sel_player = [player_controls[0]]
def player_changed(value, num_players):
global PLAYERS
PLAYERS = num_players
def speed_changed(value, s):
global SPEED
SPEED = s
def play_function():
"""
Main game function.
:return: None
"""
global PLAYERS
global SPEED
print('Game starts for {0} players'.format(PLAYERS))
# Reset main menu and disable
# You also can set another menu, like a 'pause menu', or just use the same
# main_menu as the menu that will check all your input.
GAME_ENV.main_menu.disable()
GAME_ENV.main_menu.reset(1)
PLAYER_CFG.NUM_PLAYERS = PLAYERS
PLAYER_CFG.CONTROLS = player_controls
GAME_CFG.DELAY_FACTOR = 10 - SPEED
game_engine = engine.Game()
game_engine.game_loop() # doesn't return until game is over or close is requested
GAME_ENV.main_menu.enable()
def key_input(msg, on_key_pressed, *args, **kwargs):
"""
Draws an overlay, so that a input key can be retrieved
:return: None
"""
while True:
GAME_ENV.clock.tick(FPS)
main_background()
events = pygame.event.get()
for event in events:
if event.type == pygame.QUIT:
exit()
elif event.type == pygame.KEYDOWN:
on_key_pressed(event.key, args, kwargs)
return
# main_menu.mainloop(None, disable_loop=True) # disable events in menu
GAME_ENV.main_menu._actual.draw() # has to be used because mainloop flips internally
rect_size = (256, 48)
frame_size = 6
# inner frame
pygame.draw.rect(GAME_ENV.surface, MENU_BACKGROUND_COLOR,
(WINDOW_SIZE[0] / 2 - rect_size[0] / 2,
WINDOW_SIZE[1] / 2 - rect_size[1] / 2,
rect_size[0], rect_size[1]), 0)
# outer frame
pygame.draw.rect(GAME_ENV.surface, COLOR_BACKGROUND,
(WINDOW_SIZE[0] / 2 - rect_size[0] / 2,
WINDOW_SIZE[1] / 2 - rect_size[1] / 2,
rect_size[0], rect_size[1]), frame_size)
# msg
font = pygame.font.Font(MENU_FONT, 30)
rendered_msg = font.render(msg, 1, COLOR_BLACK)
rendered_msg_size = font.size(msg)
GAME_ENV.surface.blit(rendered_msg,
(WINDOW_SIZE[0] / 2 - rendered_msg_size[0] / 2,
WINDOW_SIZE[1] / 2 - rendered_msg_size[1] / 2))
pygame.display.flip()
def store_key(key, *args, **kwargs):
# args[0][0][0] ... 1st and 2nd to access proper element, 3rd because it has to be a ref (which is a list here)
getattr(args[0][0][0], args[0][1])(key)
def ctrl_player_changed(value, sel_player):
input_ctrl_sel_player[0] = player_controls[sel_player - 1]
def main_background():
"""
Function used by menus, draw on background while menu is active.
:return: None
"""
GAME_ENV.surface.fill(COLOR_BACKGROUND)
def create_menu(menu_title, font_size=30):
return pygameMenu.TextMenu(GAME_ENV.surface,
bgfun=main_background,
color_selected=COLOR_WHITE,
font=MENU_FONT,
font_color=COLOR_BLACK,
font_size_title=30,
font_title=pygameMenu.font.FONT_8BIT,
menu_color=MENU_BACKGROUND_COLOR,
menu_color_title=COLOR_WHITE,
menu_height=int(WINDOW_SIZE[1] * 0.6),
menu_width=int(WINDOW_SIZE[0] * 0.6),
onclose=pygameMenu.events.DISABLE_CLOSE,
option_shadow=False,
text_color=COLOR_BLACK,
font_size=font_size,
title=menu_title,
window_height=WINDOW_SIZE[1],
window_width=WINDOW_SIZE[0]
)
def main():
pygame.init()
pygame.display.set_caption("GC2 Snake v1.0")
pygame.font.init()
os.environ['SDL_VIDEO_CENTERED'] = '1'
GAME_ENV.clock = pygame.time.Clock()
GAME_ENV.surface = pygame.display.set_mode((GAME_CFG.SCREEN_WIDTH, GAME_CFG.SCREEN_HEIGHT), pygame.HWSURFACE)
setting_menu = create_menu('Settings')
player_select = []
for p in range(1, MAX_PLAYERS + 1):
player_select.append((str(p), p))
setting_menu.add_selector('Players: ', player_select,
selector_id='players',
default=0,
onchange=player_changed)
speed_select = []
for p in range(1, 10):
speed_select.append((str(p), p))
setting_menu.add_selector('Speed: ', speed_select,
selector_id='speed',
default=5,
onchange=speed_changed)
ctrl_menu = create_menu('Controls')
ctrl_menu.add_selector('Select player: ', player_select,
selector_id='players',
default=0,
onchange=ctrl_player_changed)
ctrl_menu.add_option("Left", key_input, 'Press a key...', store_key, input_ctrl_sel_player, 'set_left')
ctrl_menu.add_option("Right", key_input, 'Press a key...', store_key, input_ctrl_sel_player, 'set_right')
ctrl_menu.add_option("Up", key_input, 'Press a key...', store_key, input_ctrl_sel_player, 'set_up')
ctrl_menu.add_option("Down", key_input, 'Press a key...', store_key, input_ctrl_sel_player, 'set_down')
ctrl_menu.add_option('Back', pygameMenu.events.BACK)
setting_menu.add_option('Controls', ctrl_menu)
setting_menu.add_option('Return to main', pygameMenu.events.BACK)
about_menu = create_menu('About')
for m in ABOUT:
about_menu.add_line(m)
about_menu.add_line(pygameMenu.locals.TEXT_NEWLINE)
about_menu.add_option('Return to main', pygameMenu.events.BACK)
GAME_ENV.main_menu = create_menu('Main')
GAME_ENV.main_menu.add_option('Start', play_function)
GAME_ENV.main_menu.add_option('Settings', setting_menu)
GAME_ENV.main_menu.add_option('About', about_menu)
GAME_ENV.main_menu.add_option('Quit', pygameMenu.events.EXIT)
GAME_ENV.main_menu.set_fps(FPS)
while True:
GAME_ENV.clock.tick(FPS)
main_background()
events = pygame.event.get()
for event in events:
if event.type == pygame.QUIT:
exit()
GAME_ENV.main_menu.mainloop(events, disable_loop=True)
pygame.display.flip()
if __name__ == '__main__':
main()