2017-09-28 71 views
0

私はPython上にチェッカーボードを描画したいが、黒い四角形が1つしかない。 このプログラムの修正を手伝ってもらえますか?ここPythonでチェッカーボードを描く

import turtle 
def filled_square(size, color, x, y): 
    turtle.setpos(x, y) 
    turtle.color(color) 
    turtle.begin_fill() 
    for i in range(4): 
     angle = 90 
     turtle.fd(size) 
     turtle.lt(angle) 
    turtle.end_fill() 
    turtle.up() 
import sys 
n = int(sys.argv[1]) 
s = int(sys.argv[2]) 
square_size = s//n 
y=0 
for i in range(n): 
    x = 0 
    for j in range(n): 
     if (i+j)%2==0: 
      filled_square(square_size, "red", x, y) 
     else: 
      filled_square(square_size, "black", x, y) 
    x+=square_size 
turtle.down() 
turtle.done() 
+0

コードの書式設定に何か問題がありました。 [Markdown help - コードと書式設定済みのテキスト](http://stackoverflow.com/editing-help#code)とあなたの投稿を編集してください。 – Kevin

+0

私のコードを修正しました。 – Loulou

+0

良い努力:-)しかし、それはまだかなり正しいです。私は 'x = 0'行にインデントエラーを取得しています。 (あなたは「自分自身を修正するのはかなり簡単です」と思っているかもしれませんが、それは本当ですが、元のインデントが何であるかを完全に確かめたいのですが、文脈からそれを明白に推論することはできません) – Kevin

答えて

1
y=0 
for i in range(n): 
    x = 0 
    for j in range(n): 
     if (i+j)%2==0: 
      filled_square(square_size, "red", x, y) 
     else: 
      filled_square(square_size, "black", x, y) 
    x+=square_size 

カップルの問題。

  • xの値を次の反復でゼロに戻すと、値を大きくすることはあまり意味がありません。したがって、最初の割り当てはforループの上にある必要があります。
  • あなたは決してyの値を更新しません。

 

x = 0 
for i in range(n): 
    y=0 
    for j in range(n): 
     if (i+j)%2==0: 
      filled_square(square_size, "red", x, y) 
     else: 
      filled_square(square_size, "black", x, y) 
     y+=square_size 
    x+=square_size 

今あなたがしたいチェッカーボード形状を取得する必要があります。

enter image description here


代替ソリューション:あなたはそれらの値を全く持っていないことにより、xとyの値簿記の問題を回避することができます。正方形の座標をiとjからまっすぐに引き出すことができます。

for i in range(n): 
    for j in range(n): 
     if (i+j)%2==0: 
      filled_square(square_size, "red", i*square_size, j*square_size) 
     else: 
      filled_square(square_size, "black", i*square_size, j*square_size) 

また、あなたの場合は、他のブロックに共通のロジックを統合し、唯一実際に(つまり色)を変更する値を区別するために良いことかもしれませんあなたは両方をインクリメントする必要が

for i in range(n): 
    for j in range(n): 
     if (i+j)%2==0: 
      color = "red" 
     else: 
      color = "black" 
     filled_square(square_size, color, i*square_size, j*square_size) 
0

xとy。また、内側のループでxをインクリメントする必要があることにも注意してください。ここに作業コードは、

import turtle 
def filled_square(size, color, x, y): 
    turtle.setpos(x, y) 
    turtle.color(color) 
    turtle.begin_fill() 
    for i in range(4): 
     angle = 90 
     turtle.fd(size) 
     turtle.lt(angle) 
    turtle.end_fill() 
    turtle.up() 
import sys 
n = int(sys.argv[1]) 
s = int(sys.argv[2]) 
square_size = s//n 
y=0 
for i in range(n): 
    x = 0 
    for j in range(n): 
     if (i+j)%2==0: 
      filled_square(square_size, "red", x, y) 
     else: 
      filled_square(square_size, "black", x, y) 
     x+=square_size 
    y+=square_size 
turtle.down() 
turtle.done() 
0

私はボード/ゲームの仕事のためのこのような機能プログラミングを使用することをお勧めします。すべてを簡単に保守し、新しい機能の実装を容易にします。

import turtle 

def filled_square(size, color, x, y): 
    turtle.up() 
    turtle.setpos(x, y) 
    turtle.color(color) 
    turtle.begin_fill() 
    for i in range(4): 
     angle = 90 
     turtle.fd(size) 
     turtle.lt(angle) 
    turtle.end_fill() 

def board(length, size, x_pos=0, y_pos=0): 
    for y in range(length): 
     for x in range(length): 
      if (x+y)%2==0: 
       filled_square(
        size, "red", (x * square_size) - x_pos, (y * square_size) - y_pos) 
      else: 
       filled_square(
        size, "black", (x * square_size) - x_pos, (y * square_size) - y_pos) 
    turtle.done() 

キーワードの引数も素晴らしいです。色を追加したい場合は、2つのパラメータを追加するのと同じくらい簡単です。

def board(length, size, x_pos=0, y_pos=0, color1="red", color2="black"): 
    for y in range(length): 
     for x in range(length): 
      if (x+y)%2==0: 
       filled_square(
        size, color1, (x * square_size) - x_pos, (y * square_size) - y_pos) 
      else: 
       filled_square(
        size, color2, (x * square_size) - x_pos, (y * square_size) - y_pos) 
関連する問題