0% found this document useful (0 votes)
23 views4 pages

Code

This document is a Python script that implements a simple Doodle Jump game using the Pygame library. The game features a ball that the player controls to jump on platforms, with mechanics for gravity, scoring, and restarting the game after a game over. The script includes settings for screen dimensions, colors, and platform behavior, along with functions for drawing the game elements and handling user input.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views4 pages

Code

This document is a Python script that implements a simple Doodle Jump game using the Pygame library. The game features a ball that the player controls to jump on platforms, with mechanics for gravity, scoring, and restarting the game after a game over. The script includes settings for screen dimensions, colors, and platform behavior, along with functions for drawing the game elements and handling user input.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 4

import pygame

import sys
import random

# Initialize Pygame
pygame.init()

# 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 = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
pygame.display.set_caption("Doodle Jump with Restart")
clock = pygame.time.Clock()

# Font for score and restart


font = pygame.font.SysFont(None, 36)

# Ball class
class Ball:
def __init__(self, start_y):
self.x = SCREEN_WIDTH // 2
self.y = start_y
self.velocity_y = 0
self.score = 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):


pygame.draw.circle(screen, BALL_COLOR, (int(self.x), int(self.y)),
BALL_RADIUS)

# Platform class
class Platform:
def __init__(self, x, y, id):
self.rect = pygame.Rect(x, y, PLATFORM_WIDTH, PLATFORM_HEIGHT)
self.id = id # Unique identifier for each platform

def draw(self, screen):


pygame.draw.rect(screen, PLATFORM_COLOR, self.rect)

# Draw the background (sun, clouds, and sky)


def draw_background():
# Draw the sky
screen.fill(SKY_BLUE)

# Draw the sun


pygame.draw.circle(screen, SUN_YELLOW, (SCREEN_WIDTH - 60, 60), 40)

# Draw some clouds


pygame.draw.ellipse(screen, CLOUD_WHITE, (50, 50, 150, 80))
pygame.draw.ellipse(screen, CLOUD_WHITE, (100, 120, 180, 70))
pygame.draw.ellipse(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 = random.randint(0, SCREEN_WIDTH - PLATFORM_WIDTH)
y = SCREEN_HEIGHT - (i + 1) * PLATFORM_GAP_MIN
platforms.append(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 pygame.event.get():
if event.type == pygame.QUIT:
running = False
if game_over and event.type == pygame.KEYDOWN:
if event.key == pygame.K_r:
game_over = False
ball, platforms, bottom_platform_visible = reset_game()

if not game_over:
# Ball movement
keys_pressed = pygame.key.get_pressed()
ball.move(keys_pressed)
ball.apply_gravity()

# Ball-platform collision
if ball.velocity_y > 0: # Ball is falling
for platform in platforms:
if platform.rect.collidepoint(ball.x, ball.y + BALL_RADIUS):
# Only increase score once per platform
if platform.id not in ball.reached_platforms:
ball.jump()
ball.score += 1 # Increment score for each platform
reached
ball.reached_platforms.add(platform.id) # Mark
platform as reached

# Bottom platform
if bottom_platform_visible and ball.y + BALL_RADIUS >=
SCREEN_HEIGHT - PLATFORM_HEIGHT:
ball.jump()

# Scroll platforms upward


if ball.y < SCREEN_HEIGHT // 2:
dy = SCREEN_HEIGHT // 2 - ball.y
ball.y += dy
for platform in platforms:
platform.rect.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 p.rect.y < SCREEN_HEIGHT]
while len(platforms) < PLATFORM_COUNT:
x = random.randint(0, SCREEN_WIDTH - PLATFORM_WIDTH)
y = platforms[-1].rect.y - random.randint(PLATFORM_GAP_MIN,
PLATFORM_GAP_MAX)
platforms.append(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:
pygame.draw.rect(screen, BLUE, (0, SCREEN_HEIGHT - PLATFORM_HEIGHT,
SCREEN_WIDTH, PLATFORM_HEIGHT))

for platform in platforms:


platform.draw(screen)
ball.draw(screen)
# Display score
score_text = font.render(f"Score: {ball.score}", True, BLACK)
screen.blit(score_text, (10, 10))

# Display game over message


if game_over:
game_over_text = font.render("Game Over! Press R to Restart", True,
BLACK)
screen.blit(game_over_text, (SCREEN_WIDTH // 2 -
game_over_text.get_width() // 2, SCREEN_HEIGHT // 2))

# Update display
pygame.display.flip()
clock.tick(FPS)

pygame.quit()
sys.exit()

if __name__ == "__main__":
main()

You might also like