-3

我是第一次运行 Linux mint。我尝试编写一个 python 问题,但是两天来,由于 Linux 界面,我一直面临问题

这是我的代码:-

    import turtle
    import time
    boxsize=200
    caught=False
    score=0
    #function that are called in keypress
    def up():
        mouse.forward(10)
        checkbound()

    def left():
        move.left(45)

    def right():
        move.right(45)

    def back():
        mouse.backward(10)
        checkbound()

    def quitTurtles():
        window.bye()
    #stop the mouse from leaving the square set size
    def checkbound():
        global boxsize
        if mouse.xcor()>boxsize:
            mouse.goto(boxsize, mouse.ycor())
        if mouse.xcor()<-boxsize:
             mouse.goto(-boxsize, mouse.ycor())
        if mouse.ycor()>boxsize:
            mouse.goto(mouse.xcor(),boxsize)

        if mouse.ycor()<-boxsize:
            mouse.goto(mouse.xcor(),-boxsize)

    #set up screen
    window=turtle.Screen()
    mouse=turtle.Turtle()
    cat=turtle.Turtle()
    mouse.penup()
    mouse.penup()
    mouse.goto(100,100)

    #add key listeners
    window.onkeypress(up ,'UP')
    window.onkeypress(right ,'left')
    window.onkeypress(left ,'Right')
    window.onkeypress(back ,'DOWN')
    window.onkeypress(quitTurtles, "Escape")

    difficulty=window.numinput("difficulty","Enter a difficulty from 1 to 5",minval=1,maxval=5)
    window.listen()
    #main loop
    #note how it changes with difficulty
    while not caught:
        cat.setheading(cat.towards(mouse))
        cat.forward(8+diffficulty)
        score=score+1
        if cat.distance(mouse)<5:
            caught=true

        time.sleep(0.2-(0.1*difficulty))
    window.textinput("GAME OVER","WELL DONE YOU SCORED:"+str(score*difficulty))
    window.bye()

这是错误

4

1 回答 1

0

这段代码有几个问题,其中许多会使其无法正常运行:

代替:move_mouse

def up():
    mouse.forward(10)
    checkbound()

def left():
    move.left(45)

不必要的global声明 asboxsize未分配:

def checkbound():
    global boxsize

在代码复制和粘贴中,没有更改mousecat

mouse=turtle.Turtle()
cat=turtle.Turtle()
mouse.penup()
mouse.penup()

difficulty变量拼写不一致:

    cat.forward(8+diffficulty)
    time.sleep(0.2-(0.1*difficulty))

布尔值的不正确大小写:

 caught=true

如评论中所述,键命名情况完全不一致:

window.onkeypress(right ,'left')
window.onkeypress(left ,'Right')
window.onkeypress(back ,'DOWN')

更大的问题是sleep()在事件驱动的环境中使用并且缺乏绘制的边界,因此玩家知道限制。与其在 SO 问题中一一解决这些问题,不如让我们重新编写此代码以在海龟事件环境中工作并作为游戏可玩:

from turtle import Screen, Turtle

BOX_SIZE = 600

# functions that are called in keypress
def up():
    mouse.forward(15)
    checkbound()

def left():
    mouse.left(45)

def right():
    mouse.right(45)

def back():
    mouse.backward(15)
    checkbound()

def checkbound():
    ''' stop the mouse from leaving the square set size '''

    if mouse.xcor() > BOX_SIZE/2:
        mouse.goto(BOX_SIZE/2, mouse.ycor())
    elif mouse.xcor() < -BOX_SIZE/2:
        mouse.goto(-BOX_SIZE/2, mouse.ycor())

    if mouse.ycor() > BOX_SIZE/2:
        mouse.goto(mouse.xcor(), BOX_SIZE/2)
    elif mouse.ycor() < -BOX_SIZE/2:
        mouse.goto(mouse.xcor(), -BOX_SIZE/2)

def move():
    global score

    cat.setheading(cat.towards(mouse))
    cat.forward(2 * difficulty)
    score += 1

    if cat.distance(mouse) < 5:
        screen.textinput("GAME OVER", "WELL DONE YOU SCORED: {}".format(score * difficulty))
        screen.bye()
    else:
        screen.ontimer(move, 200 - 100 * difficulty)

score = 0

# set up screen
screen = Screen()

marker = Turtle()
marker.hideturtle()
marker.penup()
marker.goto(-BOX_SIZE/2, -BOX_SIZE/2)
marker.pendown()
for _ in range(4):
    marker.forward(BOX_SIZE)
    marker.left(90)

difficulty = int(screen.numinput("difficulty", "Enter a difficulty from 1 to 5", minval=1, maxval=5))

cat = Turtle()
cat.shapesize(2)
cat.penup()

mouse = Turtle()
mouse.penup()
mouse.goto(200, 200)

# add key listeners
screen.onkeypress(up, 'Up')
screen.onkeypress(right, 'Left')
screen.onkeypress(left, 'Right')
screen.onkeypress(back, 'Down')
screen.onkeypress(screen.bye, 'Escape')
screen.listen()

screen.ontimer(move, 1000)  # give player a chance to move hand from keyboard to mouse

screen.mainloop()
于 2020-01-29T17:26:56.953 回答