cookertron

Bouncing Title Cubes - Python Pygame

Dec 26th, 2020
278
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.37 KB | None | 0 0
  1. import sys, os.path
  2. import base64, lzma, random
  3. import pygame
  4.  
  5. titleZIP = b'/Td6WFoAAATm1rRGAgAhARYAAAB0L+WjAQELiVBORw0KGgoAAAANSUhEUgAAAEIAAAALBAMAAAA5LUZ\
  6. AAAABGdBTUEAALGPC/xhBQAAAAFzUkdCAK7OHOkAAAAPUExURQAAANuqAbxYIaqFBP///5E0qmAAAACbSURBVBjTdY6B\
  7. DcQgCEU/7QISFzBOYGIHuP2nugfaNrnkiH7o50mRu8tcqoVUJXkR1aAYollDLkFcWFQoYvWT925WWVlCWKlrRlwPx+NY\
  8. oZ2flB4E3m2tLdy0CY9FAvMg3LegSeQz+yHmZG2dyMw0CfIc45xhST1nH42qkY5OYB6dcjVfAgci0H+EeL2I1hPv2gMf\
  9. gj+0vgk9xBdehhKMcdGEzAAAAABJRU5ErkJgggCQOMXDkH/P4wABpAKMAgAAqAkTn7HEZ/sCAAAAAARZWg=='
  10.  
  11. if not os.path.exists("pygame.png"):
  12.     print("Writing title image...", end = "")
  13.     data = lzma.decompress(base64.b64decode(titleZIP))
  14.     r = open("pygame.png", "bw")
  15.     r.write(data)
  16.     r.close()
  17.     print("Done!")
  18.  
  19. pygame.init()
  20. PDR = pygame.Rect(0, 0, 1280, 720)
  21. PDS = pygame.display.set_mode(PDR.size)
  22. FPS = 120
  23.  
  24. GRAVITY = 0.1
  25. CUBE_DIMENSIONS = pygame.Rect(0, 0, 10, 10)
  26. TIMER = FPS / 2
  27.  
  28. class drop:
  29.     class cube:
  30.         def __init__(s, color, x, floor):
  31.             global CUBE_DIMENSIONS
  32.             s.floor = floor * CUBE_DIMENSIONS.h
  33.             s.speed = 0
  34.             s.prevImpactSpeed = 0
  35.             s.r = pygame.Rect((x * CUBE_DIMENSIONS.w, -CUBE_DIMENSIONS.h), CUBE_DIMENSIONS.size)
  36.             s.color = color
  37.    
  38.     class column:
  39.         def __init__(s):
  40.             global CUBE_DIMENSIONS, TIMER
  41.             s.cubes = []
  42.             s.timer = random.randint(0, TIMER)
  43.  
  44.     def __init__(s, imageSurface):
  45.         global PDS, PDR
  46.         global CUBE_DIMENSIONS
  47.    
  48.         s.r = imageSurface.get_rect()
  49.         cx = int(PDR.w / CUBE_DIMENSIONS.w) / 2 - s.r.centerx
  50.         cy = int(PDR.h / CUBE_DIMENSIONS.h) / 2 - s.r.centery
  51.         s.statics = []
  52.         pa = pygame.PixelArray(imageSurface)
  53.  
  54.         s.columns = []
  55.         for x in range(s.r.w):
  56.             s.columns.append(s.column())
  57.             for y in range(s.r.h):
  58.                 if pa[x][y] != 0: #16777215 (white)
  59.                     s.columns[x].cubes.append(s.cube(PDS.unmap_rgb(pa[x][y]), cx + x, cy + y))
  60.         pa.close()
  61.  
  62.     def do(s):
  63.         global PDS, GRAVITY, TIMER
  64.        
  65.         for static in s.statics:
  66.             pygame.draw.rect(PDS, static.color, static.r)
  67.  
  68.         for c in s.columns:
  69.  
  70.             if not c.cubes: continue
  71.             if c.timer:
  72.                 c.timer -= 1
  73.                 continue
  74.  
  75.             currentCube = c.cubes[-1]
  76.             pygame.draw.rect(PDS, currentCube.color, currentCube.r)
  77.  
  78.             currentCube.r.y += currentCube.speed
  79.             currentCube.speed += GRAVITY
  80.             if currentCube.r.bottom >= currentCube.floor:
  81.                 currentCube.r.bottom = currentCube.floor
  82.                 if currentCube.prevImpactSpeed == 0:
  83.                     currentCube.prevImpactSpeed = currentCube.speed
  84.                 currentCube.prevImpactSpeed /= 3
  85.                 currentCube.speed = -currentCube.prevImpactSpeed
  86.                 if currentCube.prevImpactSpeed <= 0.5:
  87.                     s.statics.append(c.cubes.pop(-1))
  88.                     c.timer = random.randint(0, TIMER)
  89.  
  90. c = drop(pygame.image.load("pygame.png").convert())
  91.  
  92. while True:
  93.     events = pygame.event.get()
  94.     for e in events:
  95.         if e.type == pygame.QUIT: sys.exit()
  96.  
  97.     PDS.fill((0, 0, 0))
  98.    
  99.     c.do()
  100.  
  101.     pygame.display.update()
  102.     pygame.time.Clock().tick(FPS)
  103.  
  104. pygame.quit()
  105. sys.exit()
Add Comment
Please, Sign In to add comment