I wrote a function to show a welcome screen in pygame:
def welcome():
exit_game = False
while not exit_game:
global red,green
game_window.fill((233,109,102))
screen_text("welcome to snakes",black,160,240)
screen_text("press spacebar to play",black,144,260)
pygame.draw.rect(game_window,red,[76,425,70,32])
pygame.draw.rect(game_window,green,[355,425,70,32])
screen_text("PLAY",black,86,432)
screen_text("QUIT",black,365,432)
for event in pygame.event.get():
if event.type == pygame.QUIT:
exit_game = True
if event.type == pygame.MOUSEBUTTONDOWN:
print(event.button)
if 425<pygame.mouse.get_pos()[1]<457:
if 76<pygame.mouse.get_pos()[0]<146 :
try:
pygame.mixer.music.load(os.path.join(dir,song_num))
pygame.mixer.music.set_volume(0.5)
pygame.mixer.music.play()
except Exception as e:
print("could not load the music")
game_loop()
if 355<pygame.mouse.get_pos()[0]<425:
exit_game = True
if 425<pygame.mouse.get_pos()[1]<457:
if 76<pygame.mouse.get_pos()[0]<146 :
red = (255,0,0)
if 355<pygame.mouse.get_pos()[0]<425:
green = (0,255,0)
else :
red = (200,0,0)
green = (0,200,0)
pass
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_SPACE:
try:
pygame.mixer.music.load(os.path.join(dir,song_num))
pygame.mixer.music.set_volume(0.5)
pygame.mixer.music.play()
except Exception as e:
print("could not load the music")
game_loop()
pygame.display.update()
clock.tick(60)
pygame.quit()
quit()
After that, I wanted to make a pause screen when user presses p
, in which I copied above function and modified it such as
def paused():
while pause:
global red,green
game_window.fill((233,109,102))
screen_text("paused",black,160,240)
# screen_text("",black,144,260)
pygame.draw.rect(game_window,red,[76,425,135,32])
pygame.draw.rect(game_window,green,[355,425,70,32])
screen_text("CONTINUE",black,86,432)
screen_text("QUIT",black,365,432)
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
exit()
if event.type == pygame.MOUSEBUTTONDOWN:
print(event.button)
if 425<pygame.mouse.get_pos()[1]<457:
if 76<pygame.mouse.get_pos()[0]<76+ 135 :
try:
pygame.mixer.music.unpause()
except Exception as e:
print("could not load the music")
unpause()
if 355<pygame.mouse.get_pos()[0]<425:
pygame.quit()
exit()
if 425<pygame.mouse.get_pos()[1]<457:
if 76<pygame.mouse.get_pos()[0]<76 + 135:
red = (255,0,0)
if 355<pygame.mouse.get_pos()[0]<425:
green = (0,255,0)
else :
red = (200,0,0)
green = (0,200,0)
pygame.display.update()
clock.tick(60)
I only want to change a few things like pause the music and change the functions of buttons and keys.
Is there any other way of doing this rather than copying it? Can I inherit like in classes and change some code or add more changes?