python-3.x - 嗨,我正在寻找解决 Pygame 中多个按钮的问题
问题描述
对于上下文,我刚刚进入 Pygame,我正在尝试学习如何使用按钮。当然,还有制作类或函数并同样调用它们来工作。但是,当我打开一个新屏幕时,我希望有一个新按钮,它只能在那里工作。
我的问题出现在第二个场景之后,我不确定如何调用第二个按钮或第三个按钮等。如果我在按钮所在的函数中放置 return,并检查该函数是否与点发生碰撞,整个屏幕变成了一个不理想的按钮。所以我正在寻找解决方法。
这是导致问题的块,我喜欢任何关于优化它的建议。我的猜测是 HeroCreation()[1] 引起了问题,但我不完全确定如何让多个按钮同时工作。
def TextCreation(font, text, coords, screen):
label = font.render(text, True, BLACK)
labelRect = label.get_rect()
labelRect.topright = coords
window.blit(label, labelRect)
return(label, labelRect)
StartButton = TextCreation(largeFont, 'Start Game', (rect_centerx+140, rect_centery - 250), window)
QuitButton = TextCreation(largeFont, 'Quit Game', (rect_centerx+140, rect_centery + 150), window)
def HeroCreation():
window.fill(WHITE)
Greeting = TextCreation(largeFont, 'Welcome, Hero!', (rect_centerx+210, rect_centery-200), window)
Intro1 = TextCreation(paraFont, "I presume you're caught up, but just in case:", (rect_centerx+250, rect_centery + 50), window)
Intro2 = TextCreation(paraFont, "The Rat King's wrecking our kingdom, and you're the only one who can find the Orb of Power.", (rect_centerx+500, rect_centery + 100), window)
Intro3 = TextCreation(paraFont, "Deal with him once and for all! Do us proud, hero.",(rect_centerx+260, rect_centery + 150), window)
pygame.draw.rect(window, LIGHT_GREEN, (rect_centerx - 170, rect_centery-80, 400, 70), 0)
ProceedtoMapButton = TextCreation(paraFont, "Are you ready?", (rect_centerx+110, rect_centery-70), window)
return (ProceedtoMapButton)
pygame.display.flip()
run = True
while run:
for event in pygame.event.get():
if event.type == pygame.MOUSEBUTTONDOWN and event.button == 1:
mouse = pygame.mouse.get_pos()
if QuitButton[1].collidepoint(mouse):
pygame.quit()
sys.exit()
if StartButton[1].collidepoint(mouse):
HeroCreation()
if HeroCreation()[1].collidepoint(mouse):
drawGrid(width, height, window)
if event.type == QUIT:
pygame.quit()
sys.exit()
解决方案
对于每个屏幕,创建新按钮,然后在屏幕退出时将其删除。在您的循环中,在碰撞检查之前检查按钮是否在屏幕上。
试试这个代码:
HeroButton = None
while run:
for event in pygame.event.get():
if event.type == pygame.MOUSEBUTTONDOWN and event.button == 1:
mouse = pygame.mouse.get_pos()
if QuitButton[1].collidepoint(mouse): # quit button on every screen ?
pygame.quit()
sys.exit()
if StartButton != None and StartButton[1].collidepoint(mouse):
StartButton = None # done with this button
HeroButton = HeroCreation()
elif HeroButton != None and HeroButton[1].collidepoint(mouse):
HeroButton = None # done with this button
drawGrid(width, height, window) # start game?
if event.type == QUIT:
pygame.quit()
sys.exit()
推荐阅读
- c++ - 比较一般树中两个节点的所有子节点
- r - ggplot不显示点
- angular - Angular 需要微前端的 polyfills.js 以及带有 main.js 的 vendor.js 来加载元素。并且只加载第一个声明的 main.js
- javascript - 我可以用 p5.js 为一个圆从一个点移动到另一个点设置动画吗
- python - Pandas:通过在行级别应用自定义函数来创建列
- javascript - 填充 Google Charts API,得到“列标题行必须是一个数组”。
- react-testing-library - 如何使用反应测试库修复错误“在 jest.settimeout() 指定的 5000 毫秒内未调用异步回调?
- python - 通过在没有循环的情况下转换其 RGB 将 RGB 值更改为单个值
- regex - 更新 IIS 中的 URL 查询字符串 IIS 中的 URL 重写
- databricks - 仅在计划作业运行时无法推断 Parquet 的架构