make a GTA clone
- User
make a GTA clone
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
make a GTA clone
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
Here's a complete PyGame implementation of a simplified top-down GTA-style game with character movement, vehicles, buildings, pedestrians, and police:
import pygame
import random
import math
import sys
# Initialize Pygame
pygame.init()
# Screen dimensions
SCREEN_WIDTH = 1000
SCREEN_HEIGHT = 700
screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
pygame.display.set_caption("PyGame GTA-Style Game")
# Colors
SKY_BLUE = (135, 206, 235)
ROAD_GRAY = (80, 80, 80)
SIDEWALK_GRAY = (180, 180, 180)
BUILDING_COLORS = [(200, 200, 200), (180, 180, 200), (200, 180, 180), (180, 200, 180)]
GRASS_GREEN = (100, 180, 100)
VEHICLE_COLORS = [(200, 50, 50), (50, 50, 200), (50, 200, 50), (200, 200, 50)]
PLAYER_COLOR = (255, 100, 100)
PEDESTRIAN_COLORS = [(70, 70, 70), (90, 90, 90), (110, 110, 110)]
POLICE_BLUE = (50, 50, 200)
RED = (255, 50, 50)
YELLOW = (255, 255, 0)
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
UI_BG = (30, 30, 30, 200)
# Game parameters
FPS = 60
PLAYER_SPEED = 4
VEHICLE_SPEED = 8
PEDESTRIAN_SPEED = 1.5
POLICE_SPEED = 5
WANTED_LEVEL_COOLDOWN = 3000 # milliseconds
class Player:
def __init__(self, x, y):
self.x = x
self.y = y
self.width = 20
self.height = 30
self.speed = PLAYER_SPEED
self.in_vehicle = False
self.vehicle = None
self.direction = "down"
self.wanted_level = 0
self.last_crime_time = 0
self.health = 100
self.money = 500
def draw(self, screen):
# Draw player
if not self.in_vehicle:
# Body
pygame.draw.rect(screen, PLAYER_COLOR, (self.x - self.width//2, self.y - self.height//2, self.width, self.height))
# Head
head_radius = self.width // 2
pygame.draw.circle(screen, (255, 200, 150), (self.x, self.y - self.height//4), head_radius)
# Direction indicator
if self.direction == "up":
pygame.draw.rect(screen, (50, 50, 50), (self.x - 3, self.y - self.height//2 - 5, 6, 8))
elif self.direction == "down":
pygame.draw.rect(screen, (50, 50, 50), (self.x - 3, self.y + self.height//2 - 3, 6, 8))
elif self.direction == "left":
pygame.draw.rect(screen, (50, 50, 50), (self.x - self.width//2 - 5, self.y - 3, 8, 6))
elif self.direction == "right":
pygame.draw.rect(screen, (50, 50, 50), (self.x + self.width//2 - 3, self.y - 3, 8, 6))
else:
self.vehicle.draw(screen)
def move(self, keys, vehicles):
if self.in_vehicle:
self.vehicle.move(keys)
self.x = self.vehicle.x
self.y = self.vehicle.y
return
dx, dy = 0, 0
if keys[pygame.K_w] or keys[pygame.K_UP]:
dy = -self.speed
self.direction = "up"
if keys[pygame.K_s] or keys[pygame.K_DOWN]:
dy = self.speed
self.direction = "down"
if keys[pygame.K_a] or keys[pygame.K_LEFT]:
dx = -self.speed
self.direction = "left"
if keys[pygame.K_d] or keys[pygame.K_RIGHT]:
dx = self.speed
self.direction = "right"
# Diagonal movement
if dx != 0 and dy != 0:
dx *= 0.7071
dy *= 0.7071
# Move the player
self.x += dx
self.y += dy
# Keep player on screen
self.x = max(self.width//2, min(SCREEN_WIDTH - self.width//2, self.x))
self.y = max(self.height//2, min(SCREEN_HEIGHT - self.height//2, self.y))
# Check for vehicle entry
for vehicle in vehicles:
dist = math.sqrt((self.x - vehicle.x)**2 + (self.y - vehicle.y)**2)
if dist < 30 and keys[pygame.K_e]:
self.in_vehicle = True
self.vehicle = vehicle
break
def exit_vehicle(self):
if self.in_vehicle:
# Exit to the side of the vehicle
if self.vehicle.direction == "up":
self.x = self.vehicle.x - 30
elif self.vehicle.direction == "down":
self.x = self.vehicle.x + 30
elif self.vehicle.direction == "left":
self.y = self.vehicle.y + 30
elif self.vehicle.direction == "right":
self.y = self.vehicle.y - 30
self.in_vehicle = False
self.vehicle = None
def commit_crime(self, crime_level):
current_time = pygame.time.get_ticks()
if current_time - self.last_crime_time > 1000: # 1 second cooldown
self.wanted_level = min(5, self.wanted_level + crime_level)
self.last_crime_time = current_time
return True
return False
class Vehicle:
def __init__(self, x, y, color, vehicle_type="car"):
self.x = x
self.y = y
self.width = 40 if vehicle_type == "car" else 50
self.height = 20 if vehicle_type == "car" else 30
self.color = color
self.speed = VEHICLE_SPEED
self.direction = "right"
self.type = vehicle_type
def draw(self, screen):
# Car body
pygame.draw.rect(screen, self.color, (self.x - self.width//2, self.y - self.height//2, self.width, self.height))
# Windows
window_color = (100, 180, 255)
pygame.draw.rect(screen, window_color, (self.x - self.width//2 + 5, self.y - self.height//2 + 3, self.width - 10, 6))
# Wheels
wheel_color = (30, 30, 30)
pygame.draw.rect(screen, wheel_color, (self.x - self.width//2 + 5, self.y - self.height//2 - 3, 8, 5))
pygame.draw.rect(screen, wheel_color, (self.x + self.width//2 - 13, self.y - self.height//2 - 3, 8, 5))
pygame.draw.rect(screen, wheel_color, (self.x - self.width//2 + 5, self.y + self.height//2 - 2, 8, 5))
pygame.draw.rect(screen, wheel_color, (self.x + self.width//2 - 13, self.y + self.height//2 - 2, 8, 5))
def move(self, keys):
dx, dy = 0, 0
speed = self.speed
if keys[pygame.K_w] or keys[pygame.K_UP]:
dy = -speed
self.direction = "up"
if keys[pygame.K_s] or keys[pygame.K_DOWN]:
dy = speed
self.direction = "down"
if keys[pygame.K_a] or keys[pygame.K_LEFT]:
dx = -speed
self.direction = "left"
if keys[pygame.K_d] or keys[pygame.K_RIGHT]:
dx = speed
self.direction = "right"
# Diagonal movement
if dx != 0 and dy != 0:
dx *= 0.7071
dy *= 0.7071
# Move the vehicle
self.x += dx
self.y += dy
# Keep vehicle on screen
self.x = max(self.width//2, min(SCREEN_WIDTH - self.width//2, self.x))
self.y = max(self.height//2, min(SCREEN_HEIGHT - self.height//2, self.y))
def get_rect(self):
return pygame.Rect(self.x - self.width//2, self.y - self.height//2, self.width, self.height)
class Pedestrian:
def __init__(self, x, y):
self.x = x
self.y = y
self.width = 15
self.height = 25
self.speed = PEDESTRIAN_SPEED
self.direction = random.choice(["up", "down", "left", "right"])
self.color = random.choice(PEDESTRIAN_COLORS)
self.move_timer = 0
self.change_direction_timer = random.randint(60, 180)
def draw(self, screen):
# Body
pygame.draw.rect(screen, self.color, (self.x - self.width//2, self.y - self.height//2, self.width, self.height))
# Head
head_radius = self.width // 2
pygame.draw.circle(screen, (255, 220, 180), (self.x, self.y - self.height//4), head_radius)
def move(self):
self.move_timer += 1
# Change direction randomly
if self.move_timer >= self.change_direction_timer:
self.direction = random.choice(["up", "down", "left", "right"])
self.move_timer = 0
self.change_direction_timer = random.randint(60, 180)
# Move in current direction
if self.direction == "up":
self.y -= self.speed
elif self.direction == "down":
self.y += self.speed
elif self.direction == "left":
self.x -= self.speed
elif self.direction == "right":
self.x += self.speed
# Keep pedestrian on screen
self.x = max(self.width//2, min(SCREEN_WIDTH - self.width//2, self.x))
self.y = max(self.height//2, min(SCREEN_HEIGHT - self.height//2, self.y))
def get_rect(self):
return pygame.Rect(self.x - self.width//2, self.y - self.height//2, self.width, self.height)
class Police:
def __init__(self, x, y):
self.x = x
self.y = y
self.width = 20
self.height = 30
self.speed = POLICE_SPEED
self.color = POLICE_BLUE
self.target_x = x
self.target_y = y
def draw(self, screen):
# Body
pygame.draw.rect(screen, self.color, (self.x - self.width//2, self.y - self.height//2, self.width, self.height))
# Head
head_radius = self.width // 2
pygame.draw.circle(screen, (255, 220, 180), (self.x, self.y - self.height//4), head_radius)
# Police hat
pygame.draw.rect(screen, (50, 50, 50), (self.x - self.width//2, self.y - self.height//2 - 5, self.width, 6))
pygame.draw.rect(screen, (50, 50, 50), (self.x - 5, self.y - self.height//2 - 10, 10, 5))
def move_towards(self, target_x, target_y):
# Calculate direction vector
dx = target_x - self.x
dy = target_y - self.y
dist = max(1, math.sqrt(dx*dx + dy*dy))
# Normalize and move
self.x += (dx / dist) * self.speed
self.y += (dy / dist) * self.speed
def get_rect(self):
return pygame.Rect(self.x - self.width//2, self.y - self.height//2, self.width, self.height)
class Building:
def __init__(self, x, y, width, height, color):
self.x = x
self.y = y
self.width = width
self.height = height
self.color = color
def draw(self, screen):
# Building base
pygame.draw.rect(screen, self.color, (self.x, self.y, self.width, self.height))
# Windows
window_color = (200, 230, 255)
window_size = 10
window_margin = 15
spacing = 20
for row in range(3):
for col in range(4):
win_x = self.x + col * spacing + window_margin
win_y = self.y + row * spacing + window_margin
pygame.draw.rect(screen, window_color, (win_x, win_y, window_size, window_size))
def get_rect(self):
return pygame.Rect(self.x, self.y, self.width, self.height)
class Road:
def __init__(self, x, y, width, height, is_horizontal=True):
self.x = x
self.y = y
self.width = width
self.height = height
self.is_horizontal = is_horizontal
def draw(self, screen):
# Road surface
pygame.draw.rect(screen, ROAD_GRAY, (self.x, self.y, self.width, self.height))
# Road markings
if self.is_horizontal:
# Horizontal road
for i in range(0, self.width, 40):
pygame.draw.rect(screen, YELLOW, (self.x + i, self.y + self.height//2 - 1, 20, 2))
else:
# Vertical road
for i in range(0, self.height, 40):
pygame.draw.rect(screen, YELLOW, (self.x + self.width//2 - 1, self.y + i, 2, 20))
class Game:
def __init__(self):
self.player = Player(SCREEN_WIDTH // 2, SCREEN_HEIGHT // 2)
self.vehicles = []
self.pedestrians = []
self.police = []
self.buildings = []
self.roads = []
self.generate_city()
self.generate_vehicles(8)
self.generate_pedestrians(15)
self.font = pygame.font.SysFont(None, 28)
self.small_font = pygame.font.SysFont(None, 24)
self.game_over = False
self.game_won = False
self.start_time = pygame.time.get_ticks()
self.game_duration = 120000 # 2 minutes in milliseconds
def generate_city(self):
# Create roads
self.roads.append(Road(0, SCREEN_HEIGHT//2 - 50, SCREEN_WIDTH, 100, True)) # Horizontal road
self.roads.append(Road(SCREEN_WIDTH//2 - 50, 0, 100, SCREEN_HEIGHT, False)) # Vertical road
# Create buildings
building_width = 100
building_height = 120
building_margin = 20
# Top-left quadrant
for i in range(2):
for j in range(2):
x = i * (building_width + building_margin) + building_margin
y = j * (building_height + building_margin) + building_margin
self.buildings.append(Building(x, y, building_width, building_height, random.choice(BUILDING_COLORS)))
# Top-right quadrant
for i in range(2):
for j in range(2):
x = SCREEN_WIDTH - building_width - building_margin - i * (building_width + building_margin)
y = j * (building_height + building_margin) + building_margin
self.buildings.append(Building(x, y, building_width, building_height, random.choice(BUILDING_COLORS)))
# Bottom-left quadrant
for i in range(2):
for j in range(2):
x = i * (building_width + building_margin) + building_margin
y = SCREEN_HEIGHT - building_height - building_margin - j * (building_height + building_margin)
self.buildings.append(Building(x, y, building_width, building_height, random.choice(BUILDING_COLORS)))
# Bottom-right quadrant
for i in range(2):
for j in range(2):
x = SCREEN_WIDTH - building_width - building_margin - i * (building_width + building_margin)
y = SCREEN_HEIGHT - building_height - building_margin - j * (building_height + building_margin)
self.buildings.append(Building(x, y, building_width, building_height, random.choice(BUILDING_COLORS)))
def generate_vehicles(self, count):
for _ in range(count):
x = random.randint(50, SCREEN_WIDTH - 50)
y = random.randint(50, SCREEN_HEIGHT - 50)
color = random.choice(VEHICLE_COLORS)
vehicle_type = random.choice(["car", "car", "car", "truck"]) # More cars than trucks
self.vehicles.append(Vehicle(x, y, color, vehicle_type))
def generate_pedestrians(self, count):
for _ in range(count):
# Place pedestrians near sidewalks
if random.choice([True, False]):
# Near horizontal road
y = SCREEN_HEIGHT//2 + random.choice([-80, 130])
x = random.randint(50, SCREEN_WIDTH - 50)
else:
# Near vertical road
x = SCREEN_WIDTH//2 + random.choice([-80, 130])
y = random.randint(50, SCREEN_HEIGHT - 50)
self.pedestrians.append(Pedestrian(x, y))
def spawn_police(self, count):
# Spawn police near the edges of the screen
for _ in range(count):
side = random.choice(["top", "bottom", "left", "right"])
if side == "top":
x = random.randint(50, SCREEN_WIDTH - 50)
y = -30
elif side == "bottom":
x = random.randint(50, SCREEN_WIDTH - 50)
y = SCREEN_HEIGHT + 30
elif side == "left":
x = -30
y = random.randint(50, SCREEN_HEIGHT - 50)
else: # "right"
x = SCREEN_WIDTH + 30
y = random.randint(50, SCREEN_HEIGHT - 50)
self.police.append(Police(x, y))
def update(self, keys):
if self.game_over or self.game_won:
return
# Update game time
current_time = pygame.time.get_ticks()
elapsed_time = current_time - self.start_time
if elapsed_time >= self.game_duration:
self.game_won = True
return
# Update player
self.player.move(keys, self.vehicles)
# Exit vehicle
if keys[pygame.K_q] and self.player.in_vehicle:
self.player.exit_vehicle()
# Update pedestrians
for pedestrian in self.pedestrians:
pedestrian.move()
# Update police
for police in self.police:
police.move_towards(self.player.x, self.player.y)
# Check for collisions with pedestrians (crimes)
if not self.player.in_vehicle:
player_rect = pygame.Rect(self.player.x - self.player.width//2,
self.player.y - self.player.height//2,
self.player.width, self.player.height)
else:
player_rect = self.player.vehicle.get_rect()
for pedestrian in self.pedestrians[:]:
if player_rect.colliderect(pedestrian.get_rect()):
if self.player.commit_crime(1):
self.pedestrians.remove(pedestrian)
self.player.money += 50 # Reward for crime
# Check for collisions with police
for police in self.police[:]:
if player_rect.colliderect(police.get_rect()):
self.player.health -= 5
if self.player.health <= 0:
self.game_over = True
# Spawn police based on wanted level
if self.player.wanted_level > len(self.police):
self.spawn_police(self.player.wanted_level - len(self.police))
# Reduce wanted level over time
if current_time - self.player.last_crime_time > WANTED_LEVEL_COOLDOWN:
self.player.wanted_level = max(0, self.player.wanted_level - 0.002)
def draw(self, screen):
# Draw sky
screen.fill(SKY_BLUE)
# Draw grass areas
pygame.draw.rect(screen, GRASS_GREEN, (0, 0, SCREEN_WIDTH//2 - 50, SCREEN_HEIGHT//2 - 50))
pygame.draw.rect(screen, GRASS_GREEN, (SCREEN_WIDTH//2 + 50, 0, SCREEN_WIDTH//2 - 50, SCREEN_HEIGHT//2 - 50))
pygame.draw.rect(screen, GRASS_GREEN, (0, SCREEN_HEIGHT//2 + 50, SCREEN_WIDTH//2 - 50, SCREEN_HEIGHT//2 - 50))
pygame.draw.rect(screen, GRASS_GREEN, (SCREEN_WIDTH//2 + 50, SCREEN_HEIGHT//2 + 50, SCREEN_WIDTH//2 - 50, SCREEN_HEIGHT//2 - 50))
# Draw roads
for road in self.roads:
road.draw(screen)
# Draw sidewalks
pygame.draw.rect(screen, SIDEWALK_GRAY, (0, SCREEN_HEIGHT//2 - 70, SCREEN_WIDTH, 20))
pygame.draw.rect(screen, SIDEWALK_GRAY, (0, SCREEN_HEIGHT//2 + 50, SCREEN_WIDTH, 20))
pygame.draw.rect(screen, SIDEWALK_GRAY, (SCREEN_WIDTH//2 - 70, 0, 20, SCREEN_HEIGHT))
pygame.draw.rect(screen, SIDEWALK_GRAY, (SCREEN_WIDTH//2 + 50, 0, 20, SCREEN_HEIGHT))
# Draw buildings
for building in self.buildings:
building.draw(screen)
# Draw vehicles
for vehicle in self.vehicles:
vehicle.draw(screen)
# Draw pedestrians
for pedestrian in self.pedestrians:
pedestrian.draw(screen)
# Draw police
for police in self.police:
police.draw(screen)
# Draw player
self.player.draw(screen)
# Draw UI panel
self.draw_ui(screen)
# Draw game over or win message
if self.game_over:
self.draw_centered_text("GAME OVER - YOU WERE ARRESTED!", RED, 48)
elif self.game_won:
self.draw_centered_text(f"YOU SURVIVED! SCORE: ${self.player.money}", (50, 200, 50), 48)
def draw_ui(self, screen):
# Create semi-transparent background
ui_surface = pygame.Surface((SCREEN_WIDTH, 90), pygame.SRCALPHA)
ui_surface.fill(UI_BG)
screen.blit(ui_surface, (0, 0))
# Draw health bar
pygame.draw.rect(screen, (100, 100, 100), (20, 20, 204, 24))
pygame.draw.rect(screen, RED, (22, 22, 2 * self.player.health, 20))
health_text = self.font.render(f"HEALTH: {self.player.health}", True, WHITE)
screen.blit(health_text, (20, 50))
# Draw wanted level
pygame.draw.rect(screen, (100, 100, 100), (SCREEN_WIDTH - 224, 20, 204, 24))
pygame.draw.rect(screen, YELLOW, (SCREEN_WIDTH - 222, 22, 40 * self.player.wanted_level, 20))
wanted_text = self.font.render(f"WANTED LEVEL: {int(self.player.wanted_level)}", True, WHITE)
screen.blit(wanted_text, (SCREEN_WIDTH - 224, 50))
# Draw money and time
money_text = self.font.render(f"MONEY: ${self.player.money}", True, (50, 200, 50))
screen.blit(money_text, (SCREEN_WIDTH // 2 - 100, 20))
time_left = max(0, (self.game_duration - (pygame.time.get_ticks() - self.start_time)) // 1000)
time_text = self.font.render(f"TIME: {time_left}s", True, WHITE)
screen.blit(time_text, (SCREEN_WIDTH // 2 - 100, 50))
# Draw controls help
controls = [
"CONTROLS: WASD/Arrows - Move",
"E - Enter Vehicle",
"Q - Exit Vehicle"
]
for i, text in enumerate(controls):
ctrl_text = self.small_font.render(text, True, (200, 200, 200))
screen.blit(ctrl_text, (20, SCREEN_HEIGHT - 30 - i * 25))
def draw_centered_text(self, text, color, size):
font = pygame.font.SysFont(None, size)
text_surface = font.render(text, True, color)
text_rect = text_surface.get_rect(center=(SCREEN_WIDTH//2, SCREEN_HEIGHT//2))
screen.blit(text_surface, text_rect)
# Add restart instruction
restart_font = pygame.font.SysFont(None, 36)
restart_text = restart_font.render("Press R to restart", True, WHITE)
restart_rect = restart_text.get_rect(center=(SCREEN_WIDTH//2, SCREEN_HEIGHT//2 + 50))
screen.blit(restart_text, restart_rect)
# Create game instance
game = Game()
# Main game loop
clock = pygame.time.Clock()
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_r and (game.game_over or game.game_won):
game = Game() # Restart game
# Get pressed keys
keys = pygame.key.get_pressed()
# Update game state
game.update(keys)
# Draw everything
game.draw(screen)
# Update display
pygame.display.flip()
# Control game speed
clock.tick(FPS)
pygame.quit()
sys.exit()
Player Character:
City Environment:
Gameplay Mechanics:
Time-based Challenge:
Visual Elements:
To play, simply run the code. The goal is to survive for 2 minutes while avoiding police and building your criminal empire!