Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import pygame
- # Initialize Pygame
- pygame.init()
- # Set the screen dimensions
- screen_width = 640
- screen_height = 480
- screen = pygame.display.set_mode((screen_width, screen_height))
- # Set the player dimensions and position
- player_width = 50
- player_height = 50
- player_x = screen_width / 2 - player_width / 2
- player_y = screen_height / 2 - player_height / 2
- # Set the player speed
- player_speed = 5
- # Main game loop
- running = True
- while running:
- # Handle events
- for event in pygame.event.get():
- if event.type == pygame.QUIT:
- running = False
- # Get key presses
- keys = pygame.key.get_pressed()
- # Update player position based on key presses
- if keys[pygame.K_LEFT]:
- player_x -= player_speed
- if keys[pygame.K_RIGHT]:
- player_x += player_speed
- if keys[pygame.K_UP]:
- player_y -= player_speed
- if keys[pygame.K_DOWN]:
- player_y += player_speed
- # Draw the player
- pygame.draw.rect(screen, (255, 0, 0), (player_x, player_y, player_width, player_height))
- # Update the screen
- pygame.display.update()
- # Quit Pygame
- pygame.quit()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement