同时移动两只乌龟的问题

问题描述

甲鱼移动了,所以另一只由于它而滞后了。有没有一种方法可以持续移动并控制另一个而不滞后?我想控制玩家并不断向敌人投掷匕首,而敌人不断地出现随机运动。有没有办法做到这一点?

import turtle
import math
import random
import time

wn=turtle.Screen()
wn.setup(width=650,height=650)
wn.bgcolor('black')
a=turtle.Turtle()
a.penup()
a.setposition(0,-325)
wn.register_shape('character.gif')
a.shape('character.gif')
w=turtle.Turtle()
w.penup()
w.setposition(0,325)
wn.register_shape('delphi3.gif')
w.shape('delphi3.gif')
#HEALTH COUNT
sa=30
sb=30
S=turtle.Turtle()
S.speed(0)
S.color('white')
S.penup()
S.hideturtle()
S.goto(0,300)
S.write("PLAYER:{}         ORACLE:{}".format(sa,sb),align='center')

#BULLET
bullet=turtle.Turtle()
bullet.penup()
bullet.hideturtle()
bullet.setposition(0,1000)
wn.register_shape('dagger.gif')
bullet.shape('dagger.gif')
distance1=math.sqrt(math.pow(a.xcor()-w.xcor(),2)+math.pow(a.ycor()-w.ycor(),2))
distance2=math.sqrt(math.pow(w.xcor()-bullet.xcor(),2)+math.pow(w.ycor()-bullet.ycor(),2))
def right():
    a.penup()
    a.setheading(0)
    a.forward(20)
    a.pendown()
def left():
    a.penup()
    a.setheading(180)
    a.forward(20)
    a.pendown()
    
def shoot():
    wn.update()
    global sa
    global sb
    bullet.setposition(a.pos())
    bullet.showturtle()
    bullet.penup()
    while True:
        wn.update()
        bullet.speed(0)
        bullet.setheading(90)
        bullet.forward(20)
        if bullet.ycor()>325:
            bullet.hideturtle()
            bullet.setposition(a.pos())

        
        
        distance1=math.sqrt(math.pow(a.xcor()-w.xcor(),2))
        distance2=math.sqrt(math.pow(w.xcor()-bullet.xcor(),2))
        move=random.randint(-100,100)
        angle=random.randint(-180,180)
        w.forward(move)
        w.left(angle)
        
        if w.xcor()>400 or w.xcor()<-400:
            w.penup()
            w.setposition(a.pos())
        elif w.ycor()>400 or w.ycor()<-400:
            w.penup()
            w.setposition(a.pos())
        if distance1<70:
            sa-=1
            S.clear()
            S.write("Player:{}              ORACLE:{}".format(sa,align='center')
        if distance2<70:
            sb-=1
            S.clear()
            S.write("Player:{}              ORACLE:{}".format(sa,align='center')
            w.hideturtle()
            w.showturtle()
        if sa==0:
            wn.clear()
            print('YOU LOSE')
            break
        if sb==0:
            wn.clear()
            print('YOU WIN')
            break
        

turtle.listen()
turtle.onkey(shoot,'space')
turtle.onkey(left,'Left')
turtle.onkey(right,'Right')
while True:
    wn.update()
    move=random.randint(-100,100)
    angle=random.randint(-180,180)
    w.forward(move)
    w.left(angle)
    distance1=math.sqrt(math.pow(a.xcor()-w.xcor(),2))
    distance2=math.sqrt(math.pow(w.xcor()-bullet.xcor(),2))
    if w.xcor()>325 or w.xcor()<-325:
        w.penup()
        w.setposition(0,50)
    elif w.ycor()>325 or w.ycor()<-325:
        w.penup()
        w.setposition(0,50)
    if distance1<70:
        sa-=1
        S.clear()
        S.write("Player A:{}  Player B:{}".format(sa,align='center')
        print('gameover') 
    if distance2<70:
        sb-=1
        S.clear()
        S.write("Player A:{}  Player B:{}".format(sa,align='center')
        w.hideturtle()

解决方法

让两只海龟同时移动非常简单。 您需要做的就是使用 turtle.tracer()turtle.update() 函数。

这是一个例子:

import turtle

# These are just example Turtles and you will
# most likely have something different and will
# have too change out the Turtles.  

one = Turtle()
two = one.clone()

for i in range(100):
    turtle.tracer(1)
    one.forward(1)
    two.forward(1)
    turtle.update()

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...