How can I use a sprite class argument in Pygame as a keyword argument in pygame.Surface.get_rect()?

Question:

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?

Asked By: RobotZap10000

||

Answers:

midtop is the name of an virtual attribute, however there is no constant called midtop. In any case, you cannot specify the keyword of the keyword argument with a and argument. The best thing you can do is to encode the orientation in a tuple. The first item is the horizontal orientation (-1 = left, 0 = center, 1 = reight) and the 2nd item is the vertical orientation (-1 = top, 0 = center, 1 = bottom):

class MapObject(pygame.sprite.Sprite):
    def __init__(self, size, color, orientation, pos, group):
        super().__init__()
        self.surf = pygame.Surface(size)
        self.surf.fill(color)
        cx = pos[0] - orientation[0] * size[0] // 2
        cy = pos[1] - orientation[1] * size[1] // 2
        self.rect = self.surf.get_rect(center = (cx, cy))
        group.add(self)
midtop = (0, -1)
PT1 = MapObject((5760, 150), RED, midtop, (-2880, 1080), floors)
Answered By: Rabbid76
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.