Python pygame没有响应.谢尔平斯基三角形和兰丁特的问题



我遇到的第一个问题是我的随机数。 每次我尝试运行该程序时,都会收到一个随机数的错误。 该程序有效,每隔一段时间就会制作一个 sierpinski 三角形,直到我试图在等式中添加颜色,现在显示框弹出,然后一切都冻结了

import sys, pygame, random, math, array
####initializes pygame
pygame.init()
#####deffining all my functions
def verticiePos(verticies):
    for i in range(2):
        screen.set_at(verticies[i], (0,0,255))
def randV(verticies):
    v = random.choice(verticies)
    return v
def Show(delay):
    pygame.display.flip()
    pygame.time.delay(delay)
def randPoint(h,w):
    yRand = random.randint(0,h-1)
    #maxX = ((w - yRand) * 2)
    xRand = random.randint(0, w-1)
    return (xRand, yRand)
def mainFunc():
    verticiePos(verticies)
    randPoint(h,w)
def colors(point, maxRed, maxBlue, maxGreen, bv, gv):
    howRed = (math.sqrt(point[0]**2 + point[1]**2) / maxRed) * 255
    howRed = int(howRed)
    howGreen = ((math.sqrt((point[0] - gv[0])**2 + (point[1] - gv[1])**2)) / maxGreen) * 255
    howGreen = int(howGreen)
    howBlue = ((math.sqrt((point[0] - bv[0])**2 + (point[1] - bv[1])**2)) / maxBlue) * 255
    howBlue = int(howBlue)
    return (howRed, howGreen, howBlue)
#####global variables
xRand = 0
yRand = 0
howRed = 0
howBlue = 0
howGreen = 0
#####Let the user choose the size of the display and setting screne
w = input("What do you want the width of the dcreen to be: ")
w = int(w)
h = input("What do you want the height of the dcreen to be: ")
h = int(h)
size = [w,h]
screen = pygame.display.set_mode(size)
pygame.display.set_caption("Project 1, Spierpinski triangle")
######setting up my verticies and all the functions i made
verticies = [(1,h - 1), (int(w/2), 1), (w-1,h-1)]
gv = verticies[1]
bv = verticies[2]
lengthOne = w
lengthTwo = math.sqrt(((w/2)**2) + (h**2))

if lengthOne >= lengthTwo:
    maxRed = lengthOne
    maxBlue = lengthOne
else:
    maxRed = lengthTwo
    maxBlue = lengthTwo 

maxGreen = lengthTwo
############################################
mainFunc()
point = [yRand,xRand]
iteration = 0
delay = 2
#######the main loop thats plots points
for i in range(10000):
    v  = randV(verticies)
    #colors(point, maxRed, maxBlue, maxGreen, bv, gv)
    point = (int((point[0] + v[0])/2), int((point[1] + v[1])/2))
    howRed = (math.sqrt(point[0]**2 + point[1]**2) / maxRed) * 200
    howRed = int(howRed)
    howGreen = ((math.sqrt((point[0] - gv[0])**2 + (point[1] - gv[1])**2)) / maxGreen) * 200
    howGreen = int(howGreen)
    howBlue = ((math.sqrt((point[0] - bv[0])**2 + (point[1] - bv[1])**2)) / maxBlue) * 200
    howBlue = int(howBlue)

    screen.set_at(point,(howRed,howGreen,howBlue))
    Show(delay)
    iteration = iteration + 1
#####the loop went really fast and was hard to see it coming into shape so i added this
    if iteration > 2000:
        delay = 0

#howRed,howGreen,howBlue

这是代码的更新版本,我只需要现在清理它

随机问题:

您在randPoint()中使用random.randint(yRand, maxX),有时使用yRand > maxXrandom.randint(a, b)需要a <= b

http://docs.python.org/2/library/random.html#random.randint


冻结???问题

我对冻结没有问题,但你有循环for i in range(50000):,需要 1 分钟才能完成。第一次运行时,我认为程序冻结了。

您没有事件循环来获取键盘事件并使用 ESC 停止程序。


函数color()总是返回 (0,0,0) - 它是黑色的 - 所以我看不到谢尔平斯基三角形。

@EDIT:

带递归的谢尔平斯基三角形

import pygame
import math
class Sierpinski():
    def __init__(self):
        h = w = int(input("Triangle size: "))
        level = int(input("Recursion level: "))
        margin = 50 # add to size to make margin around triangle
        # initializes pygame and setting screne
        pygame.init()
        self.screen = pygame.display.set_mode( (w + margin*2, h + margin*2) ) # self. - to make accessible in all function
        pygame.display.set_caption("Sierpinski Triangle - Recursion")
        # offset to move triangle to middle of window
        const_height = math.sqrt(3)/2  # h = a * sqrt(3)/2 = a * const_height
        invert_const_height = 1 - const_height
        offset = int(h * invert_const_height / 2) # to move triange up - to middle 
        # main vertices A, B, C
        a = (margin, h + margin - offset)
        b = (w + margin, h + margin - offset)
        c = (w/2 + margin, int(h * invert_const_height) + margin - offset)
        self.screen.set_at(a, (255,255,255))
        self.screen.set_at(b, (255,255,255))
        self.screen.set_at(c, (255,255,255))
        # recursion
        self.drawTriangle(a, b, c, level)
        # main loop (in game)
        clock = pygame.time.Clock()
        running = True
        while running:
            # keyboard & mouse event
            for event in pygame.event.get():
                if event.type == pygame.QUIT:
                    running = False
                if event.type == pygame.KEYDOWN:
                    if event.key == pygame.K_ESCAPE:
                        running = False
            # move objects - nothing to do here
            # draw object & redraw on screen
            pygame.display.set_caption("Sierpinski Triangle - Recursion [FPS: %f]" % (clock.get_fps()))
            pygame.display.update()
            # frametime 
            clock.tick(25) # max 25 Frames Per Second
        # end program
        pygame.quit()
    #-------------------------------------------------------
    def drawTriangle(self, a, b, c, level):
        if level == 0:
            return
        ab = self.middlePoint(a, b)
        ac = self.middlePoint(a, c)
        bc = self.middlePoint(b, c)
        self.screen.set_at(ab, (255,255,255))
        self.screen.set_at(bc, (255,255,255))
        self.screen.set_at(ac, (255,255,255))
        self.drawTriangle(a, ab, ac, level-1)
        self.drawTriangle(b, ab, bc, level-1)
        self.drawTriangle(c, bc, ac, level-1)
    #-------------------------------------------------------
    def middlePoint(self, a, b):
        x = (a[0] + b[0])/2
        y = (a[1] + b[1])/2
        return (x,y)
#----------------------------------------------------------------------
if __name__ == '__main__':
    Sierpinski()

最新更新