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 echarts动画效果失效的问题。官网下载的例子。
  • ¥60 许可证msc licensing软件报错显示已有相同版本软件,但是下一步显示无法读取日志目录。
  • ¥15 Attention is all you need 的代码运行
  • ¥15 一个服务器已经有一个系统了如果用usb再装一个系统,原来的系统会被覆盖掉吗
  • ¥15 使用esm_msa1_t12_100M_UR50S蛋白质语言模型进行零样本预测时,终端显示出了sequence handled的进度条,但是并不出结果就自动终止回到命令提示行了是怎么回事:
  • ¥15 前置放大电路与功率放大电路相连放大倍数出现问题
  • ¥30 关于<main>标签页面跳转的问题
  • ¥80 部署运行web自动化项目
  • ¥15 腾讯云如何建立同一个项目中物模型之间的联系
  • ¥30 VMware 云桌面水印如何添加