[Python] My Turtle Crossing Game on Python : Collision function does not work

Discussão em 'Python' iniciado por Stack, Outubro 2, 2024.

  1. Stack

    Stack Membro Participativo

    I've been trying the Turtle crossing game. It's basically getting the Turtle start out at the bottom of the screen and have it move across the screen while several randomly generated cars travel from the left to the right. The goal is to have your turtle cross the screen avoiding the cars. While remaining parts of the code is okay, I'm having trouble with detecting collission with cars. I've shared the code relating to just the collision function below. And I'm following a course where the instructor did exactly this and got it to work. Is there anything wrong with my code?

    Tried matching the position of car and player via .pos - nothing seems to work. It sometimes works, sometimes just lets the car zoom past the turtle, sometimes detects a collision even though it shouldn't!

    MAIN:

    import time
    from turtle import Screen
    from player import Player
    from car_manager import CarManager
    screen = Screen()
    screen.tracer(0)
    screen.setup(width=600, height=600)
    player = Player()
    cars = CarManager()
    cars.hideturtle()
    player.hideturtle()
    game_is_on = True
    screen.listen()
    screen.onkey(fun=player.move, key="Up")

    while game_is_on:
    time.sleep(0.1)
    scoreboard.display_score()
    screen.update()
    \# Creates cars from the left side of the screen
    cars.generate_cars()
    cars.move()
    \# To detect collision with cars
    for car in cars.car_list:
    if(car.distance(player)) \< 20:
    game_is_on = False
    screen.exitonclick()

    # Player Class

    from turtle import Turtle

    STARTING_POSITION = (0, -280)
    MOVE_DISTANCE = 10
    FINISH_LINE_Y = 280
    class Player(Turtle):
    def __init__(self):
    super().__init__()
    self.player = Turtle()
    self.player.penup()
    self.player.shape("turtle")
    self.player.color("green")
    self.player.shapesize(stretch_wid=2, stretch_len=2)
    self.player.goto(0,-250)
    self.player.setheading(90)
    def move(self):
    self.player.forward(10)
    def check_y_pos(self):
    return self.player.ycor()

    # Car Manager Class

    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.car_list = \[\]
    def generate_cars(self):
    random_chance = random.randint(1,6)
    if random_chance == 5:
    cars = Turtle()
    cars.penup()
    cars.shape("square")
    cars.goto(x=270, y=random.randint(a = -270, b = 270))
    cars.setheading(180)
    cars.shapesize(stretch_len=2, stretch_wid=1)
    cars.color(COLORS\[random.randint(0, 5)\])
    self.car_list.append(cars)
    def move(self):
    for car in self.car_list:
    car.forward(STARTING_MOVE_DISTANCE)

    Continue reading...

Compartilhe esta Página