Part 1 – Getting Started

We will start of today creating a simple Python game using Python Turtle Graphics. Turtle Graphics a prebuilt set of python instructions that allow you to create and move characters around the screen allowing you to draw pictures, logos and create simple animations or games.

Step 1. If not already open please open your Python IDE application.
You should see your Shell Script window.

Step 2. Select File --> New File you should see your program window

Step 3. As it is a great idea to save your project at the beginning of any new development, select File --> Save and name the file, I called mine kbgame1.

Step 4. When coding one of the first things we should always do as a developer is leave a comment explaining what the program does at the top of the screen. Write the below on the top line:

# Turtle Graphics Game – Space Turtle Chomp

Step 5. Next we need to import the Turtle libraries and set up the screen, we do this by typing:

import turtle

# Set up screen
turtle.setup(650,650)
wn = turtle.Screen()
wn.bgcolor('navy')

turtle.setup sets the size of the Turtle game window (it’s not necessary but helps when using different screen sizes). wn is the name we are calling the screen (as we learnt in the Python introduction all variables need a name). bgcolor allows us to set the background colour. There are lots of colours to choose from. See this colour chart https://python-graph-gallery.com/wp-content/uploads/100_Color_names_python.png for options.

*note the American spelling of color, this is something to watch out for when programing.
As always it is good programming to leave a comment (#) before your code so you can remember what it does

Step 6. Next we create our first Turtle character, we do this by typing:

# Create player turtle
player = turtle.Turtle()
player.color('darkorange')
player.shape('turtle')
player.penup()

So we create a new turtle variable called player then use the inbuilt turtle attributes to help set it up, player.color (again we can select a different colour from the color chart). player.shape selects the shape (you can also use circle, arrow, square and classic). Penup means that the turtle shape won’t leave a line went it moves (think of a pen in your hand, penup() you don’t draw, pendown() you do draw.

Step 7. Now we set the speed we want our turtle to move across the screen at:

# Set speed variable
speed = 1

Step 8: Finally we need the Turtle to move and the easiest way to do this is by setting a while loop:

while True:
    player.forward(speed)

This will now continue to move your turtle forward across the screen at a speed of 1.

*note the indent of the player.forward… this needs to be there for the code to work.

Step 9: Now all we need to do is save our code, we can do this by selecting File --> Save (Ctrl S on Windows or Cmd S on Mac).Then run the module by selecting Run --> Run Module (or pressing F5)

You should see two windows open up a Python Shell window that will have “Python 3.6.1 (v3.6.1:69c0db5, Mar 21 2017, 17:54:52) [MSC v.1900 32 bit (Intel)]…” or something similar written on it and your Turtle window with a Navy background and Dark Orange turtle moving across the screen. Close both the shell and turtle window to return to your programming code.

Before moving onto the next section you can have a play with your code by trying different colours, shapes and speeds and for some fun you could change player.penup() to player.pendown()

Your code should now look like this: kbgame1.py

Congratulations Module 1 Completed

results matching ""

    No results matching ""