【问题标题】:Python turtle object wont show in windowPython乌龟对象不会显示在窗口中
【发布时间】:2021-12-09 21:28:12
【问题描述】:

我目前正在尝试使用 python 和 turtle 复制 Atari 的突破。以前我创建了一个不使用 OOP 的 pong 副本,它工作得非常好。然而,由于砖块突破,我决定使用 OOP 并为砖块创建一个对象类。一旦我运行程序,它就不会显示砖块。任何想法为什么?

import turtle

window = turtle.Screen()
window.title('Atari Breakout')
window.bgcolor('black')
window.setup(width=800, height=600)
window.tracer(0)


class Brick(Turtle):
    def __init__(self):
        super().__init__(shape='square', visible=True)
        self.myturtle = turtle.Turtle()
        self.color = 'white'
        self.shapesize(stretch_wid=10, stretch_len=4)
        self.pendown()
        self.goto(-350, 200)


board1 = Brick()
window.update()

【问题讨论】:

    标签: python python-turtle


    【解决方案1】:

    这段代码有一些问题——首先,您需要turtle.Turtle 而不仅仅是Turtle,否则会导致错误。其次,self.myturtle = turtle.Turtle() 这行是不必要的,因为super().__init__(shape='square', visible=True) 已经创建了一个海龟,第三,self.color = 'white' 应该改为self.color('white')。另外,我很确定您的意思是 self.penup() 而不是 self.pendown() 以阻止砖块从中心画一条线到它的位置。

    完成的代码:

    import turtle
    
    window = turtle.Screen()
    window.title('Atari Breakout')
    window.bgcolor('black')
    window.setup(width=800, height=600)
    window.tracer(0)
    
    
    class Brick(turtle.Turtle):
        def __init__(self):
            super().__init__(shape='square', visible=True)
            self.color('white')
            self.shapesize(stretch_wid=10, stretch_len=4)
            self.penup()
            self.goto(-350, 200)
    
    
    board1 = Brick()
    window.update()
    

    【讨论】:

    • 非常感谢!非常感谢你解释我的错误也干杯!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-09-12
    • 2023-03-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多