[text] game1

Viewer

  1. import pygame
  2.  
  3. # Initialize Pygame
  4. pygame.init()
  5.  
  6. # Set up the game window
  7. screen_width = 800
  8. screen_height = 600
  9. screen = pygame.display.set_mode((screen_width, screen_height))
  10. pygame.display.set_caption("Run, Jump, and Duck Game")
  11.  
  12. # Set up the player
  13. player_width = 50
  14. player_height = 50
  15. player_x = 100
  16. player_y = screen_height - player_height - 50
  17. player_speed = 5
  18. player_jump_height = 100
  19. player_is_jumping = False
  20.  
  21. # Set up the game loop
  22. clock = pygame.time.Clock()
  23. game_over = False
  24.  
  25. # Main game loop
  26. while not game_over:
  27.  
  28.     # Handle events
  29.     for event in pygame.event.get():
  30.         if event.type == pygame.QUIT:
  31.             game_over = True
  32.         if event.type == pygame.KEYDOWN:
  33.             if event.key == pygame.K_SPACE and not player_is_jumping:
  34.                 player_is_jumping = True
  35.             if event.key == pygame.K_DOWN:
  36.                 player_height = 25
  37.                 player_y += 25
  38.             if event.key == pygame.K_UP:
  39.                 player_height = 50
  40.                 player_y -= 25
  41.  
  42.     # Move the player
  43.     keys = pygame.key.get_pressed()
  44.     if keys[pygame.K_LEFT] and player_x > 0:
  45.         player_x -= player_speed
  46.     if keys[pygame.K_RIGHT] and player_x < screen_width - player_width:
  47.         player_x += player_speed
  48.     if player_is_jumping:
  49.         if player_jump_height >= 0:
  50.             player_y -= player_jump_height
  51.             player_jump_height -= 10
  52.         else:
  53.             player_is_jumping = False
  54.             player_jump_height = 100
  55.  
  56.     # Draw the player
  57.     pygame.draw.rect(screen, (255, 0, 0), (player_x, player_y, player_width, player_height))
  58.  
  59.     # Update the screen
  60.     pygame.display.update()
  61.     clock.tick(60)
  62.  
  63. # Quit Pygame
  64. pygame.quit()
  65.  
  66.  

Editor

You can edit this paste and save as new:


File Description
  • game1
  • Paste Code
  • 24 Mar-2023
  • 1.74 Kb
You can Share it: