day 23 - crossroads game
This commit is contained in:
43
023/car_manager.py
Normal file
43
023/car_manager.py
Normal file
@ -0,0 +1,43 @@
|
|||||||
|
import random
|
||||||
|
from turtle import Turtle
|
||||||
|
|
||||||
|
class CarManager:
|
||||||
|
|
||||||
|
COLORS = ["red", "orange", "yellow", "green", "blue", "purple"]
|
||||||
|
STARTING_MOVE_DISTANCE = 5
|
||||||
|
MOVE_INCREMENT = 10
|
||||||
|
CAR_GENERATION_PROBABILITY = 5
|
||||||
|
|
||||||
|
def __init__(self):
|
||||||
|
self.cars = []
|
||||||
|
self.add_car()
|
||||||
|
|
||||||
|
def execute_round(self, level):
|
||||||
|
if self.should_generate_car_on_turn():
|
||||||
|
self.add_car()
|
||||||
|
|
||||||
|
speed = 5 + (level - 1) * 10
|
||||||
|
self.move(speed)
|
||||||
|
|
||||||
|
def should_generate_car_on_turn(self):
|
||||||
|
return random.randint(1,self.CAR_GENERATION_PROBABILITY) % self.CAR_GENERATION_PROBABILITY == 0
|
||||||
|
|
||||||
|
def move(self, speed):
|
||||||
|
for car in self.cars:
|
||||||
|
car.goto(x=car.xcor()-speed, y=car.ycor())
|
||||||
|
|
||||||
|
def add_car(self):
|
||||||
|
car = Turtle()
|
||||||
|
car.color(random.choice(self.COLORS))
|
||||||
|
car.shape("square")
|
||||||
|
car.shapesize(stretch_wid=1, stretch_len=2)
|
||||||
|
car.pu()
|
||||||
|
car.goto(x=300, y=random.randint(-260, 280))
|
||||||
|
|
||||||
|
self.cars.append(car)
|
||||||
|
|
||||||
|
def check_collision(self, player):
|
||||||
|
for car in self.cars:
|
||||||
|
if abs(player.ycor() - car.ycor()) < 10 and abs(player.xcor() - car.xcor()) < 30:
|
||||||
|
return True
|
||||||
|
return False
|
||||||
34
023/main.py
Normal file
34
023/main.py
Normal file
@ -0,0 +1,34 @@
|
|||||||
|
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.bgcolor("white")
|
||||||
|
|
||||||
|
game_is_on = True
|
||||||
|
|
||||||
|
player = Player()
|
||||||
|
scoreboard = Scoreboard()
|
||||||
|
cars = CarManager()
|
||||||
|
|
||||||
|
while game_is_on:
|
||||||
|
screen.listen()
|
||||||
|
screen.onkey(player.move_up, "Up")
|
||||||
|
cars.execute_round(scoreboard.level)
|
||||||
|
if cars.check_collision(player):
|
||||||
|
game_is_on = False
|
||||||
|
scoreboard.game_over()
|
||||||
|
time.sleep(0.1)
|
||||||
|
screen.update()
|
||||||
|
|
||||||
|
if player.has_won():
|
||||||
|
player.reset_position()
|
||||||
|
scoreboard.level += 1
|
||||||
|
scoreboard.update_score(scoreboard.level)
|
||||||
|
print("Finished")
|
||||||
|
|
||||||
|
screen.exitonclick()
|
||||||
27
023/player.py
Normal file
27
023/player.py
Normal file
@ -0,0 +1,27 @@
|
|||||||
|
from turtle import Turtle
|
||||||
|
|
||||||
|
class Player(Turtle):
|
||||||
|
STARTING_POSITION = (0, -280)
|
||||||
|
MOVE_DISTANCE = 10
|
||||||
|
FINISH_LINE_Y = 280
|
||||||
|
|
||||||
|
def __init__(self):
|
||||||
|
super().__init__()
|
||||||
|
self.shape("turtle")
|
||||||
|
self.color("black")
|
||||||
|
self.pu()
|
||||||
|
self.setheading(90)
|
||||||
|
self.goto(self.STARTING_POSITION)
|
||||||
|
|
||||||
|
def move_up(self):
|
||||||
|
self.goto(x=self.xcor(), y=self.ycor() + self.MOVE_DISTANCE)
|
||||||
|
print(self.ycor())
|
||||||
|
|
||||||
|
def has_won(self):
|
||||||
|
if self.ycor() >= self.FINISH_LINE_Y:
|
||||||
|
return True
|
||||||
|
return False
|
||||||
|
|
||||||
|
def reset_position(self):
|
||||||
|
self.clear()
|
||||||
|
self.goto(self.STARTING_POSITION)
|
||||||
23
023/scoreboard.py
Normal file
23
023/scoreboard.py
Normal file
@ -0,0 +1,23 @@
|
|||||||
|
from turtle import Turtle
|
||||||
|
|
||||||
|
class Scoreboard(Turtle):
|
||||||
|
|
||||||
|
FONT = ("Courier", 24, "normal")
|
||||||
|
|
||||||
|
def __init__(self):
|
||||||
|
super().__init__()
|
||||||
|
self.color("black")
|
||||||
|
self.pu()
|
||||||
|
self.hideturtle()
|
||||||
|
self.goto(-280, 260)
|
||||||
|
self.level = 1
|
||||||
|
|
||||||
|
self.update_score(self.level)
|
||||||
|
|
||||||
|
def update_score(self, level):
|
||||||
|
self.clear()
|
||||||
|
self.write(f"Level: {level}", False, align="left", font=self.FONT)
|
||||||
|
|
||||||
|
def game_over(self):
|
||||||
|
self.goto(0,0)
|
||||||
|
self.write("GAME OVER", False, align="center", font=self.FONT)
|
||||||
Reference in New Issue
Block a user