import pygame
import sys
import random
# Initialize Pygame
pygame.init()
# Game Constants
SCREEN_WIDTH, SCREEN_HEIGHT = 800, 400
GRAVITY = 1
JUMP_HEIGHT = -20
# Colors
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
# Set up the display
screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
pygame.display.set_caption("Poop Dodge Game")
# Clock to control game framerate
clock = pygame.time.Clock()
def load_poop_image():
"""Create a simple poop emoji surface as there's no direct way to load custom images here."""
poop_surface = pygame.Surface((50, 50), pygame.SRCALPHA)
pygame.draw.ellipse(poop_surface, BLACK, poop_surface.get_rect(), 0)
pygame.draw.ellipse(poop_surface, (139, 69, 19), poop_surface.get_rect().inflate(-5, -5), 0)
return poop_surface
# Load the poop image
poop_image = load_poop_image()
poop_rect = poop_image.get_rect(midbottom=(80, SCREEN_HEIGHT - 30))
# Player variables
poop_gravity = 0
poop_jump = False
# Obstacle setup
obstacle_timer = 0
obstacle_list = []
def display_poop():
"""Draw the poop on the screen."""
screen.blit(poop_image, poop_rect)
def poop_movement():
"""Handle poop movement and gravity."""
global poop_gravity, poop_jump
if poop_jump:
poop_gravity = JUMP_HEIGHT
poop_jump = False
poop_gravity += GRAVITY
poop_rect.y += poop_gravity
if poop_rect.bottom >= SCREEN_HEIGHT - 30:
poop_rect.bottom = SCREEN_HEIGHT - 30
def create_obstacle():
"""Randomly create obstacles and add them to the obstacle list."""
if obstacle_timer == 0:
obstacle_height = random.randint(20, 70)
obstacle_rect = pygame.Rect(SCREEN_WIDTH, SCREEN_HEIGHT - 30 - obstacle_height, 20, obstacle_height)
obstacle_list.append(obstacle_rect)
def move_obstacles():
"""Move and draw obstacles, and remove them when they're off-screen."""
for obstacle in obstacle_list:
obstacle.x -= 5
pygame.draw.rect(screen, BLACK, obstacle)
# Remove obstacles that have moved off-screen
obstacle_list[:] = [obstacle for obstacle in obstacle_list if obstacle.x > -20]
def game_over():
"""Display game over message and end the game."""
screen.fill(WHITE)
font = pygame.font.Font(None, 74)
text = font.render('Game Over', True, BLACK)
text_rect = text.get_rect(center=(SCREEN_WIDTH//2, SCREEN_HEIGHT//2))
screen.blit(text, text_rect)
pygame.display.flip()
pygame.time.wait(2000)
pygame.quit()
sys.exit()
# Game loop
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_UP and poop_rect.bottom >= SCREEN_HEIGHT - 30:
poop_jump = True
screen.fill(WHITE)
display_poop()
poop_movement()
# Obstacle logic
obstacle_timer += 1
if obstacle_timer >= 150:
obstacle_timer = 0
create_obstacle()
move_obstacles()
# Collision detection
for obstacle in obstacle_list:
if poop_rect.colliderect(obstacle):
game_over()
pygame.display.update()
clock.tick(30)
pygame.quit()