cookertron

Platform Example 2 - Python Pygame

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