如何修复AttributeError:“ _ Screen”对象在python乌龟中没有属性“ onkeypress”?

时间:2019-07-10 15:04:40

标签: python pygame turtle-graphics

我正在编写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”

1 个答案:

答案 0 :(得分:1)

这是Python 2 vs.Python 3问题。在Python 3中添加了SELECT * FROM SOME_TABLE WHERE KEY>=@start AND KEY<@end LIMIT 10000onkeypress()形成对比。在Python 2中,只有onkeyrelease()

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