[text] doddle

Viewer

  1. import pygame
  2. import random
  3.  
  4. # Initialize Pygame
  5. pygame.init()
  6.  
  7. # Screen dimensions
  8. SCREEN_WIDTH = 400
  9. SCREEN_HEIGHT = 600
  10.  
  11. # Colors
  12. WHITE = (255, 255, 255)
  13. BLACK = (0, 0, 0)
  14.  
  15. # Player properties
  16. PLAYER_WIDTH = 50
  17. PLAYER_HEIGHT = 50
  18. PLAYER_COLOR = (0, 255, 0)
  19. PLAYER_START_X = SCREEN_WIDTH // 2
  20. PLAYER_START_Y = SCREEN_HEIGHT - PLAYER_HEIGHT
  21.  
  22. # Platform properties
  23. PLATFORM_WIDTH = 80
  24. PLATFORM_HEIGHT = 20
  25. PLATFORM_COLOR = (0, 0, 255)
  26. PLATFORM_SPEED = 5
  27.  
  28. # Score
  29. score = 0
  30.  
  31. # Set up the screen
  32. screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
  33. pygame.display.set_caption("Doodle Jump")
  34.  
  35. clock = pygame.time.Clock()
  36.  
  37. # Player class
  38. class Player(pygame.sprite.Sprite):
  39.     def __init__(self):
  40.         super().__init__()
  41.         self.image = pygame.Surface([PLAYER_WIDTH, PLAYER_HEIGHT])
  42.         self.image.fill(PLAYER_COLOR)
  43.         self.rect = self.image.get_rect()
  44.         self.rect.x = PLAYER_START_X
  45.         self.rect.y = PLAYER_START_Y
  46.         self.velocity_y = 0
  47.  
  48.     def update(self):
  49.         self.velocity_y += 0.5  # Gravity
  50.         self.rect.y += self.velocity_y
  51.         if self.rect.y > SCREEN_HEIGHT:
  52.             self.rect.y = PLAYER_START_Y
  53.             platform_group.empty()
  54.             generate_platforms()
  55.             global score
  56.             score = 0
  57.  
  58. # Platform class
  59. class Platform(pygame.sprite.Sprite):
  60.     def __init__(self, x, y):
  61.         super().__init__()
  62.         self.image = pygame.Surface([PLATFORM_WIDTH, PLATFORM_HEIGHT])
  63.         self.image.fill(PLATFORM_COLOR)
  64.         self.rect = self.image.get_rect()
  65.         self.rect.x = x
  66.         self.rect.y = y
  67.  
  68.     def update(self):
  69.         self.rect.y += PLATFORM_SPEED
  70.         if self.rect.y > SCREEN_HEIGHT:
  71.             self.rect.y = -PLATFORM_HEIGHT
  72.             self.rect.x = random.randint(0, SCREEN_WIDTH - PLATFORM_WIDTH)
  73.             global score
  74.             score += 1
  75.  
  76. # Generate platforms
  77. def generate_platforms():
  78.     for i in range(10):
  79.         platform = Platform(random.randint(0, SCREEN_WIDTH - PLATFORM_WIDTH), i * SCREEN_HEIGHT / 10)
  80.         platform_group.add(platform)
  81.  
  82. # Sprite groups
  83. all_sprites_group = pygame.sprite.Group()
  84. platform_group = pygame.sprite.Group()
  85.  
  86. # Create player
  87. player = Player()
  88. all_sprites_group.add(player)
  89.  
  90. # Generate platforms
  91. generate_platforms()
  92.  
  93. # Main game loop
  94. running = True
  95. while running:
  96.     for event in pygame.event.get():
  97.         if event.type == pygame.QUIT:
  98.             running = False
  99.  
  100.     # Check for collisions
  101.     collisions = pygame.sprite.spritecollide(player, platform_group, False)
  102.     if collisions:
  103.         player.velocity_y = -10
  104.  
  105.     # Update
  106.     all_sprites_group.update()
  107.  
  108.     # Draw
  109.     screen.fill(WHITE)
  110.     all_sprites_group.draw(screen)
  111.  
  112.     # Display score
  113.     font = pygame.font.Font(None, 36)
  114.     text = font.render("Score: " + str(score), True, BLACK)
  115.     screen.blit(text, (10, 10))
  116.  
  117.     pygame.display.flip()
  118.     clock.tick(60)
  119.  
  120. pygame.quit()

Editor

You can edit this paste and save as new:


File Description
  • doddle
  • Paste Code
  • 28 Mar-2024
  • 2.96 Kb
You can Share it: