【问题标题】:Recurive game in pygame, using recursive rectangles.(pygame)pygame中的递归游戏,使用递归矩形。(pygame)
【发布时间】: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


【解决方案1】:

问题在于你的 recursive_draw() 函数并不是真正的递归函数,因为递归函数是有条件地调用自身的函数

每个正确设计的递归函数必须至少有一个基本情况 [A]并且必须将问题重新定义为子问题基本情况[B]。

def countdown(n):
   if n < 1:
      print "Lift Off"   #[A]
   else:
      print n
      countdown(n - 1)   #[B]

你可以为你的代码做什么:

更新后的函数(代码来自http://www.balloonbuilding.com/ by 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()

截图:

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-24
    • 1970-01-01
    相关资源
    最近更新 更多