-
Notifications
You must be signed in to change notification settings - Fork 2
/
menu.py
152 lines (117 loc) · 4.86 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
import pygame
import sys
import subprocess
pygame.init()
pygame.mixer.init()
WIDTH = 600
HEIGHT = 550
SIZE = (WIDTH, HEIGHT)
LIGHT = (239, 239, 239)
ICON = pygame.image.load("./public/icon.png")
SCREEN = pygame.display.set_mode(SIZE)
# create the screens
menuScreen = pygame.Surface(SIZE)
menuScreen.fill(LIGHT)
gameScreen = pygame.Surface(SIZE)
gameScreen.fill(LIGHT)
# set up the display
window = pygame.display.set_mode(SIZE)
pygame.display.set_caption("Maze Runner")
pygame.display.set_icon(ICON)
# setup the main menu
RED = (255, 107, 107)
GREEN = (46, 204, 113)
BLACK = (0, 0, 0)
bgImg = pygame.image.load("./assets/image/bg_menu.png")
bgImgAspectRatio = bgImg.get_width() / bgImg.get_height()
bgImgH = int(950 / bgImgAspectRatio)
bgImg = pygame.transform.scale(bgImg, (900, bgImgH))
bgImgY = -50 # initial y-position of the background image
titleFont = "./assets/fonts/JoystickBold-62LA.ttf"
titleFont = pygame.font.Font(titleFont, 87)
titleText = titleFont.render("Mazerun", True, BLACK)
titlePosX = WIDTH // 2 - titleText.get_width() // 2
titlePosY = 190
titlePos = (titlePosX, titlePosY)
startFont = "./assets/fonts/TrulyMadlyDpad-a72o.ttf"
startFont = pygame.font.Font(startFont, 27)
startText = startFont.render("Start", True, LIGHT)
# store the original dimensions of the start text
original_text_width, original_text_height = startText.get_width(), startText.get_height()
# initial position and dimensions of the button
startBtnX = WIDTH // 2 - 225 // 2
startBtnY = (HEIGHT // 2 - 53 // 2) + 100
startBtnWidth = 225
startBtnHeight = 53
startBtnRect = pygame.Rect(startBtnX, startBtnY, startBtnWidth, startBtnHeight)
# set initial scale factors
btn_scale = 1.0
zoom_speed = 0.0017 # Adjust the speed of the zooming animation
# set initial values for background image movement
bg_speed = 0.1 # Adjust the speed of the movement
bg_direction = 1 # 1 for down, -1 for up
# initial screen
currentScreen = menuScreen
# create the clock to control the frame rate
clock = pygame.time.Clock()
# load the sounds
sound_click = pygame.mixer.Sound("./assets/audio/click.mp3")
sound_hover = pygame.mixer.Sound("./assets/audio/hover.mp3")
sound_menu = pygame.mixer.Sound("./assets/audio/bg_wires.mp3")
sound_menu.play(-1)
startBtnColor = GREEN
isStartBtnFocused = False
while True:
if startBtnRect.collidepoint(pygame.mouse.get_pos()):
btn_scale = 1
pygame.mouse.set_system_cursor(pygame.SYSTEM_CURSOR_HAND)
isStartBtnFocused = True
else:
isStartBtnFocused = False
pygame.mouse.set_system_cursor(pygame.SYSTEM_CURSOR_ARROW)
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
elif not isStartBtnFocused and startBtnRect.collidepoint(pygame.mouse.get_pos()):
sound_hover.play()
elif isStartBtnFocused and event.type == pygame.MOUSEBUTTONDOWN:
startBtnColor = (84, 233, 139)
sound_click.play()
subprocess.Popen(["python", "main.py"])
sys.exit()
elif isStartBtnFocused and event.type == pygame.MOUSEBUTTONUP:
startBtnColor = GREEN
# idle animation: continuously zoom the button in and out
if not isStartBtnFocused:
btn_scale += zoom_speed
if btn_scale < 1.0 or btn_scale > 1.1:
zoom_speed = -zoom_speed
# background image movement: move the background image up and down
bgImgY += bg_speed * bg_direction
if bgImgY > 1 or bgImgY < -80:
bg_direction *= -1
# scale the button rect accordingly
scaled_btn_rect = startBtnRect
scaled_btn_rect.width = int(startBtnWidth * btn_scale)
scaled_btn_rect.height = int(startBtnHeight * btn_scale)
# adjust the position of the button to keep it centered
scaled_btn_rect.x = WIDTH // 2 - scaled_btn_rect.width // 2
scaled_btn_rect.y = (HEIGHT // 2 - scaled_btn_rect.height // 2) + 50
# maintain the original aspect ratio of the text and scale it within the button rect
scaled_text_width = int(original_text_width * (scaled_btn_rect.width / startBtnWidth))
scaled_text_height = int(original_text_height * (scaled_btn_rect.height / startBtnHeight))
scaled_text = pygame.transform.scale(startText, (scaled_text_width, scaled_text_height))
# draw the button
menuScreen.fill(LIGHT)
pygame.draw.rect(menuScreen, startBtnColor, scaled_btn_rect, 0, 50)
menuScreen.blit(titleText, titlePos)
menuScreen.blit(scaled_text, (scaled_btn_rect.x + (scaled_btn_rect.width - scaled_text_width) // 2, scaled_btn_rect.y + (scaled_btn_rect.height - scaled_text_height) // 2))
# draw the background image with movement
menuScreen.blit(bgImg, (-150, bgImgY))
# draw the current screen onto the window
window.blit(menuScreen, (0, 0))
# update the display
pygame.display.flip()
# cap the frame rate
clock.tick_busy_loop(60)