简体   繁体   English

无法摆脱 (_tkinter.TclError: bad event type or keysym "UP") 问题

[英]Couldn't get rid of (_tkinter.TclError: bad event type or keysym "UP") problem

I am running a Linux mint for the first time .我第一次运行 Linux mint。 I tried coding a python problem but for two days I am continiously facing problems due to Linux interface please我尝试编写一个 python 问题,但两天来我一直面临着由于 Linux 界面而导致的问题

This is my code:-这是我的代码:-

    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()

这是错误

This code has several problems, many of which will keep it from running correctly:这段代码有几个问题,其中许多会使其无法正常运行:

Substituted move for mouse : mouse替代move

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

def left():
    move.left(45)

Unnecessary global declaration as boxsize is not assigned:没有分配作为boxsize不必要的global声明:

def checkbound():
    global boxsize

In code copy-and-paste, didn't change mouse to cat :在代码复制和粘贴中,没有将mouse更改为cat

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

The difficulty variable not spelled consistently: difficulty变量拼写不一致:

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

Incorrect case for boolean:布尔值不正确的大小写:

 caught=true

As noted in comments, total inconsistency in key naming case:如评论中所述,关键命名案例完全不一致:

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

Bigger picture issues are use of sleep() in an event-driven environment and lack of drawn boundaries so player knows the limits.更大的问题是在事件驱动的环境中使用sleep()并且缺乏绘制边界,因此玩家知道限制。 Rather than address these issues one by one in SO questions, let's rework this code to work within the turtle event environment and be playable as a game:与其在 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()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM