- import pygame
- import random
- # Initialize Pygame
- pygame.init()
- # Screen dimensions
- SCREEN_WIDTH = 400
- SCREEN_HEIGHT = 600
- # Colors
- WHITE = (255, 255, 255)
- BLACK = (0, 0, 0)
- # Player properties
- PLAYER_WIDTH = 50
- PLAYER_HEIGHT = 50
- PLAYER_COLOR = (0, 255, 0)
- PLAYER_START_X = SCREEN_WIDTH // 2
- PLAYER_START_Y = SCREEN_HEIGHT - PLAYER_HEIGHT
- # Platform properties
- PLATFORM_WIDTH = 80
- PLATFORM_HEIGHT = 20
- PLATFORM_COLOR = (0, 0, 255)
- PLATFORM_SPEED = 5
- # Score
- score = 0
- # Set up the screen
- screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
- pygame.display.set_caption("Doodle Jump")
- clock = pygame.time.Clock()
- # Player class
- class Player(pygame.sprite.Sprite):
- def __init__(self):
- super().__init__()
- self.image = pygame.Surface([PLAYER_WIDTH, PLAYER_HEIGHT])
- self.image.fill(PLAYER_COLOR)
- self.rect = self.image.get_rect()
- self.rect.x = PLAYER_START_X
- self.rect.y = PLAYER_START_Y
- self.velocity_y = 0
- def update(self):
- self.velocity_y += 0.5 # Gravity
- self.rect.y += self.velocity_y
- if self.rect.y > SCREEN_HEIGHT:
- self.rect.y = PLAYER_START_Y
- platform_group.empty()
- generate_platforms()
- global score
- score = 0
- # Platform class
- class Platform(pygame.sprite.Sprite):
- def __init__(self, x, y):
- super().__init__()
- self.image = pygame.Surface([PLATFORM_WIDTH, PLATFORM_HEIGHT])
- self.image.fill(PLATFORM_COLOR)
- self.rect = self.image.get_rect()
- self.rect.x = x
- self.rect.y = y
- def update(self):
- self.rect.y += PLATFORM_SPEED
- if self.rect.y > SCREEN_HEIGHT:
- self.rect.y = -PLATFORM_HEIGHT
- self.rect.x = random.randint(0, SCREEN_WIDTH - PLATFORM_WIDTH)
- global score
- score += 1
- # Generate platforms
- def generate_platforms():
- for i in range(10):
- platform = Platform(random.randint(0, SCREEN_WIDTH - PLATFORM_WIDTH), i * SCREEN_HEIGHT / 10)
- platform_group.add(platform)
- # Sprite groups
- all_sprites_group = pygame.sprite.Group()
- platform_group = pygame.sprite.Group()
- # Create player
- player = Player()
- all_sprites_group.add(player)
- # Generate platforms
- generate_platforms()
- # Main game loop
- running = True
- while running:
- for event in pygame.event.get():
- if event.type == pygame.QUIT:
- running = False
- # Check for collisions
- collisions = pygame.sprite.spritecollide(player, platform_group, False)
- if collisions:
- player.velocity_y = -10
- # Update
- all_sprites_group.update()
- # Draw
- screen.fill(WHITE)
- all_sprites_group.draw(screen)
- # Display score
- font = pygame.font.Font(None, 36)
- text = font.render("Score: " + str(score), True, BLACK)
- screen.blit(text, (10, 10))
- pygame.display.flip()
- clock.tick(60)
- pygame.quit()
[text] doddle
Viewer
*** This page was generated with the meta tag "noindex, nofollow". This happened because you selected this option before saving or the system detected it as spam. This means that this page will never get into the search engines and the search bot will not crawl it. There is nothing to worry about, you can still share it with anyone.
Editor
You can edit this paste and save as new: