对于我在梅西大学的课程,我也必须这样做。我使用的功能是:
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()