|
| 1 | +import pygame |
| 2 | +import os |
| 3 | +pygame.font.init() |
| 4 | +pygame.mixer.init() |
| 5 | + |
| 6 | +w, h = 900, 500 |
| 7 | +win = pygame.display.set_mode((w, h)) |
| 8 | +pygame.display.set_caption("SPACESHIP BATTLE") |
| 9 | + |
| 10 | +WHITE = (255, 255, 255) |
| 11 | +BLACK = (0, 0, 0) |
| 12 | +RED = (255, 0, 0) |
| 13 | +YELLOW = (255, 255, 0) |
| 14 | + |
| 15 | +B = pygame.Rect(w//2 - 5, 0, 10, h) |
| 16 | + |
| 17 | +hit_sound = pygame.mixer.Sound('Assets/Grenade+1.mp3') |
| 18 | +fire_sound = pygame.mixer.Sound('Assets/Gun+Silencer.mp3') |
| 19 | + |
| 20 | +health = pygame.font.SysFont('comicsans', 40) |
| 21 | +winner = pygame.font.SysFont('comicsans', 100) |
| 22 | + |
| 23 | +fps = 60 |
| 24 | +vel = 5 |
| 25 | +bul_vel = 7 |
| 26 | +bul = 3 |
| 27 | +s_w, s_h = 55, 40 |
| 28 | + |
| 29 | +YELLOW_HIT = pygame.USEREVENT + 1 |
| 30 | +RED_HIT = pygame.USEREVENT + 2 |
| 31 | + |
| 32 | +YELLOW_SPACESHIP_IMAGE = pygame.image.load( |
| 33 | + os.path.join('Assets', 'spaceship_yellow.png')) |
| 34 | +YELLOW_SPACESHIP = pygame.transform.rotate(pygame.transform.scale( |
| 35 | + YELLOW_SPACESHIP_IMAGE, (s_w, s_h)), 90) |
| 36 | + |
| 37 | +RED_SPACESHIP_IMAGE = pygame.image.load( |
| 38 | + os.path.join('Assets', 'spaceship_red.png')) |
| 39 | +RED_SPACESHIP = pygame.transform.rotate(pygame.transform.scale( |
| 40 | + RED_SPACESHIP_IMAGE, (s_w, s_h)), 270) |
| 41 | + |
| 42 | +SPACE = pygame.transform.scale(pygame.image.load( |
| 43 | + os.path.join('Assets', 'space.jpg')), (w, h)) |
| 44 | + |
| 45 | + |
| 46 | +def draw_window(red, yellow, red_bullets, yellow_bullets, red_health, yellow_health): |
| 47 | + win.blit(SPACE, (0, 0)) |
| 48 | + pygame.draw.rect(win, BLACK, B) |
| 49 | + |
| 50 | + red_health_text = health.render( |
| 51 | + "Health: " + str(red_health), 1, WHITE) |
| 52 | + yellow_health_text = health.render( |
| 53 | + "Health: " + str(yellow_health), 1, WHITE) |
| 54 | + win.blit(red_health_text, (w - red_health_text.get_width() - 10, 10)) |
| 55 | + win.blit(yellow_health_text, (10, 10)) |
| 56 | + |
| 57 | + win.blit(YELLOW_SPACESHIP, (yellow.x, yellow.y)) |
| 58 | + win.blit(RED_SPACESHIP, (red.x, red.y)) |
| 59 | + |
| 60 | + for bullet in red_bullets: |
| 61 | + pygame.draw.rect(win, RED, bullet) |
| 62 | + |
| 63 | + for bullet in yellow_bullets: |
| 64 | + pygame.draw.rect(win, YELLOW, bullet) |
| 65 | + |
| 66 | + pygame.display.update() |
| 67 | + |
| 68 | + |
| 69 | +def yellow_movement(keys_pressed, yellow): |
| 70 | + if keys_pressed[pygame.K_a] and yellow.x - vel > 0: # LEFT |
| 71 | + yellow.x -= vel |
| 72 | + if keys_pressed[pygame.K_d] and yellow.x + vel + yellow.width < B.x: # RIGHT |
| 73 | + yellow.x += vel |
| 74 | + if keys_pressed[pygame.K_w] and yellow.y - vel > 0: # UP |
| 75 | + yellow.y -= vel |
| 76 | + if keys_pressed[pygame.K_s] and yellow.y + vel + yellow.height < h - 15: # DOWN |
| 77 | + yellow.y += vel |
| 78 | + |
| 79 | + |
| 80 | +def red_movement(keys_pressed, red): |
| 81 | + if keys_pressed[pygame.K_LEFT] and red.x - vel > B.x + B.width: # LEFT |
| 82 | + red.x -= vel |
| 83 | + if keys_pressed[pygame.K_RIGHT] and red.x + vel + red.width < w: # RIGHT |
| 84 | + red.x += vel |
| 85 | + if keys_pressed[pygame.K_UP] and red.y - vel > 0: # UP |
| 86 | + red.y -= vel |
| 87 | + if keys_pressed[pygame.K_DOWN] and red.y + vel + red.height < h - 15: # DOWN |
| 88 | + red.y += vel |
| 89 | + |
| 90 | + |
| 91 | +def handle_bullets(yellow_bullets, red_bullets, yellow, red): |
| 92 | + for bullet in yellow_bullets: |
| 93 | + bullet.x += bul_vel |
| 94 | + if red.colliderect(bullet): |
| 95 | + pygame.event.post(pygame.event.Event(RED_HIT)) |
| 96 | + yellow_bullets.remove(bullet) |
| 97 | + elif bullet.x > w: |
| 98 | + yellow_bullets.remove(bullet) |
| 99 | + |
| 100 | + for bullet in red_bullets: |
| 101 | + bullet.x -= bul_vel |
| 102 | + if yellow.colliderect(bullet): |
| 103 | + pygame.event.post(pygame.event.Event(YELLOW_HIT)) |
| 104 | + red_bullets.remove(bullet) |
| 105 | + elif bullet.x < 0: |
| 106 | + red_bullets.remove(bullet) |
| 107 | + |
| 108 | + |
| 109 | +def draw_winner(text): |
| 110 | + draw_text = winner.render(text, 1, WHITE) |
| 111 | + win.blit(draw_text, (w/2 - draw_text.get_width() / |
| 112 | + 2, h/2 - draw_text.get_height()/2)) |
| 113 | + pygame.display.update() |
| 114 | + pygame.time.delay(5000) |
| 115 | + |
| 116 | + |
| 117 | +def main(): |
| 118 | + red = pygame.Rect(700, 300, s_w, s_h) |
| 119 | + yellow = pygame.Rect(100, 300, s_w, s_h) |
| 120 | + |
| 121 | + red_bullets = [] |
| 122 | + yellow_bullets = [] |
| 123 | + |
| 124 | + red_health = 10 |
| 125 | + yellow_health = 10 |
| 126 | + |
| 127 | + clock = pygame.time.Clock() |
| 128 | + run = True |
| 129 | + while run: |
| 130 | + clock.tick(fps) |
| 131 | + for event in pygame.event.get(): |
| 132 | + if event.type == pygame.QUIT: |
| 133 | + run = False |
| 134 | + pygame.quit() |
| 135 | + |
| 136 | + if event.type == pygame.KEYDOWN: |
| 137 | + if event.key == pygame.K_LCTRL and len(yellow_bullets) < bul: |
| 138 | + bullet = pygame.Rect( |
| 139 | + yellow.x + yellow.width, yellow.y + yellow.height//2 - 2, 10, 5) |
| 140 | + yellow_bullets.append(bullet) |
| 141 | + fire_sound.play() |
| 142 | + |
| 143 | + if event.key == pygame.K_RCTRL and len(red_bullets) < bul: |
| 144 | + bullet = pygame.Rect( |
| 145 | + red.x, red.y + red.height//2 - 2, 10, 5) |
| 146 | + red_bullets.append(bullet) |
| 147 | + fire_sound.play() |
| 148 | + |
| 149 | + if event.type == RED_HIT: |
| 150 | + red_health -= 1 |
| 151 | + hit_sound.play() |
| 152 | + |
| 153 | + if event.type == YELLOW_HIT: |
| 154 | + yellow_health -= 1 |
| 155 | + hit_sound.play() |
| 156 | + |
| 157 | + winner_text = "" |
| 158 | + if red_health <= 0: |
| 159 | + winner_text = "Yellow Wins!" |
| 160 | + |
| 161 | + if yellow_health <= 0: |
| 162 | + winner_text = "Red Wins!" |
| 163 | + |
| 164 | + if winner_text != "": |
| 165 | + draw_winner(winner_text) |
| 166 | + break |
| 167 | + |
| 168 | + keys_pressed = pygame.key.get_pressed() |
| 169 | + yellow_movement(keys_pressed, yellow) |
| 170 | + red_movement(keys_pressed, red) |
| 171 | + |
| 172 | + handle_bullets(yellow_bullets, red_bullets, yellow, red) |
| 173 | + |
| 174 | + draw_window(red, yellow, red_bullets, yellow_bullets, |
| 175 | + red_health, yellow_health) |
| 176 | + |
| 177 | + main() |
| 178 | + |
| 179 | + |
| 180 | +if __name__ == "__main__": |
| 181 | + main() |
0 commit comments