-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPygame menu.py
125 lines (94 loc) · 3.07 KB
/
Pygame 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
"""
Importing important libraries
"""
import pygame, sys
"""
Setting up an environment to initialize pygame
"""
mainClock = pygame.time.Clock()
from pygame.locals import *
pygame.init()
pygame.display.set_caption('game base')
screen = pygame.display.set_mode((600, 300), 0, 32)
# setting font settings
font = pygame.font.SysFont(None, 30)
"""
A function that can be used to write text on our screen and buttons
"""
def draw_text(text, font, color, surface, x, y):
textobj = font.render(text, 1, color)
textrect = textobj.get_rect()
textrect.topleft = (x, y)
surface.blit(textobj, textrect)
# A variable to check for the status later
click = False
# Main container function that holds the buttons and game functions
def main_menu():
while True:
screen.fill((0, 190, 255))
draw_text('Main Menu', font, (0, 0, 0), screen, 250, 40)
mx, my = pygame.mouse.get_pos()
# creating buttons
button_1 = pygame.Rect(200, 100, 200, 50)
button_2 = pygame.Rect(200, 180, 200, 50)
# defining functions when a certain button is pressed
if button_1.collidepoint((mx, my)):
if click:
game()
if button_2.collidepoint((mx, my)):
if click:
options()
pygame.draw.rect(screen, (255, 0, 0), button_1)
pygame.draw.rect(screen, (255, 0, 0), button_2)
# writing text on top of button
draw_text('PLAY', font, (255, 255, 255), screen, 270, 115)
draw_text('OPTIONS', font, (255, 255, 255), screen, 250, 195)
click = False
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
if event.type == KEYDOWN:
if event.key == K_ESCAPE:
pygame.quit()
sys.exit()
if event.type == MOUSEBUTTONDOWN:
if event.button == 1:
click = True
pygame.display.update()
mainClock.tick(60)
"""
This function is called when the "PLAY" button is clicked.
"""
def game():
running = True
while running:
screen.fill((0, 0, 0))
draw_text('GAME SCREEN', font, (255, 255, 255), screen, 20, 20)
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
if event.type == KEYDOWN:
if event.key == K_ESCAPE:
running = False
pygame.display.update()
mainClock.tick(60)
"""
This function is called when the "OPTIONS" button is clicked.
"""
def options():
running = True
while running:
screen.fill((0, 0, 0))
draw_text('OPTIONS SCREEN', font, (255, 255, 255), screen, 20, 20)
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
if event.type == KEYDOWN:
if event.key == K_ESCAPE:
running = False
pygame.display.update()
mainClock.tick(60)
main_menu()