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

+1
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.
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)
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
Loading
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import pygame
2+
from settings import BULLET_SPEED, HEIGHT
3+
4+
class Bullet(pygame.sprite.Sprite):
5+
def __init__(self, pos, size, side):
6+
super().__init__()
7+
self.x = pos[0]
8+
self.y = pos[1]
9+
10+
# bullet info
11+
img_path = f'assets/bullet/{side}-bullet.png'
12+
self.image = pygame.image.load(img_path)
13+
self.image = pygame.transform.scale(self.image, (size, size))
14+
self.rect = self.image.get_rect(topleft = pos)
15+
self.mask = pygame.mask.from_surface(self.image)
16+
17+
# different bullet movement direction for both player and enemy (alien)
18+
if side == "enemy":
19+
self.move_speed = BULLET_SPEED
20+
elif side == "player":
21+
self.move_speed = (- BULLET_SPEED)
22+
23+
24+
def _move_bullet(self):
25+
self.rect.y += self.move_speed
26+
27+
28+
def update(self):
29+
self._move_bullet()
30+
self.rect = self.image.get_rect(topleft=(self.rect.x, self.rect.y))
31+
32+
# delete the bullet if it get through out of the screen
33+
if self.rect.bottom <= 0 or self.rect.top >= HEIGHT:
34+
self.kill()
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import pygame
2+
from settings import WIDTH, HEIGHT, SPACE, FONT_SIZE, EVENT_FONT_SIZE
3+
4+
pygame.font.init()
5+
6+
class Display:
7+
def __init__(self, screen):
8+
self.screen = screen
9+
self.score_font = pygame.font.SysFont("monospace", FONT_SIZE)
10+
self.level_font = pygame.font.SysFont("impact", FONT_SIZE)
11+
self.event_font = pygame.font.SysFont("impact", EVENT_FONT_SIZE)
12+
self.text_color = pygame.Color("blue")
13+
self.event_color = pygame.Color("red")
14+
15+
16+
def show_life(self, life):
17+
life_size = 30
18+
img_path = "assets/life/life.png"
19+
life_image = pygame.image.load(img_path)
20+
life_image = pygame.transform.scale(life_image, (life_size, life_size))
21+
life_x = SPACE // 2
22+
23+
if life != 0:
24+
for life in range(life):
25+
self.screen.blit(life_image, (life_x, HEIGHT + (SPACE // 2)))
26+
life_x += life_size
27+
28+
29+
def show_score(self, score):
30+
score_x = WIDTH // 3
31+
score = self.score_font.render(f'score: {score}', True, self.text_color)
32+
self.screen.blit(score, (score_x, (HEIGHT + (SPACE // 2))))
33+
34+
35+
def show_level(self, level):
36+
level_x = WIDTH // 3
37+
level = self.level_font.render(f'Level {level}', True, self.text_color)
38+
self.screen.blit(level, (level_x * 2, (HEIGHT + (SPACE // 2))))
39+
40+
41+
def game_over_message(self):
42+
message = self.event_font.render('GAME OVER!!', True, self.event_color)
43+
self.screen.blit(message, ((WIDTH // 3) - (EVENT_FONT_SIZE // 2), (HEIGHT // 2) - (EVENT_FONT_SIZE // 2)))
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import pygame, sys
2+
from settings import WIDTH, HEIGHT, NAV_THICKNESS
3+
from world import World
4+
5+
pygame.init()
6+
7+
screen = pygame.display.set_mode((WIDTH, HEIGHT + NAV_THICKNESS))
8+
pygame.display.set_caption("Space Invader")
9+
10+
class Main:
11+
def __init__(self, screen):
12+
self.screen = screen
13+
self.FPS = pygame.time.Clock()
14+
15+
def main(self):
16+
world = World(self.screen)
17+
while True:
18+
self.screen.fill("black")
19+
20+
for event in pygame.event.get():
21+
if event.type == pygame.QUIT:
22+
pygame.quit()
23+
sys.exit()
24+
if event.type == pygame.KEYDOWN:
25+
if event.key == pygame.K_SPACE:
26+
world.player_move(attack = True)
27+
28+
world.player_move()
29+
world.update()
30+
pygame.display.update()
31+
self.FPS.tick(30)
32+
33+
34+
if __name__ == "__main__":
35+
play = Main(screen)
36+
play.main()
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
pygame
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
WIDTH, HEIGHT = 720, 450
2+
3+
SPACE = 30
4+
FONT_SIZE = 20
5+
EVENT_FONT_SIZE = 60
6+
NAV_THICKNESS = 50
7+
CHARACTER_SIZE = 30
8+
PLAYER_SPEED = 10
9+
ENEMY_SPEED = 1
10+
BULLET_SPEED = 15 # for both sides
11+
BULLET_SIZE = 10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import pygame
2+
3+
from settings import PLAYER_SPEED, BULLET_SIZE
4+
from bullet import Bullet
5+
6+
class Ship(pygame.sprite.Sprite):
7+
def __init__(self, pos, size):
8+
super().__init__()
9+
self.x = pos[0]
10+
self.y = pos[1]
11+
12+
# ship info
13+
img_path = 'assets/ship/ship.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.ship_speed = PLAYER_SPEED
19+
20+
# ship status
21+
self.life = 3
22+
self.player_bullets = pygame.sprite.Group()
23+
24+
25+
def move_left(self):
26+
self.rect.x -= self.ship_speed
27+
28+
def move_up(self):
29+
self.rect.y -= self.ship_speed
30+
31+
def move_right(self):
32+
self.rect.x += self.ship_speed
33+
34+
def move_bottom(self):
35+
self.rect.y += self.ship_speed
36+
37+
def _shoot(self):
38+
specific_pos = (self.rect.centerx - (BULLET_SIZE // 2), self.rect.y)
39+
self.player_bullets.add(Bullet(specific_pos, BULLET_SIZE, "player"))
40+
41+
def update(self):
42+
self.rect = self.image.get_rect(topleft=(self.rect.x, self.rect.y))

0 commit comments

Comments
 (0)