gpt4 book ai didi

python - 在 Pygame 中滚动无限背景

转载 作者:行者123 更新时间:2023-12-01 06:34:17 24 4
gpt4 key购买 nike

您好,我正在尝试在菜单屏幕中渲染 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)

最佳答案

主要问题是您已经实现了 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()

关于python - 在 Pygame 中滚动无限背景,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59751959/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com