Advertisement
cookertron

ZX Spectrum Chuckie Egg - Python Pygame (-PixelArray)

Apr 2nd, 2020
224
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 22.81 KB | None | 0 0
  1. # chuckie egg clone, as see on a ZX Spectrum
  2. # Version 1.6.1
  3. # Coded by Anthony Cook and posted to fb.com/groups/pygame
  4.  
  5. # NOTES: This is the same as version 1.6.0 but doesn't require PixelArray Functionality
  6.  
  7. import pygame
  8. import pygame.gfxdraw
  9. import base64, lzma
  10. import collections
  11. import math, time, random
  12.  
  13.  
  14. # game time
  15. class gTime:
  16.     def __init__(s, fps): # input = frames per second
  17.         s.fps = fps
  18.         s.tpf = 1 / fps * 1000000000
  19.         s.time = 0
  20.         s.timeStamp = time.time_ns()
  21.  
  22.     def tick(s):
  23.         while time.time_ns() - s.timeStamp < s.tpf:
  24.             pass
  25.         s.timeStamp = time.time_ns()
  26.         s.time += 1
  27.  
  28. def imgUnzip(lz64):
  29.     data = lzma.decompress(base64.b64decode(lz64))
  30.  
  31.     imageW = int.from_bytes(data[0:2], "big")
  32.     imageH = int.from_bytes(data[2:4], "big")
  33.     paletteLength = int.from_bytes(data[4:5], "big")
  34.     palette = [int.from_bytes(data[5 + index * 3:8 + index * 3], "big") for index in range(paletteLength)]
  35.     pixelDataStart = 5 + paletteLength * 3    
  36.  
  37.     surface = pygame.Surface((imageW, imageH))    
  38.     #pixelArray = pygame.PixelArray(surface)
  39.    
  40.     for index in range(0, len(data) - pixelDataStart):
  41.         #pixelArray[index % imageW][int(index / imageW)] = palette[data[pixelDataStart + index]]
  42.         surface.set_at((index % imageW, int(index / imageW)), surface.unmap_rgb(palette[data[pixelDataStart + index]]))
  43.     #pixelArray.close()
  44.     #del pixelArray
  45.     return surface
  46.  
  47. def decodeMap(mapB64):
  48.     mapBytes = base64.b64decode(mapB64)
  49.     mapData = collections.OrderedDict()
  50.  
  51.     while mapBytes:
  52.         # get the name of the layer and add it to the mapData dictionary
  53.         layerNameLen = mapBytes[0]
  54.         layerName = mapBytes[1:layerNameLen + 1].decode('utf-8')
  55.  
  56.         mapBytes = mapBytes[layerNameLen + 1:]
  57.  
  58.         mapData[layerName] = []
  59.        
  60.         objectCount = mapBytes[0]
  61.         mapBytes = mapBytes[1:]
  62.         for index in range(0, objectCount):
  63.             mapData[layerName].append(globals()[layerName](mapBytes[0], mapBytes[1], mapBytes[2]))
  64.             mapBytes = mapBytes[3:]
  65.     return mapData
  66.  
  67. class cycle:
  68.     def __init__(s, l, h, bounce = False):
  69.         s.index = l
  70.         s.l = l
  71.         s.h = h
  72.         s.d = 1
  73.         s.b = bounce
  74.  
  75.     def __call__(s):
  76.         s.index += s.d
  77.         if s.b:
  78.             if s.index == s.h or s.index == s.l: s.d = -s.d
  79.         else:
  80.             if s.index > s.h: s.index = s.l
  81.  
  82.     def reset(s):
  83.         s.index = s.l
  84.         s.d = 1
  85.         return s
  86.    
  87.     def set(s, index):
  88.         s.index = s.l + index
  89.         return s
  90.  
  91. class font:
  92.     def __init__(s, img, charMap, spacing):
  93.         s.surfaceWidth, s.surfaceHeight = img.get_size()
  94.  
  95.         s.fontSurface = img.copy()
  96.         s.monochromeFonts = {}
  97.  
  98.         s.cols = len(charMap)
  99.         s.charWidth = int(s.surfaceWidth / s.cols)
  100.  
  101.         s.spacing = spacing
  102.  
  103.         s.charMap = charMap
  104.  
  105.     def text(s, text, scale = None, colorName = None):
  106.         surfaceWidth = len(text) * s.charWidth + s.spacing * (len(text) - 1)
  107.         surface = pygame.Surface((surfaceWidth, s.surfaceHeight))
  108.        
  109.         charPosX = 0
  110.         for c in text:
  111.             charIndex = s.charMap.find(c)
  112.             if charIndex == -1:
  113.                 charPosX += s.charWidth + s.spacing
  114.                 continue
  115.            
  116.             charOffsetX = charIndex * s.charWidth
  117.  
  118.             if colorName:
  119.                 surface.blit(s.monochromeFonts[colorName], (charPosX, 0), (charOffsetX, 0, s.charWidth, s.surfaceHeight))
  120.             else:
  121.                 surface.blit(s.fontSurface, (charPosX, 0), (charOffsetX, 0, s.charWidth, s.surfaceHeight))
  122.             charPosX += s.charWidth + s.spacing
  123.  
  124.         if scale != None:
  125.             return pygame.transform.scale(surface, (surfaceWidth * scale, s.surfaceHeight * scale))
  126.        
  127.         return surface
  128.  
  129.     def newMonochromeFont(s, name, color):
  130.         global DS
  131.         monochromeSurface = s.fontSurface.copy()
  132.         monochromePA = pygame.PixelArray(monochromeSurface)
  133.         for x in range(s.surfaceWidth):
  134.             for y in range(s.surfaceHeight):
  135.                 if monochromePA[x][y] != 0: monochromePA[x][y] = DS.map_rgb(color)
  136.         monochromePA.close()
  137.         del monochromePA
  138.         s.monochromeFonts[name] = monochromeSurface
  139.  
  140.     def size(s, text, scale = 1):
  141.         w = len(text) * s.charWidth + s.spacing * (len(text) - 1)
  142.         h = s.surfaceHeight
  143.         return [w * scale, h * scale]
  144.  
  145. class sprite:
  146.     def __init__(s, lz64, cols, rows, colorKey = None):
  147.         global SCALE
  148.  
  149.         s.surface = imgUnzip(lz64)
  150.  
  151.         w, h = s.surface.get_size()
  152.         if SCALE != 1:
  153.             s.surface = pygame.transform.scale(s.surface, (int(w * SCALE), int(h * SCALE)))
  154.             w *= SCALE
  155.             h *= SCALE
  156.  
  157.         cw, ch = int(w / cols), int(h / rows)
  158.         s.rects = [[(index % cols) * cw, int(index / cols) * ch, cw, ch] for index in range(cols * rows)]
  159.         if colorKey: s.surface.set_colorkey(colorKey)
  160.  
  161.     def blit(s, spriteID, surface, pos):
  162.         surface.blit(s.surface, pos, s.rects[spriteID])
  163.  
  164. class rect:
  165.     def __init__(s, x, y, w, h):
  166.         s.x1, s.y1, s.w, s.h = x, y, w, h
  167.         s.x2, s.y2 = s.x1 + s.w, s.y1 + s.h
  168.    
  169.     def setX1(s, x):
  170.         s.x1 = x
  171.         s.x2 = x + s.w
  172.    
  173.     def setY1(s, y):
  174.         s.y1 = y
  175.         s.y2 = y + s.h
  176.  
  177.     def setXY(s, x, y):
  178.         s.x1, s.y1 = x, y
  179.         s.x2, s.y2 = x + s.w, y + s.h
  180.    
  181.     def setX2(s, x):
  182.         s.x2 = x
  183.         s.x1 = x - s.w
  184.    
  185.     def setY2(s, y):
  186.         s.y2 = y
  187.         s.y1 = y - s.h
  188.  
  189.     def setXY2(s, x, y):
  190.         s.x2, s.y2 = x, y
  191.         s.x1, s.y1 = x - s.w, y - s.h
  192.  
  193.     def offX1(s, x):
  194.         s.x1 += x
  195.         s.x2 = s.x1 + s.w
  196.    
  197.     def offY1(s, y):
  198.         s.y1 += y
  199.         s.y2 = s.y1 + s.h
  200.  
  201.     def pos(s, integer = False, offset = [0, 0]):
  202.         if integer: return [int(s.x1 + offset[0]), int(s.y1 + offset[1])]
  203.         return [s.x1 + offset[0], s.y1 + offset[1]]
  204.  
  205.     def rect(s, integer = False):
  206.         if integer: return [int(s.x1), int(s.y1), int(s.w), int(s.h)]
  207.         return [s.x1, s.y1, s.w, s.h]
  208.  
  209.     def box(s, integer = False):
  210.         if integer: return [int(s.x1), int(s.y1), int(s.x2), int(s.y2)]
  211.         return [s.x1, s.y1, s.x2, s.y2]
  212.  
  213.     def size(s, integer = False):
  214.         if integer: return [int(s.w), int(s.h)]
  215.         return [s.w, s.h]
  216.  
  217.     def scale(s, value):
  218.         s.x1 *= value
  219.         s.y1 *= value
  220.         s.w *= value
  221.         s.h *= value
  222.         s.x2 = s.x1 + s.w
  223.         s.y2 = s.y1 + s.h
  224.         return s
  225.    
  226.     def __str__(s):
  227.         return "x1={}, y1={}, w={}, h={} (x2={}, y2={})".format(s.x1, s.y1, s.w, s.h, s.x2, s.y2)
  228.  
  229. class platforms:
  230.     def __init__(s, x, y, w):
  231.         global TILE_SIZE
  232.         global SPRITE_SETS, PLATFORM_TILE
  233.        
  234.         s.rect = rect(x, y, w, 1).scale(TILE_SIZE)
  235.  
  236.         s.surface = pygame.Surface(s.rect.size(True))
  237.  
  238.         for index in range(w):
  239.             SPRITE_SETS['tiles'].blit(PLATFORM_TILE, s.surface, (index * TILE_SIZE, 0))
  240.  
  241. class latforms:
  242.     def __init__(s, x, y, w):
  243.         global TILE_SIZE
  244.        
  245.         s.rect = rect(x, y, w, 1).scale(TILE_SIZE)
  246.  
  247. class ratforms:
  248.     def __init__(s, x, y, w):
  249.         global TILE_SIZE
  250.  
  251.         s.rect = rect(x, y, w, 1).scale(TILE_SIZE)
  252.  
  253. class ladders:
  254.     def __init__(s, x, y, h):
  255.         global TILE_SIZE
  256.         global LADDER_SEGMENT
  257.  
  258.         s.rect = rect(x, y, 2, h).scale(TILE_SIZE)
  259.         s.surface = pygame.Surface(s.rect.size(True))
  260.         for index in range(h):
  261.             s.surface.blit(LADDER_SEGMENT, (0, index * TILE_SIZE))
  262.  
  263. class eggs:
  264.     def __init__(s, x, y, z):
  265.         global TILE_SIZE
  266.         global EGG_SPRITE
  267.  
  268.         s.rect = rect(x, y, 1, 1).scale(TILE_SIZE)
  269.         s.surface = EGG_SPRITE
  270.  
  271. class hay:
  272.     def __init__(s, x, y, z):
  273.         global TILE_SIZE
  274.         global HAY_SPRITE
  275.  
  276.         s.rect = rect(x, y, 1, 1).scale(TILE_SIZE)
  277.         s.surface = HAY_SPRITE
  278.  
  279. class lifts:
  280.     def __init__(s, x, y, z):
  281.         global TILE_SIZE
  282.         global LIFT_SPRITE
  283.  
  284.         s.moveToggle = 0
  285.         s.rect = rect(x, y, 2, 1).scale(TILE_SIZE)
  286.         s.surface = LIFT_SPRITE
  287.  
  288.     def do(s):
  289.         global SCALE, TILE_SIZE
  290.         global H
  291.  
  292.         if not s.moveToggle:
  293.             s.rect.offY1(-SCALE)
  294.         s.moveToggle = 1 - s.moveToggle
  295.  
  296.         if s.rect.y1 <= TILE_SIZE * 3:
  297.             s.rect.setY1(H - TILE_SIZE)
  298.  
  299. HARRY_WALKING_RIGHT = cycle(0, 1)
  300. HARRY_WALKING_LEFT = cycle(2, 3)
  301. HARRY_CLIMBING_LADDER = cycle(4, 6, True)
  302.  
  303. # key binds
  304. UP = pygame.K_UP
  305. DOWN = pygame.K_DOWN
  306. LEFT = pygame.K_LEFT
  307. RIGHT = pygame.K_RIGHT
  308. SPACE = pygame.K_SPACE
  309.  
  310. class harry:
  311.     def __init__(s, x, y):
  312.         global SPEED
  313.         global SCALE, TILE_SIZE
  314.         global HARRY_WALKING_RIGHT
  315.  
  316.         s.rect = rect(x, y, 1, 2).scale(TILE_SIZE)
  317.         s.xIndex = x * SPEED
  318.         s.xIndexDirection = 0
  319.         s.jumpHeight = 0
  320.  
  321.         s.state = s.falling
  322.  
  323.         s.currentObject = None
  324.  
  325.         s.anim = HARRY_WALKING_RIGHT.set(1)
  326.  
  327.     def draw(s):
  328.         global SCALE
  329.         global SPRITE_SETS
  330.         global DS
  331.  
  332.         SPRITE_SETS['harry'].blit(s.anim.index, DS, s.rect.pos(True, [-s.rect.w / 4, 0]))
  333.         #pygame.draw.rect(DS, [255, 0, 0], s.rect.rect(True))
  334.  
  335.     def do(s):
  336.         s.keys = pygame.key.get_pressed()
  337.         s.state()
  338.         if type(s.currentObject) == lifts:
  339.             s.rect.setY2(s.currentObject.rect.y1)
  340.  
  341.     def falling(s):
  342.         global Y_VELOCITY
  343.         global MAPS, MAP_INDEX
  344.  
  345.         if s.collidedWithPlatform(): return
  346.         s.rect.offY1(Y_VELOCITY)
  347.  
  348.     def stop(s):
  349.         global JUMP_HEIGHT
  350.         global HARRY_WALKING_LEFT, HARRY_WALKING_RIGHT
  351.         global LEFT, RIGHT, SPACE
  352.  
  353.         if s.hasClimbedLadder(): return
  354.  
  355.         if s.keys[LEFT]:
  356.             s.xIndexDirection = -1
  357.             s.anim = HARRY_WALKING_LEFT.reset()
  358.             s.state = s.walkLeft
  359.         elif s.keys[RIGHT]:
  360.             s.xIndexDirection = 1
  361.             s.anim = HARRY_WALKING_RIGHT.reset()
  362.             s.state = s.walkRight
  363.  
  364.         if s.keys[SPACE]:
  365.             s.jumpHeight = JUMP_HEIGHT
  366.             s.anim.set(1)
  367.             s.currentObject = None
  368.             s.state = s.jumpUp
  369.  
  370.     def walkRight(s):
  371.         global X_VELOCITY, JUMP_HEIGHT
  372.         global MAPS, MAP_INDEX
  373.         global RIGHT, SPACE
  374.         global W
  375.  
  376.         if s.hasClimbedLadder(): return
  377.  
  378.         if s.keys[SPACE]:
  379.             s.jumpHeight = JUMP_HEIGHT
  380.             s.anim.set(1)
  381.             s.currentObject = None
  382.             s.state = s.jumpUp
  383.             return
  384.  
  385.         if not s.keys[RIGHT]:
  386.             s.xIndexDirection = 0
  387.             s.anim.set(0)
  388.             s.state = s.stop
  389.             return
  390.  
  391.         if s.collidedWithLeftEdge():
  392.             s.anim.set(0)
  393.             return
  394.  
  395.         if s.xIndex == 186: return
  396.  
  397.         s.anim()
  398.         s.xIndex += s.xIndexDirection
  399.         s.rect.setX1(s.xIndex * X_VELOCITY)
  400.         if s.rect.x1 == s.currentObject.rect.x2:
  401.             if not s.collidedWithPlatform(MAPS[MAP_INDEX]['latforms'] + MAPS[MAP_INDEX]['ratforms']):
  402.                 s.xIndexDirection = 0
  403.                 s.currentObject = None
  404.                 s.anim.set(1)
  405.                 s.state = s.falling
  406.                 return
  407.  
  408.     def walkLeft(s):
  409.         global X_VELOCITY, JUMP_HEIGHT
  410.         global LEFT, UP, DOWN, SPACE
  411.  
  412.         if s.hasClimbedLadder(): return
  413.  
  414.         if s.keys[SPACE]:
  415.             s.jumpHeight = JUMP_HEIGHT
  416.             s.anim.set(1)
  417.             s.currentObject = None
  418.             s.state = s.jumpUp
  419.             return
  420.  
  421.         if not s.keys[LEFT]:
  422.             s.xIndexDirection = 0
  423.             s.anim.set(0)
  424.             s.state = s.stop
  425.             return
  426.  
  427.         if s.collidedWithRightEdge():
  428.             s.anim.set(0)
  429.             return
  430.  
  431.         if s.xIndex == 0: return
  432.  
  433.         s.anim()
  434.         s.xIndex += s.xIndexDirection
  435.         s.rect.setX1(s.xIndex * X_VELOCITY)
  436.  
  437.         if s.rect.x2 == s.currentObject.rect.x1:
  438.             if not s.collidedWithPlatform(MAPS[MAP_INDEX]['latforms'] + MAPS[MAP_INDEX]['ratforms']):            
  439.                 s.xIndexDirection = 0
  440.                 s.currentObject = None
  441.                 s.anim.set(1)
  442.                 s.state = s.falling
  443.                 return
  444.  
  445.     def jumpUp(s):
  446.         global X_VELOCITY, Y_VELOCITY
  447.  
  448.         if s.hasClimbedLadder(): return
  449.  
  450.         if s.xIndex == 0 or s.xIndex == 186:
  451.             s.xIndexDirection = -s.xIndexDirection
  452.  
  453.         s.xIndex += s.xIndexDirection
  454.         s.rect.setX1(s.xIndex * X_VELOCITY)
  455.         s.rect.offY1(-Y_VELOCITY)
  456.  
  457.         s.jumpHeight -= 1
  458.         if not s.jumpHeight:
  459.             s.state = s.jumpDown
  460.  
  461.     def jumpDown(s):
  462.         global X_VELOCITY, Y_VELOCITY
  463.  
  464.         if s.hasClimbedLadder(): return
  465.        
  466.         if s.xIndex == 0 or s.xIndex == 186:
  467.             s.xIndexDirection = -s.xIndexDirection
  468.        
  469.         if s.xIndexDirection > 0:
  470.             if s.collidedWithLeftEdge(): s.xIndexDirection = -s.xIndexDirection
  471.         elif s.xIndexDirection < 0:
  472.             if s.collidedWithRightEdge(): s.xIndexDirection = -s.xIndexDirection
  473.  
  474.         s.xIndex += s.xIndexDirection
  475.         s.rect.setX1(s.xIndex * X_VELOCITY)
  476.  
  477.         if s.collidedWithPlatform(MAPS[MAP_INDEX]['lifts']): return
  478.         s.rect.offY1(Y_VELOCITY)  
  479.  
  480.     def climbing(s):
  481.         global X_VELOCITY, Y_VELOCITY, JUMP_HEIGHT
  482.         global HARRY_WALKING_LEFT, HARRY_WALKING_RIGHT
  483.         global MAPS, MAP_INDEX
  484.         global UP, DOWN, LEFT, RIGHT, SPACE
  485.  
  486.         if s.keys[UP]:
  487.             if s.rect.y1 > s.currentObject.rect.y1:
  488.                 s.rect.offY1(-Y_VELOCITY)
  489.                 s.anim()
  490.         if s.keys[DOWN]:
  491.             if s.rect.y2 < s.currentObject.rect.y2:
  492.                 s.rect.offY1(Y_VELOCITY)
  493.                 s.anim()
  494.  
  495.         if s.keys[SPACE]:
  496.             s.jumpHeight = JUMP_HEIGHT
  497.             s.currentObject = None
  498.             s.state = s.jumpUp
  499.             if s.keys[LEFT]:
  500.                 s.xIndexDirection = -1
  501.                 s.anim = HARRY_WALKING_LEFT.set(1)
  502.             elif s.keys[RIGHT]:
  503.                 s.xIndexDirection = 1
  504.                 s.anim = HARRY_WALKING_RIGHT.set(1)
  505.             else:
  506.                 s.xIndexDirection = 0
  507.                 s.anim = HARRY_WALKING_RIGHT.set(1)
  508.             return
  509.            
  510.         #print(s.rect.y1, s.rect.y2, s.currentObject.rect.y1)
  511.  
  512.         if not (s.keys[LEFT] or s.keys[RIGHT]): return
  513.  
  514.         for platform in MAPS[MAP_INDEX]['platforms'] + MAPS[MAP_INDEX]['latforms'] + MAPS[MAP_INDEX]['ratforms']:
  515.             if s.rect.y2 == platform.rect.y1 and s.rect.x1 > platform.rect.x1 and s.rect.x2 < platform.rect.x2:
  516.                 if s.keys[LEFT] and type(platform) != ratforms:
  517.                     s.currentObject = platform
  518.                     s.xIndexDirection = -1
  519.                     s.anim = HARRY_WALKING_LEFT.reset()
  520.                     s.state = s.walkLeft
  521.                 elif s.keys[RIGHT] and type(platform) != latforms:
  522.                     s.currentObject = platform
  523.                     s.xIndexDirection = 1
  524.                     s.anim = HARRY_WALKING_RIGHT.reset()
  525.                     s.state = s.walkRight
  526.                 s.xIndex += s.xIndexDirection
  527.                 s.rect.setX1(s.xIndex * X_VELOCITY)
  528.                 break
  529.  
  530.  
  531.     def collidedWithLeftEdge(s):
  532.         global SCALE, TILE_SIZE
  533.         global MAPS, MAP_INDEX
  534.  
  535.         for platform in MAPS[MAP_INDEX]['platforms']:
  536.             if s.rect.x2 != platform.rect.x1: continue
  537.             if s.rect.y1 + TILE_SIZE > platform.rect.y2 or s.rect.y2 < platform.rect.y1 + SCALE: continue
  538.             return True
  539.         return False
  540.  
  541.     def collidedWithRightEdge(s):
  542.         global SCALE, TILE_SIZE
  543.         global MAPS, MAP_INDEX
  544.  
  545.         for platform in MAPS[MAP_INDEX]['platforms']:
  546.             if s.rect.x1 != platform.rect.x2: continue
  547.             if s.rect.y1 + TILE_SIZE > platform.rect.y2 or s.rect.y2 < platform.rect.y1 + SCALE: continue
  548.             return True
  549.         return False
  550.  
  551.     def collidedWithPlatform(s, additionalObjects = []): # worded as a question
  552.         global Y_VELOCITY
  553.         global MAPS, MAP_INDEX
  554.        
  555.         for platform in MAPS[MAP_INDEX]['platforms'] + additionalObjects:
  556.             if not (s.rect.x1 >= platform.rect.x2 or s.rect.x2 <= platform.rect.x1):
  557.                 if (s.rect.y2 < platform.rect.y1 and s.rect.y2 + Y_VELOCITY >= platform.rect.y1) or s.rect.y2 == platform.rect.y1:
  558.                     s.rect.setY2(platform.rect.y1)
  559.                     s.xIndexDirection = 0
  560.                     s.currentObject = platform
  561.                     s.anim.set(0)
  562.                     s.state = s.stop
  563.                     return True
  564.  
  565.         return False
  566.  
  567.     def hasClimbedLadder(s):
  568.         global HARRY_CLIMBING_LADDER
  569.         global SCALE
  570.         global MAPS, MAP_INDEX
  571.         global UP, DOWN
  572.  
  573.         if not (s.keys[UP] or s.keys[DOWN]): return
  574.  
  575.         for ladder in MAPS[MAP_INDEX]['ladders']:
  576.             if s.rect.x1 == ladder.rect.x1 + SCALE * 4:
  577.                 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):
  578.                     s.xIndexDirection = 0
  579.                     s.currentObject = ladder
  580.                     s.anim = HARRY_CLIMBING_LADDER.reset()
  581.                     s.state = s.climbing
  582.                     return
  583. #print("can climb, {} {} {} {}".format(ladder.rect.x1, ladder.rect.y1, ladder.rect.x2, ladder.rect.y2))
  584.  
  585. # set up scale and pygame variables
  586. SCALE = 5
  587. ZX_TILE_SIZE = 8
  588. TILE_SIZE = ZX_TILE_SIZE * SCALE
  589. WIDTH_IN_TILES = 32
  590. HEIGHT_IN_TILES = 24
  591.  
  592. FPS = 60
  593. W, H = int(WIDTH_IN_TILES * TILE_SIZE), int(HEIGHT_IN_TILES * TILE_SIZE)
  594. HW, HH = int(W / 2), int(H / 2)
  595.  
  596. pygame.init()
  597. DS = pygame.display.set_mode((W, H))
  598. TIME = gTime(FPS)
  599.  
  600. FONT_DATA = b'/Td6WFoAAATm1rRGAgAhARYAAAB0L+Wj4Aw9AW5dAACO8kOjEfa2JjD81EffSaUPknD93Eph1Fsy7hNTgvcZJxKO8vuqgc3XN\
  601. 3Lz2F/+rT9w9iuzsmBXzqTvbkREQ7Io0T/ALfy6MU/6BR7cC0iyLG9bk6y+A7e4DJDb86aloX9r2qeu1DnBUOu7ie8Y+Z8hOQg9rNd4bu5wlIPL\
  602. UIejdSo0RcIJiS5w1xmTlmFEI8n7isSpL+Zl3NNk/Hgrd+ajfx+dJ2r/+ENsaRYWNLJ8ZRkUMl3ECYmC9pzK5cMH9EZ/CTJLhNIFZq3EpapLc7+\
  603. dRZOMN3uxtEshysWcJJHip5CTAkjAoFbu4Tl0J8gtke66of1u/gv69rGbru0lbd0eY5MdQfHs1U1WtrBgJ7Di9mSu7h1YHvm07W8DhcOkb26+NG\
  604. 0/kV7JyQMczTE6VV6qN2FAK8JnjpkCh40xg63Dxi2m7N3JiKvl1tE/gPOvSJkGQIus7HQFHXjc6DouLaw3zh81ra8HDg4ZRQAvLAAAALI4wzJkk\
  605. ZgtAAGKA74YAADSUkbUscRn+wIAAAAABFla'
  606. FONT_CHARMAP = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789.!-"
  607.  
  608. HARRY_SPRITES = b'/Td6WFoAAATm1rRGAgAhARYAAAB0L+Wj4AVKAH9dAAAWAmcAfBl/5TWMp19wwFlOGq24MSuPf3eRkc2R2RivFpE4R/MfZ\
  609. GAnSrQ64HaOMEOasekciorXflNVPyjgTL3N2QFYaV0uDHNXhHxlUiuedO1fdTeMSHOW5OU6Tu7XeCRpIzMaFugdWzozAUe8Rf4DKtqMkKgOb0Ls\
  610. Qz0UbAAAAOWJY9H0+m9xAAGbAcsKAAA8908lscRn+wIAAAAABFla'
  611. TILE_SPRITES = b'/Td6WFoAAATm1rRGAgAhARYAAAB0L+Wj4AIZAGldAAARA7p1AZmJHQfQCrftyjKbVdeqFurA812ghbqqMl57MacZWPpN86\
  612. 29stHv87YrMSLM9ciN8jAbczJCFBYzApROMYK0+GEfHlIeaOOyX8DnGpk2nf88Q/iljp7QEwRszJVjeoTpI30AAAAAAABJeIIGhUDZewABhQGaB\
  613. AAAGtG8ZbHEZ/sCAAAAAARZWg=='
  614.  
  615. MAP_1 = b'CXBsYXRmb3Jtcw0IBwgSBwkdBwMPCgQGCwQYCwMVDAMSDQMPDgMEDwsXDwcCExwAFyAIbGF0Zm9ybXMBCgsCCHJhdGZvcm1zAAdsY\
  616. WRkZXJzBAoFEgUNBhARBhoRBgVsaWZ0cwADaGF5Cg8GABYGABEJABkKAAgOABgOAAgSABcSAAQWABUWAARlZ2dzDAkGABQGAB4GABAJAAcKABAN\
  617. AAQOABwOAAMSABQSAB0SAAYWAA=='
  618. MAP_2 = b'CXBsYXRmb3Jtcw0IBwoUBwwACxIUCwwADwYIDxAaDwYAEwwOEwQUEwQaEwYAFwYIFxgIbGF0Zm9ybXMACHJhdGZvcm1zAQYHAgdsY\
  619. WRkZXJzCAYFBg8FCg8RBhsFBhsNCgMJDgkJChUJBgVsaWZ0cwADaGF5AARlZ2dzAA=='
  620. MAP_3 = b'CXBsYXRmb3JtcxgVBgUaBwEdBwMGCAELCAcSCQMWCgIACwQZCwIbDAISDgMWDgIADwccDwMaEAIXEQIVEgILEwYSEwIZFAQEFgMAF\
  621. wQLFwUSFw4IbGF0Zm9ybXMDBAsCHQwCHRQCCHJhdGZvcm1zAhMGAgQIAgdsYWRkZXJzBxMEBQQGCQwGDQ8GDR0KBR0RBgENCgVsaWZ0cwIICgAI\
  622. EgADaGF5Ch0GAAsHABIIABYJAAEKABQNAAMOAAsSAAMWABYWAARlZ2dzDB4GAAYHAB0JAAIKABoKABcNAAYOABgQAA4SABsTAAYVABkWAA=='
  623. MAP_4 = b'CXBsYXRmb3JtcxoIBwILBwYVBwUbBwUHCAEGCQEFCgEACwQOCwMVCwEZCwIcCwQKDAIHDQIEDgIADwMLDwEVDwcdEAMAEwgKEwcVE\
  624. wceFAIAFwgJFwgVFwsIbGF0Zm9ybXMDFgsCDAwCDA8CCHJhdGZvcm1zAQwLAgdsYWRkZXJzBQwFEhYFCh0FBwQRBhgRBgVsaWZ0cwISCQASEQAD\
  625. aGF5Bg4GABUGAAgMAAESABAWAB0WAARlZ2dzDBoEABoHAAEKAA8KABsLAAEOABUOAB4PAAoSABsSAAEWABUWAA=='
  626. MAP_5 = b'CXBsYXRmb3JtcxEUBgQGBwgcBwQPCAERCQQVCgIACwoADwoPDwkeDwIAEwoOEwYVEwMAFwMEFw4UFwQcFwQIbGF0Zm9ybXMACHJhd\
  627. GZvcm1zARIGAgdsYWRkZXJzCRIEBRINBgcFCgMJBgsKBwsSBQQRBg8RBhURBgVsaWZ0cwADaGF5DRcFAAYGAAsGAAwGABwGABAOABcOAAcWAAgW\
  628. AAkWABQWABcWABwWAARlZ2dzDBUFAAkGAB4GABEIAAEKAAEOAB4OAA4PAAESAAgSABQTAAEWAA=='
  629.  
  630. TEST_MAP = b'CXBsYXRmb3JtcwcABwYKBwQQCAQKDAQIEgIWEgIAFyAIbGF0Zm9ybXMDDgcCDgwCChICCHJhdGZvcm1zAg4IAhQSAgdsYWRkZXJzBg4EEwoQBwwQBxAQBxIQBxQQBwVsaWZ0cwADaGF5AARlZ2dzAA=='
  631.  
  632. TILE_COLUMNS = 8
  633. PLATFORM_TILE = 0
  634. LADDER_TILE = 3
  635. EGG_TILE = 5
  636. HAY_TILE = 6
  637. LIFT_TILE = 7
  638.  
  639.  
  640. # make sprites
  641. SPRITE_SETS = {'tiles' : sprite(TILE_SPRITES, TILE_COLUMNS, 1), 'harry' : sprite(HARRY_SPRITES, 7, 1, [0, 0, 0])}
  642. LADDER_SEGMENT = pygame.Surface((TILE_SIZE * 2, TILE_SIZE))
  643. SPRITE_SETS['tiles'].blit(LADDER_TILE, LADDER_SEGMENT, (0, 0))
  644. SPRITE_SETS['tiles'].blit(LADDER_TILE + 1, LADDER_SEGMENT, (TILE_SIZE, 0))
  645. LIFT_SPRITE = pygame.Surface((TILE_SIZE * 2, TILE_SIZE))
  646. SPRITE_SETS['tiles'].blit(LIFT_TILE, LIFT_SPRITE, (0, 0))
  647. SPRITE_SETS['tiles'].blit(LIFT_TILE, LIFT_SPRITE, (TILE_SIZE, 0))
  648. EGG_SPRITE = pygame.Surface((TILE_SIZE, TILE_SIZE))
  649. SPRITE_SETS['tiles'].blit(EGG_TILE, EGG_SPRITE, (0, 0))
  650. HAY_SPRITE = pygame.Surface((TILE_SIZE, TILE_SIZE))
  651. SPRITE_SETS['tiles'].blit(HAY_TILE, HAY_SPRITE, (0, 0))
  652.  
  653.  
  654. # generate maps
  655. MAPS = [decodeMap(MAP_1), decodeMap(MAP_2), decodeMap(MAP_3), decodeMap(MAP_4), decodeMap(MAP_5), decodeMap(TEST_MAP)]
  656. MAP_INDEX = 2
  657.  
  658. # player stuff
  659. SPEED = 6
  660. X_VELOCITY = TILE_SIZE / SPEED
  661. Y_VELOCITY = 1 * SCALE
  662. JUMP_HEIGHT = 12
  663.  
  664. PLAYER = harry(12, 21)
  665.  
  666. FONT = font(imgUnzip(FONT_DATA), FONT_CHARMAP, 1)
  667.  
  668.  
  669. DEBUG = False
  670. while True:
  671.     e = pygame.event.get()
  672.     if pygame.key.get_pressed()[pygame.K_ESCAPE]: break
  673.  
  674.     DS.fill([0, 0, 0])
  675.  
  676.     for l, objs in MAPS[MAP_INDEX].items():
  677.         for obj in objs:
  678.             if hasattr(obj, "surface"):
  679.                 DS.blit(obj.surface, obj.rect.pos(True))
  680.                 #pygame.draw.rect(DS, (255, 255, 255), obj.rect.rect())
  681.             if hasattr(obj, "do"):
  682.                 obj.do()
  683.  
  684.     PLAYER.draw()
  685.     PLAYER.do()
  686.  
  687.     if DEBUG:
  688.         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))
  689.         DS.blit(FONT.text("XI-{}".format(PLAYER.xIndex), 1), (0, 35))
  690.         if PLAYER.currentObject:
  691.             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))
  692.    
  693.     pygame.display.update()
  694.     TIME.tick()
  695.  
  696. pygame.quit()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement