【发布时间】:2015-06-04 17:14:11
【问题描述】:
我一直在尝试制作一个递归矩形,我想让矩形像每次递归时一样向前移动,这样当一个人进入一个无穷无尽的矩形时它就会产生运动。我尝试在每次递归时使大小变大,但失败了,因为它不会递归或什么都不会出现。任何提示或如何做到这一点将不胜感激。这个示例是我从 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()
【问题讨论】:
-
什么是递归矩形?看起来你只是想画一个随着时间的推移而变大的矩形,对吧?
-
你能发布一个最终输出的视频吗?你在问什么真的不清楚,但我可以看到一些 NameErrors..
-
是的,树懒是对的,就像矩形显示的那样,它似乎随着尺寸的增长而移动
-
@Anim3boy:现在可以用了吗? :)
-
抱歉没有,只有三个角被放大了
标签: python-2.7 recursion pygame