gpt4 book ai didi

python - 为什么我的 game_loop 没有定义,尽管它是?

转载 作者:行者123 更新时间:2023-12-01 00:41:05 26 4
gpt4 key购买 nike

我正在制作一个开始屏幕按钮,当您按下播放键时,game_loop 就会开始,但它说 game_loop 未定义,即使我认为我已经定义了。我似乎无法弄清楚这个问题,所以有人知道如何解决这个问题吗?这会有很大帮助! :)

def button(x, y, width, height, ic, ac, action=None):
mouse = pygame.mouse.get_pos()
# print(mouse)

if x + width > mouse[0] > x and y + height > mouse[1] > y:
pygame.draw.rect(screen, ac, (x, y, width, height), 0)
for event in pygame.event.get():
if event.type == pygame.MOUSEBUTTONDOWN and action is not None:
if action == 'play':
game_loop()
elif action == 'quit':
pygame.quit()
quit()
else:
pygame.draw.rect(screen, ic, (x, y, width, height), 0)

def start_screen():

start = True

while start:
clock.tick(FPS)
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
if event.type == pygame.KEYUP:
start = False

screen.blit(background, background_rect)

button(180, 285, 120, 60, BLUE, LIGHT_BLUE, 'play')
button(202, HEIGHT - 150, 80, 40, WHITE, GRAY, 'quit')
button(310, 45, 140, 30, WHITE, GRAY)
button(20, 45, 140, 30, WHITE, GRAY)

draw_text(screen, "Shoot 'Em Up", 80, WIDTH / 2, HEIGHT / 4, WHITE)
draw_text(screen, "Instructions", 30, 90, 53, BLACK)
draw_text(screen, "Credits", 35, 378, 50, BLACK)
draw_text(screen, "Play", 55, WIDTH / 2, HEIGHT / 2, WHITE)
draw_text(screen, "Quit", 30, WIDTH / 2, HEIGHT - 137, BLUE)

pygame.display.flip()

start_screen()
running = True

def game_loop():
while running:
*game_loop code*
game_loop()

最佳答案

Format your code correctly and it should work.

 def button(x, y, width, height, ic, ac, action=None):

mouse = pygame.mouse.get_pos()
# print(mouse)

if x + width > mouse[0] > x and y + height > mouse[1] > y:
pygame.draw.rect(screen, ac, (x, y, width, height), 0)
for event in pygame.event.get():
if event.type == pygame.MOUSEBUTTONDOWN and action is not None:
if action == "play":
game_loop()
elif action == "quit":
pygame.quit()
quit()
else:
pygame.draw.rect(screen, ic, (x, y, width, height), 0)

def game_loop():
print('game loop')
game_loop()

关于python - 为什么我的 game_loop 没有定义,尽管它是?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57343478/

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