Vì vậy, đối với bài báo đại học hiện tại của chúng tôi, chúng tôi muốn tạo ra Tam giác Sierpinksi và Thu thập đệ quy các hình tam giác mới bên trong.Hình tam giác Pygame của Tam giác Sierpinski
Mã ban đầu chúng tôi đã nhận được này:
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):
draw_triangle(screen, x, y, size)
#############################################################################################
# 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]
# Set the height and width of the screen
size=[512, 512]
screen=pygame.display.set_mode(size)
# Loop until the user clicks the close button.
done=False
clock = pygame.time.Clock()
while done==False:
# 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
done=True # Flag that we are done so we exit this loop
# Clear the screen and set the screen background
screen.fill(black)
# Draw Sierpinski's triangle at a given size anchored at a given location
sierpinski(screen,0, 512, 512)
# Go ahead and update the screen with what we've drawn.
# This MUST happen after all the other drawing commands.
pygame.display.flip()
# Tidy up
pygame.quit()
Ok Tôi biết rằng điều này chỉ tạo ra một tam giác duy nhất. Đây là những gì tôi đã làm để làm cho nó làm việc "loại":
Tôi tạo ra một hàm tam giác mới để vẽ một lộn ngược tam giác:
def draw_upside_down_triangle(screen, x, y, size, color):
pygame.draw.polygon(screen, color, [[x+size, y+size], [x+size, y], [x, y]])
Sau đó, tôi được cập nhật chức năng tam giác cũ để chấp nhận một biến màu :
def draw_triangle(screen, x, y, size, color):
pygame.draw.polygon(screen, color, [[x, y], [x+size, y], [x, y-size]])
Sau đó tôi cập nhật các chức năng chính mà đệ quy sẽ vẽ hình tam giác:
def sierpinski(screen, x, y, size):
if size < 10:
return False
else:
draw_triangle(screen, x, y, size, white)
draw_upside_down_triangle(screen, x, y/2, size/2, black)
sierpinski(screen, x+size/2, y+size/2, size/2)
sierpinski(screen, x, y-size/2, size/2)
sierpinski(screen, x, y, size/2)
sierpinski(screen, x, y+size/2, size/2)
tôi bắt đầu chức năng tắt
- Bằng cách thêm tham số exit (khi hình tam giác được của lợi nhuận quá nhỏ false)
- Nếu nó không phải là quá nhỏ sau đó vẽ hình tam giác đầu tiên trong trắng
- Sau đó vẽ một hình tam giác lộn ngược một nửa kích thước tại cùng một vị trí x nhưng một nửa vị trí y trong màu đen (điều này tạo ra ảo giác tam giác)
- Sau khi tất cả tôi có 4 cuộc gọi đệ quy, dựa trên thử nghiệm, tôi biết rằng thứ tự của các cuộc gọi này quan trọng khi đầu ra thay đổi hoàn toàn khi thay đổi.
Hiện nay sản lượng hiện nay như sau:
Tôi không yêu cầu cho bất cứ ai để kết thúc hoặc sửa mã của tôi chỉ đơn giản là một sự hiểu biết tốt hơn hoặc một điểm đi đúng hướng. Đã chiến đấu với cái này trong vài giờ.
Cảm ơn!
Nhắc tôi về một trong những kịch bản pygame ban đầu của tôi, nơi tôi vẽ Tam giác Sierpinski bằng cách sử dụng kỹ thuật [hỗn loạn trò chơi] (https://en.wikipedia.org/wiki/Chaos_game). – elParaguayo