Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/321.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 问题是球循环一直在循环,你无法控制角色,因为循环一直在运行_Python_Python Turtle - Fatal编程技术网

Python 问题是球循环一直在循环,你无法控制角色,因为循环一直在运行

Python 问题是球循环一直在循环,你无法控制角色,因为循环一直在运行,python,python-turtle,Python,Python Turtle,我需要移动球和按键a/d同时工作,但由于代码自上而下运行,球不断循环,按键代码从未激活,因此不能在球移动的同时移动它 t = turtle.Turtle() t.speed(0) t.pensize(5) t.color("Black") def Rectangle(): for i in range(2): t.forward(590) t.left(90) t.forward(380) t.left(90) Rectangle()

我需要移动球和按键a/d同时工作,但由于代码自上而下运行,球不断循环,按键代码从未激活,因此不能在球移动的同时移动它

t = turtle.Turtle()
t.speed(0)
t.pensize(5)
t.color("Black")

def Rectangle():
  for i in range(2):
    t.forward(590)
    t.left(90)
    t.forward(380)
    t.left(90)

Rectangle()

t.penup()
t.goto(205,100)
t.color("Blue")
t.shape("circle")

A = random.randint(30,60)
B = random.randint(120,150)
C = random.randint(210,240)
D = random.randint(300,330)
Directions = [A, B, C, D]
direct = random.choice(Directions)

def tDirection(direct):
  t.right(direct)
tDirection(direct)
speed = 2
angle = 90

while True:
  color = ['black', 'blue', 'green', 'yellow', 'orange', "purple", "pink", "turquoise"]

  t.forward(speed)
  ty = t.ycor()
  tx = t.xcor()
  #print(ty, tx)

  #breaking out bottom
  if ty < 9:
    Col = random.choice(color)
    while Col == t.color()[0]:
      Col = random.choice(color)
    t.color(Col)
    angleCurr = t.heading()
    if(270>angleCurr>180):
      t.right(angle)
    else:
      t.left(angle)
    t.forward(2)
  #breaking out top
  if ty > 188:
    Col = random.choice(color)
    while Col == t.color()[0]:
      Col = random.choice(color)
    t.color(Col)
    angleCurr = t.heading()
    if(0<angleCurr<90):
      t.right(angle)
    else:
      t.left(angle)
    t.forward(2)
  #breaking out left
  if tx < 11:
    Col = random.choice(color)
    while Col == t.color()[0]:
      Col = random.choice(color)
    t.color(Col)
    angleCurr = t.heading()
    if(180<angleCurr<270):
      t.left(angle)
    else:
      t.right(angle)
    t.forward(2)

  #breaking out right
  if tx > 390:
    Col = random.choice(color)
    while Col == t.color()[0]:
      Col = random.choice(color)
    t.color(Col)
    angleCurr = t.heading()
    if(0<angleCurr<90):
      t.left(angle)
    else:
      t.right(angle)

    t.forward(speed)
##############################################################
#the key press code#
def keyMove(x, y, direction):
    if(not timerUp):      
        #get line of movement from users current position to the end position if they actually move
        #initial position rounded
        x1 = round(x, 3)
        y1 = round(y, 3)
        #final position(potentially) using heading and speed and trig
        x2 = round(x + math.cos(direction / 180 * math.pi) * userSpeed * 3, 3)
        y2 = round(y + math.sin(direction / 180 * math.pi) * userSpeed * 3, 3)
        movementLine = [[x1, y1], [x2, y2]]

        #check if line of movement is not over any of the wall lines
        canMove = True
        #for each wall line
        for line in lines:
            #if the movement line intersects the current wall line
            if(t(user)):
                canMove = False #can't move
                break
        # move if never set to False
        if(canMove):
            user.forward(userSpeed) 
#########################################################################
#four cardinal movement directions
def kright():
    user.setheading(0)
    keyMove(user.xcor(), user.ycor(), user.heading())

def kleft():
    user.setheading(180)
    keyMove(user.xcor(), user.ycor(), user.heading())
#########################################################################
######events######
startGame()
updateScore(0) #get score to display

#list of keyfunctions 
keyFunctions = [kright, kleft]
keyFunctions += keyFunctions 
# key inputs
keys = ["Right", "Left", "d", "a"]
# loop to get the key inputs to work
for i in range(len(keys)):
    Screen().onkey(keyFunctions[i], keys[i])
t=turtle.turtle()
t、 速度(0)
t、 养老金(5)
t、 颜色(“黑色”)
def矩形()
对于范围(2)中的i:
t、 前进(590)
t、 左(90)
t、 前进(380)
t、 左(90)
矩形()
t、 彭普()
t、 后藤(205100)
t、 颜色(“蓝色”)
t、 形状(“圆”)
A=随机随机随机数(30,60)
B=随机随机随机数(120150)
C=random.randint(210240)
D=随机随机随机数(300330)
方向=[A、B、C、D]
直接=随机。选择(方向)
def t方向(直接):
t、 右(直)
t方向(直接)
速度=2
角度=90
尽管如此:
颜色=[‘黑色’、‘蓝色’、‘绿色’、‘黄色’、‘橙色’、‘紫色’、‘粉色’、‘绿松石色’]
t、 前进(速度)
ty=t.ycor()
tx=t.xcor()
#打印(ty,tx)
#破底
如果ty<9:
Col=随机。选择(颜色)
而Col==t.color()[0]:
Col=随机。选择(颜色)
t、 颜色(Col)
角电流=t.航向()
如果(270>角电流>180):
t、 右(角)
其他:
t、 左(角)
t、 转发(2)
#破顶
如果ty>188:
Col=随机。选择(颜色)
而Col==t.color()[0]:
Col=随机。选择(颜色)
t、 颜色(Col)
角电流=t.航向()

如果(0),请查看如何实现按键事件。这将解决您的问题。