Oct-05-2017, 07:39 PM
(This post was last modified: Oct-05-2017, 07:39 PM by SnekLover123.)
(Oct-05-2017, 06:29 PM)nilamo Wrote: A function defined in the class is already bound to the instance. You call it just as you would any function, with parentheses.>>> class Spam: ... def __init__(self, foo): ... self.bar = foo ... def quz(self): ... print("I'm an instance method!") ... return self.bar ... >>> thing = Spam(42) >>> thing.quz() I'm an instance method! 42
Thanks, but how could I use this in my code?
(Oct-05-2017, 06:59 PM)metulburr Wrote:Quote:If you see any bugs or ways to optimize my code, please tell me.as side notes...
Quote:In this case its fine because there is only one, but usually you do not want to load the images within the class constructor as "enemies" for example could spawn in unlimited numbers. You would only want to load the image once.class snek(pygame.sprite.Sprite): def __init__(self,image): pygame.sprite.Sprite.__init__(self) self.image = pygame.image.load('snek.jpg')
Quote:You should utilize pygame.Rect for the positioning as pygame Rect's have methods to handle collision detection implemented already. You do create a rect attribute (self.rect) but never use it and even bypass it by positioning your own y and x attributes when Rect already contains that.self.x = 340 self.y = 256def draw(self, Surface): self.image.blit(snek, (self.x, self.y))
Thanks. I'll do that.