m0_59454930 2021-11-17 22:11 采纳率: 100%
浏览 29
已结题

求解答:为什么这段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()

求解答

  • 写回答

2条回答 默认 最新

  • 技术专家团-Bamboo 2021-11-17 22:13
    关注

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

    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()
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论 编辑记录
查看更多回答(1条)

报告相同问题?

问题事件

  • 系统已结题 11月25日
  • 已采纳回答 11月17日
  • 创建了问题 11月17日

悬赏问题

  • ¥15 对于这个复杂问题的解释说明
  • ¥50 三种调度算法报错 采用的你的方案
  • ¥15 关于#python#的问题,请各位专家解答!
  • ¥200 询问:python实现大地主题正反算的程序设计,有偿
  • ¥15 smptlib使用465端口发送邮件失败
  • ¥200 总是报错,能帮助用python实现程序实现高斯正反算吗?有偿
  • ¥15 对于squad数据集的基于bert模型的微调
  • ¥15 为什么我运行这个网络会出现以下报错?CRNN神经网络
  • ¥20 steam下载游戏占用内存
  • ¥15 CST保存项目时失败