Files
Asteroids/asteroids.py

250 lines
8.6 KiB
Python

import pygame
import sys
import random
import math
# Initialize Pygame
pygame.init()
# Set up the display
width, height = 800, 600
screen = pygame.display.set_mode((width, height), pygame.RESIZABLE)
pygame.display.set_caption("Asteroids")
# Colors
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
# Ship
ship_size = 20
ship_pos = [float(width // 2), float(height // 2)]
ship_angle = 0
ship_speed = 0
# Bullets
bullets = []
bullet_speed = 10
# Asteroids
asteroids = []
asteroid_count = 10
# Particles
particles = []
# Score
score = 0
font = pygame.font.Font(None, 36)
# Game over flag
game_over = False
def create_asteroid():
size = random.randint(20, 50)
x = random.choice([random.randint(0, width // 4), random.randint(3 * width // 4, width)])
y = random.choice([random.randint(0, height // 4), random.randint(3 * height // 4, height)])
speed = random.uniform(0.5, 2)
angle = random.uniform(0, 2 * math.pi)
sides = random.choice([5, 6])
return {"pos": [float(x), float(y)], "size": size, "speed": speed, "angle": angle, "sides": sides}
def create_explosion(x, y, color=WHITE):
for _ in range(20):
angle = random.uniform(0, 2 * math.pi)
speed = random.uniform(1, 5)
size = random.randint(1, 4)
lifetime = random.randint(20, 40)
particles.append({
"pos": [float(x), float(y)],
"vel": [math.cos(angle) * speed, math.sin(angle) * speed],
"size": size,
"lifetime": lifetime,
"max_lifetime": lifetime,
"color": color
})
def create_thrust_particles():
if ship_speed > 0:
for _ in range(3):
angle = ship_angle + math.pi + random.uniform(-0.5, 0.5)
speed = random.uniform(2, 4)
particles.append({
"pos": [ship_pos[0] - math.cos(ship_angle) * ship_size,
ship_pos[1] + math.sin(ship_angle) * ship_size],
"vel": [math.cos(angle) * speed, math.sin(angle) * speed],
"size": random.randint(2, 3),
"lifetime": random.randint(10, 20),
"max_lifetime": random.randint(10, 20),
"color": (255, 165, 0)
})
def draw_polygon(surface, color, pos, size, sides, angle=0):
points = []
for i in range(sides):
angle_point = angle + (2 * math.pi * i / sides)
point = (pos[0] + size * math.cos(angle_point),
pos[1] + size * math.sin(angle_point))
points.append(point)
pygame.draw.polygon(surface, color, points, 2)
# Create initial asteroids
for _ in range(asteroid_count):
asteroids.append(create_asteroid())
# Game loop
clock = pygame.time.Clock()
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
elif event.type == pygame.VIDEORESIZE:
width, height = event.w, event.h
screen = pygame.display.set_mode((width, height), pygame.RESIZABLE)
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_q or event.key == pygame.K_ESCAPE:
pygame.quit()
sys.exit()
elif event.key == pygame.K_SPACE and not game_over:
bullets.append([ship_pos[0], ship_pos[1], ship_angle])
if not game_over:
# Ship movement
keys = pygame.key.get_pressed()
if keys[pygame.K_LEFT] or keys[pygame.K_a]:
ship_angle += 0.1
if keys[pygame.K_RIGHT] or keys[pygame.K_d]:
ship_angle -= 0.1
if keys[pygame.K_UP] or keys[pygame.K_w]:
ship_speed = min(ship_speed + 0.1, 5)
elif keys[pygame.K_s]:
ship_speed = max(ship_speed - 0.15, 0)
else:
ship_speed = max(ship_speed - 0.05, 0)
ship_pos[0] += math.cos(ship_angle) * ship_speed
ship_pos[1] -= math.sin(ship_angle) * ship_speed
# Wrap ship around screen
if ship_pos[0] < 0:
ship_pos[0] += width
elif ship_pos[0] > width:
ship_pos[0] -= width
if ship_pos[1] < 0:
ship_pos[1] += height
elif ship_pos[1] > height:
ship_pos[1] -= height
# Update bullets
for bullet in bullets[:]:
bullet[0] += math.cos(bullet[2]) * bullet_speed
bullet[1] -= math.sin(bullet[2]) * bullet_speed
if bullet[0] < 0 or bullet[0] > width or bullet[1] < 0 or bullet[1] > height:
bullets.remove(bullet)
# Update particles
for particle in particles[:]:
particle["pos"][0] += particle["vel"][0]
particle["pos"][1] += particle["vel"][1]
particle["lifetime"] -= 1
if particle["lifetime"] <= 0:
particles.remove(particle)
# Create thrust particles
create_thrust_particles()
# Update asteroids
for asteroid in asteroids[:]:
asteroid["pos"][0] += math.cos(asteroid["angle"]) * asteroid["speed"]
asteroid["pos"][1] += math.sin(asteroid["angle"]) * asteroid["speed"]
# Wrap asteroids around screen
if asteroid["pos"][0] < 0:
asteroid["pos"][0] += width
elif asteroid["pos"][0] > width:
asteroid["pos"][0] -= width
if asteroid["pos"][1] < 0:
asteroid["pos"][1] += height
elif asteroid["pos"][1] > height:
asteroid["pos"][1] -= height
# Check collision with ship
if (abs(asteroid["pos"][0] - ship_pos[0]) < asteroid["size"] and
abs(asteroid["pos"][1] - ship_pos[1]) < asteroid["size"]):
create_explosion(ship_pos[0], ship_pos[1], (255, 0, 0))
game_over = True
# Check collision with bullets
for bullet in bullets[:]:
if (abs(asteroid["pos"][0] - bullet[0]) < asteroid["size"] and
abs(asteroid["pos"][1] - bullet[1]) < asteroid["size"]):
create_explosion(asteroid["pos"][0], asteroid["pos"][1])
asteroids.remove(asteroid)
bullets.remove(bullet)
score += 1
asteroids.append(create_asteroid())
break
# Draw everything
screen.fill(BLACK)
if not game_over:
# Draw ship with better design
# Main body
points = [
(ship_pos[0] + math.cos(ship_angle) * ship_size,
ship_pos[1] - math.sin(ship_angle) * ship_size),
(ship_pos[0] + math.cos(ship_angle + 2.5) * ship_size * 0.7,
ship_pos[1] - math.sin(ship_angle + 2.5) * ship_size * 0.7),
(ship_pos[0] + math.cos(ship_angle + 3.14) * ship_size * 0.3,
ship_pos[1] - math.sin(ship_angle + 3.14) * ship_size * 0.3),
(ship_pos[0] + math.cos(ship_angle - 2.5) * ship_size * 0.7,
ship_pos[1] - math.sin(ship_angle - 2.5) * ship_size * 0.7),
]
pygame.draw.polygon(screen, WHITE, points, 2)
# Cockpit
cockpit_pos = (
ship_pos[0] + math.cos(ship_angle) * ship_size * 0.3,
ship_pos[1] - math.sin(ship_angle) * ship_size * 0.3
)
pygame.draw.circle(screen, WHITE, (int(cockpit_pos[0]), int(cockpit_pos[1])), 3)
# Engine glow when thrusting
if ship_speed > 0:
engine_pos = (
ship_pos[0] - math.cos(ship_angle) * ship_size * 0.8,
ship_pos[1] + math.sin(ship_angle) * ship_size * 0.8
)
pygame.draw.circle(screen, (255, 165, 0), (int(engine_pos[0]), int(engine_pos[1])), 4)
# Draw bullets
for bullet in bullets:
pygame.draw.circle(screen, WHITE, (int(bullet[0]), int(bullet[1])), 2)
# Draw particles
for particle in particles:
alpha = particle["lifetime"] / particle["max_lifetime"]
color = tuple(max(0, min(255, int(c * alpha))) for c in particle["color"])
pygame.draw.circle(screen, color,
(int(particle["pos"][0]), int(particle["pos"][1])),
particle["size"])
# Draw asteroids
for asteroid in asteroids:
draw_polygon(screen, WHITE, asteroid["pos"], asteroid["size"], asteroid["sides"])
# Draw score
score_text = font.render(f"Score: {score}", True, WHITE)
screen.blit(score_text, (10, 10))
if game_over:
game_over_text = font.render("GAME OVER", True, WHITE)
screen.blit(game_over_text, (width // 2 - 70, height // 2 - 18))
pygame.display.flip()
clock.tick(60)