Skip to main content
 首页 » 编程设计

python之使用 pygame 窗口在 python 中创建 (R,G,B) Sierpinski 三角形。每个角分别是红色、绿色和蓝色,并且相互渐变

2025年05月04日8myhome

所以,对于我的计算机科学课,我们应该从网站导入 pygame:

http://www.petercollingridge.co.uk/image-processing-pygame

然后我们应该在 pygame 窗口中使用像素在 python 中创建 sierpinski 三角形。所以窗口中的每个像素都需要按照三角形的形状着色。我什至不能让我的三角形只显示黑色像素,而我们应该做的是让它的上角显示为红色,左下角显示为绿色,右下角显示为蓝色。这些颜色应该在整个三角形中慢慢地相互淡化(渐变)。完成的过程应该看起来像这样:

http://eldar.mathstat.uoguelph.ca/dashlock/ftax/Gallery/Sierpenski2D960.gif

首先,每次我设置窗口时,它都会说我的 main 函数中的 midPoint 未分配,我在其中调用了较早的 midPoint 函数。这让我感到困惑,因为我在第一个函数中明确地分配了它:def midPoint,所以对这个问题的任何帮助都会很棒。除此之外,我不确定为什么我不能让实际的三角形出现。我只想先让它显示黑色,然后再给它上色。对于我最有可能的糟糕代码有什么问题的任何帮助将不胜感激。我应该辅修计算机科学,但我没有通过这门课。我很绝望。请你甚至可以取笑我糟糕的代码,但我需要一些东西,任何东西。我迷路了。谢谢。

####################################### 
 
import pygame, math, random 
 
pygame.init() 
 
####################################### 
 
def midpoint (x0, x1, y0, y1): 
 
    panda = ((x0 + x1)/2) 
    polarbear = ((y0 + y1)/2) 
    return panda, polarbear 
 
####################################### 
 
def randomPoint (width, height): 
 
    potato = (random.random() * width) 
    pickle = (random.random() * height) 
    return potato, pickle 
 
 
#newImage 
 
#   PRE: Takes size, which is a 2-tuple (width, height) and provides size of image 
#   POST: Creates list of length size[0]. Each item in list is length size[1]. Each item of list is a 3-tuple. 
# 
#   Points of this data structure are denoted as image[x][y] and each point has 3 components: [0] for red, [1] for green, and [2] for blue 
# 
def newImage(size): 
 
return pygame.surfarray.array3d(pygame.Surface(size)) 
####################################### 
 
#showImage 
#   Takes image created by newImage and displays it in open window 
#   PRE: image is a list of lists of 3-tuples. Each 3 tuple corresponds to a (R,G,B) color. 
#   POST: image is displayed in window. 
# 
def showImage(image): 
 
    width, height, depth = image.shape 
    pygame.display.set_mode((width, height)) 
    surface = pygame.display.get_surface() 
    pygame.surfarray.blit_array(surface, image) 
    pygame.display.flip() 
 
####################################### 
 
# MAIN PROGRAM 
 
pygame.init() 
 
width = int(input("How large do you want your window? ")) 
height = int(input("How tall do you want your window? ")) 
window = newImage((width, height)) 
 
for x in range(width): 
    for y in range(height): 
        window[x][y] = (255,255,255) #Colors window white 
showImage(window) 
 
# 
 
p = randomPoint(width, height) 
 
for i in range(1000001): 
 
    corners = [(width, height),(0, height),(width//2, 0)] 
    c = random.choice(corners) 
    mid = midPoint(p[0], p[1], c[0], c[1]) 
    if i > 10: 
        window[(mid[0])][(mid[1])] = (0,0,0) 
    p = mid 
    if i % 1000 == 0: 
        showImage(window) 
 
# 
 
print('Done!') 
 
input("Enter to quit") 
 
pygame.quit() 
# 
#######################################` 

请您参考如下方法:

###################SIERPINSKI TRIANGLE (R,G,B)################### 
                   ########################### 
                       ################### 
                            ########## 
 
####################################### 
 
import pygame, math, random 
pygame.init() 
 
####################################### 
 
#newImage 
#   PRE: takes a 2-tuple (width, height) input from user and provides size of image 
#   POST: Creates list, len[0]. Each item in list is len[1]. Each item is 3-tuple. 
#   Points of data structure (pixels) are denoted as image[x][y] and each point has 3 components: 
##########################################################################################        
[0] - RED 
##########################################################################################      
[1] - GREEN 
##########################################################################################  
[2] - BLUE 
 
def newImage(size): 
    return pygame.surfarray.array3d(pygame.Surface(size)) 
 
####################################### 
 
#showImage 
#   Main function: Takes image created by "newImage" and displays it in pygame window 
#   PRE: image is a LIST OF LISTS of 3-tuples. Each 3 of the tuples corresponds to a (R,G,B) color. 
#   POST: image is displayed in window 
 
def showImage(image): 
    width, height, depth = image.shape 
    pygame.display.set_mode((width, height)) 
    surface = pygame.display.get_surface() 
    pygame.surfarray.blit_array(surface, image) 
    pygame.display.flip() 
 
####################################### 
 
#randomPoint 
#   set the variable "p" in main function to the returned values of this function which should be a random point 
#   PRE: takes in 2-tuple (width, height) 
#   POST: returns coordinates of a random point in the size of the image 
 
def randomPoint(width, height): 
    ex = (random.random() * width) 
    wye = (random.random() * height) 
    return ex, wye 
 
####################################### 
 
#midPoint 
#   find the mid-point between "p" - random point and "c" - random corner in the main function 
#   PRE: take in all 4 coordinates of the 2 points 
#   POST: calculate the mid-point between these 2 points and color it black. Set p to the midPoint once this function is complete and repeat. 
 
def midPoint(x0, y0, x1, y1): 
    eks = ((x0 + x1)/2) 
    wie = ((y0 + y1)/2) 
    return eks, wie 
 
####################################### 
 
def colorPoint (x, y, width, height): 
    w = (255/width) 
    h = (255/height) 
    x_color = x*w 
    y_color = y*h 
    r = math.fabs(255 - y_color) 
    g = math.fabs(255 - x_color) 
    b = math.fabs(255 - x_color - y_color) 
    return r, g, b 
    showImage(window) 
 
####################################### 
 
# MAIN PROGRAM 
 
# Start the CHAOS GAME 
 
pygame.init() 
 
####################################### 
 
# Get user input for the width and height of the window 
 
width = int(input("How wide would you like your window to be? ")) 
height = int(input("How tall would you like your window to be? ")) 
window = newImage((width, height)) 
 
for ecks in range(width): 
    for why in range(height): 
        window[ecks][why] = (255,255,255) #Colors window white 
showImage(window) 
 
####################################### 
 
# Set "p" to starting value 
 
p = 1 
 
# Call randomPoint in order to set "p" to a random point within the parameters of the window size 
 
p = randomPoint(width, height) 
i = 0 
for i in range(1000001): 
    corners = [(width, height),(0, height),(width//2, 0)] 
    c = random.choice(corners) 
    mid = midPoint(p[0], p[1], c[0], c[1]) 
    colour = colorPoint((mid[0]), (mid[1]), width, height) 
    if i > 10: 
        window[(mid[0])][(mid[1])] = colour  
        i += 1 
    p = mid 
    if i % 1000 == 0: 
        showImage(window) 
 
####################################### 
 
#End the CHAOS GAME 
 
print ('Done!') 
input ("ENTER to quit") 
pygame.quit() 
 
#######################################