1

Hi I am trying to render out the menu_background in my menu screen with my menu options showing. When I run my code all I get is the image scrolling but my menu options and audio do not play and show. Anyone can help write code that makes it work where the audio plays and my menu options show up?

import pygame, sys, random, time, os, math
from pygame.locals import *

fps = pygame.time.Clock()

global screen
screen = pygame.display.set_mode((WIDTH, HEIGHT),pygame.FULLSCREEN)
display = pygame.Surface((400,250))

def menu_background():
    menu_bg = pygame.image.load("assets/images/menu_bg.png").convert()
    x = 0

    while True:

        for event in pygame.event.get():
            if event.type == QUIT:
                pygame.quit()
                sys.exit()

        rel_x = x % menu_bg.get_rect().width
        screen.blit(menu_bg, (rel_x - menu_bg.get_rect().width, 0))
        if rel_x < WIDTH:
                screen.blit(menu_bg, (rel_x, 0))
        x -= 1
        pygame.display.update()
        fps.tick(120)

def menu():
    bg = menu_background()
    menu_options = ['Play','Controls','Highscores','Settings','Credits','Quit']
    menu_choice = 0
    in_menu = True

    pygame.mixer.music.load('assets/audio/mainmenu.wav')

    while in_menu:
        bg(display)

        n = 0
        for option in menu_options:

        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.key == K_UP:
                    menu_choice -= 1
                    if menu_choice < 0:
                        menu_choice = len(menu_options)-1
                if event.key == K_DOWN:
                    menu_choice += 1
                    if menu_choice >= len(menu_options):
                        menu_choice = 0
                if event.key == K_SPACE:
                    choice = menu_options[menu_choice]
                    if choice == 'Play':
                        play()
                    if choice == 'Controls':
                        controls()

        screen.blit(pygame.transform.scale(display,(WIDTH,HEIGHT)),(0,0))
        pygame.display.update()
        fps.tick(60) 
Rabbid76
  • 177,135
  • 25
  • 101
  • 146
  • I see that you load the music `pygame.mixer.load(...)`, but I don't see `pygame.mixer.music.play(0)` is it missing? This tutorial looks like it may help with getting the background sound: https://nerdparadise.com/programming/pygame/part3 – Nathan Jan 15 '20 at 13:13

1 Answers1

1

The major issue is that you have implemented 2 main application loops. The loops are executed one after the other. (By the way, the first of this loops never terminates)

Put all the implementation in one loop. The application loop has to

  • handle the events and change states

  • draw the background

  • draw the scene and menu

  • update the display

e.g.:

def menu():

    x = 0
    menu_bg = pygame.image.load("assets/images/menu_bg.png").convert()

    menu_options = ['Play','Controls','Highscores','Settings','Credits','Quit']
    menu_choice = 0
    in_menu = True

    # load and play music
    pygame.mixer.music.load('assets/audio/mainmenu.wav')
    pygame.mixer.music.play()

    while in_menu:
        fps.tick(120)

        # handle events
        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.key == K_UP:
                    menu_choice -= 1
                    if menu_choice < 0:
                        menu_choice = len(menu_options)-1
                if event.key == K_DOWN:
                    menu_choice += 1
                    if menu_choice >= len(menu_options):
                        menu_choice = 0
                if event.key == K_SPACE:
                    choice = menu_options[menu_choice]
                    if choice == 'Play':
                        play()
                    if choice == 'Controls':
                        controls()

        # draw background
        rel_x = x % menu_bg.get_rect().width
        screen.blit(menu_bg, (rel_x - menu_bg.get_rect().width, 0))
        if rel_x < WIDTH:
                screen.blit(menu_bg, (rel_x, 0))
        x -= 1

        # draw menu
        #n = 0
        #for option in menu_options:
        #    [...]

        # update disaplay
        pygame.display.update()

menu()
Rabbid76
  • 177,135
  • 25
  • 101
  • 146