Reputation:
Trying to make a clone of space invaders. I'm following a guide from here https://kidscancode.org/blog/2016/08/pygame_shmup_part_5/. This is the player sprite
class Player(pygame.sprite.Sprite):
def __init__(self):
super(Player, self).__init__()
self.image = pygame.image.load('./Assets/PNG/playerShip1_orange.png').convert()
self.image.set_colorkey(BLACK)
self.image = pygame.transform.scale(self.image, (50, 40))
# WIN_SIZE = 500, 500
self.rect = self.image.get_rect()
self.rect.centerx = WIN_SIZE[0] / 2
self.rect.bottom = WIN_SIZE[1] - 10
self.speedx = 0
# NOT WORKING FOR SOME REASON
# self.image.fill(WHITE, self.rect)
# pygame.draw.rect(self.image, WHITE, self.rect, 1)
# pygame.draw.circle(self.image, WHITE, self.rect.center, 25, 1)
pygame.draw.rect(self.image, WHITE, ( (0,0), (50, 40) ), 1)
I want to create a rectangular outline around the sprite, which i'm able to do if i use the last line in above code. However, the commented code doesn't work for some reason even though each argument is correct and no compile/runtime error is thrown.
Relevent Snippets from Main Class
class Game:
def __init__(self):
pygame.init()
self._win = pygame.display.set_mode(WIN_SIZE, 0, 32)
self._clock = pygame.time.Clock()
self._all_sprites = pygame.sprite.Group()
self._player = Player()
self._all_sprites.add(self._player)
self._game_over = False
def run(self):
while not self._game_over:
self._clock.tick(FPS)
self._all_sprites.update()
self._all_sprites.draw(self._win)
pygame.display.flip()
def main():
game = Game()
game.run()
if __name__ == "__main__":
main()
Complete Source : https://github.com/muneeb-devp/Shmup
Any sorta help would be appreciated :)
Upvotes: 2
Views: 434
Reputation: 210878
You have to draw the outline on the image, before the position of the rectangle is set:
self.rect = self.image.get_rect()
pygame.draw.rect(self.image, WHITE, self.rect, 1)
pygame.draw.circle(self.image, WHITE, self.rect.center, 25, 1)
self.rect.centerx = WIN_SIZE[0] // 2
self.rect.bottom = WIN_SIZE[1] - 10
self.speedx = 0
The location of the rectangle which is returned by get_rect()
is (0, 0). This is also the top left position of the outline in the image. Note, you draw the outline on the image not on the display.
Upvotes: 1