How To Draw A Grid With Turtle
For simple drawing, python provides a built-in module named turtle. This behaves like a drawing board and y'all can utilize various drawing commands to depict over it. The basic commands control the movement of the drawing pen itself. Nosotros start with a very bones drawing programme and work our way through to describe a complete chess board using python turtle.
The following python plan draws a square shape using the bones turtle commands,
import turtle board = turtle.Turtle() board.frontward(100) # movement forward lath.right(90) # turn pen right ninety degrees board.forward(100) board.right(90) board.frontwards(100) board.correct(ninety) board.forrard(100) board.right(90) turtle.washed()
This tin can be easily encapsulated into a simple box office as shown below,
import turtle def draw_box(t): for i in range(0,4): board.forward(100) # move forwards lath.correct(90) # turn pen right 90 degrees lath = turtle.Turtle() draw_box(board) turtle.washed()
Just what if we desire to describe different squares at different locations? The following python program shows how it can be washed using turtle commands. The draw_box() function is enhanced to draw the foursquare at a specific location. The following program draws a 3x3 grid of 30 pixel boxes.
import turtle def draw_box(t,x,y,size): t.penup() # no cartoon! t.goto(x,y) # move the pen to a different position t.pendown() # resume drawing for i in range(0,iv): board.frontward(size) # move forward lath.right(xc) # turn pen right ninety degrees board = turtle.Turtle() start_x = fifty # starting ten position of the grid start_y = 50 # starting y position of the grid box_size = 30 # pixel size of each box in the grid for i in range(0,3): # 3x3 grid for j in range(0,3): draw_box(board,start_x + j*box_size, start_y + i*box_size, box_size) turtle.done()
Let us now enhance the above python program to draw a chess board. This program illustrates the employ of colour in turtle drawing,
import turtle def draw_box(t,x,y,size,fill_color): t.penup() # no cartoon! t.goto(x,y) # movement 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 colour! for i in range(0,4): lath.forward(size) # move forward lath.right(xc) # turn pen right 90 degrees t.end_fill() # Go ahead and make full the rectangle! def draw_chess_board(): square_color = "black" # first chess board foursquare is black start_x = 0 # starting x position of the chess lath start_y = 0 # starting y position of the chess board box_size = 30 # pixel size of each foursquare in the chess board for i in range(0,eight): # 8x8 chess board for j in range(0,viii): draw_box(board,start_x+j*box_size,start_y+i*box_size,box_size,square_color) square_color = 'blackness' if square_color == 'white' else 'white' # toggle later a cavalcade square_color = 'black' if square_color == 'white' else 'white' # toggle after a row! board = turtle.Turtle() draw_chess_board() turtle.washed()
Following is the chess lath output from the higher up python plan,
Source: https://www.quickprogrammingtips.com/python/python-program-to-draw-a-square-and-chess-board-using-turtle.html
Posted by: colemanguideare.blogspot.com

0 Response to "How To Draw A Grid With Turtle"
Post a Comment