python - 为什么我的标题画面播放按钮无法启动我的游戏?
问题描述
我正在尝试在我的游戏中放置一个播放按钮。当我启动游戏时,我点击播放按钮,它给了我一个错误
Traceback (most recent call last): File "C:\Users\Zee_S\OneDrive\Desktop\python projects\lil Shooter\Player\Lil Shooter.py", line 370, in <module> game_intro() File "C:\Users\Zee_S\OneDrive\Desktop\python projects\lil Shooter\Player\Lil Shooter.py", line 336, in game_intro button("LETS PLAY!", 20, 450, 115, 50, green, bright_green, run) File "C:\Users\Zee_S\OneDrive\Desktop\python projects\lil Shooter\Player\Lil Shooter.py", line 308, in button action() TypeError: 'bool' object is not callable [Finished in 8.3s]
我已经查看了这个'bool'东西的代码,但我找不到任何东西。这是按钮和标题屏幕的代码
def text_objects(text, font):
textSurface = font.render(text, True, black)
return textSurface, textSurface.get_rect()
def button(msg, x, y, w, h, ic, ac, action=None):
mouse = pygame.mouse.get_pos()
click = pygame.mouse.get_pressed()
if x + w > mouse[0] > x and y + h > mouse[1] > y:
pygame.draw.rect(screen, ac, (x, y, w, h))
if click[0] == 1 and action != None:
action()
else:
pygame.draw.rect(screen, ic, (x, y, w, h))
smallText = pygame.font.SysFont("comicsansms", 20)
textSurf, textRect = text_objects(msg, smallText)
textRect.center = ((x + (w / 2)), (y + (h / 2)))
screen.blit(textSurf, textRect)
def quitgame():
pygame.quit()
quit()
def game_intro():
intro = True
while intro:
for event in pygame.event.get():
# print(event)
if event.type == pygame.QUIT:
pygame.quit()
quit()
screen.fill(white)
largeText = pygame.font.SysFont("comicsansms", 115)
TextSurf, TextRect = text_objects("Lilshooter", largeText)
TextRect.center = ((display_width / 2), (display_height / 2))
screen.blit(TextSurf, TextRect)
button("LETS PLAY!", 20, 450, 115, 50, green, bright_green, run)
button("Quit", 480, 450, 100, 50, red, bright_red, quitgame)
pygame.display.update()
clock.tick(15)
这就是我在游戏循环中调用它的地方
run = True
game_intro()
while run:
[...]
我能得到帮助来解决这个问题吗
解决方案
函数的最后一个参数button
必须是函数。因此它不可能是run
因为run
是一个布尔值。
如果要在intro
按下按钮时更改变量的状态,请编写一个startGame
函数:
def startGame():
global intro
intro = False
传递startGame
给button
, 而不是run
:
button("LETS PLAY!", 20, 450, 115, 50, green, bright_green, startGame)
注意变量intro
必须是全局命名空间中的变量:(
见global
语句)
intro = True
def game_intro():
global intro
while intro:
# [...]
推荐阅读
- list - 如何在函数中返回带有列表参数的 MutableMap(HashMap) 以查找列表中每个元素的频率(Kotlin)
- linux - wp-login.php 谷歌验证码验证
- google-chrome - chrome 上的网络通知限制
- python - 匹配组的数据框列中的数据模式的最佳方法?
- sql - 将 BY 子句分组到临时表
- elasticsearch - 用于审计跟踪的 Elasticsearch
- reactjs - 为什么我的 React 组件渲染多次
- events - 如何使用键盘事件调用绘图函数
- java - 如何使用带有 Java 的 AWS Lambda 使用 Google Drive API(Springboot)
- c# - 测试一个包含任务的普通 C# 函数