使用pygame窗口在python中创建(R,G,B)谢尔宾斯基三角形。每个角分别是红色、绿色和蓝色,并渐变相互融合。

2 投票
1 回答
1577 浏览
提问于 2025-04-17 20:32

在我的计算机科学课上,我们需要从这个网站导入pygame:

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

然后,我们要在pygame窗口中用Python创建一个谢尔宾斯基三角形。也就是说,窗口里的每个像素都需要按照三角形的形状上色。我现在连黑色的三角形都显示不出来,而我们要做的是让三角形的顶角是红色,左下角是绿色,右下角是蓝色。这些颜色应该在三角形内部慢慢过渡(渐变)。最后的效果应该像这样:

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

首先,每次我设置窗口时,它都会提示我在主函数中调用的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()
#
#######################################`

1 个回答

1

在编程中,有时候我们会遇到一些问题,可能会让我们感到困惑。比如,有人可能在使用某个工具或库时,发现它的某些功能没有按照预期工作。这种情况很常见,尤其是当我们刚开始学习编程的时候。

在这种情况下,最好的办法就是去查找相关的资料,看看其他人是怎么解决类似问题的。StackOverflow就是一个很好的地方,很多程序员会在这里分享他们的经验和解决方案。

如果你在使用某个特定的代码块时遇到了问题,比如

###################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()

#######################################
,你可以尝试把这个代码块放到你的代码里,看看它是否能正常运行。同时,也可以查看其他人的提问和回答,了解他们是如何处理这个问题的。

记住,遇到问题是学习编程的一部分,不要气馁,多问问题,多查资料,你会慢慢变得更熟练的。

撰写回答