forked from dsimon96/PyWars
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmainMenu.py
278 lines (252 loc) · 9.03 KB
/
mainMenu.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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
# mainMenu.py
# by David Simon ([email protected])
# Dec 2014
# Based on Advance Wars (Intelligent Systems, Nintendo)
from __future__ import absolute_import
import pygame
from pygame.locals import *
from pygameBaseClass import PygameBaseClass
from map import *
from units import *
from battle import *
from mapEditor import *
from six.moves import range
class mainMenu(PygameBaseClass):
def initGraphics(self):
self.background = self.loadBackground()
self.title = self.loadTitleSurface()
self.button = self.loadButton()
self.highlightedButton = self.loadHighlightedButton()
self.window = self.loadWindow()
def beginMusic(self):
pygame.mixer.music.fadeout(1000)
musicpath = os.path.join('audio', 'mainMenu.ogg')
pygame.mixer.music.load(musicpath)
pygame.mixer.music.set_volume(0.3)
pygame.mixer.music.play(1)
def initGame(self):
self.modes = ['Battle', 'Edit', 'Quit']
self.selectionIndex = 0
self.setupBattle = False
self.setupEditor = False
self.editorOpenFiles = False
self.files = self.getFiles()
self.beginMusic()
self.redrawAll()
self.minRows = self.rows = 10
self.minCols = self.cols = 16
def loadBackground(self):
path = 'menuArt.png'
image = pygame.image.load(path)
return image
def loadTitleSurface(self):
width = 640
height = 196
titleSurface = pygame.Surface((width, height))
color = pygame.Color('Gray')
rect = pygame.Rect(0, 0, width, height)
pygame.draw.rect(titleSurface, color, rect)
titleSurface.set_alpha(192)
font = pygame.font.SysFont('Tahoma', 128, True, True)
text = "PyWars"
textSurface = font.render(text, 1, (0, 0, 0))
vertPadding = 4
horizPadding = 64
titleSurface.blit(textSurface, (horizPadding, vertPadding))
return titleSurface
def loadButton(self):
width = 480
height = 128
button = pygame.Surface((width, height))
color = pygame.Color('Gray')
rect = pygame.Rect(0, 0, width, height)
pygame.draw.rect(button, color, rect)
button.set_alpha(192)
return button
def loadHighlightedButton(self):
width = 480
height = 128
button = pygame.Surface((width, height))
color = pygame.Color('White')
rect = pygame.Rect(0, 0, width, height)
pygame.draw.rect(button, color, rect)
button.set_alpha(192)
return button
def loadWindow(self):
width = 960
height = 576
window = pygame.Surface((width, height))
color = pygame.Color('Gray')
rect = pygame.Rect(0, 0, width, height)
pygame.draw.rect(window, color, rect)
return window
def runBattle(self):
fileName = self.files[self.selectionIndex] + '.tpm'
path = os.path.join('maps', fileName)
battleMode = Battle.fromFile(path)
if battleMode.runAsChild() == 1: self.quit()
else: self.initGame()
def runEditFile(self):
fileName = self.files[self.selectionIndex] + '.tpm'
path = os.path.join('maps', fileName)
editMode = Editor(path)
if editMode.runAsChild() == 1: self.quit()
else: self.initGame()
def runMapEditor(self):
editMode = Editor((self.rows, self.cols))
if editMode.runAsChild() == 1: self.quit()
else: self.initGame()
def getFiles(self):
files = []
for filename in os.listdir('maps'):
files.append(filename[:len(filename)-4])
return files
def select(self):
mode = self.modes[self.selectionIndex]
if mode == 'Battle':
self.setupBattle = True
self.getFiles()
self.redrawAll()
elif mode == 'Edit':
self.selectionIndex = 0
self.setupEditor = True
self.redrawAll()
elif mode == 'Quit':
self.quit()
def menu(self, keyName):
if keyName == 'return':
self.select()
elif keyName == 'escape':
self.quit()
elif keyName == 'down':
self.selectionIndex += 1
self.selectionIndex %= len(self.modes)
self.redrawAll()
elif keyName == 'up':
self.selectionIndex -= 1
self.selectionIndex %= len(self.modes)
self.redrawAll()
def battleSetup(self, keyName):
if keyName == 'down':
self.selectionIndex += 1
self.selectionIndex %= len(self.files)
self.redrawAll()
elif keyName == 'up':
self.selectionIndex -= 1
self.selectionIndex %= len(self.files)
self.redrawAll()
elif keyName == 'escape':
self.initGame()
elif keyName == 'return':
self.runBattle()
def editSetup(self, keyName):
if self.editorOpenFiles:
self.editorOpenFile(keyName)
elif keyName == 'up':
self.rows += 1
self.redrawAll()
elif keyName == 'down':
if self.rows > self.minRows:
self.rows -= 1
self.redrawAll()
elif keyName == 'right':
self.cols += 1
self.redrawAll()
elif keyName == 'left':
if self.cols > self.minCols:
self.cols -= 1
self.redrawAll()
elif keyName == 'o':
self.editorOpenFiles = True
self.getFiles()
self.redrawAll()
elif keyName == 'return':
self.runMapEditor()
elif keyName == 'escape':
self.initGame()
def editorOpenFile(self, keyName):
if keyName == 'return':
self.runEditFile()
if keyName == 'up':
self.selectionIndex -= 1
self.selectionIndex %= len(self.files)
self.redrawAll()
elif keyName == 'down':
self.selectionIndex += 1
self.selectionIndex %= len(self.files)
self.redrawAll()
elif keyName == 'escape':
self.editorOpenFiles = False
self.redrawAll()
def onKeyDown(self, event):
keyName = pygame.key.name(event.key)
if not (self.setupBattle or self.setupEditor):
self.menu(keyName)
elif self.setupBattle:
self.battleSetup(keyName)
elif self.setupEditor:
self.editSetup(keyName)
def drawButton(self, text, isHighlighted, top):
horizPadding = 96
vertPadding = 24
if isHighlighted:
button = self.highlightedButton.copy()
else:
button = self.button.copy()
font = pygame.font.SysFont('Arial', 64, True)
buttonText = font.render(text, 1, (0, 0, 0))
button.blit(buttonText, (horizPadding, vertPadding))
self.display.blit(button, (0, top))
def drawMenu(self):
self.display.blit(self.background, (0, 0))
self.display.blit(self.title, (0, 0))
buttonsTop = 256
buttonHeight = 128
padding = 16
for i in range(len(self.modes)):
text = self.modes[i]
isHighlighted = (i == self.selectionIndex)
top = buttonsTop + (buttonHeight + padding) * i
self.drawButton(text, isHighlighted, top)
def drawBattleSetup(self):
left, top = 160, 96
self.display.blit(self.background, (0, 0))
self.display.blit(self.window, (left, top))
padding = 8
fontSize = 32
font = pygame.font.SysFont('Arial', fontSize, True)
text = font.render('Maps:', 1, (0, 0, 0))
self.display.blit(text, (left + 32, top + 24))
for i in range(len(self.files)):
fileLeft = left + 32
fileTop = top + 64 + (fontSize + padding) * i
fileName = self.files[i]
if i == self.selectionIndex:
color = (96, 96, 96)
else:
color = (0, 0, 0)
text = font.render(fileName, 1, color)
self.display.blit(text, (fileLeft, fileTop))
def drawEditSetup(self):
left, top = 160, 96
self.display.blit(self.background, (0, 0))
self.display.blit(self.window, (left, top))
fontSize = 32
font = pygame.font.SysFont('Arial', fontSize, True)
instructions = ('Use arrow keys to adjust dimensions, or press (o)' +
' to open a file')
text = font.render(instructions, 1, (0, 0, 0))
self.display.blit(text, (left + 32, top + 24))
dimText = '%d rows x %d columns' % (self.rows, self.cols)
dimensions = font.render(dimText, 1, (0, 0, 0))
self.display.blit(dimensions, (left + 32, top + 64))
def redrawAll(self):
if not (self.setupBattle or self.setupEditor):
self.drawMenu()
elif self.setupBattle or self.editorOpenFiles:
self.drawBattleSetup()
elif self.setupEditor:
self.drawEditSetup()
pygame.display.flip()
PyWars = mainMenu('PyWars')
PyWars.run()