|
| 1 | +# ----------------------------------------------------------------------------- |
| 2 | +# |
| 3 | +# Space Invaders |
| 4 | +# Language - Python |
| 5 | +# Modules - pygame, sys, time |
| 6 | +# |
| 7 | +# Controls - Left and Right Keys to Move, Space to shoot |
| 8 | +# |
| 9 | +# By - Jatin Kumar Mandav |
| 10 | +# |
| 11 | +# Website - https://jatinmandav.wordpress.com |
| 12 | +# |
| 13 | +# YouTube Channel - https://www.youtube.com/channel/UCdpf6Lz3V357cIZomPwjuFQ |
| 14 | +# Twitter - @jatinmandav |
| 15 | +# |
| 16 | +# ----------------------------------------------------------------------------- |
| 17 | + |
| 18 | +import pygame |
| 19 | +import sys |
| 20 | +import time |
| 21 | + |
| 22 | +# -------------- Initialization ------------ |
| 23 | +pygame.init() |
| 24 | + |
| 25 | +width = 700 |
| 26 | +height = 500 |
| 27 | + |
| 28 | +display = pygame.display.set_mode((width, height)) |
| 29 | +clock = pygame.time.Clock() |
| 30 | +pygame.display.set_caption("Space Invaders") |
| 31 | + |
| 32 | +ship_width = 40 |
| 33 | +ship_height = 30 |
| 34 | + |
| 35 | +# -------------- Colours ----------------- |
| 36 | +background = (74, 35, 90) |
| 37 | +white = (244, 246, 247) |
| 38 | +yellow = (241, 196, 15) |
| 39 | +orange = (186, 74, 0) |
| 40 | +green = (35, 155, 86) |
| 41 | +white1 = (253, 254, 254) |
| 42 | +dark_gray = (23, 32, 42) |
| 43 | + |
| 44 | + |
| 45 | +# -------------- Space-Ship Class -------------- |
| 46 | +class SpaceShip: |
| 47 | + def __init__(self, x, y, w, h, colour): |
| 48 | + self.x = x |
| 49 | + self.y = y |
| 50 | + self.w = w |
| 51 | + self.h = h |
| 52 | + self.colour = colour |
| 53 | + |
| 54 | + def draw(self): |
| 55 | + pygame.draw.rect(display, yellow, (self.x + self.w/2 - 8, self.y - 10, 16, 10)) |
| 56 | + pygame.draw.rect(display, self.colour, (self.x, self.y, self.w, self.h)) |
| 57 | + pygame.draw.rect(display, dark_gray, (self.x + 5, self.y + 6, 10, self.h - 10)) |
| 58 | + pygame.draw.rect(display, dark_gray, (self.x + self.w - 15, self.y + 6, 10, self.h - 10)) |
| 59 | + |
| 60 | + |
| 61 | +# ----------------- Bullet Class ------------- |
| 62 | +class Bullet: |
| 63 | + def __init__(self, x, y): |
| 64 | + self.x = x |
| 65 | + self.y = y |
| 66 | + self.d = 10 |
| 67 | + self.speed = -5 |
| 68 | + |
| 69 | + def draw(self): |
| 70 | + pygame.draw.ellipse(display, orange, (self.x, self.y, self.d, self.d)) |
| 71 | + |
| 72 | + def move(self): |
| 73 | + self.y += self.speed |
| 74 | + |
| 75 | + def hit(self, x, y, d): |
| 76 | + if x < self.x < x + d: |
| 77 | + if y + d > self.y > y: |
| 78 | + return True |
| 79 | + |
| 80 | + |
| 81 | +# ------------------ Alien Class --------------- |
| 82 | +class Alien: |
| 83 | + def __init__(self, x, y, d): |
| 84 | + self.x = x |
| 85 | + self.y = y |
| 86 | + self.d = d |
| 87 | + self.x_dir = 1 |
| 88 | + self.speed = 3 |
| 89 | + |
| 90 | + def draw(self): |
| 91 | + pygame.draw.ellipse(display, green, (self.x, self.y, self.d, self.d)) |
| 92 | + pygame.draw.ellipse(display, dark_gray, (self.x + 10, self.y + self.d/3, 8, 8), 2) |
| 93 | + pygame.draw.ellipse(display, dark_gray, (self.x + self.d - 20, self.y + self.d/3, 8, 8), 2) |
| 94 | + pygame.draw.rect(display, dark_gray, (self.x, self.y+self.d-20, 50, 7)) |
| 95 | + |
| 96 | + def move(self): |
| 97 | + self.x += self.x_dir*self.speed |
| 98 | + |
| 99 | + def shift_down(self): |
| 100 | + self.y += self.d |
| 101 | + |
| 102 | + |
| 103 | +# ------------------- Saved ------------------ |
| 104 | +def saved(): |
| 105 | + font = pygame.font.SysFont("Wide Latin", 22) |
| 106 | + font_large = pygame.font.SysFont("Wide Latin", 43) |
| 107 | + text2 = font_large.render("Congratulations!", True, white1) |
| 108 | + text = font.render("You Prevented the Alien Invasion!", True, white1) |
| 109 | + display.blit(text2, (60, height/2)) |
| 110 | + display.blit(text, (45, height/2 + 100)) |
| 111 | + pygame.display.update() |
| 112 | + time.sleep(3) |
| 113 | + |
| 114 | + |
| 115 | +# -------------------- Death ---------------- |
| 116 | +def GameOver(): |
| 117 | + font = pygame.font.SysFont("Chiller", 50) |
| 118 | + font_large = pygame.font.SysFont("Chiller", 100) |
| 119 | + text2 = font_large.render("Game Over!", True, white1) |
| 120 | + text = font.render("You Could not Prevent the Alien Invasion!", True, white1) |
| 121 | + display.blit(text2, (180, height/2-50)) |
| 122 | + display.blit(text, (45, height/2 + 100)) |
| 123 | + |
| 124 | + |
| 125 | +# --------------------- The Game ------------------ |
| 126 | +def game(): |
| 127 | + invasion = False |
| 128 | + ship = SpaceShip(width/2-ship_width/2, height-ship_height - 10, ship_width, ship_height, white) |
| 129 | + |
| 130 | + bullets = [] |
| 131 | + num_bullet = 0 |
| 132 | + for i in range(num_bullet): |
| 133 | + i = Bullet(width/2 - 5, height - ship_height - 20) |
| 134 | + bullets.append(i) |
| 135 | + |
| 136 | + x_move = 0 |
| 137 | + |
| 138 | + aliens = [] |
| 139 | + num_aliens = 8 |
| 140 | + d = 50 |
| 141 | + for i in range(num_aliens): |
| 142 | + i = Alien((i+1)*d + i*20, d+20, d) |
| 143 | + aliens.append(i) |
| 144 | + |
| 145 | + while not invasion: |
| 146 | + for event in pygame.event.get(): |
| 147 | + if event.type == pygame.QUIT: |
| 148 | + pygame.quit() |
| 149 | + sys.exit() |
| 150 | + |
| 151 | + if event.type == pygame.KEYDOWN: |
| 152 | + if event.key == pygame.K_q: |
| 153 | + pygame.quit() |
| 154 | + sys.exit() |
| 155 | + |
| 156 | + if event.key == pygame.K_RIGHT: |
| 157 | + x_move = 5 |
| 158 | + |
| 159 | + if event.key == pygame.K_LEFT: |
| 160 | + x_move = -5 |
| 161 | + |
| 162 | + if event.key == pygame.K_SPACE: |
| 163 | + num_bullet += 1 |
| 164 | + i = Bullet(ship.x + ship_width/2 - 5, ship.y) |
| 165 | + bullets.append(i) |
| 166 | + |
| 167 | + if event.type == pygame.KEYUP: |
| 168 | + x_move = 0 |
| 169 | + |
| 170 | + display.fill(background) |
| 171 | + |
| 172 | + for i in range(num_bullet): |
| 173 | + bullets[i].draw() |
| 174 | + bullets[i].move() |
| 175 | + |
| 176 | + for alien in list(aliens): |
| 177 | + alien.draw() |
| 178 | + alien.move() |
| 179 | + for item in list(bullets): |
| 180 | + if item.hit(alien.x, alien.y, alien.d): |
| 181 | + bullets.remove(item) |
| 182 | + num_bullet -= 1 |
| 183 | + aliens.remove(alien) |
| 184 | + num_aliens -= 1 |
| 185 | + |
| 186 | + if num_aliens == 0: |
| 187 | + saved() |
| 188 | + invasion = True |
| 189 | + |
| 190 | + for i in range(num_aliens): |
| 191 | + if aliens[i].x + d >= width: |
| 192 | + for j in range(num_aliens): |
| 193 | + aliens[j].x_dir = -1 |
| 194 | + aliens[j].shift_down() |
| 195 | + |
| 196 | + if aliens[i].x <= 0: |
| 197 | + for j in range(num_aliens): |
| 198 | + aliens[j].x_dir = 1 |
| 199 | + aliens[j].shift_down() |
| 200 | + |
| 201 | + try: |
| 202 | + if aliens[0].y + d > height: |
| 203 | + GameOver() |
| 204 | + pygame.display.update() |
| 205 | + time.sleep(3) |
| 206 | + invasion = True |
| 207 | + except Exception as e: |
| 208 | + pass |
| 209 | + |
| 210 | + ship.x += x_move |
| 211 | + |
| 212 | + if ship.x < 0: |
| 213 | + ship.x -= x_move |
| 214 | + if ship.x + ship_width > width: |
| 215 | + ship.x -= x_move |
| 216 | + |
| 217 | + ship.draw() |
| 218 | + |
| 219 | + pygame.display.update() |
| 220 | + clock.tick(60) |
| 221 | + |
| 222 | +# ----------------- Calling the Game Function --------------------- |
| 223 | +game() |
0 commit comments