【问题标题】:Maximum recursion depth exceeded when trying to display object尝试显示对象时超出最大递归深度
【发布时间】:2018-06-08 13:37:51
【问题描述】:

我正在尝试为一个显示this image 的小程序创建一个菜单。我正在使用this tutorial。我的代码如下所示:

import pygame

pygame.init()

height = 1366 #The height and width of our window
width = 769

window = pygame.display.set_mode((height,width)) 
pygame.display.set_caption("Score")

white = (255,255,255) #This block defines all the colors in (R,G,B) format

clock = pygame.time.Clock()
crashed = False

flask = pygame.image.load('flask.jpg') #This block is for loading all images

def flask(x_f,y_f):
    window.blit(flask(x_f,y_f))
x_f = (width * 0.45)
y_f = (height * 0.8)

while not crashed:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            crashed = True

    window.fill(white)
    flask(x_f,y_f)

    pygame.display.update()
    clock.tick(60)

pygame.quit()
quit()

但是,我不断收到运行时错误:RecursionError:超出最大递归深度。这些在第 19 行(3 次)和第 29 行(一次)。我只是想知道我到底做错了什么,因为我一直在密切关注本教程。

【问题讨论】:

  • 您不能将名称flask 用于图像和显示图像的功能。

标签: python-3.x recursion pygame


【解决方案1】:

这是因为 flask 函数在其函数体内一次又一次地调用自身,直到超过 1000 次递归(默认值)的最大递归深度。

给图像一个不同的名字,然后在flask函数中blit它。

# Use the `convert` method to improve the performance.
flask_image = pygame.image.load('flask.jpg').convert()

def flask(x_f, y_f):
    window.blit(flask_image, (x_f, y_f))

【讨论】:

  • 谢谢,原来我也忘了在 window.blit 行中加逗号!
猜你喜欢
  • 2016-05-23
  • 1970-01-01
  • 1970-01-01
  • 2011-05-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-12-01
相关资源
最近更新 更多