2016-09-18 1 views
-2

나는 파이썬에서 거북이로 미국 국기를 그리려고하고 있는데, 결국 내 코드에서 길을 잃고 내 실수를 찾지 못했습니다. 나는 또한 내 깃발을 색칠하는 방법을 알아낼 수 없다 ... 내가 생각하는 것은 작동하지 않을 것이라고 ... 나는 무언가를했고, 이제는 내 코드가 절반 정도 충돌한다 ... 프로그래밍에 익숙하지 않다. ...내 버그/글리치는 어디에 있습니까?

여기 내 코드가 있습니다.

import turtle 
import time 
import random 

def draw_rectangle(length, height): 
    turtle.up() 
    x = -150 
    y = 150 
    C = height*(7/13) 
    D = length*(2/5) 
    L = stripe_width = float(round(height/13,1)) 

    ## Draw rectangle first. 
    turtle.begin_fill() 
    turtle.setpos(x,y) 
    turtle.down() 
    turtle.forward(length) 
    turtle.right(90) 
    turtle.forward(height) 
    turtle.right(90) 
    turtle.forward(length) 
    turtle.right(90) 
    turtle.forward(height) 
    turtle.end_fill() 

    ## Then draw the stripes. 
    x1 = -150 
    y1 = 150-L 
    for z in range(13): 
     if z%2 == 0: 
      r = s = t = 0 
     else: 
      r = s = t = 1 
     turtle.up() 
     turtle.speed(100) 
     turtle.setpos(x1,y1) 
     turtle.setheading(90) 
     turtle.down() 
     turtle.color(r,s,t) 
     turtle.begin_fill() 
     turtle.forward(L) 
     turtle.right(90) 
     turtle.forward(length) 
     turtle.right(90) 
     turtle.forward(L) 
     turtle.right(90) 
     turtle.forward(length) 
     turtle.end_fill() 
     y1 -= L 

    ## Finally draw the stars rectangle overlapping the stripes, next is stars. 
    x2 = -150 + D 
    y2 = 150.5 - C 
    turtle.up() 
    turtle.setpos(x2,y2) 
    turtle.down() 
    turtle.color('yellow') 
    turtle.begin_fill() 
    turtle.forward(D) 
    turtle.right(90) 
    turtle.forward(C) 
    turtle.right(90) 
    turtle.forward(D) 
    turtle.right(90) 
    turtle.forward(C) 
    turtle.end_fill() 
    turtle.up() 


def draw_star(l, h): 
    for z in range(50): 
     if z < 7: 
      row = 140 
      draw_starrows(row) 
     if z < 14: 
      row = row - 20 
      draw_starrows(row) 
     if z < 21: 
      row = row - 20 
      draw_starrows(row) 
     if z < 28: 
      row = row - 20 
      draw_starrows(row) 
     if z < 35: 
      row = row - 20 
      draw_starrows(row) 
      ## This gets the turtle pen out of the way at the very end. 
      turtle.up() 
      turtle.setpos(-180,100) 
     break 

def draw_starrows(row): 
    x = -160 
    y = 150 
    for z in range(10): 
     x += 15 
     turtle.up() 
     turtle.color(1,1,1) 
     turtle.speed(100) 
     turtle.setpos(x,row) 
     turtle.begin_fill() 
     turtle.down() 
     turtle.forward(6.154) 
     turtle.left(144) 
     turtle.forward(6.154) 
     turtle.left(144) 
     turtle.forward(6.154) 
     turtle.left(144) 
     turtle.forward(6.154) 
     turtle.left(144) 
     turtle.forward(6.154) 
     turtle.left(144) 
     turtle.end_fill() 
    #turtle.bye # closes turtle window 

def get_color(color2): 
    ## If color2 equals 1, then make the color white. 
    if color2 == 1: 
     r = g = b = 1 
     return (r, g, b) 
    ## If color2 equals 0, then make the color red. 
    if color2 == 0: 
     r = 1 
     g = 0 
     b = 0 
     return (r, g, b) 
    ## If color2 equals 2, then make the color black. 
    if color2 == 2: 
     r = 0 
     g = 0 
     b = 1 
     return (r, g, b) 

def draw_flag(): 
    A = 200 
    height = int(A) 
## length = height*1.9 
## C = height*(7/13) 
## D = length*(2/5) 
## E = F = union_height/10 
## G = H = union_length/12 
## stripe_width = height/13 
## diameter_star = stripe_width*(4/5) 
    draw_rectangle(height*1.9, height) 

draw_flag() 
+0

코드의 맨 아래에있는 draw_starrows (row) 함수를 호출하는 등 함수를 호출해야합니다. –

답변

0

나는 모든 핵심 구성 요소가 있다고 생각합니다. 하드 코딩 값보다는 크기와 시작 위치에 상대적으로 생각할 필요가 있으며, 간단하게 유지해야합니다. (예 : color(r, s, t) 대신 color("blue")) 일을 처리 할 때까지 기다려주세요. 플래그의 별 정렬을 잘 살펴보세요.

내가 위에서 내 의견의 라인을 따라 코드를 재 작업 및했습니다 어떤 스타일의 변화 :

enter image description here

스케일링은 기본적으로 지금 (draw_flag(100) 시도) 작동하지만

import turtle 

X_POSITION, Y_POSITION = -150, 150 

def draw_rectangle(length, height): 
    turtle.up() 

    C = height * (7/13.0) 
    D = length * (2/5.0) 
    L = height * (1/13.0) 

    ## Draw rectangle first. 

    turtle.setpos(X_POSITION, Y_POSITION) 

    turtle.down() 

    turtle.forward(length) 
    turtle.right(90) 
    turtle.forward(height) 
    turtle.right(90) 
    turtle.forward(length) 
    turtle.right(90) 
    turtle.forward(height) 

    ## Then draw the red stripes. 

    x, y = X_POSITION, Y_POSITION - L 

    turtle.color("red") 

    for z in range(0, 13, 2): 
     turtle.up() 

     turtle.setpos(x, y) 
     turtle.setheading(90) 

     turtle.down() 

     turtle.begin_fill() 
     turtle.forward(L) 
     turtle.right(90) 
     turtle.forward(length) 
     turtle.right(90) 
     turtle.forward(L) 
     turtle.right(90) 
     turtle.forward(length) 
     turtle.end_fill() 

     y -= 2 * L 

    ## Draw the stars rectangle overlapping the stripes 

    turtle.up() 

    turtle.color('blue') 
    turtle.setpos(X_POSITION + D, Y_POSITION - C) 

    turtle.down() 

    turtle.begin_fill() 
    turtle.forward(D) 
    turtle.right(90) 
    turtle.forward(C) 
    turtle.right(90) 
    turtle.forward(D) 
    turtle.right(90) 
    turtle.forward(C) 
    turtle.end_fill() 

    ## next is stars 

    turtle.up() 

    draw_stars(D, C) 

    turtle.up() 

    ## This gets the turtle pen out of the way at the very end. 
    turtle.hideturtle() 

def draw_stars(length, height): 
    row = height // 9 
    row_offset = row/2.0 
    column = length // 6 
    column_offset = column/2.0 

    y_position = row_offset 

    for z in range(9): 
     if z % 2 == 0: 
      draw_starrows(6, column_offset, y_position, column) 
     else: 
      draw_starrows(5, column, y_position, column) 

     y_position += row 

def draw_starrows(star_count, x_offset, y_offset, spacing): 
    x, y = X_POSITION, Y_POSITION 

    turtle.color("white") 

    for z in range(star_count): 
     turtle.up() 

     turtle.setpos(x + x_offset, y - y_offset) 
     turtle.begin_fill() 

     turtle.down() 

     for _ in range(5): 
      turtle.forward(6.154) 
      turtle.left(144) 

     turtle.end_fill() 

     x += spacing 

def draw_flag(height): 
    turtle.speed("fastest") 
    draw_rectangle(height * 1.9, height) 

draw_flag(200) 

turtle.done() 
에 별 자체 (당신은 훌륭한 직업을 보였습니다.) 여전히 고정 된 크기이므로 깃발의 나머지 부분과 일치하도록 돌아가서 크기를 조정해야합니다.

enter image description here

+0

고마워요. 내 눈이 내 눈이 멀어져서 내 눈이 멀어 졌어요. 전적으로 도움이 되었어요. – Chris

+0

@ChrisVonderwerth이 답변이 [받아 들여진 문제] (http://stackoverflow.com/help/someone-answers) (http://meta.stackexchange.com/questions/5234)를 클릭하여 답변의 왼쪽에있는 체크 표시/체크 표시를 클릭하여 녹색으로 바꿉니다. 이 질문은 당신과 만족스런 대답으로 표명되며, 당신과 응답 한 사람 모두에게 [평판] (http://stackoverflow.com/help/whats-reputation) 상을 수여합니다. 평판 포인트가 15 점 이상이면 원하는 경우 답변을 upvote 할 수 있습니다. 어느 쪽도 할 의무가 없습니다. – MattDMo

+0

파이썬 2.7을 사용하고 있습니까? 호환되지 않습니까? – Chris