I'm trying to create a sprite class that I can assign its size, color, coordinates, and group to. Here's how it looks like:
import pygame, sys
from pygame.locals import *
RED = (255,0,0)
class MapObject(pygame.sprite.Sprite):
def __init__(self, size, color, originxy, group):
super().__init__()
self.surf = pygame.Surface(size)
self.surf.fill(color)
self.rect = self.surf.get_rect(bottomleft = originxy)
group.add(self)
floors = pygame.sprite.Group()
# MapObject(size, color, originxy, group)
PT1 = MapObject((5760, 150), RED, (-2880, 1080), floors)
It works just fine, but I can only assign the coordinates to the bottomleft of the sprite, so I tried to add another argument to it to allow originxy to be, for example, the midtop of itself.
I thought that I could simply replace bottomleft with a new MapObject() argument, namely origin and that it would set the coordinates from that argument.
import pygame, sys
from pygame.locals import *
RED = (255,0,0)
class MapObject(pygame.sprite.Sprite):
def __init__(self, size, color, origin, originxy, group):
super().__init__()
self.surf = pygame.Surface(size)
self.surf.fill(color)
self.rect = self.surf.get_rect(origin = originxy)
group.add(self)
floors = pygame.sprite.Group()
# MapObject(size, color, origin, originxy, group)
PT1 = MapObject((5760, 150), RED, midtop, (-2880, 1080), floors)
I expected the origin argument to take the place of what would normally be bottomleft, but instead I got an error:
Traceback (most recent call last):
File "problem test2.py", line 17, in \<module\>
PT1 = MapObject((5760, 150), RED, midtop, (-2880, 1080), floors)
NameError: name 'midtop' is not defined
I knew that I had to define floors as a sprite group, but I don't know what I have to define midtop as in order to use it as a keyword argument in get_rect. Does anyone know how I can do this?