1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.
  2. Anuncie Aqui
    Anuncie aqui você Também: fdantas@4each.com.br

[Python] My Tron-Like game makes the turtles randomly stop

Discussão em 'Python' iniciado por Stack, Setembro 14, 2025.

  1. Stack

    Stack Membro Participativo

    When launched my game runs fine, there are 2 massive issues though; I can't seem to get my collision detection to work, and both my turtles randomly stop! I am using Repl.it

    I have tried moving pieces of code, I have tried reworking parts of it to coordinates opposed to forward/back, and i have tried making my list update without a timer but that resulted in heavy lag

    from turtle import Screen, Turtle
    import sys
    import time
    fps = 2
    # preGame setup
    player1 = Turtle()
    player1.hideturtle()
    player1.up()
    player1.goto(0, 350)
    player1.down()
    player1.right(90)
    player1.color('red')
    player1.speed(5)
    player1.showturtle()

    def r1():
    player1.left(90)

    def l1():
    player1.right(90)

    player2 = Turtle()
    player2.hideturtle()
    player2.up()
    player2.goto(0, -350)
    player2.down()
    player2.left(90)
    player2.color('blue')
    player2.speed(5)
    player2.showturtle()
    players = player1, player2
    def r2():
    player2.right(90)

    def l2():
    player2.left(90)

    # main game loop

    def move():
    player1.forward(5)

    if not (-350 < player1.xcor() < 350 and -350 < player1.ycor() < 350):
    player1.backward(30)

    player2.forward(5)

    if not (-350 < player2.xcor() < 350 and -350 < player2.ycor() < 350):
    player2.backward(30)

    if player1.distance(player2) < 5:
    print("DON'T CRASH INTO THE OTHER PLAYER!")
    sys.exit()
    screen.ontimer(move, 5)
    L = list()
    l = list()
    l.append(player1.pos())
    L.append(player1.pos())
    while True:
    l.extend(player1.pos())
    L.extend(player2.pos())
    time.sleep(.5)
    if player1.distance(L) < 5:
    print('player 2 wins!!')
    sys.exit()
    if player2.distance(l) < 5:
    print('player 1 wins!!')
    sys.exit()
    break

    screen = Screen()

    screen.onkey(r1, 'd')
    screen.onkey(l1, 'a')
    screen.onkey(r2, 'left')
    screen.onkey(l2, 'right')

    screen.listen()

    move()

    screen.mainloop()


    I expect both of the turtles to move indefinitely until one collides with another, but one always stops or both stop.

    Continue reading...

Compartilhe esta Página