作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在制作一个开始屏幕按钮,当您按下播放键时,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/
我正在制作一个开始屏幕按钮,当您按下播放键时,game_loop 就会开始,但它说 game_loop 未定义,即使我认为我已经定义了。我似乎无法弄清楚这个问题,所以有人知道如何解决这个问题吗?这会有
我是一名优秀的程序员,十分优秀!