Advertisement
cookertron

Doodle Jump Clone using Python & Pygame

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