Python Turtle Checkerboard

Python Turtle Checkerboard

Moving t.goto(s+row*s,s+column*s) to beginning of inner for loop does the trick.

Basically we need to move turtle to starting position first and then start drawing.

I also cleaned up the code to put redundant lines inside square function.
Also, added t.penup() so that turtle doesn't show draw until it has reached starting position and start drawing.

import turtle

def main():
t=turtle
t.penup()
s=int(input("Enter the length of each square: "))
t.screensize(2000,2000,"lightblue")
for row in range(0,5):
for column in range(0,5):
t.goto(s+row*s,s+column*s)
if (row+column)%2==0:
square(s,row,column,"black")
else:
square(s,row,column,"white")

def square(s,row,column,color):
t=turtle
t.pendown()
t.fillcolor(color)
t.begin_fill()
t.penup()
n=0
for count in range(4):
t.pendown()
t.forward(s)
t.left(90)
t.end_fill()
t.penup()
main()

enter image description here

New to Python: Turtle 25 squares checkerboard using loops

I simplified your "square" function to only draw a single square. Then I added a separate function which included a nested loop that calls the square function.
Trying to keep it simple and have only one responsibility for each function:

import turtle

def main():
board(5, 5, 50)
input("Hit enter to close")

def square(x,y,width,color):
turtle.penup()
turtle.fillcolor(color)
turtle.goto(x, y)
turtle.pendown()
turtle.begin_fill()
for side in range(4):
turtle.forward(width)
turtle.left(90)
turtle.end_fill()

def board(rows, columns, square_width):
turtle.showturtle()
for row in range(rows):
for column in range(columns):
color = "red" if (row + column)%2 == 1 else "white"
square(row*square_width, column*square_width, square_width, color)
turtle.hideturtle()

wondering what happened to my python code that draw a checkerboard with turtle? any helps?

I don't know how to explain your mistakes without giving you the correct code, but I will try:

basically the mistake is that black_square and white_square are drawing squares at the same location. The starting point is 50 units apart, but because the squares are drawn in opposite directions (clockwise and counter-clockwise), the resulting area overlaps. black_square would draw a square below the starting point, while white_square would draw a square above its starting point.

How to place checker board in the top left corner of a turtle screen regardless of computer screen size?

I'll show a couple of ways for doing this. First, I'm going to provide a working version of draw_board() since the one posted is broken. I will include my prefered way of importing turtle:

from turtle import Screen, Turtle

def draw_box(t, x, y, size, fill_color):
t.penup() # no drawing!
t.goto(x, y) # move the pen to a different position
t.pendown() # resume drawing

t.fillcolor(fill_color)
t.begin_fill() # Shape drawn after this will be filled with this color!

for _ in range(4):
t.forward(size) # move forward
t.right(90) # turn pen right 90 degrees

t.end_fill() # Go ahead and fill the rectangle!

We're going to use methods window_width() and window_height() for both solutions, as well as draw the board from top to bottom instead of bottom to top. The first solution simply moves the starting point, using the above methods, and moves in the opposite Y direction:

def draw_chess_board():
square_color = 'black' # first chess board square is black
box_size = 50 # pixel size of each square in the chess board
start_x = -width/2 # starting x position of the chess board
start_y = height/2 # starting y position of the chess board

for i in range(8): # 8x8 chess board
for j in range(8):
draw_box(board, start_x + j * box_size, start_y - i * box_size, box_size, square_color)
square_color = 'black' if square_color == 'white' else 'white' # toggle after a column

square_color = 'black' if square_color == 'white' else 'white' # toggle after a row!

screen = Screen()
width, height = screen.window_width(), screen.window_height()

board = Turtle()
board.speed('fastest') # because I have no patience

draw_chess_board()

board.hideturtle()
screen.mainloop()

The second approach leaves both the X starting point, and the Y direction code alone, but instead manipulates the coordinate system to achieve the same result:

def draw_chess_board():
square_color = 'black' # first chess board square is black
box_size = 50 # pixel size of each square in the chess board
start_x = 1 # starting x position of the chess board
start_y = box_size # starting y position of the chess board

for i in range(8): # 8x8 chess board
for j in range(8):
draw_box(board, start_x + j * box_size, start_y + i * box_size, box_size, square_color)
square_color = 'black' if square_color == 'white' else 'white' # toggle after a column

square_color = 'black' if square_color == 'white' else 'white' # toggle after a row!

screen = Screen()
width, height = screen.window_width(), screen.window_height()
screen.setworldcoordinates(0, height, width, 0)

board = Turtle()
board.speed('fastest') # because I have no patience

draw_chess_board()

board.hideturtle()
screen.mainloop()

This manipulation of the coordinate system will affect all that you do in the program so make sure you understand it before adding further code.

Sample Image

Finally, how I might have written this program if I were starting from scratch (or the merits of stamping over drawing):

from turtle import Screen, Turtle

BOX_SIZE = 50 # pixel size of each square in the chess board
CURSOR_SIZE = 20
COLOR_MAP = {'red': 'black', 'black': 'red'} # try red just for fun

def draw_chess_board():
square.color('black') # first chess board square is black
start = BOX_SIZE/2 # starting x and/or y position of the chess board

for row in range(8): # 8x8 chess board
square.goto(start, start + row * BOX_SIZE)

for _ in range(8):
square.stamp()
square.color(COLOR_MAP[square.fillcolor()]) # toggle after a column
square.forward(BOX_SIZE)

square.color(COLOR_MAP[square.fillcolor()]) # toggle after a row!

screen = Screen()
screen.setworldcoordinates(0, screen.window_height(), screen.window_width(), 0)

square = Turtle()
square.hideturtle()
square.penup() # no drawing!
square.shape('square')
square.speed('fastest') # because I have no patience
square.shapesize(BOX_SIZE / CURSOR_SIZE)

draw_chess_board()

screen.mainloop()

How do I create a chessboard using turtle-graphics and nested loops without using define?

This will do the trick:

import turtle

def drawSquare(turtule, isBlack = False):
if isBlack:
t.begin_fill()
for k in range(4):
t.forward(50)
t.left(90)
t.color("black")
t.end_fill()

t = turtle.Turtle()
t.speed(0)
t.penup()
t.goto(0, 0)
t.pendown()

lastWhite = False
for j in range(-150, 250, 50):
lastWhite = not lastWhite
for i in range(-150, 250, 50):
t.penup()
t.goto(i, j)
t.pendown()
if lastWhite:
drawSquare(t, True)
lastWhite = False
else:
drawSquare(t)
lastWhite = True


t.hideturtle()
turtle.done()

EDIT: this code doesn't use the drawSquare method

import turtle

t = turtle.Turtle()
t.speed(0)
t.penup()
t.goto(0, 0)
t.pendown()

lastWhite = False
for j in range(-150, 250, 50):
lastWhite = not lastWhite
for i in range(-150, 250, 50):
t.penup()
t.goto(i, j)
t.pendown()
if lastWhite:
t.begin_fill()
for k in range(4):
t.forward(50)
t.left(90)
t.color("black")
t.end_fill()
lastWhite = False
else:
for k in range(4):
t.forward(50)
t.left(90)
t.color("black")
lastWhite = True


t.hideturtle()
turtle.done()




Related Topics



Leave a reply



Submit