Skip to content

Instantly share code, notes, and snippets.

@rgaidot
Last active November 25, 2023 01:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rgaidot/daa4c405a35875ddd6fa99ed08162dd6 to your computer and use it in GitHub Desktop.
Save rgaidot/daa4c405a35875ddd6fa99ed08162dd6 to your computer and use it in GitHub Desktop.
Bomberman game written in Python using the Pygame library
import pygame
# Set up the screen dimensions
screen_width = 640
screen_height = 480
screen = pygame.display.set_mode((screen_width, screen_height))
# Set up the title of the window
pygame.display.set_caption('Bomberman')
# Define the colors
WHITE = (255, 255, 255)
GREEN = (0, 255, 0)
RED = (255, 0, 0)
# Define the bombs
BOMB_WIDTH = 40
BOMB_HEIGHT = 40
BOMB_COLOR = RED
BOMB_X = 100
BOMB_Y = 100
# Define the player
PLAYER_WIDTH = 20
PLAYER_HEIGHT = 20
PLAYER_COLOR = WHITE
PLAYER_X = 150
PLAYER_Y = 150
# Define the speed of the player and bombs
PLAYER_SPEED = 2
BOMB_SPEED = 2
# Game loop
while True:
# Handle events
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
# Move the player
keys = pygame.key.get_pressed()
if keys[pygame.K_LEFT]:
PLAYER_X -= PLAYER_SPEED
if keys[pygame.K_RIGHT]:
PLAYER_X += PLAYER_SPEED
# Draw the player and bombs
screen.fill(GREEN)
pygame.draw.rect(screen, RED, (BOMB_X, BOMB_Y, BOMB_WIDTH, BOMB_HEIGHT))
pygame.draw.rect(screen, WHITE, (PLAYER_X, PLAYER_Y, PLAYER_WIDTH, PLAYER_HEIGHT))
# Update the position of the bombs
BOMB_X += BOMB_SPEED
# Check if the bombs have hit the walls or the player
if BOMB_X >= screen_width - BOMB_WIDTH or BOMB_X <= 0:
BOMB_X = random.randrange(screen_width - BOMB_WIDTH, 0)
# Check if the player has hit the bombs
if PLAYER_X >= BOMB_X - PLAYER_WIDTH and PLAYER_X <= BOMB_X + PLAYER_WIDTH:
pygame.quit()
sys.exit()
# Update the screen
pygame.display.flip()
pygame.time.Clock().tick(60)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment