|
| 1 | +import math |
| 2 | +import random |
| 3 | + |
| 4 | +import pygame |
| 5 | +from pygame import mixer |
| 6 | + |
| 7 | +# Intialize the pygame |
| 8 | +pygame.init() |
| 9 | + |
| 10 | +# create the screen |
| 11 | +screen = pygame.display.set_mode((800, 600)) |
| 12 | + |
| 13 | +# Background |
| 14 | +background = pygame.image.load('background.png') |
| 15 | + |
| 16 | +# Sound |
| 17 | +mixer.music.load("background.wav") |
| 18 | +mixer.music.play(-1) |
| 19 | + |
| 20 | +# Caption and Icon |
| 21 | +pygame.display.set_caption("Space Invader") |
| 22 | +icon = pygame.image.load('ufo.png') |
| 23 | +pygame.display.set_icon(icon) |
| 24 | + |
| 25 | +# Player |
| 26 | +playerImg = pygame.image.load('player.png') |
| 27 | +playerX = 370 |
| 28 | +playerY = 480 |
| 29 | +playerX_change = 0 |
| 30 | + |
| 31 | +# Enemy |
| 32 | +enemyImg = [] |
| 33 | +enemyX = [] |
| 34 | +enemyY = [] |
| 35 | +enemyX_change = [] |
| 36 | +enemyY_change = [] |
| 37 | +num_of_enemies = 6 |
| 38 | + |
| 39 | +for i in range(num_of_enemies): |
| 40 | + enemyImg.append(pygame.image.load('enemy.png')) |
| 41 | + enemyX.append(random.randint(0, 736)) |
| 42 | + enemyY.append(random.randint(50, 150)) |
| 43 | + enemyX_change.append(4) |
| 44 | + enemyY_change.append(40) |
| 45 | + |
| 46 | +# Bullet |
| 47 | + |
| 48 | +# Ready - You can't see the bullet on the screen |
| 49 | +# Fire - The bullet is currently moving |
| 50 | + |
| 51 | +bulletImg = pygame.image.load('bullet.png') |
| 52 | +bulletX = 0 |
| 53 | +bulletY = 480 |
| 54 | +bulletX_change = 0 |
| 55 | +bulletY_change = 10 |
| 56 | +bullet_state = "ready" |
| 57 | + |
| 58 | +# Score |
| 59 | + |
| 60 | +score_value = 0 |
| 61 | +font = pygame.font.Font('freesansbold.ttf', 32) |
| 62 | + |
| 63 | +textX = 10 |
| 64 | +testY = 10 |
| 65 | + |
| 66 | +# Game Over |
| 67 | +over_font = pygame.font.Font('freesansbold.ttf', 64) |
| 68 | + |
| 69 | + |
| 70 | +def show_score(x, y): |
| 71 | + score = font.render("Score : " + str(score_value), True, (255, 255, 255)) |
| 72 | + screen.blit(score, (x, y)) |
| 73 | + |
| 74 | + |
| 75 | +def game_over_text(): |
| 76 | + over_text = over_font.render("GAME OVER", True, (255, 255, 255)) |
| 77 | + screen.blit(over_text, (200, 250)) |
| 78 | + |
| 79 | + |
| 80 | +def player(x, y): |
| 81 | + screen.blit(playerImg, (x, y)) |
| 82 | + |
| 83 | + |
| 84 | +def enemy(x, y, i): |
| 85 | + screen.blit(enemyImg[i], (x, y)) |
| 86 | + |
| 87 | + |
| 88 | +def fire_bullet(x, y): |
| 89 | + global bullet_state |
| 90 | + bullet_state = "fire" |
| 91 | + screen.blit(bulletImg, (x + 16, y + 10)) |
| 92 | + |
| 93 | + |
| 94 | +def isCollision(enemyX, enemyY, bulletX, bulletY): |
| 95 | + distance = math.sqrt(math.pow(enemyX - bulletX, 2) + (math.pow(enemyY - bulletY, 2))) |
| 96 | + if distance < 27: |
| 97 | + return True |
| 98 | + else: |
| 99 | + return False |
| 100 | + |
| 101 | + |
| 102 | +# Game Loop |
| 103 | +running = True |
| 104 | +while running: |
| 105 | + |
| 106 | + # RGB = Red, Green, Blue |
| 107 | + screen.fill((0, 0, 0)) |
| 108 | + # Background Image |
| 109 | + screen.blit(background, (0, 0)) |
| 110 | + for event in pygame.event.get(): |
| 111 | + if event.type == pygame.QUIT: |
| 112 | + running = False |
| 113 | + |
| 114 | + # if keystroke is pressed check whether its right or left |
| 115 | + if event.type == pygame.KEYDOWN: |
| 116 | + if event.key == pygame.K_LEFT: |
| 117 | + playerX_change = -5 |
| 118 | + if event.key == pygame.K_RIGHT: |
| 119 | + playerX_change = 5 |
| 120 | + if event.key == pygame.K_SPACE: |
| 121 | + if bullet_state is "ready": |
| 122 | + bulletSound = mixer.Sound("laser.wav") |
| 123 | + bulletSound.play() |
| 124 | + # Get the current x cordinate of the spaceship |
| 125 | + bulletX = playerX |
| 126 | + fire_bullet(bulletX, bulletY) |
| 127 | + |
| 128 | + if event.type == pygame.KEYUP: |
| 129 | + if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT: |
| 130 | + playerX_change = 0 |
| 131 | + |
| 132 | + # 5 = 5 + -0.1 -> 5 = 5 - 0.1 |
| 133 | + # 5 = 5 + 0.1 |
| 134 | + |
| 135 | + playerX += playerX_change |
| 136 | + if playerX <= 0: |
| 137 | + playerX = 0 |
| 138 | + elif playerX >= 736: |
| 139 | + playerX = 736 |
| 140 | + |
| 141 | + # Enemy Movement |
| 142 | + for i in range(num_of_enemies): |
| 143 | + |
| 144 | + # Game Over |
| 145 | + if enemyY[i] > 440: |
| 146 | + for j in range(num_of_enemies): |
| 147 | + enemyY[j] = 2000 |
| 148 | + game_over_text() |
| 149 | + break |
| 150 | + |
| 151 | + enemyX[i] += enemyX_change[i] |
| 152 | + if enemyX[i] <= 0: |
| 153 | + enemyX_change[i] = 4 |
| 154 | + enemyY[i] += enemyY_change[i] |
| 155 | + elif enemyX[i] >= 736: |
| 156 | + enemyX_change[i] = -4 |
| 157 | + enemyY[i] += enemyY_change[i] |
| 158 | + |
| 159 | + # Collision |
| 160 | + collision = isCollision(enemyX[i], enemyY[i], bulletX, bulletY) |
| 161 | + if collision: |
| 162 | + explosionSound = mixer.Sound("explosion.wav") |
| 163 | + explosionSound.play() |
| 164 | + bulletY = 480 |
| 165 | + bullet_state = "ready" |
| 166 | + score_value += 1 |
| 167 | + enemyX[i] = random.randint(0, 736) |
| 168 | + enemyY[i] = random.randint(50, 150) |
| 169 | + |
| 170 | + enemy(enemyX[i], enemyY[i], i) |
| 171 | + |
| 172 | + # Bullet Movement |
| 173 | + if bulletY <= 0: |
| 174 | + bulletY = 480 |
| 175 | + bullet_state = "ready" |
| 176 | + |
| 177 | + if bullet_state is "fire": |
| 178 | + fire_bullet(bulletX, bulletY) |
| 179 | + bulletY -= bulletY_change |
| 180 | + |
| 181 | + player(playerX, playerY) |
| 182 | + show_score(textX, testY) |
| 183 | + pygame.display.update() |
0 commit comments