Compare commits

...

2 Commits

Author SHA1 Message Date
59a3d6d830 day 23 - crossroads game 2025-07-16 14:17:51 +02:00
697d53f836 day 22 (actually, there's no 21) - finished Pong 2025-07-16 12:47:31 +02:00
9 changed files with 179 additions and 9 deletions

View File

@ -6,10 +6,11 @@ class Ball(Turtle):
super().__init__() super().__init__()
self.shape("circle") self.shape("circle")
self.color("white") self.color("white")
self.speed("fastest")
self.pu() self.pu()
self.move_speed = 0.1
self.angle = random.randint(0,45) self.angle = random.randint(0,45)
self.angle = 50
self.setheading(self.angle) self.setheading(self.angle)
print(self.angle) print(self.angle)
@ -22,6 +23,6 @@ class Ball(Turtle):
self.setheading(self.angle) self.setheading(self.angle)
def bounce_paddle(self): def bounce_paddle(self):
self.angle = -self.angle self.angle = 180 - self.angle
self.setheading(self.angle) self.setheading(self.angle)

View File

@ -1,6 +1,8 @@
from turtle import Turtle, Screen from turtle import Turtle, Screen
from paddle import Paddle from paddle import Paddle
from ball import Ball from ball import Ball
from score import Score
import time
class GameManager(): class GameManager():
BOARD_WIDTH=800 BOARD_WIDTH=800
@ -21,6 +23,9 @@ class GameManager():
self.right_paddle = Paddle(350) self.right_paddle = Paddle(350)
self.left_paddle = Paddle(-370) self.left_paddle = Paddle(-370)
self.right_score = Score(30)
self.left_score = Score(-30)
self.ball = Ball() self.ball = Ball()
def draw_delimiter(self): def draw_delimiter(self):
@ -44,13 +49,31 @@ class GameManager():
self.control_paddle() self.control_paddle()
self.ball.move() self.ball.move()
if abs(self.ball.xcor()) >= 400: if self.ball.xcor() > 400:
self.game_on = False self.game_on = False
self.left_score.increment()
self.left_score.update()
if self.ball.xcor() < -400:
self.game_on = False
self.right_score.increment()
self.right_score.update()
if self.game_on == False:
self.ball.clear()
self.ball = Ball()
self.right_paddle.reset_position(350)
self.left_paddle.reset_position(-370)
self.game_on = True
if abs(self.ball.ycor()) > 280: if abs(self.ball.ycor()) > 280:
self.ball.bounce_wall() self.ball.bounce_wall()
print(self.ball.ycor()) if (abs(self.ball.xcor() - self.right_paddle.xcor()) <= 20 and abs(self.ball.ycor() - self.right_paddle.ycor()) < 50) or (abs(self.ball.xcor() - self.left_paddle.xcor()) <= 20 and abs(self.ball.ycor() - self.left_paddle.ycor()) < 50):
self.ball.bounce_paddle()
self.ball.move_speed = self.ball.move_speed * 0.1
time.sleep(self.ball.move_speed)
def control_paddle(self): def control_paddle(self):
self.screen.listen() self.screen.listen()

View File

@ -1,9 +1,7 @@
from game import GameManager from game import GameManager
from paddle import Paddle from paddle import Paddle
import time
game = GameManager() game = GameManager()
while game.game_on: while game.game_on == True:
game.game_round() game.game_round()
time.sleep(0.1)
game.screen.exitonclick() game.screen.exitonclick()

View File

@ -10,6 +10,9 @@ class Paddle(Turtle):
self.pu() self.pu()
self.teleport(x=x_pos, y=0) self.teleport(x=x_pos, y=0)
def reset_position(self, x_pos):
self.teleport(x=x_pos, y=0)
def up(self): def up(self):
self.goto(x=self.xcor(), y=self.ycor()+20) self.goto(x=self.xcor(), y=self.ycor()+20)

18
022/score.py Normal file
View File

@ -0,0 +1,18 @@
from turtle import Turtle
class Score(Turtle):
def __init__(self, x_pos):
super().__init__()
self.score = 0
self.color("white")
self.hideturtle()
self.pu()
self.goto(x=x_pos, y=270)
self.write(self.score, move=False, align='center', font=('Arial', 24, 'normal'))
def increment(self):
self.score += 1
def update(self):
self.clear()
self.write(self.score, move=False, align='center', font=('Arial', 24, 'normal'))

43
023/car_manager.py Normal file
View 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
View 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
View 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
View 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)