-1

我遇到的第一个问题是我的随机数。每隔一次我尝试运行程序时,我都会收到一个带有随机数的错误。该程序有效,并且每隔一次就会制作一个谢尔宾斯基三角形,直到我尝试将颜色添加到方程式中,现在显示框弹出然后一切都冻结了

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

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

4

1 回答 1

1

随机问题:

您使用random.randint(yRand, maxX)inrandPoint()有时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) - 它是黑色的 - 所以我看不到谢尔宾斯基三角形。

@编辑:

带有递归的谢尔宾斯基三角形

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()
于 2013-10-30T00:30:03.013 回答