In this introduction to coding in Python for beginners, you will learn how to use variables, loops, and functions to create visual and interactive artwork, animations, simulations, games, and more.
Make interactive projects, art, games, and a simulation. By the end of this path you will have created a scalable piece of geometric art.
This path follows 321β¦Make! methodology:
Introduce creators to a set of skills and provide step-by-step instructions to help them develop initial confidence.
Allow creators to practise skills from the Explore projects, and express themselves creatively while they grow in independence.
Encourages creators to use their skills to create a unique project that meets a project brief for a particular audience.
Each project contains a quick quiz with three multiple choice questions. You are guided to the correct answer through useful feedback and are awarded a project badge.
This Design project guides you to use your new skills and encourages you to make design choices based on your interests.
You will make: Build a scrolling endless runner game where your character has to avoid obstacles - "Dodge asteroids"
Create a variable called safe to store the background colour.
In the game, the player is safe if they are touching the background colour.
def draw():
# Put code to run every frame here
global safe
safe = Color(200, 100, 0)
background(safe)
Run your code and you should see a coloured square.
The colour is three numbers - the amount of red, green and blue. Try changing the numbers to any whole number between 0 and 255 to get a different colour.
# Draw player function goes here
def draw_player():
text('π€ ', 200, 320)
Call the draw_player function so that the player is drawn on the screen.
def draw():
# Put code to run every frame here
global safe
safe = Color(200, 100, 0)
background(safe)
draw_player()
Run your code and you should see the emoji appear near the bottom of the screen.
You can paste in a different emoji if you want to.
Your device might have an emoji keyboard that you can use to select an emoji:
Below are some of the most popular emojis that you could use in your project. You can copy them from here.
There are over 3,000 emojis available for you to use. They cover a wide variety of emotions, themes, and activities.
Tip: Emojis look slightly different on different devices, so someone else may not see exactly the same image as you. Some emojis are not supported on some devices and these appear as a square instead.
To make the player follow the mouse as it moves from side to side, change the player's x position to mouse_x.
# Draw player function goes here
def draw_player():
text('π€ ', mouse_x, 320)
Run your code and check that the player moves left and right when you move the mouse.
Create the obstacles that you will have to avoid to keep playing the game.
Define a draw_obstacles function to draw a cactus emoji π΅.
# Draw obstacles function goes here
def draw_obstacles():
text('π΅', 200, 200)
Call the draw_obstacles function so that the cactus is drawn on the screen.
def draw():
# Put code to run every frame here
global safe
safe = Color(200, 100, 0)
background(safe)
draw_obstacles()
draw_player()
Run your code and you should see a cactus as well as your player.
def draw_obstacles():
obstacle_x = 200
obstacle_y = 200
text('π΅', obstacle_x, obstacle_y)
Now, add frame_count to the obstacle's y (vertical) position.
def draw_obstacles():
obstacle_x = 200
obstacle_y = 200 + frame_count
text('π΅', obstacle_x, obstacle_y)
Run your code and the cactus emoji should move down the screen until it reaches the bottom.
Select an emoji for your obstacles:
Currently, the obstacle disappears off the bottom of the screen, because its obstacle_y position becomes larger than the screen size.
Use the modulo (%) operator to divide the y position by the screen size and give you the remainder. This makes the obstacle reappear at the top!
def draw_obstacles():
obstacle_x = 200
obstacle_y = 200 + frame_count
obstacle_y = obstacle_y % screen_size
text('π΅', obstacle_x, obstacle_y)
Run your code and you should see the obstacle reach the bottom of the screen and then restart from the top.
Add a line of code for a random seed. A seed lets you generate the same random numbers in each frame.
# Draw obstacles function goes here
def draw_obstacles():
seed(1234)
obstacle_x = 200
obstacle_y = 200 + frame_count
Update the code so that the x, y coordinates for the obstacle are generated randomly.
# Draw obstacles function goes here
def draw_obstacles():
seed(1234)
obstacle_x = randint(0, screen_size)
obstacle_y = randint(0, screen_size) + frame_count
Run your code and you should see the cactus appear at a random position. Change the 1234 value inside the seed to another number and it will appear somewhere else.
Now you will add code to make lots of obstacles to avoid.
Add a loop and indent the code to draw an obstacle. The loop will run this code multiple times.
def draw_obstacles():
seed(1234)
for i in range(8):
obstacle_x = randint(0, screen_size)
obstacle_y = randint(0, screen_size) + frame_count
obstacle_y = obstacle_y % screen_size
text('π΅', obstacle_x, obstacle_y)
Make sure that the code for the seed is before the loop, otherwise all of your obstacles will be generated on top of each other!
Change the number inside range() to control how many obstacles are created.
Run your code and you should see several obstacles.
Recall that in the first step you created a 'safe' colour.
Create a variable to store the colour the player emoji is currently touching.
def draw_player():
player_on = get(mouse_x, 320).hex
text('π€ ', mouse_x, 320)
If the player is touching the safe colour, draw the player emoji. If it is not, draw an explosion emoji to show they have crashed.
def draw_player():
player_on = get(mouse_x, 320).hex
if player_on == safe.hex:
text('π€ ', mouse_x, 320)
else:
text('π₯', mouse_x, 320)
Run your code and move the player. You should see the explosion emoji if your player touches an obstacle.
Make sure that in draw(), the line of code to draw_obstacles() is before draw_player(). If you check for collisions before drawing the obstacles in a frame, then there won't be any obstacles to collide with!
Answer the three questions. There are hints to guide you to the correct answer.
When you have answered each question, click on your chosen answer to see feedback.
Have fun!
You have used a lot of if statements to control your game's behaviour. Some of them might have had more complex conditions, using and to make multiple tests at once. If you ran the following piece of conditional code, what would you expect the output to be?
score = 5000
lives = 2
if score >= 5000 and lives >= 3:
print('Great flying!')
if score >= 5000:
print('Doing well!')
if lives > 1:
print('Keep going!')
else:
print('But be careful!')
elif lives > 1:
print('Push harder!')
else:
print('Head for base!')
In this project you used procedural generation β having the computer create and place parts of your world for you. While doing this is a great time saver, particularly if you're creating very large levels, it can create some issues. Which of these issues should you look out for when testing your procedural generation?
You made a game that you're really pleased with and shared it. A player has reviewed it and has shared some thoughtful feedback with some changes they would like to see.
What would be the best way to react to their suggestions?
β Create and use variables like safe, player_on, obstacle_x, obstacle_y
β Understand RGB color values (0-255)
β Use frame_count for animation
β Create functions: draw_player(), draw_obstacles()
β Call functions in proper order
β Use def keyword to define functions
β Use for loops with range()
β Implement if/else statements for collision detection
β Use modulo operator (%) for wrapping
β Procedural generation with randint() and seed()
β Collision detection using color checking
β Player movement with mouse_x
β Game state management
You've built a complete endless runner game with:
Now it is over to you! Use the skills you have learned to finish the game.
0/10 challenges completed
Complete these challenges to enhance your game!