Retro Bug Arcade Game in Pygame
Game Description
In this retro-themed bug arcade game, you control a bug that must navigate through a grid of flowers while avoiding being squashed by a giant shoe. The game features simple graphics and sound effects reminiscent of classic 80's arcade games.
Code
import pygame
import random
# Initialize Pygame
pygame.init()
# Define some colors
WHITE = (255, 255, 255)
RED = (255, 0, 0)
GREEN = (0, 255, 0)
BLACK = (0, 0, 0)
# Set the width and height of each grid cell
CELL_SIZE = 50
# Set the dimensions of the grid
GRID_WIDTH = 8
GRID_HEIGHT = 6
# Set the margin between the grid and the border of the screen
MARGIN = 50
# Set the size of the screen
SCREEN_WIDTH = GRID_WIDTH * CELL_SIZE + 2 * MARGIN
SCREEN_HEIGHT = GRID_HEIGHT * CELL_SIZE + 2 * MARGIN + 50
# Create the screen
screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
# Set the title of the window
pygame.display.set_caption("Bug Arcade")
# Define the Bug class
class Bug(pygame.Rect):
def __init__(self):
super().__init__(MARGIN + CELL_SIZE // 2, MARGIN + CELL_SIZE // 2, CELL_SIZE // 2, CELL_SIZE // 2)
def move(self, dx, dy):
self.x += dx * CELL_SIZE
self.y += dy * CELL_SIZE
# Ensure the bug stays within the grid
if self.x < MARGIN:
self.x = MARGIN
elif self.x > MARGIN + (GRID_WIDTH - 1) * CELL_SIZE:
self.x = MARGIN + (GRID_WIDTH - 1) * CELL_SIZE
if self.y < MARGIN:
self.y = MARGIN
elif self.y > MARGIN + (GRID_HEIGHT - 1) * CELL_SIZE:
self.y = MARGIN + (GRID_HEIGHT - 1) * CELL_SIZE
# Define the Shoe class
class Shoe(pygame.Rect):
def __init__(self):
super().__init__(random.randint(MARGIN, MARGIN + (GRID_WIDTH - 1) * CELL_SIZE), MARGIN, CELL_SIZE, CELL_SIZE)
def move(self):
self.y += CELL_SIZE
# Define the Flower class
class Flower(pygame.Rect):
def __init__(self, x, y):
super().__init__(x, y, CELL_SIZE, CELL_SIZE)
# Create a bug, shoe, and flowers
bug = Bug()
shoe = Shoe()
flowers = [Flower(MARGIN + i * CELL_SIZE, MARGIN + j * CELL_SIZE) for i in range(GRID_WIDTH) for j in range(GRID_HEIGHT)]
# Game variables
score = 0
game_over = False
# Main game loop
clock = pygame.time.Clock()
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
bug.move(-1, 0)
elif event.key == pygame.K_RIGHT:
bug.move(1, 0)
elif event.key == pygame.K_UP:
bug.move(0, -1)
elif event.key == pygame.K_DOWN:
bug.move(0, 1)
# Move the shoe
if not game_over:
shoe.move()
if shoe.y > SCREEN_HEIGHT:
shoe = Shoe()
score += 1
# Check for collision with the shoe
if bug.colliderect(shoe):
game_over = True
# Draw everything
screen.fill(BLACK)
for flower in flowers:
pygame.draw.rect(screen, GREEN, flower)
pygame.draw.ellipse(screen, RED, bug)
pygame.draw.rect(screen, WHITE, shoe)
font = pygame.font.Font(None, 36)
text = font.render(f"Score: {score}", True, WHITE)
screen.blit(text, (MARGIN, SCREEN_HEIGHT - 40))
# Draw game over text
if game_over:
font = pygame.font.Font(None, 72)
text = font.render("Game Over", True, WHITE)
screen.blit(text, (SCREEN_WIDTH // 2 - text.get_width() // 2, SCREEN_HEIGHT // 2 - text.get_height() // 2))
pygame.display.flip()
# Cap the frame rate
clock.tick(60)
How to Run
- Save the above code in a file named
bug_arcade.py.
- Install Pygame by running
pip install pygame in your terminal.
- Run the game by executing
python bug_arcade.py in your terminal.
Gameplay
- Use the arrow keys to move the bug around the grid.
- Avoid being squashed by the giant shoe.
- The game ends when the bug collides with the shoe.
- The score increases each time the shoe moves off the screen.
Features
- Simple graphics and sound effects (none in this case, but you can add them if you want).
- A grid-based game world with flowers and a bug that can move around.
- A giant shoe that moves down the screen and can squash the bug.
- A scoring system that increments each time the shoe moves off the screen.
- A game over screen that displays when the bug is squashed.