2014-10-11 75 views
0

我想重现这种效果的东西。颜色不变 - 蟒图形

enter image description here

import graphics 
from graphics import color_rgb 
import random 
window= graphics.GraphWin("x", 600, 400) 
stripes = input("How many stripes should be on the flag") 
stripes = int(stripes) 
count = 0 
count = int(count) 
P1=graphics.Point(0,0) #left corner - anchor point 
for x in range(stripes): #loop for number of stripes 
    col= random.randint(1,255) 
    stepdim = 400/stripes #size of divisions 
    stepdim = int(stepdim) 
    shrink = count*stepdim 
    shrink = int(shrink) 
    stepdim = stepdim*10 #enlarge to an increment below the last 
    stepdim = stepdim-shrink 
    stepdim = int(stepdim) 
    P2=graphics.Point(600,stepdim) #bottom right corner - ever shrinking 
    outsiderec=graphics.Rectangle(P1,P2) # 
    outsiderec.setFill(color_rgb(100, col, 0)) 
    outsiderec.draw(window) 
    count= count + 1 
    count= int(count) 
window.getMouse() 
window.close() 

我不是接收一个平坦的颜色。 enter image description here 我认为问题出在我的rand(int)中。我真的不知道它的来龙去脉。它不是运行多次?

回答

0

使用您的代码作为基础我试图重现预期的结果。

import graphics 
from graphics import color_rgb 
import random 

window= graphics.GraphWin("x", 600, 400) 
stripes = input("How many stripes should be on the flag") 
stripes = int(stripes) 
#count = 0 
#count = int(count) 
#P1=graphics.Point(0,0) #left corner - anchor point 
stepdim = 400/stripes #size of divisions 
for x in range(stripes): #loop for number of stripes 
    #col= random.randint(1,255) 
    #stepdim = int(stepdim) 
    #shrink = count*stepdim 
    #shrink = int(shrink) 
    #stepdim = stepdim*10 #enlarge to an increment below the last 
    #stepdim = stepdim-shrink 
    #stepdim = int(stepdim) 
    #P2=graphics.Point(600,stepdim) #bottom right corner - ever shrinking 
    P1=graphics.Point(0, stepdim * x) #left corner - anchor point 
    P2=graphics.Point(600,stepdim * (x + 1)) #bottom right corner - ever shrinking 
    outsiderec=graphics.Rectangle(P1,P2) 
    #outsiderec.setFill(color_rgb(100, col, 0)) 
    red = random.randint(1, 255) 
    green = random.randint(1, 255) 
    blue = random.randint(1, 255) 
    outsiderec.setFill(color_rgb(red, green, blue)) 
    outsiderec.draw(window) 
    #count= count + 1 
    #count= int(count) 
window.getMouse() 
window.close() 

我:

  • 注释掉那些不需要的
  • 移动从内环路stepdim = 400/stripes之外,因为你不需要来计算每一个相同的值声明循环
  • 移动P1=graphics.Point(0,0)在循环内略有修改只是指向条纹的左上角
  • 修改P2=graphics.Point(600,stepdim)指向条纹
  • 增加了三个颜色分量red = random.randint(1, 255)green = random.randint(1, 255)随机计算的右下角,并且blue = random.randint(1, 255)

观察:

我已经改变了一点点的方式条纹被绘制。而不是为每个循环绘制缩小版本,修改版本只是在连续位置绘制固定大小的条纹。