/
menu.py
244 lines (227 loc) · 8.94 KB
/
menu.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
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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
#!/usr/bin/env python2
# Import libraries
try:
import pygame_sdl2
pygame_sdl2.import_as_pygame()
import pygame, sys, singleplayer, settings
from pygame.locals import *
except:
import pygame, sys, singleplayer, settings
from pygame.locals import *
def launch():
# Initialize pygame
pygame.init()
# Initialize the pygame font module
pygame.font.init()
# Set the width and height of the window
width, height = int(pygame.display.Info().current_w), int(pygame.display.Info().current_h)
# Create the window
screen = pygame.display.set_mode((width, height), pygame.HWSURFACE | pygame.DOUBLEBUF)
# Set title
pygame.display.set_caption("Galaxy Wars")
# Initialize the joystick module
pygame.joystick.init()
# Check if there are any joysticks
joystick_count = pygame.joystick.get_count()
# If there are any joysticks, initialize the first one, else quit the joystick module
if joystick_count:
joystick = pygame.joystick.Joystick(0)
joystick.init()
JS = True
else:
pygame.joystick.quit()
JS = False
# Set choice
choice = 1
Schoice = 1
MorS = "main"
# Set 1/12 height
height12 = height/12
# Load images
# Load the background image
bgmain = pygame.image.load("resources/images/bgmain.jpg")
bgmain = pygame.transform.scale(bgmain, (width, height))
# Set display mode
prevFS = settings.getFullscreen()
if settings.getFullscreen() == True:
screen = pygame.display.set_mode((width, height), pygame.HWSURFACE | pygame.DOUBLEBUF | pygame.FULLSCREEN)
elif settings.getFullscreen() == False:
screen = pygame.display.set_mode((width, height), pygame.HWSURFACE | pygame.DOUBLEBUF)
# Keep looping through
while True:
# Set display mode if changed
if prevFS != settings.getFullscreen():
if settings.getFullscreen() == True:
screen = pygame.display.set_mode((width, height), pygame.HWSURFACE | pygame.DOUBLEBUF | pygame.FULLSCREEN)
prevFS = settings.getFullscreen()
elif settings.getFullscreen() == False:
screen = pygame.display.set_mode((width, height), pygame.HWSURFACE | pygame.DOUBLEBUF)
prevFS = settings.getFullscreen()
# Set joystick buttons, if there are any
if JS:
buttonUP = joystick.get_button(4)
buttonDOWN = joystick.get_button(6)
buttonX = joystick.get_button(14)
# Clear the screen before drawing it again
screen.fill(0)
# Draw the background
screen.blit(bgmain, (0,0))
# Loop through the events
for event in pygame.event.get():
# Check if the event is the X button
if event.type == pygame.QUIT:
# If it is quit the game
pygame.quit()
sys.exit()
# Check if a key is pressed
elif event.type == pygame.KEYDOWN:
# Check if arrow down is pressed
if event.key == K_DOWN:
if MorS == "main":
if choice == 1:
choice = 2
elif choice == 2:
choice = 3
elif choice == 3:
choice = 1
elif MorS == "settings":
if Schoice == 1:
Schoice = 2
elif Schoice == 2:
Schoice = 1
# Check if arrow up is pressed
elif event.key == K_UP:
if MorS == "main":
if choice == 1:
choice = 3
elif choice == 2:
choice = 1
elif choice == 3:
choice = 2
elif MorS == "settings":
if Schoice == 1:
Schoice = 2
elif Schoice == 2:
Schoice = 1
# Check if return is pressed
elif event.key == K_RETURN:
if MorS == "main":
if choice == 1:
singleplayer.play()
elif choice == 2:
MorS = "settings"
elif choice == 3:
pygame.quit()
sys.exit()
elif MorS == "settings":
if Schoice == 1:
settings.changeFullscreen()
elif Schoice == 2:
MorS = "main"
# Quit by pressing escape
elif event.key == K_ESCAPE:
pygame.quit()
sys.exit()
# Fullscreen by pressing F4
elif event.key == K_F4:
settings.changeFullscreen()
# Check if there are any joysticks
if JS:
# Check if UP is pressed
if buttonUP:
if choice == 1:
choice = 3
elif choice == 2:
choice = 1
elif choice == 3:
choice = 2
# Check if DOWN is pressed
elif buttonDOWN:
if choice == 1:
choice = 2
elif choice == 2:
choice = 3
elif choice == 3:
choice = 1
# Check if X is pressed
elif buttonX:
if choice == 1:
singleplayer.play()
elif choice == 2:
MorS = "settings"
elif choice == 3:
pygame.quit()
sys.exit()
# Set font
font = pygame.font.Font("freesansbold.ttf", 24)
bigfont = pygame.font.Font("freesansbold.ttf", 48)
if MorS == "main":
# Check for cursor
if choice == 1:
spText = "-- Single Player --"
stText = "Settings"
xtText = "Exit"
elif choice == 2:
spText = "Single Player"
stText = "-- Settings --"
xtText = "Exit"
elif choice == 3:
spText = "Single Player"
stText = "Settings"
xtText = "-- Exit --"
# Render text
# Render title text
title = bigfont.render("Galaxy Wars", True, (255,255,255))
titleRect = title.get_rect()
titleRect.centerx = screen.get_rect().centerx
titleRect.centery = height12*2
# Render single player text
sp = font.render(spText, True, (255,255,255))
spRect = sp.get_rect()
spRect.centerx = screen.get_rect().centerx
spRect.centery = height12*5
# Render settings text
st = font.render(stText, True, (255,255,255))
stRect = st.get_rect()
stRect.centerx = screen.get_rect().centerx
stRect.centery = height12*6
# Render exit text
xt = font.render(xtText, True, (255,255,255))
xtRect = xt.get_rect()
xtRect.centerx = screen.get_rect().centerx
xtRect.centery = height12*7
# Draw text
screen.blit(title, titleRect)
screen.blit(sp, spRect)
screen.blit(st, stRect)
screen.blit(xt, xtRect)
elif MorS == "settings":
# Check for cursor
if Schoice == 1:
fsText = "-- Fullscreen: " + str(settings.getFullscreen()) + " --"
bcText = "Back"
elif Schoice == 2:
fsText = "Fullscreen: " + str(settings.getFullscreen())
bcText = "-- Back --"
# Render text
# Render title text
title = bigfont.render("Galaxy Wars", True, (255,255,255))
titleRect = title.get_rect()
titleRect.centerx = screen.get_rect().centerx
titleRect.centery = height12*2
# Render fullscreen text
fs = font.render(fsText, True, (255,255,255))
fsRect = fs.get_rect()
fsRect.centerx = screen.get_rect().centerx
fsRect.centery = height12*5
# Render back text
bc = font.render(bcText, True, (255,255,255))
bcRect = bc.get_rect()
bcRect.centerx = screen.get_rect().centerx
bcRect.centery = height12*6
# Draw text
screen.blit(title, titleRect)
screen.blit(fs, fsRect)
screen.blit(bc, bcRect)
# Flip the display
pygame.display.flip()