Hey coder! In this tutorial, I will show you how to draw a fish with the help of the python turtle module. If you are not aware of what turtle
module is, check out the tutorial here.
You need to import the turtle library that comes with Python and there is no need to do any additional installing.
import turtle
The next step involves creating a canvas to draw the fish. We can name the canvas variable according to our needs. For now, we have the name of the screen as fish_scr
. The code below does the creation and displaying of the screen for the user. We have also added some extra properties including the color of the screen and pen.
import turtle
fish_scr = turtle
fish_scr.color('black')
fish_scr.Screen().bgcolor("#85C1E9")
Now let’s create a function that will draw the fish for us. The name of the function is Draw_Fish
that will draw the fish for us on the screen. The goto
function takes the pointer to a certain position. The penup
and pendown
function controls when to draw and when to not draw. Also, the forward
and backward
function needs the distance as a parameter, on the other hand, the left
and right
function needs an angle of turning as a parameter.
def Draw_Fish(i,j):
fish_scr.penup()
fish_scr.goto(i,j)
fish_scr.speed(10)
fish_scr.left(45)
fish_scr.pendown()
fish_scr.forward(100)
fish_scr.right(135)
fish_scr.forward(130)
fish_scr.right(130)
fish_scr.forward(90)
fish_scr.left(90)
fish_scr.right(90)
fish_scr.circle(200,90)
fish_scr.left(90)
fish_scr.circle(200,90)
fish_scr.penup()
fish_scr.left(130)
fish_scr.forward(200)
fish_scr.pendown()
fish_scr.circle(10,360)
fish_scr.right(270)
fish_scr.penup()
fish_scr.forward(50)
fish_scr.pendown()
fish_scr.left(90)
fish_scr.circle(100,45)
fish_scr.penup()
fish_scr.forward(300)
fish_scr.left(135)
fish_scr.pendown()
fish_scr.right(180)
Let’s draw three fishes on the screen using the code below. And after we are done drawing the fish we will close the application screen using the done
function.
Draw_Fish(0,0)
Draw_Fish(150,150)
Draw_Fish(150,-150)
fish_scr.done()
The Complete Code
import turtle
fish_scr = turtle
fish_scr.color('black')
fish_scr.Screen().bgcolor("#85C1E9")
def Draw_Fish(i,j):
fish_scr.penup()
fish_scr.goto(i,j)
fish_scr.speed(10)
fish_scr.left(45)
fish_scr.pendown()
fish_scr.forward(100)
fish_scr.right(135)
fish_scr.forward(130)
fish_scr.right(130)
fish_scr.forward(90)
fish_scr.left(90)
fish_scr.right(90)
fish_scr.circle(200,90)
fish_scr.left(90)
fish_scr.circle(200,90)
fish_scr.penup()
fish_scr.left(130)
fish_scr.forward(200)
fish_scr.pendown()
fish_scr.circle(10,360)
fish_scr.right(270)
fish_scr.penup()
fish_scr.forward(50)
fish_scr.pendown()
fish_scr.left(90)
fish_scr.circle(100,45)
fish_scr.penup()
fish_scr.forward(300)
fish_scr.left(135)
fish_scr.pendown()
fish_scr.right(180)
Draw_Fish(0,0)
Draw_Fish(150,150)
Draw_Fish(150,-150)
fish_scr.done()
When we execute the code above, a new screen comes on the system screen and the fishes begin to draw on the screen in the application. The same is shown below.
Congratulations! Now you know how to draw a fish on the screen using the Turtle module in Python. Thank you for reading! If you liked this tutorial, I would recommend you to go through the following tutorials as well:
Keep reading to learn more! Happy coding! 😄