import pygame
import sys
import random
# Initialize Pygame
[Link]()
# Screen settings
SCREEN_WIDTH = 400
SCREEN_HEIGHT = 600
FPS = 60
# Colors
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
GREEN = (0, 200, 0)
RED = (255, 0, 0)
BLUE = (0, 0, 255)
SKY_BLUE = (135, 206, 235)
SUN_YELLOW = (255, 255, 0)
CLOUD_WHITE = (255, 255, 255)
# Ball settings
BALL_RADIUS = 20
BALL_COLOR = RED
BALL_SPEED_X = 5
GRAVITY = 0.5
JUMP_VELOCITY = 12
# Platform settings
PLATFORM_WIDTH = 80
PLATFORM_HEIGHT = 10
PLATFORM_COLOR = GREEN
PLATFORM_COUNT = 6
PLATFORM_GAP_MIN = 80 # Reduced gap for closer platforms
PLATFORM_GAP_MAX = 120 # Adjusted to make platforms more reachable
# Initialize screen
screen = [Link].set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
[Link].set_caption("Doodle Jump with Restart")
clock = [Link]()
# Font for score and restart
font = [Link](None, 36)
# Ball class
class Ball:
def __init__(self, start_y):
self.x = SCREEN_WIDTH // 2
self.y = start_y
self.velocity_y = 0
[Link] = 0
self.reached_platforms = set() # Track platforms that have been reached
def move(self, keys_pressed):
if keys_pressed[pygame.K_a] and self.x - BALL_RADIUS > 0:
self.x -= BALL_SPEED_X
if keys_pressed[pygame.K_d] and self.x + BALL_RADIUS < SCREEN_WIDTH:
self.x += BALL_SPEED_X
def apply_gravity(self):
self.velocity_y += GRAVITY
self.y += self.velocity_y
def jump(self):
self.velocity_y = -JUMP_VELOCITY
def draw(self, screen):
[Link](screen, BALL_COLOR, (int(self.x), int(self.y)),
BALL_RADIUS)
# Platform class
class Platform:
def __init__(self, x, y, id):
[Link] = [Link](x, y, PLATFORM_WIDTH, PLATFORM_HEIGHT)
[Link] = id # Unique identifier for each platform
def draw(self, screen):
[Link](screen, PLATFORM_COLOR, [Link])
# Draw the background (sun, clouds, and sky)
def draw_background():
# Draw the sky
[Link](SKY_BLUE)
# Draw the sun
[Link](screen, SUN_YELLOW, (SCREEN_WIDTH - 60, 60), 40)
# Draw some clouds
[Link](screen, CLOUD_WHITE, (50, 50, 150, 80))
[Link](screen, CLOUD_WHITE, (100, 120, 180, 70))
[Link](screen, CLOUD_WHITE, (250, 80, 170, 60))
# Main game function
def main():
def reset_game():
ball = Ball(SCREEN_HEIGHT - 100)
platforms = []
for i in range(PLATFORM_COUNT):
x = [Link](0, SCREEN_WIDTH - PLATFORM_WIDTH)
y = SCREEN_HEIGHT - (i + 1) * PLATFORM_GAP_MIN
[Link](Platform(x, y, i)) # Use a unique id for each
platform
bottom_platform_visible = True
return ball, platforms, bottom_platform_visible
# Initial reset
ball, platforms, bottom_platform_visible = reset_game()
# Game loop
running = True
game_over = False
while running:
# Event handling
for event in [Link]():
if [Link] == [Link]:
running = False
if game_over and [Link] == [Link]:
if [Link] == pygame.K_r:
game_over = False
ball, platforms, bottom_platform_visible = reset_game()
if not game_over:
# Ball movement
keys_pressed = [Link].get_pressed()
[Link](keys_pressed)
ball.apply_gravity()
# Ball-platform collision
if ball.velocity_y > 0: # Ball is falling
for platform in platforms:
if [Link](ball.x, ball.y + BALL_RADIUS):
# Only increase score once per platform
if [Link] not in ball.reached_platforms:
[Link]()
[Link] += 1 # Increment score for each platform
reached
ball.reached_platforms.add([Link]) # Mark
platform as reached
# Bottom platform
if bottom_platform_visible and ball.y + BALL_RADIUS >=
SCREEN_HEIGHT - PLATFORM_HEIGHT:
[Link]()
# Scroll platforms upward
if ball.y < SCREEN_HEIGHT // 2:
dy = SCREEN_HEIGHT // 2 - ball.y
ball.y += dy
for platform in platforms:
[Link].y += dy
# Hide bottom platform when scrolling up
bottom_platform_visible = False
# Remove off-screen platforms and generate new ones
platforms = [p for p in platforms if [Link].y < SCREEN_HEIGHT]
while len(platforms) < PLATFORM_COUNT:
x = [Link](0, SCREEN_WIDTH - PLATFORM_WIDTH)
y = platforms[-1].rect.y - [Link](PLATFORM_GAP_MIN,
PLATFORM_GAP_MAX)
[Link](Platform(x, y, len(platforms))) # Use new unique
id
# Check for game over
if ball.y > SCREEN_HEIGHT:
game_over = True
# Drawing
draw_background() # Draw the background (sun, clouds, and sky)
if bottom_platform_visible:
[Link](screen, BLUE, (0, SCREEN_HEIGHT - PLATFORM_HEIGHT,
SCREEN_WIDTH, PLATFORM_HEIGHT))
for platform in platforms:
[Link](screen)
[Link](screen)
# Display score
score_text = [Link](f"Score: {[Link]}", True, BLACK)
[Link](score_text, (10, 10))
# Display game over message
if game_over:
game_over_text = [Link]("Game Over! Press R to Restart", True,
BLACK)
[Link](game_over_text, (SCREEN_WIDTH // 2 -
game_over_text.get_width() // 2, SCREEN_HEIGHT // 2))
# Update display
[Link]()
[Link](FPS)
[Link]()
[Link]()
if __name__ == "__main__":
main()