import pygame import math import time # Init pygame.init() WIDTH, HEIGHT = 1900, 1000 screen = pygame.display.set_mode((WIDTH, HEIGHT), pygame.FULLSCREEN) clock = pygame.time.Clock() font = pygame.font.SysFont(None, 24) # Constants THRUST = 0.2 ROTATION_SPEED = 2 FUEL_USAGE = 0.1 BULLET_SPEED = 8 bullet_cooldown = 0.2 FRICTION = 0.98 EXPLOSION_RADIUS = 50 # Radius of the explosion for collision detection # Scaling factor SCALING_FACTOR = 1.5 # Scale ships and bullets by 1.5 times # Shared spawn Y level spawn_y = HEIGHT - 500 # Rocket state (Ship 1) x, y = WIDTH - 300, spawn_y vx, vy = 0, 0 angle = 90 fuel = 10000000 spinning = False spin_angle_remaining = 0 spin_direction = -1 thrusting = False ship1_bullets = [] last_bullet_time_ship1 = 0 # Ship 2 ship2_x, ship2_y = 300, spawn_y ship2_vx, ship2_vy = 0, 0 ship2_angle = 90 ship2_fuel = 10000000 ship2_bullets = [] ship2_thrusting = False last_bullet_time_ship2 = 0 # Points tracker (Ship 1 kills and Ship 2 kills) ship1_points = 0 ship2_kills = 0 # Now tracking kills for Ship 2 running = True while running: screen.fill((0, 0, 0)) dt = clock.tick(60) / 1000 keys = pygame.key.get_pressed() for event in pygame.event.get(): if event.type == pygame.QUIT: running = False if keys[pygame.K_ESCAPE]: running = False # Ship 1 spin logic if not spinning: if keys[pygame.K_RALT]: spinning = True spin_angle_remaining = 180 spin_direction = -1 elif keys[pygame.K_LALT]: spinning = True spin_angle_remaining = 180 spin_direction = 1 if spinning: spin_step = ROTATION_SPEED * 3.75 if spin_angle_remaining > 0: angle += spin_step * spin_direction spin_angle_remaining -= spin_step else: spinning = False # Ship 1 controls if not spinning: if keys[pygame.K_LCTRL]: if keys[pygame.K_LEFT]: angle += ROTATION_SPEED * 2.5 if keys[pygame.K_RIGHT]: angle -= ROTATION_SPEED * 2.5 else: if keys[pygame.K_LEFT]: angle += ROTATION_SPEED if keys[pygame.K_RIGHT]: angle -= ROTATION_SPEED # Ship 1 thrust if keys[pygame.K_UP] and fuel > 0: rad = math.radians(angle) ax = math.cos(rad) * THRUST ay = -math.sin(rad) * THRUST vx += ax vy += ay fuel -= FUEL_USAGE thrusting = True else: thrusting = False # Apply friction (if no thrust) if not thrusting: vx *= FRICTION vy *= FRICTION # Slow down when DOWN key is pressed (Ship 1) if keys[pygame.K_DOWN]: vx *= 0.985 # Apply a slowdown factor vy *= 0.985 # Apply a slowdown factor x += vx y += vy # Ship 1 Bounce Logic (Top, Bottom, Left, Right) if x < 0: x = 0 vx *= -1 elif x > WIDTH: x = WIDTH vx *= -1 if y < 0: y = 0 vy *= -1 elif y > HEIGHT: y = HEIGHT vy *= -1 # Ship 1 shooting current_time = time.time() if keys[pygame.K_RETURN] and (current_time - last_bullet_time_ship1) > bullet_cooldown: rad = math.radians(angle) bullet_x = x + math.cos(rad) * 15 # Scale the bullet bullet_y = y - math.sin(rad) * 15 # Scale the bullet bullet_dx = math.cos(rad) * BULLET_SPEED bullet_dy = -math.sin(rad) * BULLET_SPEED ship1_bullets.append([bullet_x, bullet_y, bullet_dx, bullet_dy]) last_bullet_time_ship1 = current_time for bullet in ship1_bullets: bullet[0] += bullet[2] bullet[1] += bullet[3] ship1_bullets = [b for b in ship1_bullets if 0 < b[0] < WIDTH and 0 < b[1] < HEIGHT] # --- Ship 2 controls --- if keys[pygame.K_a]: ship2_angle += ROTATION_SPEED if keys[pygame.K_d]: ship2_angle -= ROTATION_SPEED # --- Ship 2 thrust --- if keys[pygame.K_w] and ship2_fuel > 0: rad2 = math.radians(ship2_angle) ax2 = math.cos(rad2) * THRUST ay2 = -math.sin(rad2) * THRUST ship2_vx += ax2 ship2_vy += ay2 ship2_fuel -= FUEL_USAGE ship2_thrusting = True else: ship2_thrusting = False # Apply friction (if no thrust for ship 2) if not ship2_thrusting: ship2_vx *= FRICTION ship2_vy *= FRICTION # Slow down when S key is pressed (Ship 2) if keys[pygame.K_s]: ship2_vx *= 0.985 # Apply a slowdown factor ship2_vy *= 0.985 # Apply a slowdown factor ship2_x += ship2_vx ship2_y += ship2_vy # Ship 2 Bounce Logic (Top, Bottom, Left, Right) if ship2_x < 0: ship2_x = 0 ship2_vx *= -1 elif ship2_x > WIDTH: ship2_x = WIDTH ship2_vx *= -1 if ship2_y < 0: ship2_y = 0 ship2_vy *= -1 elif ship2_y > HEIGHT: ship2_y = HEIGHT ship2_vy *= -1 # --- Ship 2 shooting --- if keys[pygame.K_SPACE] and (current_time - last_bullet_time_ship2) > bullet_cooldown: rad2 = math.radians(ship2_angle) bullet_x = ship2_x + math.cos(rad2) * 15 # Scale the bullet bullet_y = ship2_y - math.sin(rad2) * 15 # Scale the bullet bullet_dx = math.cos(rad2) * BULLET_SPEED bullet_dy = -math.sin(rad2) * BULLET_SPEED ship2_bullets.append([bullet_x, bullet_y, bullet_dx, bullet_dy]) last_bullet_time_ship2 = current_time for bullet in ship2_bullets: bullet[0] += bullet[2] bullet[1] += bullet[3] ship2_bullets = [b for b in ship2_bullets if 0 < b[0] < WIDTH and 0 < b[1] < HEIGHT] # --- Collision check (Ship 1 vs Ship 2) --- def check_collision(x1, y1, points1, x2, y2, points2): # Check if any point of ship1's polygon intersects ship2's polygon for p1 in points1: for p2 in points2: if math.sqrt((p1[0] - p2[0])**2 + (p1[1] - p2[1])**2) < EXPLOSION_RADIUS: return True return False # Define the polygons of the ships rocket_points1 = [ (x + math.cos(math.radians(angle)) * 15, y - math.sin(math.radians(angle)) * 15), (x + math.cos(math.radians(angle + 140)) * 15, y - math.sin(math.radians(angle + 140)) * 15), (x + math.cos(math.radians(angle - 140)) * 15, y - math.sin(math.radians(angle - 140)) * 15) ] rocket_points2 = [ (ship2_x + math.cos(math.radians(ship2_angle)) * 15, ship2_y - math.sin(math.radians(ship2_angle)) * 15), (ship2_x + math.cos(math.radians(ship2_angle + 140)) * 15, ship2_y - math.sin(math.radians(ship2_angle + 140)) * 15), (ship2_x + math.cos(math.radians(ship2_angle - 140)) * 15, ship2_y - math.sin(math.radians(ship2_angle - 140)) * 15) ] # Check for collision between ships if check_collision(x, y, rocket_points1, ship2_x, ship2_y, rocket_points2): if math.sqrt(vx**2 + vy**2) > math.sqrt(ship2_vx**2 + ship2_vy**2): # Ship 1 explodes (reset) x, y, vx, vy = WIDTH - 300, spawn_y, 0, 0 fuel = 10000000 # Reset fuel ship2_kills += 1 # Ship 2 gets a kill print("Ship 1 exploded due to collision with Ship 2!") else: # Ship 2 explodes (reset) ship2_x, ship2_y, ship2_vx, ship2_vy = 300, spawn_y, 0, 0 ship2_fuel = 10000000 # Reset fuel ship1_points += 1 # Ship 1 gets a kill print("Ship 2 exploded due to collision with Ship 1!") # --- Bullet-Ship Collision (Ship 1 hit by Ship 2's bullet) --- for bullet in ship2_bullets: distance = math.sqrt((bullet[0] - x)**2 + (bullet[1] - y)**2) if distance < EXPLOSION_RADIUS: # Ship 1 hit by ship 2's bullet, reset ship 1 x, y, vx, vy = WIDTH - 300, spawn_y, 0, 0 fuel = 10000000 # Reset fuel ship2_bullets.remove(bullet) ship2_kills += 1 # Ship 2 gets a kill print("Ship 1 hit by Ship 2's bullet! Ship 1 exploded!") # --- Bullet-Ship Collision (Ship 2 hit by Ship 1's bullet) --- for bullet in ship1_bullets: distance = math.sqrt((bullet[0] - ship2_x)**2 + (bullet[1] - ship2_y)**2) if distance < EXPLOSION_RADIUS: # Ship 2 hit by ship 1's bullet, reset ship 2 ship2_x, ship2_y, ship2_vx, ship2_vy = 300, spawn_y, 0, 0 ship2_fuel = 10000000 # Reset ship 2's fuel ship1_bullets.remove(bullet) ship1_points += 1 # Ship 1 gets a kill print("Ship 2 hit by Ship 1's bullet! Ship 2 exploded!") # --- Draw Ship 1 --- pygame.draw.polygon(screen, (200, 200, 255), rocket_points1) if thrusting: flame_x = x - math.cos(math.radians(angle)) * 22 flame_y = y + math.sin(math.radians(angle)) * 22 pygame.draw.circle(screen, (255, 100, 0), (int(flame_x), int(flame_y)), 10) for bullet in ship1_bullets: pygame.draw.circle(screen, (255, 255, 255), (int(bullet[0]), int(bullet[1])), 6) # --- Draw Ship 2 (Yellow) --- pygame.draw.polygon(screen, (255, 255, 0), rocket_points2) if ship2_thrusting: flame_x2 = ship2_x - math.cos(math.radians(ship2_angle)) * 22 flame_y2 = ship2_y + math.sin(math.radians(ship2_angle)) * 22 pygame.draw.circle(screen, (255, 100, 0), (int(flame_x2), int(flame_y2)), 10) for bullet in ship2_bullets: pygame.draw.circle(screen, (255, 255, 0), (int(bullet[0]), int(bullet[1])), 6) # --- Scoreboard (Ship 2's points in yellow) --- score_text = font.render(f"Ship 1 Kills: {ship1_points} Ship 2 Kills: {ship2_kills}", True, (255, 255, 0)) screen.blit(score_text, (WIDTH // 2 - score_text.get_width() // 2, 10)) pygame.display.flip() pygame.quit()