Write a game in Python using Pygame, here are things to consider:
It's a shape-based game
Use vibrant colors
Player is a "shade" of blue square with a smiley face
Enemies are a "shade" of red squares with evil faces
Make a lava underneath: Make sure it has lava physics (waves, particles going upwards, when touched should make splashes, etc.)
Player has an AK-47 (Made of shapes)
Make it a platformer game, enemies spawning from the platforms (Similar to how zombies from graves)
Player moves using WASD or arrow keys
Shooting is with left click
Aiming is with the mouse
Make sure the AK-47 is aiming with a red laser so the player knows where they're aiming precisely
Assistant
Sure! I'll help you create a simple platformer game in Python using Pygame that incorporates all the features you've described. Below is the step-by-step code with explanations for each part.
Prerequisites
Make sure you have Pygame installed. You can install it using pip:
bash
pip install pygame
Game Overview
Player: A blue square with a smiley face, controlled using WASD or arrow keys.
Enemies: Red squares with evil faces, spawning from platforms.
Weapon: An AK-47 made of shapes, aiming with the mouse, shooting with left click, with a red laser for precise aiming.
Lava: At the bottom of the screen with lava physics (waves, particles going upwards, splashes on touch).
Platforms: The player can jump on these; enemies spawn from them.
Full Game Code
Here's the complete code for the game:
python
import pygame
import sys
import math
import random
# Initialize Pygame
pygame.init()
# Screen dimensions
SCREEN_WIDTH = 800
SCREEN_HEIGHT = 600
# Colors
BLUE = (50, 150, 255)
RED = (255, 50, 50)
GREEN = (50, 255, 100)
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
LAVA_COLOR = (255, 100, 0)
# Create the screen
screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
pygame.display.set_caption("Shape-Based Platformer Game")
# Clock object to control FPS
clock = pygame.time.Clock()
FPS = 60
# Player Class
class Player(pygame.sprite.Sprite):
def __init__(self):
super().__init__()
# Image is a blue square with a smiley face
self.image = pygame.Surface((40, 40), pygame.SRCALPHA)
self.rect = self.image.get_rect()
self.rect.center = (SCREEN_WIDTH // 2, SCREEN_HEIGHT - 150)
self.speed_x = 0
self.speed_y = 0
self.on_ground = False
# Draw the player (blue square with a smiley face)
pygame.draw.rect(self.image, BLUE, (0, 0, 40, 40))
# Add a smiley face (simple representation)
pygame.draw.circle(self.image, BLACK, (13, 15), 5)
pygame.draw.circle(self.image, BLACK, (27, 15), 5)
pygame.draw.arc(self.image, BLACK, (10, 20, 20, 10), math.radians(0), math.radians(180), 2)
# Weapon properties
self.weapon_length = 30
self.weapon_color = BLACK
def update(self, platforms):
# Apply gravity
self.speed_y += 0.5 # Gravity strength
# Move horizontally
self.rect.x += self.speed_x
# Check horizontal collisions
self.collide(self.speed_x, 0, platforms)
# Move vertically
self.rect.y += self.speed_y
# Check vertical collisions
self.on_ground = False
self.collide(0, self.speed_y, platforms)
# Keep player within screen bounds
if self.rect.left < 0:
self.rect.left = 0
if self.rect.right > SCREEN_WIDTH:
self.rect.right = SCREEN_WIDTH
def collide(self, speed_x, speed_y, platforms):
for platform in platforms:
if self.rect.colliderect(platform.rect):
if speed_x > 0:
self.rect.right = platform.rect.left
if speed_x < 0:
self.rect.left = platform.rect.right
if speed_y > 0:
self.rect.bottom = platform.rect.top
self.speed_y = 0
self.on_ground = True
if speed_y < 0:
self.rect.top = platform.rect.bottom
self.speed_y = 0
def draw_weapon(self, surface, mouse_pos):
# Calculate angle
dx = mouse_pos[0] - self.rect.centerx
dy = mouse_pos[1] - self.rect.centery
angle = math.atan2(dy, dx)
# Weapon position
start_pos = (self.rect.centerx, self.rect.centery)
end_pos = (self.rect.centerx + self.weapon_length * math.cos(angle),
self.rect.centery + self.weapon_length * math.sin(angle))
# Draw the weapon (AK-47 made of shapes)
pygame.draw.line(surface, self.weapon_color, start_pos, end_pos, 5)
# Add more shapes to represent the AK-47 (simplified for this example)
pygame.draw.rect(surface, self.weapon_color, (end_pos[0] - 5, end_pos[1] - 3, 10, 6))
# Draw aiming laser
laser_end = (self.rect.centerx + 1000 * math.cos(angle),
self.rect.centery + 1000 * math.sin(angle))
pygame.draw.line(surface, RED, start_pos, laser_end, 1)
# Platform Class
class Platform(pygame.sprite.Sprite):
def __init__(self, x, y, width, height):
super().__init__()
self.image = pygame.Surface((width, height))
self.image.fill(GREEN)
self.rect = self.image.get_rect()
self.rect.x = x
self.rect.y = y
# Enemy Class
class Enemy(pygame.sprite.Sprite):
def __init__(self, x, y):
super().__init__()
# Image is a red square with an evil face
self.image = pygame.Surface((40, 40), pygame.SRCALPHA)
self.rect = self.image.get_rect()
self.rect.center = (x, y)
self.speed_x = random.choice([-2, 2]) # Move left or right randomly
# Draw the enemy (red square with an evil face)
pygame.draw.rect(self.image, RED, (0, 0, 40, 40))
# Add evil eyes
pygame.draw.polygon(self.image, BLACK, [(10, 15), (15, 10), (20, 15)])
pygame.draw.polygon(self.image, BLACK, [(20, 15), (25, 10), (30, 15)])
# Add evil mouth (frown)
pygame.draw.arc(self.image, BLACK, (10, 25, 20, 10), math.radians(180), math.radians(360), 2)
def update(self, platforms):
# Move
self.rect.x += self.speed_x
# Change direction upon hitting platform edges
hit_platform = False
for platform in platforms:
if self.rect.colliderect(platform.rect):
hit_platform = True
break
if not hit_platform or self.rect.left <= 0 or self.rect.right >= SCREEN_WIDTH:
self.speed_x *= -1 # Change direction
# Lava Particle Class
class LavaParticle:
def __init__(self, x, y):
self.x = x
self.y = y
self.speed_y = random.uniform(-2, -1)
self.radius = random.randint(2, 4)
def update(self):
self.y += self.speed_y
self.speed_y += 0.05 # Gravity
if self.y > SCREEN_HEIGHT - 20:
self.y = SCREEN_HEIGHT - 20
self.speed_y *= -0.5 # Bounce slightly
def draw(self, surface):
pygame.draw.circle(surface, LAVA_COLOR, (int(self.x), int(self.y)), self.radius)
# Bullet Class
class Bullet(pygame.sprite.Sprite):
def __init__(self, pos, angle):
super().__init__()
self.image = pygame.Surface((5, 5))
self.image.fill(BLACK)
self.rect = self.image.get_rect(center=pos)
self.speed = 10
self.angle = angle
self.dx = math.cos(self.angle) * self.speed
self.dy = math.sin(self.angle) * self.speed
def update(self):
self.rect.x += self.dx
self.rect.y += self.dy
# Remove bullet if out of screen
if (self.rect.right < 0 or self.rect.left > SCREEN_WIDTH or
self.rect.bottom < 0 or self.rect.top > SCREEN_HEIGHT):
self.kill()
# Main Game Function
def main():
# Sprite groups
all_sprites = pygame.sprite.Group()
platforms = pygame.sprite.Group()
enemies = pygame.sprite.Group()
bullets = pygame.sprite.Group()
# Create player
player = Player()
all_sprites.add(player)
# Create platforms
platform1 = Platform(100, 500, 200, 20)
platform2 = Platform(400, 400, 200, 20)
platform3 = Platform(150, 300, 150, 20)
platforms.add(platform1, platform2, platform3)
all_sprites.add(platform1, platform2, platform3)
# Lava particles
lava_particles = [LavaParticle(random.randint(0, SCREEN_WIDTH), SCREEN_HEIGHT - 10) for _ in range(50)]
# Enemy spawn timer
enemy_spawn_event = pygame.USEREVENT + 1
pygame.time.set_timer(enemy_spawn_event, 2000) # Spawn an enemy every 2 seconds
# Game loop
running = True
while running:
clock.tick(FPS)
# Event handling
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
if event.type == enemy_spawn_event:
# Spawn enemy on a random platform
platform = random.choice(platforms.sprites())
enemy = Enemy(platform.rect.centerx, platform.rect.top - 20)
enemies.add(enemy)
all_sprites.add(enemy)
if event.type == pygame.MOUSEBUTTONDOWN:
if event.button == 1: # Left click to shoot
mouse_pos = pygame.mouse.get_pos()
dx = mouse_pos[0] - player.rect.centerx
dy = mouse_pos[1] - player.rect.centery
angle = math.atan2(dy, dx)
bullet = Bullet(player.rect.center, angle)
bullets.add(bullet)
all_sprites.add(bullet)
# Get key presses
keys = pygame.key.get_pressed()
player.speed_x = 0
if keys[pygame.K_LEFT] or keys[pygame.K_a]:
player.speed_x = -5
if keys[pygame.K_RIGHT] or keys[pygame.K_d]:
player.speed_x = 5
if (keys[pygame.K_UP] or keys[pygame.K_w]) and player.on_ground:
player.speed_y = -10 # Jump
# Update
player.update(platforms)
enemies.update(platforms)
bullets.update()
# Check for bullet collisions with enemies
for bullet in bullets:
hit_enemies = pygame.sprite.spritecollide(bullet, enemies, True)
if hit_enemies:
bullet.kill() # Remove bullet upon hitting an enemy
# Update lava particles
for particle in lava_particles:
particle.update()
# Check if player touches the lava
if player.rect.bottom >= SCREEN_HEIGHT - 20:
# Lava splash effect
for _ in range(10):
x = random.randint(player.rect.left, player.rect.right)
lava_particles.append(LavaParticle(x, SCREEN_HEIGHT - 30))
# For simplicity, reset player position
player.rect.center = (SCREEN_WIDTH // 2, SCREEN_HEIGHT - 150)
player.speed_y = 0
# Drawing
screen.fill(WHITE)
# Draw lava
pygame.draw.rect(screen, LAVA_COLOR, (0, SCREEN_HEIGHT - 20, SCREEN_WIDTH, 20))
for particle in lava_particles:
particle.draw(screen)
all_sprites.draw(screen)
player.draw_weapon(screen, pygame.mouse.get_pos())
pygame.display.flip()
pygame.quit()
sys.exit()
if __name__ == "__main__":
main()
Explanation
Imports and Initialization: We import the necessary libraries and initialize Pygame.
Screen Setup: Define screen dimensions and create the display surface.
Colors: Define vibrant colors for various game elements.
Player Class:
The player is a blue square with a smiley face drawn using basic shapes.
Handles movement, gravity, collisions, and weapon drawing.
The weapon (AK-47 made of shapes) points towards the mouse cursor.
A red laser line indicates the aim direction.
Platform Class:
Represents platforms the player can stand on.
Platforms are green rectangles.
Enemy Class:
Enemies are red squares with evil faces.
They move left and right on platforms and reverse direction upon reaching an edge.
Evil faces are drawn using polygons and arcs.
Lava Particle Class:
Simulates lava particles that move upwards and fall back down.
Creates a visual effect of lava waves and splashes.
Bullet Class:
Represents bullets fired by the player.
Moves in the direction of the mouse cursor when fired.
Checks for collisions with enemies and removes enemies upon collision.
Main Game Function:
Sets up sprite groups and game elements.
Handles the main game loop, including event handling, updating, collision detection, and drawing.
Spawns enemies periodically from random platforms.
Implements player controls for movement and jumping.
Controls
Movement: Use WASD keys or Arrow keys.
Left: A or Left Arrow
Right: D or Right Arrow
Jump: W or Up Arrow
Shooting:
Aim with the mouse cursor.
Shoot by Left Clicking.
Running the Game
Save the code in a file named, for example, platformer_game.py.
Run the game by executing the script:
bash
python platformer_game.py
Possible Enhancements
Lava Physics: For more realistic lava, consider using Perlin noise or a fluid simulation library.
Graphics: Replace the shapes with images for more detailed characters and objects.
Sound Effects: Add sounds for shooting, jumping, enemy hits, and lava splashes.
Score System: Implement a scoring mechanism to track how many enemies the player defeats.
Health and Lives: Add health points for the player and enemies.
Levels and Difficulty: Introduce multiple levels or increasing difficulty over time.
Power-ups: Include items the player can collect for bonuses.
Conclusion
You've now got a basic platformer game built with Pygame that matches your specifications. This foundation allows you to further expand and customize the game as you wish. Enjoy coding and gaming!