Write a python pygame game. It is a platformer where user can run, jump, attack and block. When player is moving time flows and enemies are moving as well. If player is not moving (idle) time slows down to 5%
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
Here's a Python Pygame platformer game with the features you described. This is a simplified version that you can build upon:
python
import pygame
import sys
import math
# Initialize Pygame
pygame.init()
# Constants
WIDTH, HEIGHT = 800, 600
FPS = 60
GRAVITY = 0.8
JUMP_FORCE = -15
MOVE_SPEED = 5
SLOW_MOTION_FACTOR = 0.05 # 5% speed when idle
# Colors
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
RED = (255, 0, 0)
BLUE = (0, 0, 255)
# Screen setup
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("Platformer Game")
clock = pygame.time.Clock()
class Player(pygame.sprite.Sprite):
def __init__(self, x, y):
super().__init__()
self.width, self.height = 40, 60
self.image = pygame.Surface((self.width, self.height))
self.image.fill(BLUE)
self.rect = self.image.get_rect()
self.rect.x = x
self.rect.y = y
self.vel_x = 0
self.vel_y = 0
self.on_ground = False
self.is_moving = False
self.is_attacking = False
self.is_blocking = False
self.attack_cooldown = 0
self.health = 100
def update(self, platforms, time_factor):
# Apply movement
self.rect.x += self.vel_x * time_factor
self.check_collision(platforms, 'x')
# Apply gravity
self.vel_y += GRAVITY * time_factor
self.rect.y += self.vel_y * time_factor
self.check_collision(platforms, 'y')
# Update attack cooldown
if self.attack_cooldown > 0:
self.attack_cooldown -= 1 * time_factor
# Reset movement flag
self.is_moving = abs(self.vel_x) > 0
def check_collision(self, platforms, direction):
self.on_ground = False
for platform in platforms:
if self.rect.colliderect(platform.rect):
if direction == 'x':
if self.vel_x > 0:
self.rect.right = platform.rect.left
elif self.vel_x < 0:
self.rect.left = platform.rect.right
self.vel_x = 0
elif direction == 'y':
if self.vel_y > 0:
self.rect.bottom = platform.rect.top
self.on_ground = True
self.vel_y = 0
elif self.vel_y < 0:
self.rect.top = platform.rect.bottom
self.vel_y = 0
def jump(self):
if self.on_ground:
self.vel_y = JUMP_FORCE
def attack(self):
if self.attack_cooldown <= 0:
self.is_attacking = True
self.attack_cooldown = 30 # 0.5 seconds at 60 FPS
return pygame.Rect(self.rect.right, self.rect.y, 50, self.height)
return None
def block(self):
self.is_blocking = True
class Enemy(pygame.sprite.Sprite):
def __init__(self, x, y):
super().__init__()
self.width, self.height = 40, 60
self.image = pygame.Surface((self.width, self.height))
self.image.fill(RED)
self.rect = self.image.get_rect()
self.rect.x = x
self.rect.y = y
self.vel_x = -2
self.health = 50
def update(self, platforms, time_factor):
self.rect.x += self.vel_x * time_factor
if self.rect.left < 0 or self.rect.right > WIDTH:
self.vel_x = -self.vel_x
class Platform(pygame.sprite.Sprite):
def __init__(self, x, y, width, height):
super().__init__()
self.image = pygame.Surface((width, height))
self.image.fill(BLACK)
self.rect = self.image.get_rect()
self.rect.x = x
self.rect.y = y
def main():
# Create sprite groups
all_sprites = pygame.sprite.Group()
platforms = pygame.sprite.Group()
enemies = pygame.sprite.Group()
# Create player
player = Player(100, 400)
all_sprites.add(player)
# Create platforms
ground = Platform(0, HEIGHT - 40, WIDTH, 40)
platform1 = Platform(200, 400, 200, 20)
platform2 = Platform(450, 300, 200, 20)
platforms.add(ground, platform1, platform2)
all_sprites.add(ground, platform1, platform2)
# Create enemy
enemy = Enemy(600, 400)
enemies.add(enemy)
all_sprites.add(enemy)
running = True
while running:
# Handle events
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_SPACE:
player.jump()
elif event.key == pygame.K_j:
attack_rect = player.attack()
if attack_rect:
for enemy in enemies:
if attack_rect.colliderect(enemy.rect):
enemy.health -= 20
if enemy.health <= 0:
enemy.kill()
elif event.key == pygame.K_k:
player.block()
elif event.type == pygame.KEYUP:
if event.key == pygame.K_k:
player.is_blocking = False
# Handle continuous movement
keys = pygame.key.get_pressed()
player.vel_x = 0
if keys[pygame.K_LEFT]:
player.vel_x = -MOVE_SPEED
if keys[pygame.K_RIGHT]:
player.vel_x = MOVE_SPEED
# Determine time factor based on player movement
time_factor = 1.0 if player.is_moving or player.is_attacking else SLOW_MOTION_FACTOR
# Update
player.update(platforms, time_factor)
enemies.update(platforms, time_factor)
player.is_attacking = False
# Draw
screen.fill(WHITE)
all_sprites.draw(screen)
# Draw attack rectangle if attacking
if player.is_attacking:
attack_rect = pygame.Rect(player.rect.right, player.rect.y, 50, player.height)
pygame.draw.rect(screen, (255, 165, 0), attack_rect, 2)
# Draw block indicator
if player.is_blocking:
block_rect = pygame.Rect(player.rect.x - 10, player.rect.y, 10, player.height)
pygame.draw.rect(screen, (0, 255, 0), block_rect)
# Draw health bars
pygame.draw.rect(screen, RED, (player.rect.x, player.rect.y - 20, player.health, 10))
for enemy in enemies:
pygame.draw.rect(screen, RED, (enemy.rect.x, enemy.rect.y - 20, enemy.health, 10))
pygame.display.flip()
clock.tick(FPS)
pygame.quit()
sys.exit()
if __name__ == "__main__":
main()
This game includes the following features:
Player movement (left/right) using arrow keys
Jumping using spacebar
Attacking using 'J' key (creates a hitbox to the right of the player)
Blocking using 'K' key (shows a green shield indicator)
Time slow-down to 5% speed when player is idle
Basic enemy that moves back and forth
Platform collision detection
Health bars for player and enemy
Basic attack system that damages enemies
To run this game, you'll need to have Pygame installed. You can install it using:
code
pip install pygame
Key controls:
Left/Right arrows: Move
Space: Jump
J: Attack
K: Block (hold)
Features explanation:
The player is a blue rectangle, enemies are red rectangles
Time slows down to 5% speed when the player is idle (not moving or attacking)
Platforms are black rectangles
Attack creates an orange hitbox that damages enemies
Block shows a green shield indicator
Health bars are shown above characters
Enemies die when their health reaches 0
To enhance this game, you could:
Add animations
Include sound effects
Add more enemy types
Implement a scoring system
Add power-ups
Create multiple levels
Add background graphics
Implement a more sophisticated combat system
Add particle effects
Include a menu system
Remember that this is a basic implementation, and you can expand upon it based on your needs. The code is structured in a way that makes it relatively easy to add new features and modify existing ones.