python - 在 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)
解决方案
主要问题是您已经实现了 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()
推荐阅读
- android - 无法解决导入 com.google.android.gms
- php - 如何解决有关“Laravel 5.5 中可安装的软件包集( laravelcollective/html 无法安装。)
- ios - 获取 [未知进程名称] CGImageCreate:不支持的图像宽度:84492328
- javascript - 这种通过节点发送电子邮件的方式不安全吗?
- python - 当其中一个线程(chrome)出现异常时,Uswgi 挂起(10 -60 秒)
- cassandra - Cassandra 中 Memtables 和 SStables 中的数据存储
- c# - Iphone 移动应用程序中的圆形图像范围 (Xamarin.Ios / C#)
- google-analytics - 在重定向到 Leadpages 登录页面的域上设置 Google Analytics
- python - Python 3 中的 Python 2 打印功能
- android - Periodic WorkManager 不等待图像下载完成来设置壁纸