我一直试图制作一个递归的矩形,我想让每次重复时矩形都向前移动,这样当它进入一个无限的矩形时它会产生一个动作。我试图在每次递归时使尺寸变大但失败,因为它不会递归或什么都不会出现。任何提示或如何做到这一点将不胜感激。我从pygamearcade实现了这个示例。我希望得到这样的感觉,因为一个人进入矩形,并且可以实现,因为每次经历重复时矩形变大。所以任何提示或如何做到这一点都很好。谢谢
import pygame
# Colors
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
def recursive_draw(x, y, width, height):
""" Recursive rectangle function. """
pygame.draw.rect(screen, WHITE,
[x, y, width, height],
1)
speed = [10,0]
# Is the rectangle wide enough to draw again?
while (width > 14):
# Scale down
x += width * .1
y += height * .1
width *= .8
height *= .8
# Recursively draw again
recursive_draw(x, y, width, height)
pygame.init()
#rectanglelist = [big()]
# Set the height and width of the screen
size = [700, 500]
screen = pygame.display.set_mode(size)
pygame.display.set_caption("My Game")
# Loop until the user clicks the close button.
done = False
# Used to manage how fast the screen updates
clock = pygame.time.Clock()
# -------- Main Program Loop -----------
while not done:
for event in pygame.event.get():
if event.type == pygame.QUIT:
done = True
# Set the screen background
screen.fill(BLACK)
# ALL CODE TO DRAW SHOULD GO BELOW THIS COMMENT
recursive_draw(0, 0, 700, 500)
# ALL CODE TO DRAW SHOULD GO ABOVE THIS COMMENT
# Go ahead and update the screen with what we've drawn.
pygame.display.flip()
# Limit to 60 frames per second
clock.tick(60)
# Be IDLE friendly. If you forget this line, the program will 'hang'
# on exit.
pygame.quit()
答案 0 :(得分:0)
问题是你的 recursive_draw()
函数实际上不是递归函数,因为递归函数是有条件地调用自身的函数< /强>:
每个设计合理的递归函数必须至少一个基本案例 [A]并且必须将问题重新定义为子问题才能实现基础案例[B]。
def countdown(n):
if n < 1:
print "Lift Off" #[A]
else:
print n
countdown(n - 1) #[B]
您可以为您的代码做些什么:
while
循环
这是重要的部分或递归:该函数调用自身,除非某个条件不再为真(在您的情况下为width > 14
)。有关详细信息,请参阅SO上的How can I build a recursive function in python?或Recursion function in python。更新后的功能(来自http://www.balloonbuilding.com/的代码 Paul Vincent Craven ):
def recursive_draw(x, y, width, height):
""" Recursive rectangle function. """
pygame.draw.rect(screen, BLACK, (x, y, width, height), 1)
# Is the rectangle wide enough to draw again?
if(width > 14):
# Scale down
x += width * .1
y += height * .1
width *= .8
height *= .8
# Recursively draw again
recursive_draw(x, y, width, height)
我希望这会有所帮助:)
编辑:
更新后的计划:
import pygame
# Colors
BLUE = (55, 155, 255)
WHITE = (255, 255, 255)
def recursive_draw(x, y, width, height):
""" Recursive rectangle function. """
pygame.draw.rect(screen, WHITE, (x, y, width, height), 2)
# Is the rectangle wide enough to draw again?
if(width > 14):
# Scale down
x += width * .1
y += height * .1
width *= .8
height *= .8
# Recursively draw again
recursive_draw(x, y, width, height)
speed = [10,0]
pygame.init()
#rectanglelist = [big()]
# Set the height and width of the screen
size = [700, 500]
screen = pygame.display.set_mode(size)
pygame.display.set_caption("My Game")
# Loop until the user clicks the close button.
done = False
# Used to manage how fast the screen updates
clock = pygame.time.Clock()
# -------- Main Program Loop -----------
while not done:
for event in pygame.event.get():
if event.type == pygame.QUIT:
done = True
# Set the screen background
screen.fill(BLUE)
# ALL CODE TO DRAW SHOULD GO BELOW THIS COMMENT
recursive_draw(0, 0, 700, 500)
# ALL CODE TO DRAW SHOULD GO ABOVE THIS COMMENT
# Go ahead and update the screen with what we've drawn.
pygame.display.flip()
# Limit to 60 frames per second
clock.tick(60)
# Be IDLE friendly. If you forget this line, the program will 'hang'
# on exit.
pygame.quit()
截图: