Today I made the capstone project for the beginners section of my 100 days of python challenge. It is similar to the Crossy Roads game. There is a turtle which has to make it to the end of the screen before any of the colourful blocks(which move from the right to left) hit it. Each time it crosses the northern boundary(the turtle starts in the south and heads upwards) it returns to its original position for the next level, except this time the blocks are moving faster.
Overall I had a great time building this project. After a quick overview into how the game was supposed to work I found that it was quite straightforward and didn't need much problem solving. I flew though creating the turtle, scoreboard and game over screen but got a little stuck on how to make so many blocks and how to detect if a block has collided with the turtle. In the end, it worked out quite nicely.
When it comes to the actual program, I should have cleaned up the code a bit but was not quite sure how to. Mostly on the car_manager part(The cars are the blocks). I could have turned all of the workings for the blocks/cars I did in the while loop into separate functions in the CarManager class and then simply called them but I wasn't quite sure how to. That is definitely something to improve/look into next time. I am quite happy to finally be in the intermediate territory of python programming.
Here's the code -
main.py
import time
from turtle import Screen
from player import Player
from car_manager import CarManager
from scoreboard import Scoreboard
screen = Screen()
screen.setup(width=600, height=600)
screen.tracer(0)
screen.title("Turtle Crossing Game")
player = Player()
scoreboard = Scoreboard()
cars = []
car = CarManager()
cars.append(car)
loop_count = 0
MOVE_SEGMENT = 5
game_is_on = True
while game_is_on:
time.sleep(0.1)
screen.update()
for c in cars:
c.backward(MOVE_SEGMENT)
screen.onkey(player.move_up, "Up")
screen.listen()
if player.ycor() >= 280:
scoreboard.update_score()
player.reset_pos()
MOVE_SEGMENT += 7
loop_count += 1
if loop_count == 6:
car = CarManager()
cars.append(car)
loop_count = 0
for cr in cars:
if player.distance(cr) < 20:
game_is_on = False
screen.tracer(1)
scoreboard.game_over()
screen.exitonclick()
player.py
from turtle import Turtle
STARTING_POSITION = (0, -280)
MOVE_DISTANCE = 10
FINISH_LINE_Y = 280
class Player(Turtle):
def __init__(self):
super().__init__()
self.color("black")
self.shape("turtle")
self.setheading(90)
self.penup()
self.goto(STARTING_POSITION)
def move_up(self):
self.forward(MOVE_DISTANCE)
def reset_pos(self):
self.goto(STARTING_POSITION)
scoreboard.py
from turtle import Turtle
FONT = ("Courier New", 22, "bold")
class Scoreboard(Turtle):
def __init__(self):
super().__init__()
self.color("black")
self.penup()
self.hideturtle()
self.goto(-280,260)
self.write("Score:0", font = FONT)
self.score = 0
def update_score(self):
self.score += 1
self.clear()
self.write("Score:" + str(self.score), font = FONT)
def game_over(self):
self.goto(-70,-10)
self.write("Game Over.", font=FONT)
car_manager.py
from turtle import Turtle
import random
COLORS = ["red", "orange", "yellow", "green", "blue", "purple"]
STARTING_MOVE_DISTANCE = 5
MOVE_INCREMENT = 10
class CarManager(Turtle):
def __init__(self):
super().__init__()
self.color(random.choice(COLORS))
self.shape("square")
self.penup()
self.shapesize(stretch_wid=1, stretch_len=2)
self.random_y = random.randint(-250, 250)
self.goto(300,self.random_y)
def collision(self):
pass
Top comments (6)
It sure is crazy how complex simple games can be. When I first started programming, I only ever though of the surface level details, not the other parts required to make it. I can most definitely see that now. Crossy roads was my inspiration for this over-simplified game but next time I'll try to recreate some of the harder parts. What sort of puzzles do you like building?
I'm sorry, I'm not sure I understand the second bit(after the GIF), could you pls reiterate?