Advertisement
Danila_lipatov

Example_of_game

Apr 22nd, 2023 (edited)
183
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.15 KB | None | 0 0
  1. import pygame
  2.  
  3. # Initialize Pygame
  4. pygame.init()
  5.  
  6. # Set the screen dimensions
  7. screen_width = 640
  8. screen_height = 480
  9. screen = pygame.display.set_mode((screen_width, screen_height))
  10.  
  11. # Set the player dimensions and position
  12. player_width = 50
  13. player_height = 50
  14. player_x = screen_width / 2 - player_width / 2
  15. player_y = screen_height / 2 - player_height / 2
  16.  
  17. # Set the player speed
  18. player_speed = 5
  19.  
  20. # Main game loop
  21. running = True
  22. while running:
  23.     # Handle events
  24.     for event in pygame.event.get():
  25.         if event.type == pygame.QUIT:
  26.             running = False
  27.    
  28.     # Get key presses
  29.     keys = pygame.key.get_pressed()
  30.    
  31.     # Update player position based on key presses
  32.     if keys[pygame.K_LEFT]:
  33.         player_x -= player_speed
  34.     if keys[pygame.K_RIGHT]:
  35.         player_x += player_speed
  36.     if keys[pygame.K_UP]:
  37.         player_y -= player_speed
  38.     if keys[pygame.K_DOWN]:
  39.         player_y += player_speed
  40.    
  41.     # Draw the player
  42.     pygame.draw.rect(screen, (255, 0, 0), (player_x, player_y, player_width, player_height))
  43.    
  44.     # Update the screen
  45.     pygame.display.update()
  46.  
  47. # Quit Pygame
  48. pygame.quit()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement