首页 > 解决方案 > 在 Pygame 中滚动无限背景

问题描述

嗨,我正在尝试在我的菜单屏幕中渲染 menu_background 并显示我的菜单选项。当我运行我的代码时,我得到的只是图像滚动,但我的菜单选项和音频没有播放和显示。任何人都可以帮助编写代码,使其在音频播放和我的菜单选项出现的地方工作?

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) 

标签: pythonpygame

解决方案


主要问题是您已经实现了 2 个主要的应用程序循环。循环一个接一个地执行。(顺便说一句,这个循环中的第一个永远不会终止)

将所有实现放在一个循环中。应用程序循环必须

  • 处理事件和改变状态

  • 绘制背景

  • 绘制场景和菜单

  • 更新显示

例如:

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()

推荐阅读