The secondo part… here comes the aliens. Github repository with code and assets.
First part
What if… Space invaders was made with pygame?
Part 2: the aliens squad
We make the aliens thanks to this class and the following list
class Alien(pygame.sprite.Sprite):
def __init__(self, file, x, y):
super(Alien, self).__init__()
self.x = x
self.y = y
self.image = load("imgs\\" + file)
self.w, self.h = self.image.get_size()
self.rect = pygame.Rect(self.x, self.y, self.w, self.h)
#self.rect.center = self.x, self.y
g.add(self)
aliens.add(self)
We will create an alien for each item in the list in different rows. We added the aliens in their own group: aliens.
squad = [
[1,1,1,1,1,1,1,1], # line 1... [item, item]
[1,1,1,1,1,1,1,1],
[1,1,1,1,1,1,1,1],
[1,1,1,1,1,1,1,1]
]
for y, line in enumerate(squad):
for x, item in enumerate(line):
if item == 1:
alien = Alien("alien1", 70 + 50 * x, 30 + 50 * y)
Go to the next part
Subscribe to the newsletter for updates
Tkinter templatesTwitter: @pythonprogrammi - python_pygame
Claude's Games
1. Memory gameVideos
Speech recognition gamePygame's Platform Game
