---
# System prepended metadata

title: '**Doodle Jump**'

---

# **Doodle Jump**

```
import pygame
import sys
import random

class DoodleJump:
    def __init__(self):
        # initialize the attributes
        pygame.font.init()
        self.screen = pygame.display.set_mode((400, 533))
        self.title = pygame.display.set_caption("Doodle Jump!")
        self.background = pygame.image.load("images/background.png")
        self.doodle1 = pygame.image.load("images/doodle1.png")
        self.doodle2 = pygame.image.load("images/doodle2.png")
        self.platform = pygame.image.load("images/platform.png")
        self.font = pygame.font.SysFont("comicsans", 30)
        self.playerX = 150
        self.playerY = 150
        self.platX = 0
        self.platY = 0
        self.platList = []
        self.dy = 0
        self.cameraY = 200
        self.score = 0

    def messageToScreen(self, msg, color, x, y, size):
        # showing the message in the screen
        pygame.font.init()
        self.screenfont = pygame.font.SysFont("comicsans", size)
        self.screen.blit(self.screenfont.render(msg, True, color), (x, y))
        pygame.display.update()

    def generatePlatforms(self):
        # generating the initial platforms
        for i in range(12):
            self.platX = random.randrange(0, 400)
            self.platY = random.randrange(0, 533) 
            self.platList.append([self.platX, self.platY])

    def updatePlayer(self):
        # manipulating the player
        keys = pygame.key.get_pressed()
        if keys[pygame.K_LEFT]:
            self.playerX -= 3
            self.doodle2 = pygame.image.load("images/doodle2.png")
        elif keys[pygame.K_RIGHT]:
            self.playerX += 3
            self.doodle2 = pygame.transform.flip(pygame.image.load("images/doodle2.png"), True, False)

        # moving through the left or the right boundary
        if self.playerX > 400:
            self.playerX = -50
        elif self.playerX < -50:
            self.playerX = 400

        # falling
        self.dy += 0.12
        self.playerY += self.dy

        # jumping: step in the plates
        for plat in self.platList:
            if (self.playerX + 50 > plat[0]) and (self.playerX + 20 < plat[0] + 68) and (self.playerY + 70 > plat[1]) and (self.playerY + 70 < plat[1] + 14) and self.dy > 0:
                self.dy = -10

    def updatePlatforms(self):
        # moving the screen
        if self.playerY < self.cameraY:
            self.playerY = self.cameraY
            for plat in self.platList:
                plat[1] = plat[1] - self.dy
                if plat[1] > 533:
                    plat[1] = 0
                    plat[0] = random.randrange(0, 400)
                    self.score += 50

    def gameOver(self):
        # stopping the game when gameover
        if self.playerY > 533:
            self.screen.fill((255, 255, 255))
            self.messageToScreen("Game Over!", (255, 0, 0), (400 / 2 - 100), (533 / 2 - 30), 40)
            self.messageToScreen("Press SPACE to play again.", (0, 0, 0), (400 / 2 - 190), (533 / 2 + 10), 40)
            while True:
                for event in pygame.event.get():
                    if event.type == pygame.KEYDOWN:
                        if event.key == pygame.K_SPACE:
                            DoodleJump().run() 
                    if event.type == pygame.QUIT:
                        pygame.quit()
                        sys.exit()

    def run(self):
        # main function
        self.generatePlatforms()
        while True:
            # builting items in the screen
            self.screen.blit(self.background, (0, 0))
            for plat in self.platList:
                self.screen.blit(self.platform, (plat[0], plat[1]))
            self.screen.blit(self.font.render("Score: " + str(self.score), True, (0, 0, 0)), (10, 10))
            self.screen.blit(self.doodle2, (self.playerX, self.playerY))
            # closing the screen
            for event in pygame.event.get():
                if event.type == pygame.QUIT:
                    pygame.quit()
                    sys.exit()
            
            self.updatePlayer()
            self.updatePlatforms()
            self.gameOver()

            pygame.display.update()

DoodleJump().run()
```