1
+ """
2
+ Importing important libraries
3
+ """
4
+ import pygame , sys
5
+
6
+ """
7
+ Setting up an environment to initialize pygame
8
+ """
9
+ mainClock = pygame .time .Clock ()
10
+ from pygame .locals import *
11
+
12
+ pygame .init ()
13
+ pygame .display .set_caption ('game base' )
14
+ screen = pygame .display .set_mode ((600 , 300 ), 0 , 32 )
15
+
16
+ # setting font settings
17
+ font = pygame .font .SysFont (None , 30 )
18
+
19
+ """
20
+ A function that can be used to write text on our screen and buttons
21
+ """
22
+
23
+
24
+ def draw_text (text , font , color , surface , x , y ):
25
+ textobj = font .render (text , 1 , color )
26
+ textrect = textobj .get_rect ()
27
+ textrect .topleft = (x , y )
28
+ surface .blit (textobj , textrect )
29
+
30
+
31
+ # A variable to check for the status later
32
+ click = False
33
+
34
+
35
+ # Main container function that holds the buttons and game functions
36
+ def main_menu ():
37
+ while True :
38
+
39
+ screen .fill ((0 , 190 , 255 ))
40
+ draw_text ('Main Menu' , font , (0 , 0 , 0 ), screen , 250 , 40 )
41
+
42
+ mx , my = pygame .mouse .get_pos ()
43
+
44
+ # creating buttons
45
+ button_1 = pygame .Rect (200 , 100 , 200 , 50 )
46
+ button_2 = pygame .Rect (200 , 180 , 200 , 50 )
47
+
48
+ # defining functions when a certain button is pressed
49
+ if button_1 .collidepoint ((mx , my )):
50
+ if click :
51
+ game ()
52
+ if button_2 .collidepoint ((mx , my )):
53
+ if click :
54
+ options ()
55
+ pygame .draw .rect (screen , (255 , 0 , 0 ), button_1 )
56
+ pygame .draw .rect (screen , (255 , 0 , 0 ), button_2 )
57
+
58
+ # writing text on top of button
59
+ draw_text ('PLAY' , font , (255 , 255 , 255 ), screen , 270 , 115 )
60
+ draw_text ('OPTIONS' , font , (255 , 255 , 255 ), screen , 250 , 195 )
61
+
62
+ click = False
63
+ for event in pygame .event .get ():
64
+ if event .type == QUIT :
65
+ pygame .quit ()
66
+ sys .exit ()
67
+ if event .type == KEYDOWN :
68
+ if event .key == K_ESCAPE :
69
+ pygame .quit ()
70
+ sys .exit ()
71
+ if event .type == MOUSEBUTTONDOWN :
72
+ if event .button == 1 :
73
+ click = True
74
+
75
+ pygame .display .update ()
76
+ mainClock .tick (60 )
77
+
78
+
79
+ """
80
+ This function is called when the "PLAY" button is clicked.
81
+ """
82
+
83
+
84
+ def game ():
85
+ running = True
86
+ while running :
87
+ screen .fill ((0 , 0 , 0 ))
88
+
89
+ draw_text ('GAME SCREEN' , font , (255 , 255 , 255 ), screen , 20 , 20 )
90
+ for event in pygame .event .get ():
91
+ if event .type == QUIT :
92
+ pygame .quit ()
93
+ sys .exit ()
94
+ if event .type == KEYDOWN :
95
+ if event .key == K_ESCAPE :
96
+ running = False
97
+
98
+ pygame .display .update ()
99
+ mainClock .tick (60 )
100
+
101
+
102
+ """
103
+ This function is called when the "OPTIONS" button is clicked.
104
+ """
105
+
106
+
107
+ def options ():
108
+ running = True
109
+ while running :
110
+ screen .fill ((0 , 0 , 0 ))
111
+
112
+ draw_text ('OPTIONS SCREEN' , font , (255 , 255 , 255 ), screen , 20 , 20 )
113
+ for event in pygame .event .get ():
114
+ if event .type == QUIT :
115
+ pygame .quit ()
116
+ sys .exit ()
117
+ if event .type == KEYDOWN :
118
+ if event .key == K_ESCAPE :
119
+ running = False
120
+
121
+ pygame .display .update ()
122
+ mainClock .tick (60 )
123
+
124
+
125
+ main_menu ()
0 commit comments