Skip to content

Commit 4a5a040

Browse files
committed
add space invaders game tutorial
1 parent 666b655 commit 4a5a040

File tree

18 files changed

+390
-0
lines changed

18 files changed

+390
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -313,6 +313,7 @@ This is a repository of all the tutorials of [The Python Code](https://www.thepy
313313
- [How to Create a Platformer Game in Python](https://www.thepythoncode.com/article/platformer-game-with-pygame-in-python). ([code](gui-programming/platformer-game))
314314
- [How to Make a Flappy Bird Game in Python](https://thepythoncode.com/article/make-a-flappy-bird-game-python). ([code](gui-programming/flappy-bird-game))
315315
- [How to Create a Pong Game in Python](https://thepythoncode.com/article/build-a-pong-game-in-python). ([code](gui-programming/pong-game))
316+
- [How to Create a Space Invaders Game in Python](https://thepythoncode.com/article/make-a-space-invader-game-in-python). ([code](gui-programming/space-invaders-game))
316317

317318

318319
For any feedback, please consider pulling requests.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# [How to Create a Space Invaders Game in Python](https://thepythoncode.com/article/make-a-space-invader-game-in-python)
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import pygame
2+
3+
from settings import BULLET_SIZE
4+
from bullet import Bullet
5+
6+
class Alien(pygame.sprite.Sprite):
7+
def __init__(self, pos, size, row_num):
8+
super().__init__()
9+
self.x = pos[0]
10+
self.y = pos[1]
11+
12+
# alien info
13+
img_path = f'assets/aliens/{row_num}.png'
14+
self.image = pygame.image.load(img_path)
15+
self.image = pygame.transform.scale(self.image, (size, size))
16+
self.rect = self.image.get_rect(topleft = pos)
17+
self.mask = pygame.mask.from_surface(self.image)
18+
self.move_speed = 5
19+
self.to_direction = "right"
20+
21+
# alien status
22+
self.bullets = pygame.sprite.GroupSingle()
23+
24+
25+
def move_left(self):
26+
self.rect.x -= self.move_speed
27+
28+
def move_right(self):
29+
self.rect.x += self.move_speed
30+
31+
def move_bottom(self):
32+
self.rect.y += self.move_speed
33+
34+
def _shoot(self):
35+
specific_pos = (self.rect.centerx - (BULLET_SIZE // 2), self.rect.centery)
36+
self.bullets.add(Bullet(specific_pos, BULLET_SIZE, "enemy"))
37+
38+
def update(self):
39+
self.rect = self.image.get_rect(topleft=(self.rect.x, self.rect.y))
Loading
Loading
Loading
Loading
Loading
Loading
Loading

0 commit comments

Comments
 (0)