启动后,我的游戏运行正常,但是有2个严重问题;我似乎无法使碰撞检测正常工作,并且两只乌龟都随机停止了! 我正在使用Repl.it
我尝试移动代码段,尝试重做部分代码以与前进/后退相对应,并且我尝试在没有计时器的情况下更新列表,但这会导致严重的延迟
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()
我希望两只乌龟都可以无限移动,直到一只与另一只发生碰撞,但是一只总是停下来或者两只都停下来。