Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import sys
- import base64, lzma
- import pygame
- class player:
- def __init__(s):
- s.x, s.y, s.vx, s.vy = (0, 0, 0, 0)
- s.jumping = False
- def spawn(s, x, y):
- global TILE_SIZE
- s.x, s.y, s.vx, s.vy = (x * TILE_SIZE, y * TILE_SIZE, 0, 0)
- s.jumping = False
- def draw(s):
- global PDS, TILE_SIZE
- pygame.draw.rect(PDS, WHITE, (s.x, s.y, TILE_SIZE, TILE_SIZE))
- def update(s, tilemap):
- global GRAVITY, TILE_SIZE, TILEMAP_RES
- k = pygame.key.get_pressed()
- if k[pygame.K_RIGHT]: s.vx = 4
- elif k[pygame.K_LEFT]: s.vx = -4
- else:
- s.vx = 0
- onPlatform = False
- inWall = False
- playerTileX = int(s.x // TILE_SIZE)
- playerTileY = int(s.y // TILE_SIZE)
- for tileY in range(playerTileY - 1, playerTileY + 3):
- for tileX in range(playerTileX - 2, playerTileX + 3):
- if tileX < 0 or tileX >= TILEMAP_RES[0] or tileY < 0 or tileY >= TILEMAP_RES[1]: continue
- if tilemap[tileY][tileX]:
- tx, ty = (tileX * TILE_SIZE, tileY * TILE_SIZE)
- if tilemap[tileY][tileX] == 2:
- 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:
- inWall = True
- if s.y + TILE_SIZE - 1 < ty or s.y > ty + TILE_SIZE - 1: continue
- if s.vx > 0:
- if s.x + TILE_SIZE <= tx and s.x + TILE_SIZE + s.vx >= tx:
- s.x = tx - TILE_SIZE
- s.vx = 0
- if s.vx < 0:
- if s.x >= tx + TILE_SIZE and s.x + s.vx <= tx + TILE_SIZE:
- s.x = tx + TILE_SIZE
- s.vx = 0
- if tilemap[tileY][tileX] == 1:
- if s.x + TILE_SIZE - 1 < tx or s.x > tx + TILE_SIZE - 1: continue
- if s.y + TILE_SIZE <= ty and s.y + TILE_SIZE + s.vy >= ty and not inWall:
- onPlatform = True
- s.jumping = False
- s.y = ty - TILE_SIZE
- s.vy = 0
- if not onPlatform:
- s.jumping = True
- s.vy += GRAVITY
- s.y += s.vy
- s.x += s.vx
- if k[pygame.K_SPACE] and not s.jumping:
- s.jumping = True
- s.vy = -11
- def createTilemap(lzma64):
- global TILEMAP_RES
- data = list(bytes(lzma.decompress(base64.b64decode(lzma64))))
- return [data[i : i + TILEMAP_RES[0]] for i in range(0, len(data), TILEMAP_RES[0])]
- BLACK = (0, 0, 0)
- WHITE = (255, 255, 255)
- RED = (128, 0, 0)
- GREEN = (0, 128, 0)
- BLUE = (0, 0, 128)
- GRAY = (128, 128, 128)
- DARK_GRAY = (64, 64, 64)
- TILE_COLORS = [BLACK, GRAY, GREEN, BLUE]
- PDV = (1280, 800)
- TILE_SIZE = 32
- TILEMAP_RES = (PDV[0] // TILE_SIZE, PDV[1] // TILE_SIZE)
- FPS = 120
- GRAVITY = 0.5
- tilemap_LZMA64 = b'/Td6WFoAAATm1rRGAgAhARYAAAB0L+Wj4APnAEFdAAEAhAjIcfBHz046pX2I9q3TN9ls6Flkf4Dgv1ecjPr4QSts4uPQCNY4NPBrzGUFFq1dGgdrEiPQ834zfVP3rAVAAAAAAAKCObu/I2DHAAFd6AcAAABTezb2scRn+wIAAAAABFla'
- tilemap = createTilemap(tilemap_LZMA64)
- p = player()
- p.spawn(3, 1)
- pygame.init()
- PDS = pygame.display.set_mode(PDV)
- while True:
- events = pygame.event.get()
- for e in events:
- if e.type == pygame.QUIT: sys.exit()
- PDS.fill(BLACK)
- for y in range(TILEMAP_RES[1]):
- for x in range(TILEMAP_RES[0]):
- tileID = tilemap[y][x]
- if not tileID: continue
- x1 = x * TILE_SIZE
- pygame.draw.rect(PDS, TILE_COLORS[tileID], (x * TILE_SIZE, y * TILE_SIZE, TILE_SIZE, TILE_SIZE))
- p.draw()
- pygame.display.update()
- pygame.time.Clock().tick(FPS)
- p.update(tilemap)
- pygame.quit()
Add Comment
Please, Sign In to add comment