Let’s play a game with Turtle

Akanksha Sinha
Analytics Vidhya
Published in
4 min readAug 27, 2020

--

Are you tired with the over burdening office work where coding is no more fun ?

Has all work and no play made you a dull person ?

How about we try to rekindle our friendship with codes ? Why should kids have all the fun :D

Let’s have some Fun with Turtle by developing a simple game in Python.

How bout this game 👇🏽

Demo Video of the Game

The other day my toddler niece was learning about shapes, and I got the idea of this game where you guess any number between 1 to 30 and this game will come up with a random shape.

I tried it with my niece, and even though its a command line based game with no UI yet, she still loved it when the turtle drew shapes for her, and I was a happy aunt :)

Prerequisite — Basic Python knowledge

I divided my game development in 2 stages

Stage I

Developing python modules that will draw shapes.

  1. Import turtle
  2. Define module for shapes you wish to draw

Code explanation -

a. I am passing ‘t’ and ‘color’ as parameters which I’ll explain in Stage II.

b. We define the title of the screen. Here, it is ‘A Square’

c. We next define pen color. You may hardcode the color of your choice. Here, I have declared an array called color where I have kept my favorite colors and I am randomly picking up pen color from that color array.

d. Next, we specify the color with which the square will be filled.

e. begin_fill and end_fill mark the area where color needs to be filled.

f. I make the turtle move forward by 100 and then turn right by 90 deg. As we are making a square shape here, the number of times I repeat this step is 4. Each iteration accounting for a side of square.

3. You may repeat the same for other shapes. Please refer to my code for sample shapes

Stage II

Now it’s time to call the various shape modules that we have prepared.

  1. Define the main function. This function contains -

a. Turtle Object (t) — This is passed as parameter to the called shape drawing modules.

b. Shape (Square/Arrow/Circle/Turtle/Triangle/Classic)

c. Color — For color, I create an array of my favorite colors. This is passed as parameter to the called shape drawing modules.

d. Background Color of the screen

e. Pen size and speed of drawing. Speed ranges from 0 to 10.

f. Calling shape drawing modules based on choice entered by the user.

g. As else loop, I have put a module that scribbles randomly on screen. This way, if the user enters any number outside the range specified by input, the user will not hit error. Instead, the turtle will say ‘It doesn’t know what to draw’ and will scribble some random lines on the screen.

f. By default, once turtle completes the drawing, the screen closes. To prevent this, we use turtle.exitonclick(). This way, the screen will disappear only when user touches the screen.

P.S — Turtle’s library provides Preset Figures like circle and dot which can be drawn by single line of code.

e.g. turtle.circle(50), turtle.dot(20)

2. Creating input for command line

Add below at the end of your script —

Here, choice variable is the input number entered by the user. I have passed this as parameter when calling drawshapes function. Based on the choice entered, the respective shape module will be called.

Complete Code is present here 👇🏽

This is a very basic python game. I’ll keep adding to the shapes gradually to make it more fun.

You may play along with more shapes/patterns and colors. You may even create basic animations using turtle. Have fun !

In case, you are interested to learn more about Turtle, please see below references:

--

--

Akanksha Sinha
Analytics Vidhya

A digital knowledge seeker who loves to write about Automation, Digital Transformation, Google Technologies & Web Development