MichaelPaulukonis / python_experiments_anthony

0 stars 0 forks source link

space #2

Open MichaelPaulukonis opened 2 months ago

MichaelPaulukonis commented 2 months ago
import pygame
import sys

# Initialize Pygame
pygame.init()

# Screen dimensions
SCREEN_WIDTH, SCREEN_HEIGHT = 800, 600
screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))

# Colors
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)

# Game variables
rocket_pos = SCREEN_HEIGHT - 50
rocket_vel = 0
rocket_acc = 0
gravity = 0.1
thrust = -0.3  # Negative because the screen's y-axis is inverted
fuel = 100
ground_height = SCREEN_HEIGHT - 40
orbit_height = 100
crash_speed = -2

# Main game loop
clock = pygame.time.Clock()
running = True
while running:
    screen.fill(BLACK)

    rocket_acc = gravity

    # Event handling
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False

    # Thrust control
    keys = pygame.key.get_pressed()
    if keys[pygame.K_SPACE] and fuel > 0:
        rocket_acc += thrust
        fuel -= 1

    # Update physics
    rocket_vel += rocket_acc
    rocket_pos += rocket_vel

    # Check for win/lose conditions
    if rocket_pos >= ground_height:
        if rocket_vel > crash_speed:  # Check if velocity is positive and greater than crash speed
            print(f"Crash! Velocity: {rocket_vel} Height: {rocket_pos} Fuel: {fuel}")
            # running = False
        elif fuel <= 0 and rocket_vel <= crash_speed:  # Added condition for safe landing
            print("Safe landing!")
            # running = False
        rocket_pos = ground_height  # Reset position to ground
        rocket_vel = 0  # Reset velocity to 0 to stop moving

    if rocket_pos <= orbit_height:
        print("Reached orbit!")
        # running = False

    # Draw rocket
    pygame.draw.rect(screen, WHITE, (SCREEN_WIDTH // 2 - 5, int(rocket_pos) - 10, 10, 20))

    # Update display
    pygame.display.flip()

    # Cap the frame rate
    clock.tick(60)

pygame.quit()
sys.exit()
MichaelPaulukonis commented 2 months ago

I need a space game in python, using pygame. The player controls the accelleration of a rocket (there's no changing of direction) versus the effects of gravity and a limited fuel supply. The rocket starts on the ground with 0 acceleration. While the user presses the spacebar, thrust is applied (accelleration increases) and fuel is used. Gravity is a continual force slowing the rocket, so when no thrust is applied the rocket slows or reverses direction. If it hits the ground at too high of a speed, the rocket will crash. The game is won if the rocket "reaches orbit" (travels high enough). A secondary win-state is If the rocket goes into the air, but lands again without crashing.

To create a simple space game in Python using Pygame where the player controls the acceleration of a rocket, you'll need to follow these steps:

Initialize Pygame and Create a Window Define Game Variables Rocket position, velocity, and acceleration Gravity and thrust constants Fuel amount Ground and orbit heights Game Loop Process input events (spacebar for thrust) Update rocket physics Check for win/lose conditions Draw everything to the screen Win/Lose Conditions Crash: if the rocket hits the ground with high velocity Reach orbit: if the rocket exceeds a certain height Safe landing: if the rocket lands with low velocity

[there were some issues, fix applied in the above, but explained by the below)

To correct the issue with the acceleration starting without keypresses and to properly apply gravity, you need to adjust how gravity and thrust are applied in your game logic. Gravity should always be applied to the rocket to simulate the constant pull towards the ground, while thrust should only be applied when the spacebar is pressed. Since the screen's y-axis is inverted, gravity should be a positive value (to pull the rocket down), and thrust should be negative (to push the rocket up).

Key Changes: Gravity is now a positive value to reflect its direction towards the bottom of the screen. Acceleration (rocket_acc) starts with gravity to ensure it's always applied. Thrust is added to gravity when the spacebar is pressed and there's fuel. This allows the rocket to counteract gravity and move upwards. Velocity check for crashing is adjusted to rocket_vel > crash_speed because a positive velocity (downwards) greater than the crash speed threshold indicates a crash. Safe landing condition now explicitly checks if the fuel is depleted and the landing speed is safe (rocket_vel <= crash_speed). This adjustment ensures that gravity correctly pulls the rocket down at all times, and thrust only affects the rocket when the player presses the spacebar and has fuel.

NOTE: ground height is still a weird condition, may need to be more explicit in how it should work (human readability, not code convenience). Also, display values have been omitted (were not in the request tsk tsk).