求解答:为什么这段Python2的代码在Python 3里运行不了

from random import random
from random import choice
from turtle import *
#Define variables
player=[0,-140]
ball=[0,140]
direction=[choice([-2,-1,1,2]),choice([-2,-1])]
#Define founctions
def move(aim):
player[0] += aim

def bounce():
if ball[0]<=-300 or ball[0]>=290:
direction[0]=-direction[0]
elif ball[1]>=150:
direction[1]=-direction[1]
elif ball[1]<=-140+10+5 and player[0]<=ball[0]<=player[0]+70:
direction[1]=-direction[1]

def outside():
if ball[1]<=-140 :return True

def rectangle(x,y,width,height):
up()
goto(x,y)
begin_fill()
for n in range(2):
forward(width)
left(90)
forward(height)
left(90)
end_fill()

def draw():
clear()
up()
goto(ball[0],ball[1])
dot(10,"red")
rectangle(player[0],player[1],70,10)
update()

def gameLoop():
bounce()
ball[0] += direction[0]*2 # ball[0] = ball[0] + direction[0]*2
ball[1] += direction[1]*2
draw()
if outside():
return
ontimer(gameLoop,50)
#Main project
setup(620,320,0,0)
hideturtle()
tracer(False)
listen()
onkey(lambda:move(20),'d')
onkey(lambda:move(-20),'a')
gameLoop()
done()

求解答

尝试过了,可以运行,游戏还挺好玩的,你的问题出在哪?

from random import random
from random import choice
from turtle import *
#Define variables
player=[0,-140]
ball=[0,140]
direction=[choice([-2,-1,1,2]),choice([-2,-1])]
#Define founctions
def move(aim):
    player[0] += aim

def bounce():
    if ball[0]<=-300 or ball[0]>=290:
        direction[0]=-direction[0]
    elif ball[1]>=150:
        direction[1]=-direction[1]
    elif ball[1]<=-140+10+5 and player[0]<=ball[0]<=player[0]+70:
        direction[1]=-direction[1]

def outside():
    if ball[1]<=-140 :
        return True

def rectangle(x,y,width,height):
    up()
    goto(x,y)
    begin_fill()
    for n in range(2):
        forward(width)
        left(90)
        forward(height)
        left(90)
    end_fill()

def draw():
    clear()
    up()
    goto(ball[0],ball[1])
    dot(10,"red")
    rectangle(player[0],player[1],70,10)
    update()

def gameLoop():
    bounce()
    ball[0] += direction[0]*2 # ball[0] = ball[0] + direction[0]*2
    ball[1] += direction[1]*2
    draw()
    if outside():
        return
    ontimer(gameLoop,50)
#Main project
setup(620,320,0,0)
hideturtle()
tracer(False)
listen()
onkey(lambda:move(20),'d')
onkey(lambda:move(-20),'a')
gameLoop()
done()

Python 3版本改动太大了
Python2的代码都是不能在Python 3里运行的