简体   繁体   中英

Why won't the sprites move upward?

I made a class for my game:

class Magazine(pygame.sprite.Sprite):
    def __init__(self, image, shooter):
        self.image = image
        self.xCoord = shooter.xCoord + (shooter.image.get_width()/2) - (self.image.get_width()/2)
        self.yCoord = shooter.yCoord - self.image.get_height()

    def update(self):
        self.yCoord -= 5
        if self.yCoord <= 0 - self.image.get_height():
            self.kill()

I created an item of this class, blitting it using the xCoord and the yCoord in the game loop:

    for gameObject in gameObjects:
        gameObject.update()
gameObjectsBlittingCounter = 0

for gameObject in gameObjects:
    screen.blit(gameObjects[gameObjectBlittingCounter].image,       ((gameObjects[gameObjectBlittingCounter].xCoord),         (gameObjects[gameObjectBlittingCounter].yCoord)))
    gameObjectBlittingCounter += 1

The item is in the gameObjects list. However, it only blits the object in the same spot over and over, without moving. Why?

I don't see the code where you're blitting, but if you are using the image's own rect via like MySprite.image.get_rect() or whatever, its x and y coordinates need to be updated (or set) as well. Just moving the obj.xCoord and obj.yCoord won't automatically update the rect; one thing you can do in such a situation is to pass an argument for the rect's position as you're calling get_rect() . For example:

target_rect = MySprite.image.get_rect(topleft=(MySprite.xCoord, MySprite.yCoord))
MyScreen.blit(MySprite.image, target_rect)

The above code would need to be called each time the image is drawn.

Another option is to have the sprite's rect attribute updated in tandem with the yCoord. There are a couple of ways to do this. You can do it explicitly:

def update(self):
    self.y -= 5
    self.rect.y -= 5

You can also use properties to make sure that your object is always referencing and updating the rect's x and y coordinates, but that turns into quite a bit more code.

class MySprite(pygame.sprite.Sprite):
    def __init__(self, image, x, y):
        super(MySprite, self).__init__()
        self.image = image
        self.rect = image.get_rect(topleft=(x, y))
        self._x = x
        self._y = y

    @property
    def x(self):
        return self._x

    @x.setter
    def x(self, v):
        self._x = v
        self.rect.x = self._x

    @property
    def y(self):
        return self._y

    @y.setter
    def y(self, v):
        self._y = v
        self.rect.y = self._y

In this way, MySprite.x is always the x of the rect object, as is MySprite.y .

Regardless, whatever coordinates are being used to draw the sprite need to be updated along with the sprite's independent x and y coordinates.

Check your self.yCoord is >= 0, there's a chance it won't be therefore resulting in no movement.

Also, can't you replace

gameObjectBlittingCounter = 0
for gameObject in gameObjects:
    gameObjects[gameObjectBlittingCounter].update()
    gameObjectBlittingCounter += 1 

with:

for gameObject in gameObjects:
    gameObject.update()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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