screen.tracer(0) player = Player() car_manager = CarManager() scoreboard = Scoreboard() screen.listen() screen.onkey(player.go_up, "Up") game_is_on = True while game_is_on: time.sleep(0.1) screen.update() car_manager.create_car() car_manager.move_cars() # Detect collision with car for car in car_manager.all_cars: if car.distance(player) < 20: game_is_on = False scoreboard.game_over() # Detect successful crossing if player.is_at_finish_line(): player.go_to_start() car_manager.level_up() scoreboard.increase_level() screen.exitonclick()
if ball.move_speed > 0.3: ball.move_speed = 0.3 # making sure no paddle gets out of view: left_paddle.limit_vertical_movement() right_paddle.limit_vertical_movement() # tackling ball bounce logic if ball.ycor() > 280 or ball.ycor() < -280: ball.bounce_y() # when ball collides with any paddles: # collision with right paddle: if ball.distance( right_paddle) < 60 and ball.xcor() > 330 and ball.xcor() < 350: ball.bounce_x() score.increment_score() # collision with left paddle: if ball.distance( left_paddle) < 60 and ball.xcor() < -330 and ball.xcor() > -360: ball.bounce_x() score.increment_score() # game over if ball touches side walls if (ball.xcor() > 395) or (ball.xcor() < -395): score.game_over() game_on = False # keep display active till click is registered screen.exitonclick()
screen.setup(width=WIDTH, height=HEIGHT) #screen.bgcolor("black") screen.title("Cross The Road") screen.tracer(0) myturtle = MyTurtle() my_car = Car() my_score = Scoreboard() screen.listen() screen.onkey(myturtle.go, "Up") is_game_on = True while is_game_on: time.sleep(0.1) screen.update() my_car.create_car() my_car.move() for car in my_car.all_cars: if car.distance(myturtle) < 29: is_game_on = False my_score.game_over() if myturtle.reached(): myturtle.start_again() my_car.level_up() my_score.increase_score() screen.exitonclick()