1

it has no error now but i want to draw a chess board but this thing just randomly output something else:)))))

i think there is something wrong with the fill function tho

import turtle

screen = turtle.Screen()
screen.setup(800,800)
screen.bgcolor('pink')
screen.colormode(255)
screen.title("chessboard")

turtle = turtle.Turtle()

def draw(length,angle):
    for i in range (4):
        turtle.forward(length)
        turtle.right(angle)
                
length = 30
angle = 90
coor = -120
num = 64

def board(length):
        draw(length,angle)
        turtle.forward(length)

def limit():
    for q in range (8):
        board(length)


def pos(coor,length):
    for w in range(8):
        turtle.penup()
        turtle.setpos(coor,coor+30*w)
        turtle.pendown()
        limit()

def fill():
     for q in range (8):
        for w in range(8):
            if (q+w)%2==0:
                turtle.fillcolor('#000000')
            else:
                turtle.fillcolor('#FFFFFF')

def repeat():
    for h in range (8):
        draw(length,angle)
        turtle.forward(length)

def color():
    turtle.begin_fill()
    repeat()
    pos(coor,length)
    fill()
    turtle.end_fill()

        

turtle.speed(0)
turtle.penup()
turtle.goto(coor, coor)
turtle.pendown()
turtle.hideturtle()

screen.update()

color()

it has no error now but i want to draw a chess board but this thing just randomly output something else:)))))

i think there is something wrong with the fill function tho

  • what do you mean by squares? squares is not defined in your code – Hisham Alfoqaha Oct 26 '22 at 12:23
  • 1
    Try to use `screen.colormode(255)` before you call your function `color()`. For some details check [this link](https://stackoverflow.com/questions/16778324/what-does-bad-color-sequence-mean-in-python-turtle). – frankfalse Oct 26 '22 at 12:33
  • Does this answer your question? [what does bad color sequence mean in python turtle?](https://stackoverflow.com/questions/16778324/what-does-bad-color-sequence-mean-in-python-turtle) – 0stone0 Oct 26 '22 at 12:43
  • Because `squares = 8` to start with, your loop in the `draw` function is always false (`while squares < 0`), so I'd start by fixing that logic. – FAB Oct 26 '22 at 12:56

1 Answers1

0

I'm just learning python as well, so I found your problem being an interesting thing to try to do myself.

Please see below a solution that works, using some ideas from your code and my own implementation too.

If anything of the above helps you fix your own code, or have any questions I might be able to answer, please let me know :)

import turtle

# draw a square at given pos
def draw_square(t, fwd, angle):
    for _ in range(4):
        t.forward(fwd)
        t.right(angle)

def changePos(t, startPos, endPos, penUp):
    if penUp:
        t.pu()
    t.goto(startPos, endPos)
    if penUp:
        t.pd()

# Let's skip the drawing all together?
turtle.tracer(0, 0)

# taking input for the side of the square
squareSize = 100
angle = 90
boardSquares = 8
startPos = -(squareSize * (boardSquares/2)), (squareSize * (boardSquares/2)) #start from top-left
print("Start position: ", startPos)

screen = turtle.Screen()
screen.setup(850, 850)
screen.bgcolor('pink')
screen.title("chessboard")

# create the white squares
squares = turtle.Turtle()
squares.hideturtle()
squares.fillcolor("white")
squares.pensize(1)
squares.pencolor("red")
changePos(squares, startPos[0], startPos[1], True)

for r in range(8):
    for c in range(8):
        if (r + c) % 2 == 0:
            squares.fillcolor('#000000')
        else:
            squares.fillcolor('#FFFFFF')

        changePos(squares, startPos[0] + (squareSize * c), startPos[1] - (squareSize * r), True)
        squares.begin_fill()
        draw_square(squares, squareSize, angle)
        squares.end_fill()

# let's draw the board edge
board = turtle.Turtle()
board.hideturtle()
changePos(board, startPos[0], startPos[1], True)
board.pensize(5)
board.pencolor("brown")
draw_square(board, boardSquares * squareSize, angle)

turtle.update()
turtle.exitonclick()
FAB
  • 2,505
  • 1
  • 10
  • 21