import pygame import sys # Initialize Pygame pygame.init() # Set up the screen screen_width = 800 screen_height = 600 screen = pygame.display.set_mode((screen_width, screen_height)) pygame.display.set_caption("Box Shooter") # Define colors WHITE = (255, 255, 255) BLACK = (0, 0, 0) PURPLE = (128, 0, 128) # Player attributes player_size = 50 player_x = screen_width // 2 player_y = screen_height // 2 player_speed = 0.5 # Normal speed for movement player_dash_speed = 15 # Increased speed for dash player_gravity = 2 # Stronger gravity affecting player player_jump_strength = 20 # Strength of player's jump player_on_ground = False # Flag indicating if player is on the ground player_velocity_x = 0 # Initial horizontal velocity component player_velocity_y = 0 # Initial vertical velocity component player_acceleration = 0.5 # Acceleration factor for player movement # Player hitbox attributes hitbox_size = player_size # Hitbox size matches player size # Dummy entity attributes dummy_size = 50 dummy_x = 100 dummy_y = 100 dummy_health = 100 dummy_gravity = 2 # Stronger gravity affecting dummy # Floor attributes floor_y = screen_height - 50 # Y-coordinate of the floor # Laser attributes laser_cooldown = 1000 # 1 second cooldown for laser damage (in milliseconds) last_laser_time = pygame.time.get_ticks() - laser_cooldown # Initialize shooting variable and laser endpoint shooting = False laser_end = None # Initialize dash variables dashing = False dash_end = None dash_velocity_x = 0 dash_velocity_y = 0 # Main game loop running = True while running: screen.fill(WHITE) # Handle events for event in pygame.event.get(): if event.type == pygame.QUIT: running = False elif event.type == pygame.MOUSEBUTTONDOWN: if event.button == 1: # Check laser cooldown current_time = pygame.time.get_ticks() if current_time - last_laser_time >= laser_cooldown: laser_end = pygame.mouse.get_pos() last_laser_time = current_time # Draw laser if shooting if laser_end: start_pos = (player_x + player_size // 2, player_y + player_size // 2) end_pos = laser_end pygame.draw.line(screen, PURPLE, start_pos, end_pos, 3) # Dash player to the endpoint of the laser if laser_end and not dashing: start_pos = (player_x + player_size // 2, player_y + player_size // 2) end_pos = laser_end dx = end_pos[0] - start_pos[0] dy = end_pos[1] - start_pos[1] dist = ((dx ** 2) + (dy ** 2)) ** 0.5 if dist > player_speed: # Ensure player doesn't overshoot the endpoint norm_dx = dx / dist norm_dy = dy / dist player_velocity_x = norm_dx * player_dash_speed player_velocity_y = norm_dy * player_dash_speed dash_velocity_x = norm_dx * player_dash_speed dash_velocity_y = norm_dy * player_dash_speed player_x += int(norm_dx * player_dash_speed) player_y += int(norm_dy * player_dash_speed) dashing = True else: player_velocity_x = norm_dx * dist player_velocity_y = norm_dy * dist player_x, player_y = end_pos[0] - player_size // 2, end_pos[1] - player_size // 2 # Center player at end of dash laser_end = None # Apply gravity to player if not player_on_ground: if not dashing: # Apply gravity only if not dashing player_velocity_y += player_gravity player_y += player_velocity_y # Check if player is on the ground if player_y + player_size >= floor_y: player_y = floor_y - player_size player_on_ground = True else: player_on_ground = False # Handle player movement with acceleration keys = pygame.key.get_pressed() if keys[pygame.K_w] and player_on_ground: # Jump if on the ground player_velocity_y = -player_jump_strength player_on_ground = False if keys[pygame.K_a]: # Move left player_velocity_x -= player_acceleration elif keys[pygame.K_d]: # Move right player_velocity_x += player_acceleration else: # Apply deceleration if no keys are pressed if player_velocity_x > 0: player_velocity_x -= player_acceleration elif player_velocity_x < 0: player_velocity_x += player_acceleration # Limit the player's horizontal velocity within a certain range player_velocity_x = max(-player_speed, min(player_speed, player_velocity_x)) # Update player position based on velocity player_x += player_velocity_x # Draw player pygame.draw.rect(screen, BLACK, (player_x, player_y, player_size, player_size)) # Draw player hitbox pygame.draw.rect(screen, PURPLE, (player_x + (player_size - hitbox_size) // 2, player_y + (player_size - hitbox_size) // 2, hitbox_size, hitbox_size)) # Apply gravity to dummy if dummy_y + dummy_size < floor_y: dummy_y += dummy_gravity # Draw dummy entity pygame.draw.rect(screen, BLACK, (dummy_x, dummy_y, dummy_size, dummy_size)) # Draw health bar for dummy entity pygame.draw.rect(screen, PURPLE, (dummy_x, dummy_y - 10, dummy_size, 5)) pygame.draw.rect(screen, BLACK, (dummy_x + dummy_size * (1 - dummy_health / 100), dummy_y - 10, dummy_size * (dummy_health / 100), 5)) # Update the display pygame.display.update() # Cap the frame rate pygame.time.Clock().tick(60) pygame.quit() sys.exit()