如何修复 AttributeError: '_Screen' 对象在 python 海龟中没有属性 'onkeypress' ?

问题描述 投票:0回答:1

我正在编写我的Python游戏,我想添加一个基本菜单,你可以在其中选择游戏的难度。但我收到了一个以前没有的新错误。有人可以帮我吗?

代码如下:

import turtle, random, math, time, os, sys
from pygame import mixer
from tkinter import *

def game():

    mixer.init()

    WIDTH = 800
    HEIGHT = 600

    fenster = turtle.Screen()
    fenster.setup(WIDTH, HEIGHT,0,0)
    fenster.title("Crossy Road von Julian")
    fenster.bgpic('back.gif')
    fenster.tracer(0)

    colors = ["white", "orange", "yellow", "blue", "gray"]
    images = ["a.gif", "b.gif", "c.gif", "p.gif", "f.gif"]
    music = ["m.waf"]

    sound1 = mixer.Sound("m.wav")
    sound1.play()

    for image in images:
        turtle.register_shape(image)    

    class Bus(turtle.Turtle):
        def __init__(self):
            super(Bus, self).__init__()
            self.penup()
            self.shape("f.gif")
            self.shapesize(1,1)
            self.setpos(0, 280)
            self.in_game_speed = 0.2
            # self.fillcolor("Gray")

        def update(self):
            self.goto(self.xcor() + self.in_game_speed, self.ycor())
            if self.xcor() > WIDTH:
                self.setx(-600)
                self.fillcolor("Gray")



    class Muenzen(turtle.Turtle):
        def __init__(self):
            super(Muenzen, self).__init__()
            self.penup()
            self.shape("circle")
            self.shapesize(0.5)
            self.pencolor("yellow")
            self.fillcolor("gold")
            self.setpos(0,0)
            self.y_positions = [-150, 200, 60 ,90]
            self.setx(random.randrange(-400,400))
            self.sety(random.choice(self.y_positions))


    class Auto(turtle.Turtle):
        def __init__(self):
            super(Auto, self).__init__()
            self.penup()
            self.shape("square")
            self.shapesize(2,5)
            # self.pencolor("blue")
            self.directions = ["left", "right"]
            self.direction = "left"
            self.in_game_speed = 2.3

        def update(self):
            if self.direction == "right":
                if self.xcor() > WIDTH:
                    self.setx(random.randrange(-600,-400))

            if self.direction == "left":
                if self.xcor() < -WIDTH:
                    self.setx(400)

            if self.direction == "right":
                self.goto(self.xcor() + self.in_game_speed, self.ycor())
            else:
                self.goto(self.xcor() - self.in_game_speed, self.ycor())



    class Spieler(turtle.Turtle):
        def __init__(self, auto, coin, bus):
            super(Spieler, self).__init__()
            self.penup()
            self.shape("p.gif")
            self.shapesize(1,1)
            self.setpos(0, -250)
            # self.pencolor("yellow")
            self.in_game_speed = 4.3
            self.speed(0)
            self.score = 0

        def right(self):
            self.goto(self.xcor() + self.in_game_speed, self.ycor())

        def left(self):
            self.goto(self.xcor() - self.in_game_speed, self.ycor())

        def up(self):
            self.goto(self.xcor(), self.ycor() + self.in_game_speed)

        def down(self):
            self.goto(self.xcor(), self.ycor() - self.in_game_speed)

        def powershell(self):
            os.system('start powershell.exe')

        def is_collision(self, other):
            a = self.xcor() - other.xcor()
            b = self.ycor() - other.ycor()
            distance = math.sqrt((a**2) + (b**2))
            if distance < 25:
                return True
            else:
                return False

        def update(self):
            for coin in coins:
                if self.is_collision(coin):
                    self.score += 1
                    scorecount.clear()
                    scorecount.write(self.score, font = font)
                    coin.setpos(0,450)
                    coins.remove(coin)
            for auto in group_6:
                if self.is_collision(auto):
                    scorecount.clear()
                    self.score = 0
                    if auto.direction == "right":
                        self.setpos(10,-280)
                        self.setpos(10,-280)
                    if auto.direction == "left":
                        self.setpos(10,-280)
                        self.setpos(10,-280)
            for auto in group_5:
                if self.is_collision(auto):
                    scorecount.clear()
                    self.score = 0
                    if auto.direction == "right":
                        self.setpos(10,-280)
                        self.setpos(10,-280)
                    if auto.direction == "left":
                        self.setpos(10,-280)
                        self.setpos(10,-280)
            for auto in group_4:
                if self.is_collision(auto):
                    scorecount.clear()
                    self.score = 0
                    if auto.direction == "right":
                        self.setpos(10,-280)
                        self.setpos(10,-280)
                    if auto.direction == "left":
                        self.setpos(10,-280)
                        self.setpos(10,-280)
            for auto in group_3:
                if self.is_collision(auto):
                    scorecount.clear()
                    self.score = 0
                    if auto.direction == "right":
                        self.setpos(10,-280)
                        self.setpos(10,-280)
                    if auto.direction == "left":
                        self.setpos(10,-280)
                        self.setpos(10,-280)
            for auto in group_2:
                if self.is_collision(auto):
                    scorecount.clear()
                    self.score = 0
                    if auto.direction == "right":
                        self.setpos(10,-280)
                        self.setpos(10,-280)
                    if auto.direction == "left":
                        self.setpos(10,-280)
                        self.setpos(10,-280)
            for auto in group_1:
                if self.is_collision(auto):
                    scorecount.clear()
                    self.score = 0
                    if auto.direction == "right":
                        self.setpos(10,-280)
                        self.setpos(10,-280)
                    if auto.direction == "left":
                        self.setpos(10,-280)
                        self.setpos(10,-280)

            if self.is_collision(bus):
                self.fillcolor("Gray")
                self.setpos(500,500)
                scorecount.setpos(0,0)
                font2 = ("arial", 40)
                scorecount.color("black")
                scorecount.write("Super!!!", font = font2)


    class Score(turtle.Turtle):
        def __init__(self):
            super(Score, self).__init__()
            self.hideturtle
            self.penup()
            self.pencolor("white")
            self.pensize(2)

    scorefont = Score()
    scorefont.setpos(200,-280)
    font = ("Germania One", 18)         
    scorefont.write("Geld:", font = font)

    scorecount = Score()
    scorecount.setpos(310,-280)

    coins = []
    for coin in range(3):
        coin = Muenzen()
        coins.append(coin)

    group_1 = []
    for auto in range(1):
        auto = Auto()
        auto.sety(200)
        auto.setx(-400)
        auto.in_game_append = 5
        auto.shape("a.gif")
        group_1.append(auto)

    group_2 = []
    for auto in range(1):
        auto = Auto()
        auto.sety(-50)
        auto.setx(-500)
        auto.in_game_append = 5
        auto.shape("b.gif")
        group_2.append(auto)

    group_3 = []
    for auto in range(1):
        auto = Auto()
        auto.sety(180)
        auto.setx(300)
        auto.in_game_append = 5
        auto.shape("c.gif")
        group_3.append(auto)

    group_4 = []
    for auto in range(1):
        auto = Auto()
        auto.sety(0)
        auto.setx(400)
        auto.in_game_append = 1
        auto.shape("a.gif")
        group_4.append(auto)

    group_5 = []
    for auto in range(1):
        auto = Auto()
        auto.sety(100)
        auto.setx(100)
        auto.in_game_append = 1
        auto.shape("b.gif")
        group_5.append(auto)

    group_6 = []
    for auto in range(1):
        auto = Auto()
        auto.sety(-100)
        auto.setx(-50)
        auto.in_game_append = 1
        auto.shape("c.gif")
        group_6.append(auto)

    bus = Bus()


    spieler = Spieler(auto, coin, bus)

    turtle.listen()
    fenster.onkeypress(spieler.right,"Right")
    fenster.onkeypress(spieler.left,"Left")
    fenster.onkeypress(spieler.up,"Up") 
    fenster.onkeypress(spieler.down,"Down")
    fenster.onkeypress(spieler.powershell,"p")

    running = True
    while running:
        fenster.update()
        spieler.update()
        for auto in group_1:
            auto.update()
        for auto in group_2:
            auto.update()
        for auto in group_3:
            auto.update()
        for auto in group_4:
            auto.update()
        for auto in group_5:
            auto.update()
        for auto in group_6:
            auto.update()
        bus.update()


    turtle.mainloop()

def main():

    os.system('clear')
    diff=input('Wilkommen zu Crossy Road von Julian und Eren!\nLeicht = 1 | Mittel = 2 | Schwer = 3\n\n')
    if diff == 1:
        game()
    if diff == 2:
        sys.exit()
    if diff == 3:
        sys.exit()

main()

当您按 1 时游戏应该开始,但我收到此消息:

AttributeError:“_Screen”对象没有属性“onkeypress”

python pygame turtle-graphics python-turtle
1个回答
2
投票

这是一个 Python 2 与 Python 3 的问题。 在 Python 3 中添加了

onkeypress()
来与
onkeyrelease()
形成对比。 在 Python 2 中,只有
onkey()

onkeypress()
替换为
onkey()
,看看这是否适合您。

额外提示:

而不是自己做:

def is_collision(self, other):
    a = self.xcor() - other.xcor()
    b = self.ycor() - other.ycor()
    distance = math.sqrt((a**2) + (b**2))
    if distance < 25:
        return True
    else:
        return False

考虑使用海龟自己的

distance()
方法,它的作用相同:

def is_collision(self, other):
    return self.distance(other) < 25
© www.soinside.com 2019 - 2024. All rights reserved.