Lesson 1: Launching Your Game Setup and Cannon ๐ฎ๐
Prerequisites: Before You Dive In!
Objective ๐ง๐ฟโ
Kick off the fun by setting up the game screen and creating a cool laser cannon that you can move around!
Step 1. Import Turtle ๐ขโ
Open up the file we made in our Setup section.
To use the Python Turtle library, we first need to import it into our environment. Type this command in:
# YourLastName_SpaceInvaders.py
import turtle
turtle.done()
Our code will go in between these lines, and turtle.done()
should be the last line.
Step 2. Initializing the Game Window ๐ฅ๏ธโ
We need to set up the screen where your game will be. Think of it like setting up a canvas for painting. Hereโs how you do it:
window = turtle.Screen()
This variable window
will be our game screen. We can change any properties we want like size and color.
Now, let's head over to the Turtle Documentation. https://docs.python.org/3/library/turtle.html#turtle.setup
Walk-Through:โ
- Set the window size: Find how to resize the window to 50% of the screen's width and 75% of its height.
- Change the background color: Look up how to set the background color. Weโll start with a dark grey using the RGB values
(0.2, 0.2, 0.2)
. - Update the window title: Discover how to change the title of the window to "Space Invaders".
Need a Hint? ๐
Step 3. Creating the Laser Cannon ๐ฅโ
Let's start by drawing a simple laser cannon using Turtle. We'll make a square that represents the cannon for now.
Set-Upโ
Walk-Through:โ
- Initialize your Turtle object: Find how to create a new Turtle object in the documentation.
- Move the Turtle without drawing: Look up how to lift the pen up so your turtle moves without drawing.
- Set the Turtle color: Explore how to change the color of your Turtle. We are starting with white, but you'll get to pick your own colors!
- Choose a shape for the Turtle: Your cannon will start off as a square. Check how to set the shape of your turtle.
Need a Hint? ๐
Directionโ
To do this we need to define where each direction is.
#import turtle
window = turtle.Screen()
window.setup(0.5, 0.75)
window.bgcolor(0.2, 0.2, 0.2)
window.title("The Python Space Invaders")
LEFT = -window.window_width() / 2
RIGHT = window.window_width() / 2
TOP = window.window_height() / 2
BOTTOM = -window.window_height() / 2
FLOOR_LEVEL = 0.9 * BOTTOM
# Create laser cannon
cannon = turtle.Turtle()
cannon.penup()
cannon.color(1, 1, 1)
cannon.shape("square")
cannon.setposition(0, FLOOR_LEVEL)
turtle.done()
Final Touchesโ
Now that we put our cannon at the bottom, we can begin to shape it. Let's create our own laser cannon!
# ...
# Draw cannon
cannon.turtlesize(1, 4) # Base
cannon.stamp()
cannon.sety(FLOOR_LEVEL + 10)
cannon.turtlesize(1, 1.5) # Next tier
cannon.stamp()
cannon.sety(FLOOR_LEVEL + 20)
cannon.turtlesize(0.8, 0.3) # Tip of cannon
cannon.stamp()
cannon.sety(FLOOR_LEVEL)
turtle.done()
We use .stamp()
to leave a copy of the rectangle shape on the screen.
Step 4. Moving the Laser Cannon ๐ฎโ
Let's move our turtle with keyboard events!
import turtle
CANNON_STEP = 10
# ...
def move_left():
cannon.setx(cannon.xcor() - CANNON_STEP)
def move_right():
cannon.setx(cannon.xcor() + CANNON_STEP)
window.onkeypress(move_left, "Left")
window.onkeypress(move_right, "Right")
window.onkeypress(turtle.bye, "q")
window.listen()
turtle.done()
We are making a variable to set the speed of our cannon, then whenever we press a key it performs an action. We move to the left or right, and we can also quit the game.
Now we need to move the whole cannon.
# ...
def draw_cannon():
cannon.clear()
cannon.turtlesize(1, 4) # Base
cannon.stamp()
cannon.sety(FLOOR_LEVEL + 10)
cannon.turtlesize(1, 1.5) # Next tier
cannon.stamp()
cannon.sety(FLOOR_LEVEL + 20)
cannon.turtlesize(0.8, 0.3) # Tip of cannon
cannon.stamp()
cannon.sety(FLOOR_LEVEL)
def move_left():
cannon.setx(cannon.xcor() - CANNON_STEP)
draw_cannon()
def move_right():
cannon.setx(cannon.xcor() + CANNON_STEP)
draw_cannon()
window.onkeypress(move_left, "Left")
window.onkeypress(move_right, "Right")
window.onkeypress(turtle.bye, "q")
window.listen()
draw_cannon()
turtle.done()
When you call .clear()
you erase the previous cannon image so you can make a new one.
Now you have a Laser Cannon that can move in your very own game! ๐
- You've just embarked on an amazing coding journey, mastering the basics of turtle movement and positioning, setting the stage for more creative adventures! ๐