forked from Dave-YP/cosmic-heat-pygame
-
Notifications
You must be signed in to change notification settings - Fork 0
/
menu.py
136 lines (115 loc) · 4.31 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
import sys
import random
import pygame
import pygame.mixer
from classes.constants import WIDTH, HEIGHT, BLACK, WHITE, RED
def animate_screen():
for i in range(0, 20):
screen.blit(mainmenu_img, (0, 0))
pygame.display.flip()
pygame.time.wait(10)
screen.blit(mainmenu_img, (random.randint(-5, 5), random.randint(-5, 5)))
pygame.display.flip()
pygame.time.wait(10)
pygame.mixer.init()
pygame.init()
pygame.mixer.music.load('game_sounds/menu.mp3')
pygame.mixer.music.set_volume(0.25)
pygame.mixer.music.play(-1)
pygame.mixer.set_num_channels(20)
for i in range(20):
channel = pygame.mixer.Channel(i)
channel.set_volume(0.25)
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("Main Menu")
clock = pygame.time.Clock()
mainmenu_img = pygame.image.load('images/mainmenu.jpg').convert()
mainmenu_img = pygame.transform.scale(mainmenu_img, (WIDTH, HEIGHT))
logo_img = pygame.image.load('images/ch.png').convert_alpha()
logo_x = (WIDTH - logo_img.get_width()) // 2
logo_y = 50
play_button_rect = pygame.Rect(WIDTH // 2 - 100, HEIGHT // 2 - 25, 205, 50)
quit_button_rect = pygame.Rect(WIDTH // 2 - 100, HEIGHT // 2 + 50, 205, 50)
pygame.mixer.music.load('game_sounds/menu.mp3')
pygame.mixer.music.play(-1)
explosion_sound = pygame.mixer.Sound('game_sounds/explosions/explosion1.wav')
explosion_sound.set_volume(0.25)
selected_button = 0
show_menu = True
joystick = None
if pygame.joystick.get_count() > 0:
joystick = pygame.joystick.Joystick(0)
joystick.init()
while show_menu:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
if event.type == pygame.MOUSEBUTTONDOWN:
x, y = event.pos
if play_button_rect.collidepoint(x, y):
explosion_sound.play()
animate_screen()
show_menu = False
import main
main.main()
break
elif quit_button_rect.collidepoint(x, y):
pygame.quit()
sys.exit()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_UP:
selected_button = 0
elif event.key == pygame.K_DOWN:
selected_button = 1
elif event.key == pygame.K_RETURN:
if selected_button == 0:
explosion_sound.play()
animate_screen()
show_menu = False
screen.fill(BLACK)
import main
main.main()
break
elif selected_button == 1:
pygame.quit()
sys.exit()
if joystick:
if event.type == pygame.JOYBUTTONDOWN:
if event.button == 0:
if selected_button == 0:
explosion_sound.play()
animate_screen()
show_menu = False
screen.fill(BLACK)
import game
game.main()
break
elif selected_button == 1:
pygame.quit()
sys.exit()
elif event.type == pygame.JOYHATMOTION:
if event.value[1] == 1:
selected_button = 0
elif event.value[1] == -1:
selected_button = 1
screen.blit(mainmenu_img, (0, 0))
screen.blit(logo_img, (logo_x, logo_y))
font = pygame.font.SysFont('Comic Sans MS', 40)
text = font.render("Play", True, WHITE)
pygame.draw.rect(screen, BLACK, play_button_rect, border_radius=10)
if selected_button == 0:
pygame.draw.rect(screen, RED, play_button_rect, border_radius=10, width=4)
text_rect = text.get_rect()
text_rect.center = play_button_rect.center
screen.blit(text, text_rect)
text = font.render("Exit", True, WHITE)
pygame.draw.rect(screen, BLACK, quit_button_rect, border_radius=10)
if selected_button == 1:
pygame.draw.rect(screen, RED, quit_button_rect, border_radius=10, width=4)
text_rect = text.get_rect()
text_rect.center = quit_button_rect.center
screen.blit(text, text_rect)
pygame.display.flip()
clock.tick(60)
pygame.quit()