Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # chuckie egg clone, as see on a ZX Spectrum
- # Version 1.6.1
- # Coded by Anthony Cook and posted to fb.com/groups/pygame
- # NOTES: This is the same as version 1.6.0 but doesn't require PixelArray Functionality
- import pygame
- import pygame.gfxdraw
- import base64, lzma
- import collections
- import math, time, random
- # game time
- class gTime:
- def __init__(s, fps): # input = frames per second
- s.fps = fps
- s.tpf = 1 / fps * 1000000000
- s.time = 0
- s.timeStamp = time.time_ns()
- def tick(s):
- while time.time_ns() - s.timeStamp < s.tpf:
- pass
- s.timeStamp = time.time_ns()
- s.time += 1
- def imgUnzip(lz64):
- data = lzma.decompress(base64.b64decode(lz64))
- imageW = int.from_bytes(data[0:2], "big")
- imageH = int.from_bytes(data[2:4], "big")
- paletteLength = int.from_bytes(data[4:5], "big")
- palette = [int.from_bytes(data[5 + index * 3:8 + index * 3], "big") for index in range(paletteLength)]
- pixelDataStart = 5 + paletteLength * 3
- surface = pygame.Surface((imageW, imageH))
- #pixelArray = pygame.PixelArray(surface)
- for index in range(0, len(data) - pixelDataStart):
- #pixelArray[index % imageW][int(index / imageW)] = palette[data[pixelDataStart + index]]
- surface.set_at((index % imageW, int(index / imageW)), surface.unmap_rgb(palette[data[pixelDataStart + index]]))
- #pixelArray.close()
- #del pixelArray
- return surface
- def decodeMap(mapB64):
- mapBytes = base64.b64decode(mapB64)
- mapData = collections.OrderedDict()
- while mapBytes:
- # get the name of the layer and add it to the mapData dictionary
- layerNameLen = mapBytes[0]
- layerName = mapBytes[1:layerNameLen + 1].decode('utf-8')
- mapBytes = mapBytes[layerNameLen + 1:]
- mapData[layerName] = []
- objectCount = mapBytes[0]
- mapBytes = mapBytes[1:]
- for index in range(0, objectCount):
- mapData[layerName].append(globals()[layerName](mapBytes[0], mapBytes[1], mapBytes[2]))
- mapBytes = mapBytes[3:]
- return mapData
- class cycle:
- def __init__(s, l, h, bounce = False):
- s.index = l
- s.l = l
- s.h = h
- s.d = 1
- s.b = bounce
- def __call__(s):
- s.index += s.d
- if s.b:
- if s.index == s.h or s.index == s.l: s.d = -s.d
- else:
- if s.index > s.h: s.index = s.l
- def reset(s):
- s.index = s.l
- s.d = 1
- return s
- def set(s, index):
- s.index = s.l + index
- return s
- class font:
- def __init__(s, img, charMap, spacing):
- s.surfaceWidth, s.surfaceHeight = img.get_size()
- s.fontSurface = img.copy()
- s.monochromeFonts = {}
- s.cols = len(charMap)
- s.charWidth = int(s.surfaceWidth / s.cols)
- s.spacing = spacing
- s.charMap = charMap
- def text(s, text, scale = None, colorName = None):
- surfaceWidth = len(text) * s.charWidth + s.spacing * (len(text) - 1)
- surface = pygame.Surface((surfaceWidth, s.surfaceHeight))
- charPosX = 0
- for c in text:
- charIndex = s.charMap.find(c)
- if charIndex == -1:
- charPosX += s.charWidth + s.spacing
- continue
- charOffsetX = charIndex * s.charWidth
- if colorName:
- surface.blit(s.monochromeFonts[colorName], (charPosX, 0), (charOffsetX, 0, s.charWidth, s.surfaceHeight))
- else:
- surface.blit(s.fontSurface, (charPosX, 0), (charOffsetX, 0, s.charWidth, s.surfaceHeight))
- charPosX += s.charWidth + s.spacing
- if scale != None:
- return pygame.transform.scale(surface, (surfaceWidth * scale, s.surfaceHeight * scale))
- return surface
- def newMonochromeFont(s, name, color):
- global DS
- monochromeSurface = s.fontSurface.copy()
- monochromePA = pygame.PixelArray(monochromeSurface)
- for x in range(s.surfaceWidth):
- for y in range(s.surfaceHeight):
- if monochromePA[x][y] != 0: monochromePA[x][y] = DS.map_rgb(color)
- monochromePA.close()
- del monochromePA
- s.monochromeFonts[name] = monochromeSurface
- def size(s, text, scale = 1):
- w = len(text) * s.charWidth + s.spacing * (len(text) - 1)
- h = s.surfaceHeight
- return [w * scale, h * scale]
- class sprite:
- def __init__(s, lz64, cols, rows, colorKey = None):
- global SCALE
- s.surface = imgUnzip(lz64)
- w, h = s.surface.get_size()
- if SCALE != 1:
- s.surface = pygame.transform.scale(s.surface, (int(w * SCALE), int(h * SCALE)))
- w *= SCALE
- h *= SCALE
- cw, ch = int(w / cols), int(h / rows)
- s.rects = [[(index % cols) * cw, int(index / cols) * ch, cw, ch] for index in range(cols * rows)]
- if colorKey: s.surface.set_colorkey(colorKey)
- def blit(s, spriteID, surface, pos):
- surface.blit(s.surface, pos, s.rects[spriteID])
- class rect:
- def __init__(s, x, y, w, h):
- s.x1, s.y1, s.w, s.h = x, y, w, h
- s.x2, s.y2 = s.x1 + s.w, s.y1 + s.h
- def setX1(s, x):
- s.x1 = x
- s.x2 = x + s.w
- def setY1(s, y):
- s.y1 = y
- s.y2 = y + s.h
- def setXY(s, x, y):
- s.x1, s.y1 = x, y
- s.x2, s.y2 = x + s.w, y + s.h
- def setX2(s, x):
- s.x2 = x
- s.x1 = x - s.w
- def setY2(s, y):
- s.y2 = y
- s.y1 = y - s.h
- def setXY2(s, x, y):
- s.x2, s.y2 = x, y
- s.x1, s.y1 = x - s.w, y - s.h
- def offX1(s, x):
- s.x1 += x
- s.x2 = s.x1 + s.w
- def offY1(s, y):
- s.y1 += y
- s.y2 = s.y1 + s.h
- def pos(s, integer = False, offset = [0, 0]):
- if integer: return [int(s.x1 + offset[0]), int(s.y1 + offset[1])]
- return [s.x1 + offset[0], s.y1 + offset[1]]
- def rect(s, integer = False):
- if integer: return [int(s.x1), int(s.y1), int(s.w), int(s.h)]
- return [s.x1, s.y1, s.w, s.h]
- def box(s, integer = False):
- if integer: return [int(s.x1), int(s.y1), int(s.x2), int(s.y2)]
- return [s.x1, s.y1, s.x2, s.y2]
- def size(s, integer = False):
- if integer: return [int(s.w), int(s.h)]
- return [s.w, s.h]
- def scale(s, value):
- s.x1 *= value
- s.y1 *= value
- s.w *= value
- s.h *= value
- s.x2 = s.x1 + s.w
- s.y2 = s.y1 + s.h
- return s
- def __str__(s):
- return "x1={}, y1={}, w={}, h={} (x2={}, y2={})".format(s.x1, s.y1, s.w, s.h, s.x2, s.y2)
- class platforms:
- def __init__(s, x, y, w):
- global TILE_SIZE
- global SPRITE_SETS, PLATFORM_TILE
- s.rect = rect(x, y, w, 1).scale(TILE_SIZE)
- s.surface = pygame.Surface(s.rect.size(True))
- for index in range(w):
- SPRITE_SETS['tiles'].blit(PLATFORM_TILE, s.surface, (index * TILE_SIZE, 0))
- class latforms:
- def __init__(s, x, y, w):
- global TILE_SIZE
- s.rect = rect(x, y, w, 1).scale(TILE_SIZE)
- class ratforms:
- def __init__(s, x, y, w):
- global TILE_SIZE
- s.rect = rect(x, y, w, 1).scale(TILE_SIZE)
- class ladders:
- def __init__(s, x, y, h):
- global TILE_SIZE
- global LADDER_SEGMENT
- s.rect = rect(x, y, 2, h).scale(TILE_SIZE)
- s.surface = pygame.Surface(s.rect.size(True))
- for index in range(h):
- s.surface.blit(LADDER_SEGMENT, (0, index * TILE_SIZE))
- class eggs:
- def __init__(s, x, y, z):
- global TILE_SIZE
- global EGG_SPRITE
- s.rect = rect(x, y, 1, 1).scale(TILE_SIZE)
- s.surface = EGG_SPRITE
- class hay:
- def __init__(s, x, y, z):
- global TILE_SIZE
- global HAY_SPRITE
- s.rect = rect(x, y, 1, 1).scale(TILE_SIZE)
- s.surface = HAY_SPRITE
- class lifts:
- def __init__(s, x, y, z):
- global TILE_SIZE
- global LIFT_SPRITE
- s.moveToggle = 0
- s.rect = rect(x, y, 2, 1).scale(TILE_SIZE)
- s.surface = LIFT_SPRITE
- def do(s):
- global SCALE, TILE_SIZE
- global H
- if not s.moveToggle:
- s.rect.offY1(-SCALE)
- s.moveToggle = 1 - s.moveToggle
- if s.rect.y1 <= TILE_SIZE * 3:
- s.rect.setY1(H - TILE_SIZE)
- HARRY_WALKING_RIGHT = cycle(0, 1)
- HARRY_WALKING_LEFT = cycle(2, 3)
- HARRY_CLIMBING_LADDER = cycle(4, 6, True)
- # key binds
- UP = pygame.K_UP
- DOWN = pygame.K_DOWN
- LEFT = pygame.K_LEFT
- RIGHT = pygame.K_RIGHT
- SPACE = pygame.K_SPACE
- class harry:
- def __init__(s, x, y):
- global SPEED
- global SCALE, TILE_SIZE
- global HARRY_WALKING_RIGHT
- s.rect = rect(x, y, 1, 2).scale(TILE_SIZE)
- s.xIndex = x * SPEED
- s.xIndexDirection = 0
- s.jumpHeight = 0
- s.state = s.falling
- s.currentObject = None
- s.anim = HARRY_WALKING_RIGHT.set(1)
- def draw(s):
- global SCALE
- global SPRITE_SETS
- global DS
- SPRITE_SETS['harry'].blit(s.anim.index, DS, s.rect.pos(True, [-s.rect.w / 4, 0]))
- #pygame.draw.rect(DS, [255, 0, 0], s.rect.rect(True))
- def do(s):
- s.keys = pygame.key.get_pressed()
- s.state()
- if type(s.currentObject) == lifts:
- s.rect.setY2(s.currentObject.rect.y1)
- def falling(s):
- global Y_VELOCITY
- global MAPS, MAP_INDEX
- if s.collidedWithPlatform(): return
- s.rect.offY1(Y_VELOCITY)
- def stop(s):
- global JUMP_HEIGHT
- global HARRY_WALKING_LEFT, HARRY_WALKING_RIGHT
- global LEFT, RIGHT, SPACE
- if s.hasClimbedLadder(): return
- if s.keys[LEFT]:
- s.xIndexDirection = -1
- s.anim = HARRY_WALKING_LEFT.reset()
- s.state = s.walkLeft
- elif s.keys[RIGHT]:
- s.xIndexDirection = 1
- s.anim = HARRY_WALKING_RIGHT.reset()
- s.state = s.walkRight
- if s.keys[SPACE]:
- s.jumpHeight = JUMP_HEIGHT
- s.anim.set(1)
- s.currentObject = None
- s.state = s.jumpUp
- def walkRight(s):
- global X_VELOCITY, JUMP_HEIGHT
- global MAPS, MAP_INDEX
- global RIGHT, SPACE
- global W
- if s.hasClimbedLadder(): return
- if s.keys[SPACE]:
- s.jumpHeight = JUMP_HEIGHT
- s.anim.set(1)
- s.currentObject = None
- s.state = s.jumpUp
- return
- if not s.keys[RIGHT]:
- s.xIndexDirection = 0
- s.anim.set(0)
- s.state = s.stop
- return
- if s.collidedWithLeftEdge():
- s.anim.set(0)
- return
- if s.xIndex == 186: return
- s.anim()
- s.xIndex += s.xIndexDirection
- s.rect.setX1(s.xIndex * X_VELOCITY)
- if s.rect.x1 == s.currentObject.rect.x2:
- if not s.collidedWithPlatform(MAPS[MAP_INDEX]['latforms'] + MAPS[MAP_INDEX]['ratforms']):
- s.xIndexDirection = 0
- s.currentObject = None
- s.anim.set(1)
- s.state = s.falling
- return
- def walkLeft(s):
- global X_VELOCITY, JUMP_HEIGHT
- global LEFT, UP, DOWN, SPACE
- if s.hasClimbedLadder(): return
- if s.keys[SPACE]:
- s.jumpHeight = JUMP_HEIGHT
- s.anim.set(1)
- s.currentObject = None
- s.state = s.jumpUp
- return
- if not s.keys[LEFT]:
- s.xIndexDirection = 0
- s.anim.set(0)
- s.state = s.stop
- return
- if s.collidedWithRightEdge():
- s.anim.set(0)
- return
- if s.xIndex == 0: return
- s.anim()
- s.xIndex += s.xIndexDirection
- s.rect.setX1(s.xIndex * X_VELOCITY)
- if s.rect.x2 == s.currentObject.rect.x1:
- if not s.collidedWithPlatform(MAPS[MAP_INDEX]['latforms'] + MAPS[MAP_INDEX]['ratforms']):
- s.xIndexDirection = 0
- s.currentObject = None
- s.anim.set(1)
- s.state = s.falling
- return
- def jumpUp(s):
- global X_VELOCITY, Y_VELOCITY
- if s.hasClimbedLadder(): return
- if s.xIndex == 0 or s.xIndex == 186:
- s.xIndexDirection = -s.xIndexDirection
- s.xIndex += s.xIndexDirection
- s.rect.setX1(s.xIndex * X_VELOCITY)
- s.rect.offY1(-Y_VELOCITY)
- s.jumpHeight -= 1
- if not s.jumpHeight:
- s.state = s.jumpDown
- def jumpDown(s):
- global X_VELOCITY, Y_VELOCITY
- if s.hasClimbedLadder(): return
- if s.xIndex == 0 or s.xIndex == 186:
- s.xIndexDirection = -s.xIndexDirection
- if s.xIndexDirection > 0:
- if s.collidedWithLeftEdge(): s.xIndexDirection = -s.xIndexDirection
- elif s.xIndexDirection < 0:
- if s.collidedWithRightEdge(): s.xIndexDirection = -s.xIndexDirection
- s.xIndex += s.xIndexDirection
- s.rect.setX1(s.xIndex * X_VELOCITY)
- if s.collidedWithPlatform(MAPS[MAP_INDEX]['lifts']): return
- s.rect.offY1(Y_VELOCITY)
- def climbing(s):
- global X_VELOCITY, Y_VELOCITY, JUMP_HEIGHT
- global HARRY_WALKING_LEFT, HARRY_WALKING_RIGHT
- global MAPS, MAP_INDEX
- global UP, DOWN, LEFT, RIGHT, SPACE
- if s.keys[UP]:
- if s.rect.y1 > s.currentObject.rect.y1:
- s.rect.offY1(-Y_VELOCITY)
- s.anim()
- if s.keys[DOWN]:
- if s.rect.y2 < s.currentObject.rect.y2:
- s.rect.offY1(Y_VELOCITY)
- s.anim()
- if s.keys[SPACE]:
- s.jumpHeight = JUMP_HEIGHT
- s.currentObject = None
- s.state = s.jumpUp
- if s.keys[LEFT]:
- s.xIndexDirection = -1
- s.anim = HARRY_WALKING_LEFT.set(1)
- elif s.keys[RIGHT]:
- s.xIndexDirection = 1
- s.anim = HARRY_WALKING_RIGHT.set(1)
- else:
- s.xIndexDirection = 0
- s.anim = HARRY_WALKING_RIGHT.set(1)
- return
- #print(s.rect.y1, s.rect.y2, s.currentObject.rect.y1)
- if not (s.keys[LEFT] or s.keys[RIGHT]): return
- for platform in MAPS[MAP_INDEX]['platforms'] + MAPS[MAP_INDEX]['latforms'] + MAPS[MAP_INDEX]['ratforms']:
- if s.rect.y2 == platform.rect.y1 and s.rect.x1 > platform.rect.x1 and s.rect.x2 < platform.rect.x2:
- if s.keys[LEFT] and type(platform) != ratforms:
- s.currentObject = platform
- s.xIndexDirection = -1
- s.anim = HARRY_WALKING_LEFT.reset()
- s.state = s.walkLeft
- elif s.keys[RIGHT] and type(platform) != latforms:
- s.currentObject = platform
- s.xIndexDirection = 1
- s.anim = HARRY_WALKING_RIGHT.reset()
- s.state = s.walkRight
- s.xIndex += s.xIndexDirection
- s.rect.setX1(s.xIndex * X_VELOCITY)
- break
- def collidedWithLeftEdge(s):
- global SCALE, TILE_SIZE
- global MAPS, MAP_INDEX
- for platform in MAPS[MAP_INDEX]['platforms']:
- if s.rect.x2 != platform.rect.x1: continue
- if s.rect.y1 + TILE_SIZE > platform.rect.y2 or s.rect.y2 < platform.rect.y1 + SCALE: continue
- return True
- return False
- def collidedWithRightEdge(s):
- global SCALE, TILE_SIZE
- global MAPS, MAP_INDEX
- for platform in MAPS[MAP_INDEX]['platforms']:
- if s.rect.x1 != platform.rect.x2: continue
- if s.rect.y1 + TILE_SIZE > platform.rect.y2 or s.rect.y2 < platform.rect.y1 + SCALE: continue
- return True
- return False
- def collidedWithPlatform(s, additionalObjects = []): # worded as a question
- global Y_VELOCITY
- global MAPS, MAP_INDEX
- for platform in MAPS[MAP_INDEX]['platforms'] + additionalObjects:
- if not (s.rect.x1 >= platform.rect.x2 or s.rect.x2 <= platform.rect.x1):
- if (s.rect.y2 < platform.rect.y1 and s.rect.y2 + Y_VELOCITY >= platform.rect.y1) or s.rect.y2 == platform.rect.y1:
- s.rect.setY2(platform.rect.y1)
- s.xIndexDirection = 0
- s.currentObject = platform
- s.anim.set(0)
- s.state = s.stop
- return True
- return False
- def hasClimbedLadder(s):
- global HARRY_CLIMBING_LADDER
- global SCALE
- global MAPS, MAP_INDEX
- global UP, DOWN
- if not (s.keys[UP] or s.keys[DOWN]): return
- for ladder in MAPS[MAP_INDEX]['ladders']:
- if s.rect.x1 == ladder.rect.x1 + SCALE * 4:
- if (s.keys[UP] and s.rect.y1 > ladder.rect.y1 and s.rect.y2 <= ladder.rect.y2) or (s.keys[DOWN] and s.rect.y2 < ladder.rect.y2 and s.rect.y1 >= ladder.rect.y1):
- s.xIndexDirection = 0
- s.currentObject = ladder
- s.anim = HARRY_CLIMBING_LADDER.reset()
- s.state = s.climbing
- return
- #print("can climb, {} {} {} {}".format(ladder.rect.x1, ladder.rect.y1, ladder.rect.x2, ladder.rect.y2))
- # set up scale and pygame variables
- SCALE = 5
- ZX_TILE_SIZE = 8
- TILE_SIZE = ZX_TILE_SIZE * SCALE
- WIDTH_IN_TILES = 32
- HEIGHT_IN_TILES = 24
- FPS = 60
- W, H = int(WIDTH_IN_TILES * TILE_SIZE), int(HEIGHT_IN_TILES * TILE_SIZE)
- HW, HH = int(W / 2), int(H / 2)
- pygame.init()
- DS = pygame.display.set_mode((W, H))
- TIME = gTime(FPS)
- FONT_DATA = b'/Td6WFoAAATm1rRGAgAhARYAAAB0L+Wj4Aw9AW5dAACO8kOjEfa2JjD81EffSaUPknD93Eph1Fsy7hNTgvcZJxKO8vuqgc3XN\
- 3Lz2F/+rT9w9iuzsmBXzqTvbkREQ7Io0T/ALfy6MU/6BR7cC0iyLG9bk6y+A7e4DJDb86aloX9r2qeu1DnBUOu7ie8Y+Z8hOQg9rNd4bu5wlIPL\
- UIejdSo0RcIJiS5w1xmTlmFEI8n7isSpL+Zl3NNk/Hgrd+ajfx+dJ2r/+ENsaRYWNLJ8ZRkUMl3ECYmC9pzK5cMH9EZ/CTJLhNIFZq3EpapLc7+\
- dRZOMN3uxtEshysWcJJHip5CTAkjAoFbu4Tl0J8gtke66of1u/gv69rGbru0lbd0eY5MdQfHs1U1WtrBgJ7Di9mSu7h1YHvm07W8DhcOkb26+NG\
- 0/kV7JyQMczTE6VV6qN2FAK8JnjpkCh40xg63Dxi2m7N3JiKvl1tE/gPOvSJkGQIus7HQFHXjc6DouLaw3zh81ra8HDg4ZRQAvLAAAALI4wzJkk\
- ZgtAAGKA74YAADSUkbUscRn+wIAAAAABFla'
- FONT_CHARMAP = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789.!-"
- HARRY_SPRITES = b'/Td6WFoAAATm1rRGAgAhARYAAAB0L+Wj4AVKAH9dAAAWAmcAfBl/5TWMp19wwFlOGq24MSuPf3eRkc2R2RivFpE4R/MfZ\
- GAnSrQ64HaOMEOasekciorXflNVPyjgTL3N2QFYaV0uDHNXhHxlUiuedO1fdTeMSHOW5OU6Tu7XeCRpIzMaFugdWzozAUe8Rf4DKtqMkKgOb0Ls\
- Qz0UbAAAAOWJY9H0+m9xAAGbAcsKAAA8908lscRn+wIAAAAABFla'
- TILE_SPRITES = b'/Td6WFoAAATm1rRGAgAhARYAAAB0L+Wj4AIZAGldAAARA7p1AZmJHQfQCrftyjKbVdeqFurA812ghbqqMl57MacZWPpN86\
- 29stHv87YrMSLM9ciN8jAbczJCFBYzApROMYK0+GEfHlIeaOOyX8DnGpk2nf88Q/iljp7QEwRszJVjeoTpI30AAAAAAABJeIIGhUDZewABhQGaB\
- AAAGtG8ZbHEZ/sCAAAAAARZWg=='
- MAP_1 = b'CXBsYXRmb3Jtcw0IBwgSBwkdBwMPCgQGCwQYCwMVDAMSDQMPDgMEDwsXDwcCExwAFyAIbGF0Zm9ybXMBCgsCCHJhdGZvcm1zAAdsY\
- WRkZXJzBAoFEgUNBhARBhoRBgVsaWZ0cwADaGF5Cg8GABYGABEJABkKAAgOABgOAAgSABcSAAQWABUWAARlZ2dzDAkGABQGAB4GABAJAAcKABAN\
- AAQOABwOAAMSABQSAB0SAAYWAA=='
- MAP_2 = b'CXBsYXRmb3Jtcw0IBwoUBwwACxIUCwwADwYIDxAaDwYAEwwOEwQUEwQaEwYAFwYIFxgIbGF0Zm9ybXMACHJhdGZvcm1zAQYHAgdsY\
- WRkZXJzCAYFBg8FCg8RBhsFBhsNCgMJDgkJChUJBgVsaWZ0cwADaGF5AARlZ2dzAA=='
- MAP_3 = b'CXBsYXRmb3JtcxgVBgUaBwEdBwMGCAELCAcSCQMWCgIACwQZCwIbDAISDgMWDgIADwccDwMaEAIXEQIVEgILEwYSEwIZFAQEFgMAF\
- wQLFwUSFw4IbGF0Zm9ybXMDBAsCHQwCHRQCCHJhdGZvcm1zAhMGAgQIAgdsYWRkZXJzBxMEBQQGCQwGDQ8GDR0KBR0RBgENCgVsaWZ0cwIICgAI\
- EgADaGF5Ch0GAAsHABIIABYJAAEKABQNAAMOAAsSAAMWABYWAARlZ2dzDB4GAAYHAB0JAAIKABoKABcNAAYOABgQAA4SABsTAAYVABkWAA=='
- MAP_4 = b'CXBsYXRmb3JtcxoIBwILBwYVBwUbBwUHCAEGCQEFCgEACwQOCwMVCwEZCwIcCwQKDAIHDQIEDgIADwMLDwEVDwcdEAMAEwgKEwcVE\
- wceFAIAFwgJFwgVFwsIbGF0Zm9ybXMDFgsCDAwCDA8CCHJhdGZvcm1zAQwLAgdsYWRkZXJzBQwFEhYFCh0FBwQRBhgRBgVsaWZ0cwISCQASEQAD\
- aGF5Bg4GABUGAAgMAAESABAWAB0WAARlZ2dzDBoEABoHAAEKAA8KABsLAAEOABUOAB4PAAoSABsSAAEWABUWAA=='
- MAP_5 = b'CXBsYXRmb3JtcxEUBgQGBwgcBwQPCAERCQQVCgIACwoADwoPDwkeDwIAEwoOEwYVEwMAFwMEFw4UFwQcFwQIbGF0Zm9ybXMACHJhd\
- GZvcm1zARIGAgdsYWRkZXJzCRIEBRINBgcFCgMJBgsKBwsSBQQRBg8RBhURBgVsaWZ0cwADaGF5DRcFAAYGAAsGAAwGABwGABAOABcOAAcWAAgW\
- AAkWABQWABcWABwWAARlZ2dzDBUFAAkGAB4GABEIAAEKAAEOAB4OAA4PAAESAAgSABQTAAEWAA=='
- TEST_MAP = b'CXBsYXRmb3JtcwcABwYKBwQQCAQKDAQIEgIWEgIAFyAIbGF0Zm9ybXMDDgcCDgwCChICCHJhdGZvcm1zAg4IAhQSAgdsYWRkZXJzBg4EEwoQBwwQBxAQBxIQBxQQBwVsaWZ0cwADaGF5AARlZ2dzAA=='
- TILE_COLUMNS = 8
- PLATFORM_TILE = 0
- LADDER_TILE = 3
- EGG_TILE = 5
- HAY_TILE = 6
- LIFT_TILE = 7
- # make sprites
- SPRITE_SETS = {'tiles' : sprite(TILE_SPRITES, TILE_COLUMNS, 1), 'harry' : sprite(HARRY_SPRITES, 7, 1, [0, 0, 0])}
- LADDER_SEGMENT = pygame.Surface((TILE_SIZE * 2, TILE_SIZE))
- SPRITE_SETS['tiles'].blit(LADDER_TILE, LADDER_SEGMENT, (0, 0))
- SPRITE_SETS['tiles'].blit(LADDER_TILE + 1, LADDER_SEGMENT, (TILE_SIZE, 0))
- LIFT_SPRITE = pygame.Surface((TILE_SIZE * 2, TILE_SIZE))
- SPRITE_SETS['tiles'].blit(LIFT_TILE, LIFT_SPRITE, (0, 0))
- SPRITE_SETS['tiles'].blit(LIFT_TILE, LIFT_SPRITE, (TILE_SIZE, 0))
- EGG_SPRITE = pygame.Surface((TILE_SIZE, TILE_SIZE))
- SPRITE_SETS['tiles'].blit(EGG_TILE, EGG_SPRITE, (0, 0))
- HAY_SPRITE = pygame.Surface((TILE_SIZE, TILE_SIZE))
- SPRITE_SETS['tiles'].blit(HAY_TILE, HAY_SPRITE, (0, 0))
- # generate maps
- MAPS = [decodeMap(MAP_1), decodeMap(MAP_2), decodeMap(MAP_3), decodeMap(MAP_4), decodeMap(MAP_5), decodeMap(TEST_MAP)]
- MAP_INDEX = 2
- # player stuff
- SPEED = 6
- X_VELOCITY = TILE_SIZE / SPEED
- Y_VELOCITY = 1 * SCALE
- JUMP_HEIGHT = 12
- PLAYER = harry(12, 21)
- FONT = font(imgUnzip(FONT_DATA), FONT_CHARMAP, 1)
- DEBUG = False
- while True:
- e = pygame.event.get()
- if pygame.key.get_pressed()[pygame.K_ESCAPE]: break
- DS.fill([0, 0, 0])
- for l, objs in MAPS[MAP_INDEX].items():
- for obj in objs:
- if hasattr(obj, "surface"):
- DS.blit(obj.surface, obj.rect.pos(True))
- #pygame.draw.rect(DS, (255, 255, 255), obj.rect.rect())
- if hasattr(obj, "do"):
- obj.do()
- PLAYER.draw()
- PLAYER.do()
- if DEBUG:
- DS.blit(FONT.text("PLAY X1-{} Y1-{} X2-{} Y2-{}".format(PLAYER.rect.x1, PLAYER.rect.y1, PLAYER.rect.x2, PLAYER.rect.y2), 1), (0, 0))
- DS.blit(FONT.text("XI-{}".format(PLAYER.xIndex), 1), (0, 35))
- if PLAYER.currentObject:
- DS.blit(FONT.text("OBJ X1-{} Y1-{} X2-{} Y2-{}".format(PLAYER.currentObject.rect.x1, PLAYER.currentObject.rect.y1, PLAYER.currentObject.rect.x2, PLAYER.currentObject.rect.y2), 1), (0, 70))
- pygame.display.update()
- TIME.tick()
- pygame.quit()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement