just prompted a coding GPT with three prompts. what came out was a surprisingly playable game. and it really only took 2 prompts, the ship was just pointed in the wrong direction.
- “write a realistic atari-style asteroids game”
- “next iteration: when an asteroid is hit by a bullet, it should break into 4 smaller asteroids. the smaller asteroids are removed when they are hit by a bullet.”
- “the initial angle of the ship direction and the bullets direction needs to be rotated by -90 degrees. not visually, just directionally.”
the gpt doesn’t even use code interpreter. wtf? i’m still a little bit stunned. maybe i just wasn’t aware of ChatGPT’s capability vis-a-vis atari. maybe i’m easily impressed.
this was the full block of code it generated:
import pygame
import math
import random
# Initialize Pygame
pygame.init()
# Screen dimensions
SCREEN_WIDTH, SCREEN_HEIGHT = 800, 600
screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
# Colors
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
# Frame rate
FPS = 60
clock = pygame.time.Clock()
# Spaceship properties
spaceship_img = pygame.Surface((40, 30), pygame.SRCALPHA)
pygame.draw.polygon(spaceship_img, WHITE, [(20, 0), (0, 30), (40, 30)])
spaceship_pos = [SCREEN_WIDTH // 2, SCREEN_HEIGHT // 2]
spaceship_vel = [0, 0]
spaceship_angle = 0 # The visual angle of the spaceship
spaceship_speed = 0.1
rotation_speed = 5
# Bullets properties
bullets = []
bullet_speed = 5
bullet_lifetime = 1200 # How long a bullet lasts before disappearing
# Asteroid properties
asteroids = []
for _ in range(5): # Number of initial asteroids
asteroid_pos = [random.randrange(SCREEN_WIDTH), random.randrange(SCREEN_HEIGHT)]
asteroid_vel = [random.random() * 2 - 1, random.random() * 2 - 1]
asteroid_size = random.randint(40, 60) # Larger initial size
asteroids.append([asteroid_pos, asteroid_vel, asteroid_size])
def detect_collision(obj1_pos, obj1_size, obj2_pos, obj2_size):
distance = math.sqrt((obj1_pos[0] - obj2_pos[0]) ** 2 + (obj1_pos[1] - obj2_pos[1]) ** 2)
return distance < obj1_size + obj2_size
def break_asteroid(asteroid):
if asteroid[2] > 15: # Only break if larger than the smallest size
for _ in range(4): # Create 4 smaller asteroids
new_size = asteroid[2] // 2
new_pos = asteroid[0][:]
new_vel = [random.random() * 3 - 1.5, random.random() * 3 - 1.5]
asteroids.append([new_pos, new_vel, new_size])
# Adjust angle calculation for movement and bullets
def adjust_angle_for_direction(angle):
# Adjust the angle by -90 degrees for directional calculations
return angle - 90
# Main game loop
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_SPACE:
# Adjust bullet angle at the time of firing
adjusted_angle = adjust_angle_for_direction(spaceship_angle)
bullets.append([spaceship_pos[:], math.radians(adjusted_angle), bullet_lifetime])
# Key press events
keys = pygame.key.get_pressed()
if keys[pygame.K_LEFT]:
spaceship_angle -= rotation_speed
if keys[pygame.K_RIGHT]:
spaceship_angle += rotation_speed
if keys[pygame.K_UP]:
# Adjust spaceship velocity using the adjusted angle
adjusted_angle = adjust_angle_for_direction(spaceship_angle)
spaceship_vel[0] += math.cos(math.radians(adjusted_angle)) * spaceship_speed
spaceship_vel[1] += math.sin(math.radians(adjusted_angle)) * spaceship_speed
# Update spaceship position
spaceship_pos[0] += spaceship_vel[0]
spaceship_pos[1] += spaceship_vel[1]
# Screen wrapping for spaceship
spaceship_pos[0] %= SCREEN_WIDTH
spaceship_pos[1] %= SCREEN_HEIGHT
# Update and draw asteroids
screen.fill(BLACK)
asteroids_to_remove = []
for asteroid in asteroids:
asteroid[0][0] += asteroid[1][0]
asteroid[0][1] += asteroid[1][1]
# Screen wrapping for asteroids
asteroid[0][0] %= SCREEN_WIDTH
asteroid[0][1] %= SCREEN_HEIGHT
if detect_collision(spaceship_pos, 15, asteroid[0], asteroid[2]):
print("Game Over!")
running = False
pygame.draw.circle(screen, WHITE, asteroid[0], asteroid[2])
# Update and draw bullets
bullets_to_remove = []
for bullet in bullets:
bullet[0][0] += math.cos(bullet[1]) * bullet_speed
bullet[0][1] += math.sin(bullet[1]) * bullet_speed
bullet[2] -= 1
if bullet[2] <= 0:
bullets_to_remove.append(bullet)
continue
pygame.draw.circle(screen, WHITE, (int(bullet[0][0]), int(bullet[0][1])), 2)
for asteroid in asteroids:
if detect_collision(bullet[0], 2, asteroid[0], asteroid[2]):
if asteroid[2] > 15:
break_asteroid(asteroid)
asteroids_to_remove.append(asteroid)
bullets_to_remove.append(bullet)
break
for bullet in bullets_to_remove:
bullets.remove(bullet)
for asteroid in asteroids_to_remove:
asteroids.remove(asteroid)
# Drawing spaceship
rotated_spaceship = pygame.transform.rotate(spaceship_img, -spaceship_angle)
new_rect = rotated_spaceship.get_rect(center=spaceship_img.get_rect(topleft=spaceship_pos).center)
screen.blit(rotated_spaceship, new_rect.topleft)
pygame.display.flip()
clock.tick(FPS)
pygame.quit()