OOP Cross the road Game With Python Turtle

OOP Cross the road Game With Python Turtle

OOP Cross the road Game With Python Turtle

Today was a challenge for me. Because today I needed to make the Game entirely from scretch and I didnt want to use any given hints. Just me, Pycharm an Goodle. So here is the result.

So the game rules are that the turtle needs to cross the road without colliding with cars. So I thought I will need the player class and the car class that 'rules' the cars. Also, I will need some score holding class to handle the score and surely I will need the main.py to compose it all together

I started with creating the stracture and main.py where we create the screen with specific width and we made it to stay open until the click.

from turtle import Screen
screen = Screen()
screen.setup(width = 800, height = 600)
screen.title("Crossing the road")

screen.exitonclick()

After that I created the car class where it should be bunch of cars that create the traffic and they are moving same direction, so: First, I created one car which inherits the Turtle class.

class CarRuling(Turtle):
    def __init__(self):
        super().__init__()
        self.hideturtle()
        self.cars=[]
        self.create_car()
        self.car_speed = 20

Also, there should be multiple cars, so it should be the list of cars, so we need some kind of loop that creates the new car numerous times. So, at the beginning I created the Constant List with different positions. I could create the list of colors and iterate through the list of colors but I went with positions. Also,in order to make it a little bit more interesting I added random numbers into the list.

POSITIONS = [(random.randint(380, 680), 0), (random.randint(380, 680),50), (random.randint(380, 680), 100), (random.randint(380, 680), 150),(random.randint(380, 680), 200), (random.randint(380, 680),-50),(random.randint(380, 680),-100),(random.randint(380, 680),-150), (random.randint(380, 680),-200)]

now inside the car class we can iterate through the list and create car for each position. Also, I created the COLORS list contant to randomly choose the color from it.

    def create_car(self):
        for pos in POSITIONS:
            self.car = Turtle("square")
            self.car.color(random.choice(COLORS))
            self.car.shapesize(1, 2)
            self.car.penup()
            self.car.goto(pos)
            self.cars.append(self.car)

also, we will need to move all of those cars, so we will need to create new move method where we are going to change only the x coordinates and it should be endlessly. Also, as soon as the cars reach the other side the x coordinates need to reset to start position. Also, i ceep the track of the speed because later I will need it to level up the game.

    def move(self):
        for key in self.cars:
            key.backward(random.randint(0,self.car_speed))
            if key.xcor() < -380:
                key.setx(380)

After I created the car Class I create the Object from it inside the main.py:

car_ruling = CarRuling()

And inside the main.py we make the car move by calling the method move from car class:

car_ruling.move()

Than I created the player Class(in my case my player is called Tim ) which also inherits the Turtle class, cause I want to easily use same functionality as Turtle class:

class Tim(Turtle):
    def __init__(self):
        super().__init__()

        self.shape("turtle")
        self.shapesize(1)
        self.setheading(90)
        self.penup()
        self.goto(0, -280)

also, the player will need some controls so I could control the player (sorry,in the past I was playing with making games Unity)

    def up(self):
        self.forward(20)

    def down(self):
        self.backward(20)

and inside the main.py I finally can create object from my Tim class (aka player class) and also call the event listener:

tim = Tim()

screen.listen()
screen.onkey(tim.up,"Up")
screen.onkey(tim.down,"Down")

*Lastly, for the player as soon as the player reaches the other side of the screen, his position will be reseted to the starting point.

    def reset_position(self):
        self.goto(0, -280)

Than, I thought that I will need some while loop because if the player will reset the position the game needs to restart too. Also, I remember about the trigger for the while loop,some boolean specifically:game_is_on = True(So, while something is true the game continues) so that way I can finish game or continue. Also, here we can put car movements and everything that needs to continue until the game is on and stop as soon as the game stoppes.

while game_is_on:
car_ruling.move()

*The next step is to detect the collision.So logically, the game ends as soon as the player collides with one of the cars an to detect the collision with another object in Turtle class there is a distance function that returns the distance from one object to another object.

    for car in car_ruling.cars:
        if tim.distance(car) < 12:
            game_is_on = False

Also, here we put if statement where we manipulate the reset method for the player position. Also, if the player reaches the other side the level goes up(that's why I needed to keep a trach on a speed of car in the car class.

    if tim.ycor() > 280:
        score.increase_level()
        tim.reset_position()
        car_ruling.level_up()

And inside the car class I need to create the level_up method where I just encrease the speed by 10:

    def level_up(self):
        self.car_speed += 10

After, all of that I will finally need the score class to keep a track on a score:

class Score(Turtle):
    def __init__(self):
        super().__init__()
        self.color("black")
        self.penup()
        self.hideturtle()
        self.point = 0
        self.goto(0, 230)
        self.write(self.point, align="center", font=("Arial", 50, "normal"))

Than I create the increase_score method where the self.point will increase by 1. Also, in order for the new score not to overlap the last score I will need the method where the old score will be cleared and replaced with the new score :

    def update_score(self):
        self.clear()
        self.goto(0, 230)
        self.write(self.point, align="center", font=("Arial", 50, "normal"))

    def increase_score(self):
        self.point += 1
        self.update_score()

So, now I can call this method inside the main.py inside the if statement I created before. So if the player reaches the other side the score will increase:

score.increase_score()