Advertisement
cookertron

Doodle Jump Clone using Python & Pygame

Nov 18th, 2018
225
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 9.58 KB | None | 0 0
  1. import pygame
  2. from pygame.locals import *
  3. from pygame import gfxdraw
  4.  
  5. import math
  6. import random
  7. import time
  8. import sys
  9. import os
  10.  
  11. # define display surface           
  12. W, H = 405, 720
  13. HW, HH = W / 2, H / 2
  14.  
  15. # initialise display
  16. pygame.init()
  17. os.environ['SDL_VIDEO_WINDOW_POS'] = "{},{}".format(pygame.display.Info().current_w / 2 - HW, 0)
  18. DS = pygame.display.set_mode((W, H), NOFRAME, 24)
  19. CLOCK = pygame.time.Clock()
  20. pygame.display.set_caption("Doodle Jump Clone")
  21. FPS = 60
  22.  
  23. # functions
  24. def sprite2D(polyData, scale, bkgdColor, colorKey):
  25.     global H
  26.    
  27.     sprite = pygame.Surface((polyData['width'], polyData['height']), 0, 24)
  28.     sprite.fill(bkgdColor)
  29.     if colorKey:
  30.         sprite.set_colorkey(bkgdColor) 
  31.        
  32.     for poly in polyData['data']:
  33.         #pygame.gfxdraw.aapolygon(self.sprite, poly['points'], poly['color'])  
  34.         pygame.gfxdraw.filled_polygon(sprite, poly['points'], poly['color'])       
  35.  
  36.         #pygame.draw.polygon(self.sprite, poly['color'], poly['points'])
  37.        
  38.     if scale:
  39.         if scale[0] and not scale[1]:
  40.             width = scale[0]
  41.             height = (float(width) / polyData['width']) * polyData['height']
  42.         elif not scale[0] and scale[1]:
  43.             height = scale[1]
  44.             width = (float(height) / polyData['height']) * polyData['width']
  45.         else:
  46.             width = scale[0]
  47.             height = scale[1]
  48.     else:
  49.         width = polyData['width']
  50.         height = polyData['height']
  51.        
  52.     displayScale = float(H) / 720  
  53.     return pygame.transform.scale(sprite, (int(width * displayScale), int(height * displayScale)))
  54.            
  55. # exit the program
  56. def quit():
  57.     for event in pygame.event.get():
  58.         if event.type == QUIT or (event.type == KEYDOWN and event.key == K_ESCAPE):
  59.             return True
  60.     return False
  61.  
  62. class platform:
  63.     def __init__(self, x, y, id):
  64.         global PLATFORM_WIDTH
  65.        
  66.         self.x, self.y = x, y
  67.         self.floor = [x - 5, x + PLATFORM_WIDTH + 5, y]
  68.         self.id = id
  69.    
  70. class platforms:
  71.     def __init__(self):
  72.         global SOLID_PLATFORM_MAX_DISTANCE_AT_START
  73.        
  74.         self.container = list()
  75.        
  76.         self.solidPlatformMaxDistance = SOLID_PLATFORM_MAX_DISTANCE_AT_START
  77.        
  78.         self.container.append(platform(0, H, 0))
  79.         while self.container[-1].y > 0:
  80.             self.add()
  81.    
  82.     def add(self):
  83.         global W, H
  84.         global PLATFORM_WIDTH, PLATFORM_HEIGHT, PLATFORM_EXCLUSION_SIZE
  85.        
  86.         while True:
  87.             x = random.randint(0, W - PLATFORM_WIDTH)
  88.             y = self.container[-1].y - random.randint(0, self.solidPlatformMaxDistance)
  89.                
  90.             p = self.container[-1]
  91.            
  92.             if x > p.x - PLATFORM_WIDTH - PLATFORM_EXCLUSION_SIZE and x < p.x + PLATFORM_WIDTH + PLATFORM_EXCLUSION_SIZE:
  93.                 if y > p.y - PLATFORM_HEIGHT - PLATFORM_EXCLUSION_SIZE:
  94.                     continue
  95.                 else:
  96.                     break
  97.             break
  98.         self.container.append(platform(x, y, 0))
  99.    
  100.     def draw(self, surface):
  101.         global BLACK
  102.         for p in self.container:
  103.             surface.fill(BLACK, (p.x, p.y, PLATFORM_WIDTH, PLATFORM_HEIGHT))
  104.    
  105.     def move(self, speed = 0):
  106.         global H
  107.         global PLATFORM_WIDTH, PLATFORM_HEIGHT, PLATFORM_EXCLUSION_SIZE
  108.        
  109.         for p in self.container:
  110.             p.y += speed
  111.             p.floor[2] += speed
  112.            
  113.         if self.container[-1].y > -PLATFORM_HEIGHT - PLATFORM_EXCLUSION_SIZE:
  114.             self.add()
  115.        
  116.         if self.container[0].y > H:
  117.             self.container.pop(0)
  118.    
  119.     def collision(self, x, y, speed):
  120.         for p in self.container:
  121.             if x >= p.floor[0] and x <= p.floor[1]:
  122.                 if y < p.floor[2] and y + speed >= p.floor[2]:
  123.                     return p.floor[2]
  124.         return False
  125.        
  126. # define some colors
  127. FUCHSIA = (255,   0, 255)
  128. PURPLE  = (128,   0, 128)
  129. TEAL    = (  0, 128, 128)
  130. LIME    = (  0, 255,   0)
  131. GREEN   = (  0, 128,   0)
  132. OLIVE   = (128, 128,   0)
  133. YELLOW  = (255, 255,   0)
  134. ORANGE  = (255, 165,   0)
  135. RED     = (255,   0,   0)
  136. MAROON  = (128,   0,   0)
  137. SILVER  = (192, 192, 192)
  138. GRAY    = (128, 128, 128)
  139. BLUE    = (  0,   0, 255)
  140. NAVY    = (  0,   0, 128)
  141. AQUA    = (  0, 255, 255)
  142. WHITE   = (255, 255, 255)
  143. BLACK   = (  0,   0,   0)
  144.  
  145. doodlePoly1 = {'width':433,'height':420,'data':
  146.     [
  147.         {
  148.             'color':BLACK,
  149.             'points':
  150.                 [[156,0],[200,13],[232,35],[258,66],[275,92],[288,119],[294,132],[302,140],[314,147],[334,151],
  151.                 [357,152],[381,147],[404,140],[416,136],[426,157],[433,189],[431,216],[424,230],[412,238],[405,239],
  152.                 [389,228],[370,217],[342,209],[314,208],[292,214],[280,225],[276,243],[279,274],[279,305],[280,329],
  153.                 [276,349],[275,352],[245,357],[243,366],[243,385],[243,392],[264,394],[277,396],[278,402],[274,407],
  154.                 [257,408],[227,406],[225,398],[227,380],[228,366],[224,356],[198,356],[185,357],[180,373],[179,390],
  155.                 [181,402],[193,402],[201,403],[201,410],[198,417],[188,418],[162,410],[163,392],[164,365],[164,357],
  156.                 [144,357],[121,356],[115,387],[114,401],[117,406],[132,406],[143,408],[142,415],[135,420],[118,420],
  157.                 [105,415],[100,406],[102,383],[105,360],[94,359],[73,360],[61,362],[58,376],[56,397],[56,401],
  158.                 [69,404],[79,404],[82,408],[76,415],[69,418],[52,414],[37,404],[41,395],[44,379],[45,359],
  159.                 [32,359],[17,352],[11,352],[15,318],[14,295],[7,284],[14,276],[14,261],[7,244],[0,237],
  160.                 [8,234],[11,231],[9,211],[5,155],[9,118],[22,83],[49,49],[81,23],[109,6],[133,1],
  161.                 [156,0]]
  162.         },
  163.         {
  164.             'color':(231,223,49),
  165.             'points':
  166.                 [[265,240],[155,237],[92,235],[25,230],[20,168],[25,123],[40,81],[73,42],[112,20],[150,15],
  167.                 [183,22],[224,45],[252,80],[275,126],[282,143],[301,157],[333,165],[363,165],[378,163],[378,181],
  168.                 [382,214],[371,205],[334,196],[309,196],[289,200],[273,211],[267,226],[265,240]]
  169.         },
  170.         {
  171.             'color':(231,223,49),
  172.             'points':
  173.                 [[391,162],[397,155],[406,155],[414,165],[421,188],[419,212],[413,224],[404,215],[394,194],[392,177], [391,162]]
  174.         },     
  175.         {
  176.             'color':BLACK,
  177.             'points':
  178.                 [[196,126],[205,122],[213,124],[215,137],[212,146],[205,148],[197,142],[196,135],[196,126]]
  179.  
  180.         },     
  181.         {
  182.             'color' : BLACK,
  183.             'points' :
  184.                 [[235,132],[238,127],[247,126],[252,129],[254,136],[252,146],[245,151],[238,150],[234,143],[235,132]]
  185.  
  186.         },     
  187.         {
  188.             'color':(85,143,64),
  189.             'points':
  190.                 [[27,242],[109,248],[175,250],[268,254],[267,281],[235,282],[193,282],[133,279],[59,278],[29,278],[27,242]]
  191.         },
  192.         {
  193.             'color':(85,143,64),
  194.             'points':
  195.                 [[28,292],[93,289],[151,290],[205,293],[267,298],[266,310],[266,323],[192,320],[141,320],[94,319],
  196.                 [64,318],[34,315],[30,322],[29,317],[30,306],[28,292]]
  197.         },
  198.         {
  199.             'color':(85,143,64),
  200.             'points':
  201.                 [[29,340],[33,333],[44,331],[90,332],[120,334],[178,333],[217,332],[242,334],[261,339],[247,340],
  202.                 [209,343],[152,344],[98,345],[67,344],[29,340]]
  203.         },
  204.     ]
  205. }
  206.  
  207. doodlePoly2 = {'width':433,'height':390,'data':
  208.     [
  209.         {
  210.             'color':BLACK,
  211.             'points':
  212.                 [[156,0],[200,13],[232,35],[258,66],[275,92],[288,119],[294,132],[302,140],[314,147],[334,151],
  213.                 [357,152],[381,147],[404,140],[416,136],[426,157],[433,189],[431,216],[424,230],[412,238],[405,239],
  214.                 [389,228],[370,217],[342,209],[314,208],[292,214],[280,225],[276,243],[279,274],[279,305],[280,329],
  215.                 [276,349],[275,352],[245,357],[243,358],[242,361],[243,362],[264,364],[277,366],[278,372],[274,377],
  216.                 [257,378],[227,376],[225,368],[225,361],[225,358],[224,356],[198,356],[185,357],[180,363],[179,370],
  217.                 [181,372],[193,372],[201,373],[201,380],[198,387],[188,388],[162,380],[163,362],[163,360],[164,357],
  218.                 [144,357],[121,356],[115,357],[114,371],[117,376],[132,376],[143,378],[142,385],[135,390],[118,390],
  219.                 [105,385],[100,376],[103,365],[103,359],[94,359],[73,360],[61,362],[57,365],[56,367],[56,371],
  220.                 [69,374],[79,374],[82,378],[76,385],[69,388],[52,384],[37,374],[41,365],[43,361],[45,359],
  221.                 [32,359],[17,352],[11,352],[15,318],[14,295],[7,284],[14,276],[14,261],[7,244],[0,237],
  222.                 [8,234],[11,231],[9,211],[5,155],[9,118],[22,83],[49,49],[81,23],[109,6],[133,1],
  223.                 [156,0],]
  224.         },
  225.     ]
  226. }
  227. doodlePoly2['data'] += doodlePoly1['data'][1:8]
  228.  
  229. doodleSprites = [sprite2D(doodlePoly1, (50, None), PURPLE, True)]
  230. doodleSprites += [pygame.transform.flip(doodleSprites[0], True, False)]
  231. doodleSprites += [sprite2D(doodlePoly2, (50, None), PURPLE, True)]
  232. doodleSprites += [pygame.transform.flip(doodleSprites[2], True, False)]
  233.  
  234. doodleSpriteID = 2
  235. doodleWidth = doodleSprites[0].get_rect().width
  236. doodleWidthHalf = doodleWidth / 2
  237.  
  238. PLATFORM_WIDTH = 50
  239. PLATFORM_HEIGHT = 15
  240. PLATFORM_W2 = PLATFORM_WIDTH * 2
  241. PLATFORM_EXCLUSION_SIZE = 20
  242. SOLID_PLATFORM_MAX_DISTANCE_AT_START = 100
  243. PLATFORM_START_COUNT = 10
  244. P = platforms()
  245.        
  246. GRAVITY = 0.5
  247. INCREMENT_VELOCITY_X = 0.5
  248. MAX_VELOCITY_X = 4
  249. MAX_VELOCITY_Y = 20
  250. VELOCITY_X_SLOW_DOWN = 1.05
  251.  
  252. dx = float(HW)
  253. dy = float(H - doodleSprites[2].get_rect().height)
  254. dyv = -MAX_VELOCITY_Y
  255. dxv = 0
  256.  
  257. screenshot = False
  258. # main loop
  259. while not quit():
  260.     P.draw(DS)
  261.     DS.blit(doodleSprites[doodleSpriteID], (dx - doodleWidthHalf, dy - doodleSprites[doodleSpriteID].get_rect().height))
  262.     pygame.display.update()
  263.    
  264.     if screenshot:
  265.         pygame.image.save(DS, "screenshot.png")
  266.         pygame.quit()
  267.         sys.exit()
  268.        
  269.     DS.fill(WHITE)
  270.     CLOCK.tick(FPS)
  271.    
  272.     collision = False
  273.     if dy <= H / 2 and dyv < 0:
  274.         P.move(abs(dyv))
  275.     else:
  276.         if dyv >=0:
  277.             collision = P.collision(dx, dy, dyv)               
  278.             if collision:
  279.                 dy = collision
  280.                 dyv = -MAX_VELOCITY_Y
  281.         if not collision:
  282.             dy += dyv
  283.     if not collision: dyv += GRAVITY
  284.    
  285.     k = pygame.key.get_pressed()
  286.     if k[K_LEFT]:
  287.         dxv -= INCREMENT_VELOCITY_X
  288.         if dxv < -MAX_VELOCITY_X: dxv = -MAX_VELOCITY_X
  289.     if k[K_RIGHT]:
  290.         dxv += INCREMENT_VELOCITY_X
  291.         if dxv > MAX_VELOCITY_X: dxv = MAX_VELOCITY_X
  292.     if not k[K_LEFT] and not k[K_RIGHT]: dxv /= VELOCITY_X_SLOW_DOWN
  293.     if k[K_RETURN]: screenshot = True
  294.    
  295.     dx += dxv
  296.     if dx < -doodleWidthHalf: dx = W + doodleWidthHalf
  297.     if dx > W + doodleWidthHalf: dx = -doodleWidthHalf
  298.    
  299.     if dyv > 0:
  300.         doodleSpriteID = 0
  301.         if dxv < 0:
  302.             doodleSpriteID = 1
  303.     else:
  304.         doodleSpriteID = 2
  305.         if dxv < 0:
  306.             doodleSpriteID = 3
  307.        
  308. pygame.quit()
  309. sys.exit()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement