Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- """Version 1.7.2
- Chuckie Egg clone, as see on a ZX Spectrum
- Coded by Anthony Cook and posted to fb.com/groups/pygame
- NOTES:
- The variable SCALE must be an integer (a whole number)!
- Added chickens! They can go up and down ladders and across platforms.
- Their decision making totally random but I may switch that to a random
- seed to keep it consistent like in the original
- """
- 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
- class spritesSheet:
- def b(s, a):
- return a[1 :], int.from_bytes(a[: 1], "big")
- def w(s, a):
- return a[2 :], int.from_bytes(a[: 2], "big")
- def d(s, a):
- return a[4 :], int.from_bytes(a[: 4], "big")
- def rgb(s, a):
- return a[3 :], int.from_bytes(a[: 3], "big")
- def name(s, a):
- sl = int.from_bytes(a[: 1], "big")
- return a[1 + sl :], a[1 : sl + 1].decode("utf-8")
- def cr(s, index):
- return [index % s.c * s.cw, int(index / s.c) * s.ch, s.cw, s.ch]
- def __init__(s, lz64_data):
- # decompress data
- data = lzma.decompress(base64.b64decode(lz64_data))
- # get image dimensions
- data, s.c = s.b(data) # cols
- data, s.r = s.b(data) # rows
- data, s.cw = s.b(data) # cell width
- data, s.ch = s.b(data) # cell height
- s.iw = s.c * s.cw # image width
- s.ih = s.r * s.ch # image height
- # get palette length
- data, s.pl = s.b(data) # palette length
- # get palette
- s.palette = []
- for index in range(s.pl):
- data, irgb = s.rgb(data)
- s.palette.append(irgb)
- # create pygame surface to place spritesheet
- s.surface = pygame.Surface((s.iw, s.ih))
- pa = pygame.PixelArray(s.surface)
- # get image data length in bytes
- idl = s.iw * s.ih
- # extract image data
- for index in range(idl):
- data, pi = s.b(data) # palette index
- pa[index % s.iw][int(index / s.iw)] = s.palette[pi]
- pa.close()
- del pa
- # make the sprites using the assembly data
- s.sprites = {}
- cell = pygame.Surface((s.cw, s.ch)) # to temp store cell
- while data:
- data, sn = s.name(data) # sprite name
- data, sw = s.w(data) # sprite width, if width is zero then it's a copy instruction
- if sw == 0: # copy instruction?
- data, snc = s.name(data) #sprite name to copy
- data, at = s.b(data) # assembly attribute
- # apply attribute 0 = none, 1 = h flip, 2 = v flip, 3 = rot 90, 4 = rot 180, 5 = rot 270
- if at == 0:
- s.sprites[sn] = s.sprites[snc].copy()
- elif at == 1:
- s.sprites[sn] = pygame.transform.flip(s.sprites[snc], True, False)
- elif at == 2:
- s.sprites[sn] = pygame.transform.flip(s.sprites[snc], False, True)
- elif at == 3:
- s.sprites[sn] = pygame.transform.rotate(s.sprites[snc], -90)
- elif at == 4:
- s.sprites[sn] = pygame.transform.rotate(s.sprites[snc], -180)
- elif at == 5:
- s.sprites[sn] = pygame.transform.rotate(s.sprites[snc], -270)
- continue
- data, sh = s.w(data) # sprite height
- sc = math.ceil(sw / s.cw) # sprite columns
- sr = math.ceil(sh / s.ch) # sprite rows
- scc = sc * sr # sprite cell count
- scc_index = 0
- # create a surface for the sprite
- s.sprites[sn] = pygame.Surface((sw, sh))
- # cycle through assembly instructions
- while scc_index < scc:
- #print(scc_index, scc)
- data, ci = s.w(data) # cell index
- data, at = s.b(data) # assembly attribute
- if at < 6: # single cell placement?
- # calc x, y coords of cell placement
- x = scc_index % sc * s.cw
- y = int(scc_index / sc) * s.ch
- # get cell image
- cell.blit(s.surface, (0, 0), s.cr(ci))
- # apply attribute 0 = none, 1 = h flip, 2 = v flip, 3 = rot 90, 4 = rot 180, 5 = rot 270
- if at == 0:
- s.sprites[sn].blit(cell, (x, y))
- elif at == 1:
- s.sprites[sn].blit(pygame.transform.flip(cell, True, False), (x, y))
- elif at == 2:
- s.sprites[sn].blit(pygame.transform.flip(cell, False, True), (x, y))
- elif at == 3:
- s.sprites[sn].blit(pygame.transform.rotate(cell, -90), (x, y))
- elif at == 4:
- s.sprites[sn].blit(pygame.transform.rotate(cell, -180), (x, y))
- elif at == 5:
- s.sprites[sn].blit(pygame.transform.rotate(cell, -270), (x, y))
- scc_index += 1
- else:
- data, r = s.w(data) # get range count
- for index in range(r):
- # get x, y coords of cell placement
- x = (scc_index + index) % sc * s.cw
- y = int((scc_index + index) / sc) * s.ch
- # get cell image
- cell.blit(s.surface, (0, 0), s.cr(ci))
- # apply attribute 6 = none, 7 = h flip, 8 = v flip, 9 = rot 90, 10 = rot 180, 11 = rot 270
- if at == 6 or at == 12 or at == 18:
- s.sprites[sn].blit(cell, (x, y))
- elif at == 7 or at == 13 or at == 19:
- s.sprites[sn].blit(pygame.transform.flip(cell, True, False), (x, y))
- elif at == 8 or at == 14 or at == 20:
- s.sprites[sn].blit(pygame.transform.flip(cell, False, True), (x, y))
- elif at == 9 or at == 15 or at == 21:
- s.sprites[sn].blit(pygame.transform.rotate(cell, -90), (x, y))
- elif at == 10 or at == 16 or at == 22:
- s.sprites[sn].blit(pygame.transform.rotate(cell, -180), (x, y))
- elif at == 11 or at == 17 or at == 23:
- s.sprites[sn].blit(pygame.transform.rotate(cell, -270), (x, y))
- # increment/decrement the sprite sheet cell index
- if at > 11 and at < 18:
- ci += 1
- elif at > 17:
- ci -= 1
- scc_index += r
- 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 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)
- def maps(lz64_data):
- global PLATFORM, LATFORM, RATFORM, LADDER, EGG, HAY, LIFT, CHICKEN, CHICKEN_RIGHT, CHICKEN_LEFT
- def b(a):
- return a[1 :], int.from_bytes(a[0 : 1], "big")
- maps = []
- data = lzma.decompress(base64.b64decode(lz64_data))
- while data:
- maps.append(collections.OrderedDict({1 : [], 2 : [], 3 : [], 4 : [], 5 : [], 6 : [], 8 : []}))
- data, objectCount = b(data)
- maps[-1]
- for index in range(objectCount):
- data, id = b(data)
- data, x = b(data)
- data, y = b(data)
- if id == PLATFORM:
- data, w = b(data)
- maps[-1][PLATFORM].append(platform(x, y, w))
- elif id == LATFORM: maps[-1][LATFORM].append(latform(x, y))
- elif id == RATFORM: maps[-1][RATFORM].append(ratform(x, y))
- elif id == LADDER:
- data, h = b(data)
- maps[-1][LADDER].append(ladder(x, y, h))
- elif id == EGG: maps[-1][EGG].append(egg(x, y))
- elif id == HAY: maps[-1][HAY].append(hay(x, y))
- elif id == LIFT: maps[-1][LIFT].append(lift(x, y))
- elif id == CHICKEN_RIGHT: maps[-1][CHICKEN].append(chicken(x, y, 1))
- elif id == CHICKEN_LEFT: maps[-1][CHICKEN].append(chicken(x, y, -1))
- return maps
- class platform:
- def __init__(s, x, y, w):
- global TILE_SIZE
- global SPRITES
- s.rect = rect(x, y, w, 1).scale(TILE_SIZE)
- s.surface = pygame.Surface(s.rect.size(True))
- for index in range(w):
- s.surface.blit(SPRITES.sprites['pf'], (index * TILE_SIZE, 0))
- class latform:
- def __init__(s, x, y):
- global TILE_SIZE
- s.rect = rect(x, y, 2, 1).scale(TILE_SIZE)
- class ratform:
- def __init__(s, x, y):
- global TILE_SIZE
- s.rect = rect(x, y, 2, 1).scale(TILE_SIZE)
- class ladder:
- def __init__(s, x, y, h):
- global TILE_SIZE
- global SPRITES
- 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(SPRITES.sprites['ls'], (0, index * TILE_SIZE))
- class egg:
- def __init__(s, x, y):
- global TILE_SIZE
- global SPRITES
- s.rect = rect(x, y, 1, 1).scale(TILE_SIZE)
- s.surface = SPRITES.sprites['eg']
- class hay:
- def __init__(s, x, y):
- global TILE_SIZE
- global SPRITES
- s.rect = rect(x, y, 1, 1).scale(TILE_SIZE)
- s.surface = SPRITES.sprites['hy']
- class lift:
- def __init__(s, x, y):
- global TILE_SIZE
- global SPRITES
- s.moveToggle = 0
- s.rect = rect(x, y, 2, 1).scale(TILE_SIZE)
- s.surface = SPRITES.sprites['lt']
- def do(s):
- global TILE_SIZE
- global H
- if not s.moveToggle:
- s.rect.offY1(-1)
- s.moveToggle = 1 - s.moveToggle
- if s.rect.y1 <= TILE_SIZE * 3:
- s.rect.setY1(H - TILE_SIZE)
- CHICKEN_WALK_RIGHT = [0, 1]
- CHICKEN_WALK_LEFT = [2, 3]
- CHICKEN_CLIMB = [6, 7]
- CHICKEN_DELAY = 5
- CHICKEN_DIRECTIONS = [-1, 1]
- class chicken:
- def __init__(s, x, y, direction):
- global TILE_SIZE
- global CHICKEN_DELAY
- global CHICKEN_WALK_RIGHT, CHICKEN_WALK_LEFT
- s.rect = rect(x, y, 1, 2).scale(TILE_SIZE)
- s.currentObject = None
- s.state = s.walking
- s.moveToggle = CHICKEN_DELAY
- s.dx = direction
- s.dy = 0
- if direction == 1:
- s.anim = CHICKEN_WALK_RIGHT
- else:
- s.anim = CHICKEN_WALK_LEFT
- s.animIndex = 0
- def draw(s):
- global CHICKEN_SPRITES
- global ZX_SURFACE
- ZX_SURFACE.blit(CHICKEN_SPRITES[s.anim[s.animIndex]], s.rect.pos())
- def do(s):
- global MAPS, MAP_INDEX, PLATFORM, CHICKEN
- global CHICKEN_DELAY
- #if s != MAPS[MAP_INDEX][CHICKEN][0]: return # debug
- if not s.currentObject:
- for p in MAPS[MAP_INDEX][PLATFORM]:
- if s.rect.y2 != p.rect.y1: continue
- if s.rect.x2 < p.rect.x1 or s.rect.x1 > p.rect.x2: continue
- s.currentObject = p
- break
- s.moveToggle -= 1
- if s.moveToggle > 0: return
- s.moveToggle = CHICKEN_DELAY
- s.animIndex = 1 - s.animIndex
- s.state()
- def walking(s):
- global MAPS, MAP_INDEX, PLATFORM, CHICKEN, LATFORM, RATFORM, LADDER
- global CHICKEN_WALK_RIGHT, CHICKEN_WALK_LEFT, CHICKEN_CLIMB, CHICKEN_DIRECTIONS
- s.rect.offX1(s.dx)
- # check if chicken wants to climb a ladder
- if (s.rect.x1 + 4) % 8 == 0:
- for l in MAPS[MAP_INDEX][LADDER]:
- if l.rect.x1 == s.rect.x1 - 4:
- if s.rect.y1 >= l.rect.y1 and s.rect.y2 <= l.rect.y2:
- getOnLadder = random.randint(0, 1)
- if getOnLadder:
- if s.rect.y1 == l.rect.y1:
- s.dy = 1
- elif s.rect.y2 == l.rect.y2:
- s.dy = -1
- else:
- s.dy = CHICKEN_DIRECTIONS[random.randint(0, 1)]
- s.currentObject = l
- s.animIndex = 0
- s.anim = CHICKEN_CLIMB
- s.state = s.climbing
- return
- break
- # walk along the platform
- platformExtended = False
- if s.dx == 1 and s.rect.x2 == s.currentObject.rect.x2:
- for p in MAPS[MAP_INDEX][PLATFORM] + MAPS[MAP_INDEX][LATFORM]:
- if s.currentObject.rect.x2 == p.rect.x1 and s.currentObject.rect.y1 == p.rect.y1:
- s.currentObject = p
- platformExtended = True
- break
- if not platformExtended:
- s.dx = -1
- s.anim = CHICKEN_WALK_LEFT
- s.animIndex = 0
- elif s.dx == -1 and s.rect.x1 == s.currentObject.rect.x1:
- for p in MAPS[MAP_INDEX][PLATFORM] + MAPS[MAP_INDEX][RATFORM]:
- if s.currentObject.rect.x1 == p.rect.x2 and s.currentObject.rect.y1 == p.rect.y1:
- s.currentObject = p
- platformExtended = True
- break
- if not platformExtended:
- s.dx = 1
- s.anim = CHICKEN_WALK_RIGHT
- s.animIndex = 0
- def climbing(s):
- global MAPS, MAP_INDEX, PLATFORM, RATFORM, LATFORM
- global CHICKEN_WALK_RIGHT, CHICKEN_WALK_LEFT, CHICKEN_DIRECTIONS
- s.rect.offY1(s.dy)
- # does the chicken want to get off the ladder?
- if s.rect.y2 % 8 == 0:
- for p in MAPS[MAP_INDEX][PLATFORM] + MAPS[MAP_INDEX][RATFORM] + MAPS[MAP_INDEX][LATFORM]:
- if s.rect.y2 != p.rect.y1: continue
- if s.rect.x1 > p.rect.x2 or s.rect.x2 < p.rect.x1: continue
- getOffLadder = random.randint(0, 1)
- if getOffLadder:
- if type(p) == ratform:
- s.dx = 1
- elif type(p) == latform:
- s.dx = -1
- else:
- s.dx = CHICKEN_DIRECTIONS[random.randint(0, 1)]
- if s.dx == 1:
- s.anim = CHICKEN_WALK_RIGHT
- else:
- s.anim = CHICKEN_WALK_LEFT
- s.animIndex = 0
- s.currentObject = p
- s.state = s.walking
- return
- if s.rect.y2 == s.currentObject.rect.y2 or s.rect.y1 == s.currentObject.rect.y1:
- s.dy = -s.dy
- 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 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 HARRY_SPRITES
- global ZX_SURFACE
- ZX_SURFACE.blit(HARRY_SPRITES[s.anim.index], s.rect.pos(True, [-s.rect.w / 4, 0]))
- #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) == lift:
- s.rect.setY2(s.currentObject.rect.y1)
- # check for collision with eggs, hay and birds
- def falling(s):
- global Y_VELOCITY
- 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, LATFORM, RATFORM
- 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][LATFORM] + MAPS[MAP_INDEX][RATFORM]):
- s.xIndexDirection = 0
- s.currentObject = None
- s.anim.set(1)
- s.state = s.falling
- return
- def walkLeft(s):
- global X_VELOCITY, JUMP_HEIGHT
- global MAPS, MAP_INDEX, LATFORM, RATFORM
- 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][LATFORM] + MAPS[MAP_INDEX][RATFORM]):
- 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
- global MAPS, MAP_INDEX, LIFT
- 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 LIFT in MAPS[MAP_INDEX]:
- if s.collidedWithPlatform(MAPS[MAP_INDEX][LIFT]): return
- else:
- if s.collidedWithPlatform(): 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, PLATFORM, RATFORM, LATFORM
- 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][PLATFORM] + MAPS[MAP_INDEX][LATFORM] + MAPS[MAP_INDEX][RATFORM]:
- 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) != ratform:
- s.currentObject = platform
- s.xIndexDirection = -1
- s.anim = HARRY_WALKING_LEFT.reset()
- s.state = s.walkLeft
- elif s.keys[RIGHT] and type(platform) != latform:
- 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 TILE_SIZE
- global MAPS, MAP_INDEX, PLATFORM
- for platform in MAPS[MAP_INDEX][PLATFORM]:
- if s.rect.x2 != platform.rect.x1: continue
- if s.rect.y1 + TILE_SIZE > platform.rect.y2 or s.rect.y2 < platform.rect.y1 + 1: continue
- return True
- return False
- def collidedWithRightEdge(s):
- global TILE_SIZE
- global MAPS, MAP_INDEX, PLATFORM
- for platform in MAPS[MAP_INDEX][PLATFORM]:
- if s.rect.x1 != platform.rect.x2: continue
- if s.rect.y1 + TILE_SIZE > platform.rect.y2 or s.rect.y2 < platform.rect.y1 + 1: continue
- return True
- return False
- def collidedWithPlatform(s, additionalObjects = []): # worded as a question
- global Y_VELOCITY
- global MAPS, MAP_INDEX, PLATFORM
- for platform in MAPS[MAP_INDEX][PLATFORM] + 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 MAPS, MAP_INDEX, LADDER
- global UP, DOWN
- if not (s.keys[UP] or s.keys[DOWN]): return
- for ladder in MAPS[MAP_INDEX][LADDER]:
- if s.rect.x1 == ladder.rect.x1 + 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
- 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 * SCALE, H * SCALE))
- ZX_SURFACE = pygame.Surface((W, H))
- TIME = gTime(FPS)
- # a tonne of data
- CHUCKIE_SPRITES = b'/Td6WFoAAATm1rRGAgAhARYAAAB0L+Wj4B9PBF5dAASDqq1TN1vaXm7k3zboYptpmiLlGK/nz2hNtPva\
- h/InbMa+8OnbcQatGrITZdE2gey/xwuWnBbd8OAhoIoG5RzLaOwVg150Qt6+wy5vOOIfE1iY7X/nsvkzPdJiz6vpX5rR045sHHe6\
- G2Qv9iQxcWiUaFwDtBGQWBug27uyQ1GMiaj88WZz4f0S2iojyiXgYJ/gvIHTDIjRShb2krisOlGLHoPY3otfA4nzX3ekFjwLdDmc\
- oJuyILAgHh1B0xy7urQeMvMfsiA76Z/phxvRcBPuVbHeo+h4qeYSa7A6XYBuFfNZFCGVwghbVaVxAp5pOTXkLftWwuBRGIZGE+41\
- lBgNhbp8OAofi8PRNmh9MySaHF+DG+76rlMjqUBCyIIfzMACbfXlbzHUhOpk9D1KoCIqa/a/escN2d5SUnr9FP/3xMZF2kZm2V+A\
- uXhnIluc1JQSoWf73+kQj78Pf9FxwbMCr44W9w3l7Ghzbck6OWHczvHVh0jLjOC+QI+58qn5HwOW6xQsFay88bCb01rIMNCJ06J1\
- 3BuLiceX2AyzclGXNIiGLaoNbG0iDD4YKFkX5DM63AwipfndzO7fj4TM0/DHrTRzdaMEMKUivnOTOMk560IH42siCZQz1rNYEQ8J\
- XLG3lO031I+8ly591BG2KneUx7dZeEf2xy0JeagmGA2n/t9xxDo3HNO5sq7q4lboNDRMKwI+TTBmuCyUz8rG0JH3iXcDM0o3FuOU\
- ajOEt8W+zUZKoR2hsAoooQSwnnfuH1qNacfcjczWnS/FouhwNKqFGowOBZvrbsiSDMPXRQV73pUWXjgGo9Z1aW+kO8IEZI+rKc+9\
- vBiDQLv0ixStHYaArO1lKVOG61Bc7tULQgOItoxyONQrrJOmMmO06t3QsZW2IRUAAMpYgf97loTBinYsHsywYrvPWj0SjIXsERjJ\
- 73DmNSWv9AFWmwkJBFpCsAV24BNUVu4QaY2zn+a0Plm4tvB5l66b/OTBde7bE1fn1JCB2B8bbNMSr/MjwvrqfXH2gJ7d8ECUAjzv\
- J21SaPn6jqTT12K3m3ix6c64Q7YP1eKq6hcUNsVfdX4rRe/apAUIZnt2WGzSXhBkP8BiHx+nnZ1shrWCwrKfnQbpIfZXM+8TBBHY\
- /AOQevvxYWehily9YoHAeIF3M89eSWv8UC9VV2KwQ3MX6xPTwilfK4/nNOXn7TS3tDVcwtwvNS4cbvxaJQVlXZRFK1auLC0/+cUc\
- KDPoWCqBlt4vyWYFrh/jogJI4G5IkmrygsmTH+CLuP+4+bWbItpjzFlLpJMcikQAt7LsH0vdpRECQn4pKM12o5ROvqI38yQuyyBx\
- LcXavSIjk6Hd91ZadeBwkohc/Sb6+aR8pN5OdMjDo34vZRC46m+ForbpXJxCYQMEYfsCwc0b3sCuH235H+3znQyYSRbwSoqpXH3E\
- sRHT/Hr3SzLhKY+nCaqVYsT63iGzoBTQaNUQiFvUdbwImMLb8isAAAAA/kdTHBIIrMkAAfoI0D4AAGDUjU+xxGf7AgAAAAAEWVo='
- MAPS = b'/Td6WFoAAATm1rRGAgAhARYAAAB0L+Wj4AEpANddABUA/UoJu3jJ4BZISBX4qyJ8e1ZMTy4yPPcARMkYyurpSEqERo+\
- eSQHx9tycAFXNjYV8YxSm2C+7r/hqKP3A5GjVAlU7k0grSFH3PCfvGZhmde7sKSe8JEsRrckwaYzlJ/257PTpQWXn3Q5zFkIjJ+O\
- 8PnB9KJAkaqONh5IZPNcULqvmAaLMi8DRavSpc5hLP+RppKs5BB8iXHDuBzFTA0GM1zNPwU0PYYmTPM6SKs8ZECKYjfMv4qw8DHq\
- 7ENgFIQk0wIQVz19gNKM/2MPQa2o81eh0F2IAAAAS/m2jGnQr0QAB8wGqAgAA1bthTLHEZ/sCAAAAAARZWg=='
- # make sprites
- SPRITES = spritesSheet(CHUCKIE_SPRITES)
- FONT = font(SPRITES.sprites['fnt'], "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789.!?:,'", 0)
- HARRY_SPRITES = [SPRITES.sprites['hsr'], SPRITES.sprites['hwr'], SPRITES.sprites['hsl'], SPRITES.sprites['hwl'], SPRITES.sprites['hc1'], SPRITES.sprites['hc2'], SPRITES.sprites['hc3']]
- [HARRY_SPRITES[index].set_colorkey([0, 0, 0]) for index in range(len(HARRY_SPRITES))]
- CHICKEN_SPRITES = [SPRITES.sprites['csr'], SPRITES.sprites['cwr'], SPRITES.sprites['csl'], SPRITES.sprites['cwl'], SPRITES.sprites['cpr'], SPRITES.sprites['cpl'], SPRITES.sprites['cc1'], SPRITES.sprites['cc2']]
- [CHICKEN_SPRITES[index].set_colorkey([0, 0, 0]) for index in range(len(CHICKEN_SPRITES))]
- # map tile IDs
- PLATFORM = 1
- LATFORM = 2
- RATFORM = 3
- LADDER = 4
- EGG = 5
- HAY = 6
- LIFT = 7
- CHICKEN = 8
- CHICKEN_RIGHT = 24
- CHICKEN_LEFT = 27
- # generate maps
- MAPS = maps(MAPS)
- MAP_INDEX = 1
- # player stuff
- SPEED = 6
- X_VELOCITY = TILE_SIZE / SPEED
- Y_VELOCITY = 1
- JUMP_HEIGHT = 12
- PLAYER = harry(12, 21)
- # screens
- # title screen
- TITLE_SCREEN = pygame.Surface((W, H))
- TITLE_SCREEN.blit(SPRITES.sprites['be1'], (7 * TILE_SIZE, 8 * TILE_SIZE))
- TITLE_SCREEN.blit(SPRITES.sprites['be2'], (9 * TILE_SIZE, 8 * TILE_SIZE))
- TITLE_SCREEN.blit(SPRITES.sprites['tle'], (6 * TILE_SIZE, 10 * TILE_SIZE))
- TITLE_SCREEN.blit(FONT.text("S...START ESC...EXIT"), (6 * TILE_SIZE, 13 * TILE_SIZE))
- TITLE_SCREEN = pygame.transform.scale(TITLE_SCREEN, (W * SCALE, H * SCALE))
- DEBUG = False
- while True:
- if not DEBUG:
- exitGame = False
- while True:
- e = pygame.event.get()
- if pygame.key.get_pressed()[pygame.K_s]: break
- if pygame.key.get_pressed()[pygame.K_ESCAPE]:
- exitGame = True
- break
- DS.blit(TITLE_SCREEN, (0, 0))
- pygame.display.update()
- TIME.tick()
- if exitGame: break
- while True:
- e = pygame.event.get()
- if pygame.key.get_pressed()[pygame.K_ESCAPE]: break
- ZX_SURFACE.fill([0, 0, 0])
- for l, objs in MAPS[MAP_INDEX].items():
- for obj in objs:
- if hasattr(obj, "surface"):
- ZX_SURFACE.blit(obj.surface, obj.rect.pos(True))
- #pygame.draw.rect(DS, (255, 255, 255), obj.rect.rect())
- if hasattr(obj, "draw"):
- obj.draw()
- if hasattr(obj, "do"):
- obj.do()
- ZX_SURFACE.blit(SPRITES.sprites['cg'], (0, 3 * TILE_SIZE))
- PLAYER.draw()
- PLAYER.do()
- if DEBUG:
- ZX_SURFACE.blit(FONT.text("PLAY X1:{} Y1:{} X2:{} Y2:{}".format(round(PLAYER.rect.x1, 1), PLAYER.rect.y1, round(PLAYER.rect.x2, 1), PLAYER.rect.y2)), (0, 0))
- ZX_SURFACE.blit(FONT.text("XI:{}".format(PLAYER.xIndex)), (0, 8))
- if PLAYER.currentObject:
- ZX_SURFACE.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)), (0, 16))
- DS.blit(pygame.transform.scale(ZX_SURFACE, (W * SCALE, H * SCALE)), (0, 0))
- pygame.display.update()
- TIME.tick()
- if DEBUG: break
- while True:
- e = pygame.event.get()
- if not pygame.key.get_pressed()[pygame.K_ESCAPE]: break
- pygame.quit()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement