【问题标题】:Sierpinski's Triangle Pygame Recursive FunctionSierpinski 的三角 Pygame 递归函数
【发布时间】:2020-08-13 08:48:18
【问题描述】:

我一直在写一篇教程论文,我们打算在其中创建一个 Sierpinksi 三角形。它应该看起来像这样: enter image description here

而且我不知道我的代码有什么问题,因为它不像我想要的输出。有人可以帮我解决这个问题吗?

我无法让我的功能正常工作。

import sys, pygame


# a function that will draw a right-angled triangle of a given size anchored at a given location
def draw_triangle(screen, x, y, size):
    pygame.draw.polygon(screen, white, [[x, y], [x + size, y], [x, y - size]])

############################################################################################# 
# Define a function that will draw Sierpinski's Triangle at a given size anchored at a given location
# You need to update this function 
# currently only one triangle is drawn

def sierpinski(screen, x, y, size):
    mini_size = 10

    if size < mini_size:
        return draw_triangle(screen, x, y, size)
    else:

        new_size = int(size * 0.5)
        sierpinski(screen, x + new_size, y, new_size)
        sierpinski(screen, x, y - new_size, new_size)
        sierpinski(screen, x - new_size, y + new_size, new_size)

【问题讨论】:

    标签: python-3.x pygame


    【解决方案1】:

    需要一些改变:

    def sierpinski(screen, x, y, size):
        mini_size = 10
    
        if size < mini_size:
            return draw_triangle(screen, x, y, size) # give white colour
        else:
            new_size = int(size * 0.5)
            #draw_triangle(screen, x, y, size) # give black colour colour
            sierpinski(screen, x + new_size, y, new_size) # here location x,y
            sierpinski(screen, x, y - new_size, new_size) 
            sierpinski(screen, x - new_size, y + new_size, new_size)# here x+new_size,y
    

    看起来像:

    def sierpinski(screen, x, y, size):
        mini_size = 10
        if size < mini_size:
            return draw_triangle(screen, x, y, size)
        else:
            new_size = int(size*0.5)
            draw_triangle1(screen, x, y, new_size)
            sierpinski(screen,x, y,new_size)
            sierpinski(screen, x, y-new_size,new_size)
            sierpinski(screen, x+new_size, y,new_size )
    

    【讨论】:

      【解决方案2】:

      对于我在梅西大学的课程,我也必须这样做。我使用的功能是:

      def sierpinski(screen, x, y, size, MinSize):
      
          if size <= MinSize:
              #creating a new triangle object
              T = triangle(x, y, size, white)
              #drawing the triangle to screen
              T.draw(screen)
              #adding the triangle to the array
              Triangle.append(T)
          else:
              #halving the size and then recalling this function
              size = int(size / 2)
              sierpinski(screen, x, y, size, MinSize)
              sierpinski(screen, x + size, y, size, MinSize)
              sierpinski(screen, x, y - size, size, MinSize)
      

      我为每个三角形使用一个类而不是一个数组,并使用一个变量来存储谢尔宾斯基三角形的所有对象,以便我可以在需要时重新绘制它。 确保只调用一次 sierpinski 函数(在主循环之前调用它)。 我的最终脚本是:

      import sys, pygame, pyautogui
      
      
      # Triangle class, Takes x, y, size, and color#
      class triangle():
          def __init__(self, x, y, size, color):
              self.x = x
              self.y = y
              self.size = size
              self.color = color
      
          # Draws the triangle to the display, then updates the screen. Takes screen.
          def draw(self, screen):
              pygame.draw.polygon(screen, self.color,
                                  [[self.x, self.y], [self.x + self.size, self.y], [self.x, self.y - self.size]])
              pygame.display.flip()
      
          # Allows for the object to be printed. Used for testing
          def __str__(self):
              return "X: {}, Y: {}, Size: {}".format(self.x, self.y, self.size)
      
      
      #############################################################################################
      # Define a function that will draw Sierpinski's Triangle at a given size anchored at a given location
      # You need to update this function
      # currently only one triangle is drawn
      #############################################################################################
      
      #Sierpinski function, requires a surface(screen), x, y (anchor point), size (outer triangle), and minimum size.
      def sierpinski(screen, x, y, size, MinSize):
      
          if size <= MinSize:
              #creating a new triangle object
              T = triangle(x, y, size, white)
              #drawing the triangle to screen
              T.draw(screen)
              #adding the triangle to the array
              Triangle.append(T)
          else:
              #halving the size and then recalling this function
              size = int(size / 2)
              sierpinski(screen, x, y, size, MinSize)
              sierpinski(screen, x + size, y, size, MinSize)
              sierpinski(screen, x, y - size, size, MinSize)
      
      #############################################################################################
      
      # Initialize the game engine
      pygame.init()
      
      # Define the colors we will use in RGB format
      black = [0, 0, 0]
      white = [255, 255, 255]
      blue = [0, 0, 255]
      green = [0, 255, 0]
      red = [255, 0, 0]
      
      # Array for the triangles
      Triangle = []
      
      # sets the height and width of the screen using pyautogui
      Screen_Width, Screen_Height = pyautogui.size()
      screen = pygame.display.set_mode((Screen_Width, Screen_Height),pygame.FULLSCREEN)
      
      clock = pygame.time.Clock()
      
      # Draw Sierpinski's triangle at a given size anchored at a given location
      # Only needs to be called once#
      
      sierpinski(screen, 0, 800, 800, 2)
      
      while True:
          # This limits the while loop to a max of 10 times per second.
          # Leave this out and we will use all CPU we can.
          clock.tick(10)
      
          for event in pygame.event.get():  # User did something
              if event.type == pygame.QUIT:  # If user clicked close
                  break  # Flag that we are done so we exit this loop
      
          #Getting key input
          Key = pygame.key.get_pressed()
          #if the user presses the escape key, the loop breaks, and program closes
          if Key[pygame.K_ESCAPE]:
              break
      
      
      # Tidy up
      pygame.quit()
      

      【讨论】:

        猜你喜欢
        • 2015-10-24
        • 2020-05-14
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-02-12
        • 2022-12-20
        相关资源
        最近更新 更多