Python Forum
[PyGame] TypeError: unsupported operand type(s) for -: 'int' and 'instancemethod'
Thread Rating:
  • 2 Vote(s) - 4 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[PyGame] TypeError: unsupported operand type(s) for -: 'int' and 'instancemethod'
#6
(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:
class snek(pygame.sprite.Sprite):
 
    def __init__(self,image):
        pygame.sprite.Sprite.__init__(self)
        self.image = pygame.image.load('snek.jpg')
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.

Quote:
        self.x = 340
        self.y = 256
    def draw(self, Surface):
        self.image.blit(snek, (self.x, self.y))
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.

Thanks. I'll do that.
Reply


Messages In This Thread
RE: TypeError: unsupported operand type(s) for -: 'int' and 'instancemethod' - by SnekLover123 - Oct-05-2017, 07:39 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  [PyGame] PLEASE HELP! TypeError: unsupported operand type(s) for +: 'pygame.Surface' and 'int' keyfive 1 5,370 Jun-19-2018, 01:20 PM
Last Post: volcano63

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020