cookertron

Platform Example 1 - Python Pygame

Jul 3rd, 2021
254
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.81 KB | None | 0 0
  1. import base64, lzma
  2. import pygame
  3.  
  4. class player:
  5.     def __init__(s):
  6.         s.x, s.y, s.vx, s.vy = (0, 0, 0, 0)
  7.         s.jumping = False
  8.  
  9.     def spawn(s,  x, y):
  10.         global TILE_SIZE
  11.  
  12.         s.x, s.y, s.vx, s.vy = (x * TILE_SIZE, y * TILE_SIZE, 0, 0)
  13.         s.jumping = False
  14.  
  15.     def draw(s):
  16.         global PDS, TILE_SIZE
  17.  
  18.         pygame.draw.rect(PDS, WHITE, (s.x, s.y - TILE_SIZE, TILE_SIZE, TILE_SIZE))
  19.  
  20.     def update(s, tm):
  21.         global GRAVITY, TILE_SIZE, TILEMAP_RES
  22.        
  23.         onPlatform = False
  24.         x1 = int(s.x // TILE_SIZE)
  25.         y1 = int(s.y // TILE_SIZE)
  26.         for y in range(y1, y1 + 2):
  27.             for x in range(x1, x1 + 2):
  28.                 if x < 0 or x >= TILEMAP_RES[0] or y < 0 or  y >= TILEMAP_RES[1]: continue
  29.                 if tm[y][x]:
  30.                     # tilePosX = x * TILE_SIZE
  31.                     tilePosY = y * TILE_SIZE
  32.                     if s.y <= tilePosY and s.y + s.vy >= tilePosY:
  33.                         onPlatform = True
  34.                         s.jumping = False
  35.                         s.y = tilePosY
  36.                         s.vy = 0
  37.  
  38.         if not onPlatform:
  39.             s.jumping = True    
  40.             s.vy += GRAVITY
  41.         s.y += s.vy    
  42.  
  43.         k = pygame.key.get_pressed()
  44.         if k[pygame.K_RIGHT]: s.vx = 4
  45.         elif k[pygame.K_LEFT]: s.vx = -4
  46.         else:
  47.             s.vx = 0
  48.         s.x += s.vx
  49.  
  50.         if k[pygame.K_SPACE] and not s.jumping:
  51.             s.jumping = True
  52.             s.vy = -11
  53.  
  54. def createTilemap(lzma64):
  55.     global TILEMAP_RES
  56.     data = list(bytes(lzma.decompress(base64.b64decode(lzma64))))
  57.     return [data[i : i + TILEMAP_RES[0]] for i in range(0, len(data), TILEMAP_RES[0])]
  58.  
  59. BLACK = (0, 0, 0)
  60. WHITE = (255, 255, 255)
  61. RED = (128, 0, 0)
  62. GREEN = (0, 128, 0)
  63. BLUE = (0, 0, 128)
  64. GRAY = (128, 128, 128)
  65. DARK_GRAY = (64, 64, 64)
  66.  
  67. PDV = (1280, 800)
  68. TILE_SIZE = 32
  69. TILEMAP_RES = (PDV[0] // TILE_SIZE, PDV[1] // TILE_SIZE)
  70. FPS = 120
  71.  
  72. GRAVITY = 0.5
  73.  
  74. tilemap_LZMA64 = b'/Td6WFoAAATm1rRGAgAhARYAAAB0L+Wj4APnAC1dAACAQCtotkpfGorGP6lXd8DSBXD8vbJFntLka7G7CZaVPZGEfipxYUhTFwUpAAAAAAB3f+pq22lYrwABSegHAAAA3jtxbrHEZ/sCAAAAAARZWg=='
  75. tilemap = createTilemap(tilemap_LZMA64)
  76.  
  77. p = player()
  78. p.spawn(1, 1)
  79.  
  80. pygame.init()
  81. PDS = pygame.display.set_mode(PDV)
  82.  
  83. while True:
  84.     events = pygame.event.get()
  85.     for e in events:
  86.         if e.type == pygame.QUIT: sys.exit()
  87.  
  88.     PDS.fill(BLACK)
  89.  
  90.     for y in range(TILEMAP_RES[1]):
  91.         for x in range(TILEMAP_RES[0]):
  92.             tileID = tilemap[y][x]
  93.             if not tileID: continue
  94.             x1 = x * TILE_SIZE
  95.  
  96.             pygame.draw.rect(PDS, GRAY, (x * TILE_SIZE, y * TILE_SIZE, TILE_SIZE, TILE_SIZE))
  97.  
  98.     p.draw()
  99.  
  100.     pygame.display.update()
  101.     pygame.time.Clock().tick(FPS)
  102.  
  103.     p.update(tilemap)
  104.  
  105. pygame.quit()
Add Comment
Please, Sign In to add comment