import pygameimport randomimport timepygame.init()screen_width = 1000screen_height = 1000try: pygame.mixer.music.load("music.wav") pygame.mixer.music.play(-1)except Exception: passtry: explosion = pygame.mixer.Sound("boom.wav")except Exception: explosion = Nonetry: passsound = pygame.mixer.Sound("pass.wav")except Exception: passsound = Nonescreen = pygame.display.set_mode((screen_width, screen_height))pygame.display.set_caption("Bomb Tag")try: backgroundimg = pygame.image.load("backgroundimg.png")except Exception: backgroundimg = pygame.Surface((screen_width, screen_height)) backgroundimg.fill((50, 50, 50))menu = pygame.image.load("menu.png")startimage = pygame.image.load("play.png")quitimage = pygame.image.load("quit.png")try: bombimage = pygame.image.load("bomb.png")except Exception: bombimage = pygame.Surface((40, 40)) bombimage.fill((255, 165, 0))font = pygame.font.Font(None, 50)clock = pygame.time.Clock()def load_anim(prefix, count): frames = [] for i in range(1, count + 1): try: img = pygame.image.load(f"{prefix}{i}.png") frames.append(pygame.transform.scale(img, (50, 70))) except Exception: surf = pygame.Surface((50, 70)) surf.fill((0, 0, 255)) frames.append(surf) return framesexplosion_frames = load_anim("Explosion", 12)down_images = load_anim("down", 6)up_images = load_anim("up", 6)right_images = load_anim("right", 6)upright_images = load_anim("upright", 6)downright_images = load_anim("downright", 6)player = pygame.Rect(300, 250, 50, 70)player2 = pygame.Rect(700, 250, 50, 70)player_image = down_images[0]player2_image = down_images[0]startbutton = pygame.Rect(350, 700, 300, 70)quitbutton = pygame.Rect(382, 820, 240, 70)playerspeed = 5player2speed = 5Startmenu = Truerunning = Truedef reset_game(): global time_val, timer global playerlives, player2lives global playerbomb, player2bomb global bombclock, bombx, bomby global is_exploding, exp_anim_index, exp_anim_counter global anim_index, anim_counter global player_image, player2_image time_val = 10 timer = 60 playerlives = 3 player2lives = 3 playerbomb = False player2bomb = True bombclock = 0 bombx = 600 bomby = 200 player.x, player.y = 300, 250 player2.x, player2.y = 700, 250 is_exploding = False exp_anim_index = 0 exp_anim_counter = 0 anim_index = 0 anim_counter = 0 player_image = down_images[0] player2_image = down_images[0]reset_game()while running: if Startmenu: for event in pygame.event.get(): if event.type == pygame.QUIT: running = False if event.type == pygame.MOUSEBUTTONDOWN: if event.button == 1: if quitbutton.collidepoint(event.pos): running = False if startbutton.collidepoint(event.pos): reset_game() Startmenu = False screen.blit(menu, (0, 0)) pygame.draw.rect(screen, "Black", startbutton) pygame.draw.rect(screen, "Black", quitbutton) screen.blit(startimage, startbutton) screen.blit(quitimage, quitbutton) pygame.display.flip() clock.tick(60) else: for event in pygame.event.get(): if event.type == pygame.QUIT: running = False keys = pygame.key.get_pressed() dx = 0 dy = 0 if keys[pygame.K_w]: dy -= 1 if keys[pygame.K_s]: dy += 1 if keys[pygame.K_a]: dx -= 1 if keys[pygame.K_d]: dx += 1 p1_sprinting = keys[pygame.K_LSHIFT] p1_crouched = keys[pygame.K_LCTRL] if p1_sprinting: playerspeed = 10 elif p1_crouched: playerspeed = 1 else: playerspeed = 5 if dx != 0 or dy != 0: if dx > 0 and dy < 0: anim_images = upright_images elif dx < 0 and dy < 0: anim_images = upright_images elif dx > 0 and dy > 0: anim_images = downright_images elif dx < 0 and dy > 0: anim_images = downright_images elif dx > 0: anim_images = right_images elif dx < 0: anim_images = right_images elif dy < 0: anim_images = up_images else: anim_images = down_images player.x += dx * playerspeed player.y += dy * playerspeed anim_counter += 1 if anim_counter >= 5: anim_index = (anim_index + 1) % len(anim_images) player_image = anim_images[anim_index] if dx < 0 and dy == 0: player_image = pygame.transform.flip( player_image, True, False ) elif dx < 0: player_image = pygame.transform.flip( player_image, True, False ) anim_counter = 0 dx2 = 0 dy2 = 0 if keys[pygame.K_UP]: dy2 -= 1 if keys[pygame.K_DOWN]: dy2 += 1 if keys[pygame.K_LEFT]: dx2 -= 1 if keys[pygame.K_RIGHT]: dx2 += 1 p2_sprinting = keys[pygame.K_RSHIFT] p2_crouched = keys[pygame.K_RCTRL] if p2_sprinting: player2speed = 10 elif p2_crouched: player2speed = 1 else: player2speed = 5 if dx2 != 0 or dy2 != 0: if dx2 > 0 and dy2 < 0: anim_images = upright_images elif dx2 < 0 and dy2 < 0: anim_images = upright_images elif dx2 > 0 and dy2 > 0: anim_images = downright_images elif dx2 < 0 and dy2 > 0: anim_images = downright_images elif dx2 > 0: anim_images = right_images elif dx2 < 0: anim_images = right_images elif dy2 < 0: anim_images = up_images else: anim_images = down_images player2.x += dx2 * player2speed player2.y += dy2 * player2speed anim_counter += 1 if anim_counter >= 5: anim_index = (anim_index + 1) % len(anim_images) player2_image = anim_images[anim_index] if dx2 < 0: player2_image = pygame.transform.flip( player2_image, True, False ) anim_counter = 0 player.clamp_ip(screen.get_rect()) player2.clamp_ip(screen.get_rect()) timer -= 1 if timer <= 0: time_val -= 1 timer = 60 if time_val <= 0: if explosion: explosion.play() if playerbomb: playerlives -= 1 explosion_x, explosion_y = player.x, player.y else: player2lives -= 1 explosion_x, explosion_y = player2.x, player2.y is_exploding = True exp_anim_index = 0 exp_anim_counter = 0 time_val = 10 if playerlives <= 0 or player2lives <= 0: Startmenu = True bombclock += 1 if player2.colliderect(player) and not playerbomb and bombclock >= 60: if passsound: passsound.play() playerbomb = True player2bomb = False bombclock = 0 elif player2.colliderect(player) and playerbomb and bombclock >= 30: if passsound: passsound.play() playerbomb = False player2bomb = True bombclock = 0 if playerbomb: bombx = player.x bomby = player.y elif player2bomb: bombx = player2.x bomby = player2.y screen.blit(backgroundimg, (0, 0)) screen.blit(player_image, player) screen.blit(player2_image, player2) if not is_exploding: screen.blit(bombimage, (bombx, bomby)) if is_exploding: if exp_anim_index < len(explosion_frames): screen.blit( explosion_frames[exp_anim_index], (explosion_x, explosion_y) ) exp_anim_counter += 1 if exp_anim_counter >= 5: exp_anim_index += 1 exp_anim_counter = 0 else: is_exploding = False score_text = font.render( "Time: " + str(time_val), True, "White" ) playerhealth_text = font.render( "P1 Lives: " + str(playerlives), True, "Red" ) player2health_text = font.render( "P2 Lives: " + str(player2lives), True, "Red" ) screen.blit(score_text, (400, 60)) screen.blit(playerhealth_text, (10, 160)) screen.blit(player2health_text, (10, 200)) pygame.display.flip() clock.tick(60)pygame.quit()Bomb Tag
Two-player pass-the-bomb game
My role: I wrote the whole game in Python with Pygame: player movement and animation, the bomb timer and passing, lives and the explosion effects.
A two-player game built with Python and Pygame. One player is holding a bomb on a 10-second fuse, and you pass it on by touching the other player. When it goes off, the holder loses a life, and the first player to run out of lives loses. Player 1 uses W, A, S, D and player 2 uses the arrow keys, and both can sprint or crouch.
- Tools
- Python, Pygame
- Players
- 2, same keyboard