2014-10-11 3 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)을 루프 내부로 옮겼습니다.
  • 을 수정했습니다.세 가지 색상 구성 요소 red = random.randint(1, 255), green = random.randint(1, 255)에 대한 임의의 계산을 추가
  • 스트라이프의 오른쪽 아래 모서리를 가리 키도록하고, blue = random.randint(1, 255)

관찰 :

나는 조금을 끝까지를 변경 줄무늬가 그려집니다. 수정 된 버전은 모든 루프에 대해 축소 된 버전을 그리는 대신 연속 된 위치에 고정 크기의 스트라이프를 그립니다.

관련 문제