Certainly! Below is a simple retro-themed horse-racing arcade game using Python and the Pygame library. This game will have a basic design with pixel art and simple mechanics.
First, ensure you have Pygame installed. You can install it using pip if you haven't already:
pip install pygame
Here's the complete code for the retro-themed horse-racing arcade game:
import pygame
import random
# Initialize Pygame
pygame.init()
# Constants
SCREEN_WIDTH = 800
SCREEN_HEIGHT = 600
BACKGROUND_COLOR = (0, 128, 0)
TRACK_COLOR = (255, 255, 255)
HORSE_COLORS = [(255, 0, 0), (0, 255, 0), (0, 0, 255), (255, 255, 0)]
HORSE_WIDTH = 50
HORSE_HEIGHT = 50
HORSE_SPEED = 5
FONT_SIZE = 36
# Set up the display
screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
pygame.display.set_caption("Retro Horse Racing")
# Fonts
font = pygame.font.Font(None, FONT_SIZE)
# Horse class
class Horse(pygame.sprite.Sprite):
def __init__(self, color, y):
super().__init__()
self.image = pygame.Surface((HORSE_WIDTH, HORSE_HEIGHT))
self.image.fill(color)
self.rect = self.image.get_rect()
self.rect.x = 0
self.rect.y = y
self.speed = HORSE_SPEED
def update(self):
self.rect.x += self.speed
# Create horses
horses = pygame.sprite.Group()
for i, color in enumerate(HORSE_COLORS):
horse = Horse(color, 100 + i * 150)
horses.add(horse)
# Main game loop
running = True
clock = pygame.time.Clock()
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
# Update horses
horses.update()
# Check if any horse has reached the finish line
for horse in horses:
if horse.rect.x > SCREEN_WIDTH:
running = False
winner_color = horse.image.get_at((0, 0))
# Draw everything
screen.fill(BACKGROUND_COLOR)
pygame.draw.line(screen, TRACK_COLOR, (0, 50), (SCREEN_WIDTH, 50), 10)
pygame.draw.line(screen, TRACK_COLOR, (0, 570), (SCREEN_WIDTH, 570), 10)
horses.draw(screen)
# Cap the frame rate
clock.tick(30)
pygame.display.flip()
# Display winner
screen.fill(BACKGROUND_COLOR)
winner_text = font.render(f"Winner: Horse {HORSE_COLORS.index(winner_color) + 1}", True, (255, 255, 255))
screen.blit(winner_text, (SCREEN_WIDTH // 2 - winner_text.get_width() // 2, SCREEN_HEIGHT // 2 - winner_text.get_height() // 2))
pygame.display.flip()
# Wait for a few seconds before quitting
pygame.time.wait(3000)
# Quit Pygame
pygame.quit()
Explanation:
- Initialization: We initialize Pygame and set up the screen dimensions and colors.
- Horse Class: We create a
Horse class that inherits from pygame.sprite.Sprite. Each horse is a rectangle with a specific color.
- Main Game Loop:
- We handle events to allow quitting the game.
- We update the position of each horse.
- We check if any horse has reached the end of the screen (finish line).
- We draw the track, horses, and update the display.
- Winner Display: After the race ends, we display which horse won and wait for a few seconds before quitting.
This code creates a simple, retro-themed horse-racing game with a basic track and four horses racing towards the finish line.