简体   繁体   English

使用 python 类(OOP)创建功能性海龟对象?

[英]Using python classes (OOP) to create FUNCTIONAL turtle objects?

I'm currently coding in python to create a somewhat similar game to 'Space Invaders'.我目前正在用 python 编写代码,以创建一个与“太空入侵者”有些相似的游戏。 It was going well until I tried to make all of my invader objects (which are turtles) by using a class, so I could easily manipulate the number of enemies I wanted when I adjust the difficulty.在我尝试使用类制作所有入侵者对象(即海龟)之前,一切进展顺利,因此我可以在调整难度时轻松操纵我想要的敌人数量。

However now I have done this I have removed functionality to the TURTLE objects as they are now seen as CLASS OBJECTS instead.但是现在我已经这样做了,我已经删除了 TURTLE 对象的功能,因为它们现在被视为 CLASS OBJECTS。 Therefore I cannot use turtle module features such as [object].xcor and [object].ycor which are somewhat essential.因此,我不能使用乌龟模块功能,例如[object].xcor[object].ycor ,它们在某种程度上是必不可少的。

Here is the code that makes the class of objects:这是创建对象类的代码:

class invaders():
def __init__(self, speed, position):
    self = turtle.Turtle()   # how I thought I could make them turtle objects (didn't work)
    self.color('red')   
    self.shape('circle')
    self.up()
    self.setposition(position)
    self.speed(speed)

def change_speed(newSpeed):
    invader.speed(newSpeed)   # use to make new speed if changing difficulty  #variable.changeSpeed(newSpeed)

def change_position(x, y):
    invader.setposition(x, y)

I have also attached a photo below so you can see the difference between the two objects I am talking about.我还在下面附上了一张照片,以便您可以看到我正在谈论的两个对象之间的区别。

文本下方的错误不相关

Objects can contain another class instance or objects can be ( subclass ) an instance of another class.对象可以包含另一个类的实例,或者对象可以子类)另一个类的实例。 What you initially described and attempted was subclassing , what you settled for was contain .你最初描述和尝试的是子类化,你解决的是包含 Here's a (Python3) example of how we might subclass turtle to make an invader:这是一个(Python3)示例,说明我们如何将乌龟子类化为入侵者:

from turtle import Turtle, Screen

class Invader(Turtle):
    def __init__(self, speed, position):
        super().__init__(shape='circle', visible=False)
        self.color('red')
        self.penup()
        self.setposition(position)
        self.speed(speed)
        self.showturtle()

    def change_speed(self, newSpeed):
        self.speed(newSpeed)

    def change_position(self, x, y):
        self.setposition(x, y)

invader = Invader('slowest', (100, 100))

invader.change_position(-100, -100)

invader.change_speed('fastest')

invader.pendown()  # try an original turtle method

invader.change_position(100, -100)

screen = Screen()

screen.exitonclick()

Now when we create an invader instance, it describes itself as such:现在,当我们创建一个入侵者实例时,它会这样描述自己:

>>> fred = Invader("normal", (0, 0))
>>> fred
<__main__.Invader object at 0x1021784e0>
>>> 

Self = turtle.Turtle() should be self.turtle = turtle.Turtle() Self = turtle.Turtle()应该是self.turtle = turtle.Turtle()

Thanks @Stack谢谢@Stack

实际上,您可以使用 self.turtle.color("red") 来访问海龟的颜色属性。

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

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