-
Notifications
You must be signed in to change notification settings - Fork 3
/
mainMenu.py
113 lines (90 loc) · 4.55 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
# -*- coding: utf-8 -*-
"""
Created on Tue Jan 28 19:25:11 2020
Ironseed Main Menu.
@author: Nuke Bloodaxe
"""
import pygame, buttons, sys, os
import helper_functions as h
import global_constants as g
class MainMenu(object):
def __init__(self):
self.menuStage = 0 # Current menu state.
self.rollingStarfield = 0 # Pixel index for the background starfield.
self.starField = pygame.image.load(os.path.join('Graphics_Assets', 'cloud.png'))
self.musicState = False
# Prepare starfield for blitting.
self.starFieldScaled = pygame.transform.scale(self.starField, (g.width, g.height))
# Main Menu Graphics
self.menuGraphic = pygame.image.load(os.path.join('Graphics_Assets', 'intro5.png'))
# Prepare menu graphic for blitting
self.menuGraphicScaled = pygame.transform.scale(self.menuGraphic, (g.width, g.height))
# Positional buttons for the screen options.
self.newGame = buttons.Button(int((g.height/200)*10),
int((g.width/320)*113),
(int((g.width/320)*35), int((g.height/200)*153)))
self.intro = buttons.Button(int((g.height/200)*10),
int((g.width/320)*96),
(int((g.width/320)*54), int((g.height/200)*173)))
self.quitGame = buttons.Button(int((g.height/200)*10),
int((g.width/320)*88),
(int((g.width/320)*168), int((g.height/200)*173)))
self.loadGame = buttons.Button(int((g.height/200)*10),
int((g.width/320)*105),
(int((g.width/320)*168), int((g.height/200)*153)))
# Mouse interactions, click on a menu item and make it work.
def interact(self, mouseButton):
currentPosition = pygame.mouse.get_pos()
if self.newGame.within(currentPosition):
print("New Game")
self.musicState = False
return 1
elif self.intro.within(currentPosition):
print("Rerun Intro")
self.musicState = False
return 3
elif self.quitGame.within(currentPosition): # Quit game viciously.
print("Exit")
pygame.quit()
sys.exit()
elif self.loadGame.within(currentPosition):
print("Load Game")
self.musicState = False
return 11 # Should be 11, others are test values.
# Note: Running load game after begin game allows all
# necessary data to be setup before testing a different state.
return 3 # Restart the intro as a failsafe.
# Update state routine
def update(self, displaySurface):
return self.MainMenuLoop(displaySurface)
def MainMenuLoop(self, displaySurface):
# Start main intro music
if self.menuStage == 0:
pygame.mixer.music.load(os.path.join('sound', 'INTRO2.OGG'))
pygame.mixer.music.play()
self.musicState = True
self.menuStage = 1 # normally 1, use other stages for debug.
if self.menuStage == 1:
if self.musicState == False:
pygame.mixer.music.load(os.path.join('sound', 'INTRO2.OGG'))
pygame.mixer.music.play()
self.musicState = True
# rewind and start music playing again if track end reached.
if not pygame.mixer.music.get_busy():
pygame.mixer.music.play()
# Blit starfield.
starFieldBlit = pygame.PixelArray(self.starFieldScaled)
displaySurfaceBlit = pygame.PixelArray(displaySurface)
# Advance and wrap the starfield at 15 pixels per frame.
self.rollingStarfield += 15
if self.rollingStarfield >= g.width:
self.rollingStarfield = 0
for x in range(0, g.width):
bitmapSafeX = h.safeWrap(g.width, x, self.rollingStarfield)
safeX = h.safeWrap(g.width, x, 0)
for y in range(0, g.height):
displaySurfaceBlit[safeX][y] = starFieldBlit[bitmapSafeX][y]
starFieldBlit.close()
displaySurfaceBlit.close()
displaySurface.blit(self.menuGraphicScaled, (0, 0))
return 2