-
Notifications
You must be signed in to change notification settings - Fork 0
/
game_menu.py
131 lines (106 loc) · 3.84 KB
/
game_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
"""
Importing important libraries
"""
import pygame, sys
"""
Setting up an environment to initialize pygame
"""
from pygame.locals import *
"""
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)
# Main container function that holds the buttons and game functions
def start_options_menu(screen,font,mainClock,click):
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(screen,font,mainClock,click=False):
running = True
game_rules=['-Space -> Fire Cannon',
'-Arrow Left -> Spaceship Move Left',
'-Arrow Right -> Spaceship Move Right',
'-Keyboard Button: ESC -> Game Exit / Back']
while running:
bg3 = pygame.image.load("./images/james3.jpg")
screen.blit(bg3, (0, 0))
#screen.fill((0,190,255))
draw_text('Control Instructions', pygame.font.SysFont(None, 45), (0, 0, 255), screen, 200, 130)
# draw_text(game_rules[0], font, (255, 255, 255), screen, 20, 20)
for index,rules in enumerate(game_rules):
# print(index,rules)
draw_text(rules, font, (100, 0, 0), screen, 160, index*30+200)
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)
if __name__ == '__main__':
mainClock = pygame.time.Clock()
pygame.init()
pygame.display.set_caption('game base')
screen = pygame.display.set_mode((600, 300),0,32)
# A variable to check for the status later
click = False
font = pygame.font.SysFont(None, 30)
#setting font settings
font = pygame.font.SysFont(None, 30)
options(screen,font,mainClock,click)