-1

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?

Wolf
  • 9,679
  • 7
  • 62
  • 108
charchit
  • 1,492
  • 2
  • 6
  • 17
  • 1
    I think what you want is split the the code into blocks that are common between the two sections and place them into their own functions. Then call those functions in the original section instead. You should group your code logically according to what it does and also to maximize the amount of potential reuse. Please note though that Stackoverflow is meant for specific issues with your code. This might be more suited for https://codereview.stackexchange.com/ Good luck! – mandulaj Jan 15 '21 at 10:28
  • You can restructure your code to use a concept called *scenes*. You can find an example [here](https://stackoverflow.com/questions/59678318/how-can-i-make-my-sprite-launch-an-object-towards-the-mouse-position/59681310?). It works more or less like you described: by using different classes for things like the title screen, the game itself, and a winning/losing screen. – sloth Jan 15 '21 at 10:45

1 Answers1

0

Set up a conditional argument for each thing you want to change within the larger function. Maybe something similar to the following.

welcome=1
pause=0

def myfunction(variable=""):
    if welcome:
       print("welcome") # set some buttons/music
       variable=1
       print(variable)
    if pause:
       print("pause") #  set different buttons/music
       variable=2
       print(variable)
     
    if welcome:
       print("do something unique")
thehand0
  • 1,123
  • 4
  • 14