Create a basic pygame script where you move around a block. It should have gravity and a…
- User
Create a basic pygame script where you move around a block. It should have gravity and a ground to stand on.
- Assistant
Sure! Here's a basic pygame script that allows you to move a block on the screen with gravity and a ground to stand on:
import pygame import sys # Initialize pygame pygame.init() # Set up the display width, height = 640, 480 screen = pygame.display.set_mode((width, height)) pygame.display.set_caption("Moving Block") # Set up the block block_size = 50 block_color = (255, 0, 0) # Red block_x = width // 2 - block_size // 2 block_y = height // 2 - block_size // 2 block_speed = 5 is_jumping = False # Set up gravity gravity = 0.5 vertical_velocity = 0 # Set up the ground ground = height - block_size # Run the game loop while True: for event in pygame.event.get(): if event.type == pygame.QUIT: pygame.quit() sys.exit() # Handle keyboard events keys = pygame.key.get_pressed() if keys[pygame.K_LEFT] and block_x > 0: block_x -= block_speed if keys[pygame.K_RIGHT] and block_x < width - block_size: block_x += block_speed if keys[pygame.K_SPACE] and not is_jumping: is_jumping = True vertical_velocity = -10 # Perform the jump # Apply gravity vertical_velocity += gravity block_y += vertical_velocity # Check if the block hits the ground if block_y > ground - block_size: block_y = ground - block_size is_jumping = False # Clear the screen screen.fill((0, 0, 0)) # Draw the block pygame.draw.rect(screen, block_color, (block_x, block_y, block_size, block_size)) # Update the display pygame.display.flip() - User
please slow down the movement speed a bit
- Assistant
Certainly! To slow down the movement speed of the block, you can modify the
block_speedvariable. Here's the modified code with a slower movement speed:import pygame import sys # Initialize pygame pygame.init() # Set up the display width, height = 640, 480 screen = pygame.display.set_mode((width, height)) pygame.display.set_caption("Moving Block") # Set up the block block_size = 50 block_color = (255, 0, 0) # Red block_x = width // 2 - block_size // 2 block_y = height // 2 - block_size // 2 block_speed = 2 # Slower movement speed is_jumping = False # Set up gravity gravity = 0.5 vertical_velocity = 0 # Set up the ground ground = height - block_size # Run the game loop while True: for event in pygame.event.get(): if event.type == pygame.QUIT: pygame.quit() sys.exit() # Handle keyboard events keys = pygame.key.get_pressed() if keys[pygame.K_LEFT] and block_x > 0: block_x -= block_speed if keys[pygame.K_RIGHT] and block_x < width - block_size: block_x += block_speed if keys[pygame.K_SPACE] and not is_jumping: is_jumping = True vertical_velocity = -10 # Perform the jump # Apply gravity vertical_velocity += gravity block_y += vertical_velocity # Check if the block hits the ground if block_y > ground - block_size: block_y = ground - block_size is_jumping = False # Clear the screen screen.fill((0, 0, 0)) # Draw the block pygame.draw.rect(screen, block_color, (block_x, block_y, block_size, block_size)) # Update the display pygame.display.flip() - User
I also want it to use WASD, color the ground green, and color the background light blue.
- Assistant
Sure! Here's the modified code that uses WASD for movement, colors the ground green, and sets the background to light blue:
import pygame import sys # Initialize pygame pygame.init() # Set up the display width, height = 640, 480 screen = pygame.display.set_mode((width, height)) pygame.display.set_caption("Moving Block") # Set up the block block_size = 50 block_color = (255, 0, 0) # Red block_x = width // 2 - block_size // 2 block_y = height // 2 - block_size // 2 block_speed = 2 # Slower movement speed is_jumping = False # Set up gravity gravity = 0.5 vertical_velocity = 0 # Set up the ground and colors ground_color = (0, 255, 0) # Green ground_height = 20 ground = pygame.Rect(0, height - ground_height, width, ground_height) background_color = (173, 216, 230) # Light blue # Run the game loop while True: for event in pygame.event.get(): if event.type == pygame.QUIT: pygame.quit() sys.exit() # Handle keyboard events keys = pygame.key.get_pressed() if keys[pygame.K_a] and block_x > 0: # Move left block_x -= block_speed if keys[pygame.K_d] and block_x < width - block_size: # Move right block_x += block_speed if keys[pygame.K_w] and not is_jumping: # Jump is_jumping = True vertical_velocity = -10 # Apply gravity vertical_velocity += gravity block_y += vertical_velocity # Check if the block hits the ground if block_y > height - ground_height - block_size: block_y = height - ground_height - block_size is_jumping = False # Clear the screen screen.fill(background_color) # Draw the ground pygame.draw.rect(screen, ground_color, ground) # Draw the block pygame.draw.rect(screen, block_color, (block_x, block_y, block_size, block_size)) # Update the display pygame.display.flip() - User
Can you also make it so that it can go fullscreen?
- Assistant
- User
Can you add so that if S is pressed the cube flattens to half its original size. Kinda like crouching?
- Assistant
Certainly! Here's the modified code that allows the cube to "crouch" or flatten to half its original size when the 'S' key is pressed:
- User
Please also make it hide the cursor if on full screen.
- Assistant
- User
The block should flatten, not shrink. it should be half of its original height.
- Assistant
Apologies for the confusion. I misunderstood your requirement. Here's the modified code that flattens the block to half its original height when the 'S' key is pressed:
import pygame import sys # Initialize pygame pygame.init() # Set up the display width, height = 640, 480 screen = pygame.display.set_mode((width, height)) pygame.display.set_caption("Moving Block") # Set up the block block_width = 50 block_height = 50 block_color = (255, 0, 0) # Red block_x = width // 2 - block_width // 2 block_y = height // 2 - block_height // 2 block_speed = 2 # Slower movement speed is_jumping = False is_crouching = False # Set up gravity gravity = 0.5 vertical_velocity = 0 # Set up the ground and colors ground_color = (0, 255, 0) # Green ground_height = 20 ground = pygame.Rect(0, height - ground_height, width, ground_height) background_color = (173, 216, 230) # Light blue # Crouching variables crouch_scale = 0.5 # Scale to flatten the block # Run the game loop while True: for event in pygame.event.get(): if event.type == pygame.QUIT: pygame.quit() sys.exit() # Handle keyboard events keys = pygame.key.get_pressed() if keys[pygame.K_a] and block_x > 0: # Move left block_x -= block_speed if keys[pygame.K_d