Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import pygame
- import pygame.gfxdraw
- import sys, os, time, math, random
- import zlib, base64
- # Pygame contants
- W = 1280
- H = 720
- HW = int(W / 2)
- HH = int(H / 2)
- FPS = 60
- # Define some colors
- FUCHSIA = [255, 0, 255]
- PURPLE = [128, 0, 128]
- TEAL = [ 0, 128, 128]
- LIME = [ 0, 255, 0]
- GREEN = [ 0, 128, 0]
- OLIVE = [128, 128, 0]
- DOLIVE = [ 64, 64, 0]
- YELLOW = [255, 255, 0]
- ORANGE = [255, 165, 0]
- RED = [255, 0, 0]
- MAROON = [128, 0, 0]
- SILVER = [192, 192, 192]
- GRAY = [128, 128, 128]
- BLUE = [ 0, 0, 255]
- NAVY = [ 0, 0, 128]
- AQUA = [ 0, 255, 255]
- WHITE = [255, 255, 255]
- BLACK = [ 0, 0, 0]
- BEACON_COLOR = [128, 64, 64]
- # embedded graphics
- FONT_DATA = b'eJx1UktOxTAM9HjipGmRHvAWHIAFWwQr7n8xxu4HgUQrt7E9GX/xbquFdU\
- jsphN4JwMkcdolk+czLRo8EM1tCCFBX+FfvHsLR+Nn4ZpOTyBuTnubBHabMAh3sTsWfQyxem\
- JTyuq47kNgqXnXxSyslEgvDsZ6Anu+yWgOxHa5uJx8Wc2g4lFvd2WvczKu7eCblQ24qBOIIb\
- bu0oXqRm585MUFeldGxRUqJqMK1cV6+59JuC6b7Erg4MocwAG4F5saRHuRMiy7+qtGuX5qlC\
- dbEToyfdUocSEn0mHO4qOPNTLj4k9c7Kx/ZpG2I2tquvskd4+nXbxrTqlJk7WkKV51tmIpE0\
- 7wNaY24UH2LVQ9py4MsAJ/6MZzG4lsGXGprZs9t27W7m3HpNCX/Is3jq5j37XcUc0kdxJddV\
- Y937F0CVs='
- FONT_CHARMAP = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789.!"
- # Game constants
- GRAVITY = 0.5
- BACKGROUND_COLOR_TOP = [0, 35, 35]
- BACKGROUND_COLOR_BOTTOM = [0, 32, 32]
- BACKGROUND_COLOR_TOP_INT = int.from_bytes(bytes(BACKGROUND_COLOR_TOP), "big")
- TITLE_TEXT = "DROP!"
- TITLE_BLOCK_SIZE = 10
- TITLE_BLOCK_MAX_EASE_FRAMES = 1 * FPS
- TITLE_BLOCK_OPACITY_STEP = 10
- TITLE_COLOR_TOP = [170, 156, 28]
- TITLE_COLOR_BOTTOM = [215, 111, 25]
- PLATFORM_BLOCK_SIZE = 10
- PLATFORM_BLOCK_SIZE_HALF = int(PLATFORM_BLOCK_SIZE / 2)
- PLATFORM_BLOCK_WIDTH = 10
- PLATFORM_BLOCK_HEIGHT = 3
- PLATFORM_WIDTH = PLATFORM_BLOCK_WIDTH * PLATFORM_BLOCK_SIZE
- PLATFORM_HEIGHT = PLATFORM_BLOCK_HEIGHT * PLATFORM_BLOCK_SIZE
- PLATFORM_WIDTH_HALF = int(PLATFORM_WIDTH / 2)
- PLATFORM_HEIGHT_HALF = int(PLATFORM_HEIGHT / 2)
- PLATFORM_OPACITY_STEP = 10
- PLATFORM_EXISTS_TIME = 3 # secs
- PLATFORM_CRUMBLE_TIME = PLATFORM_EXISTS_TIME / PLATFORM_BLOCK_HEIGHT
- PLAYER_SIZE = 10
- PLAYER_SIZE_HALF = int(PLAYER_SIZE / 2)
- PLAYER_LIVES = 3
- PLAYER_FALL_SCORE_LOSS = 0.25
- FOOD_SIZE = 10
- FOOD_SIZE_HALF = int(FOOD_SIZE / 2)
- FOOD_MAX_EASE_FRAMES = 1 * FPS
- FOOD_EXISTS_TIME = 20
- FOOD_BEACON_END_SIZE = 200
- FOOD_BEACON_DURATION = 0.5 # seconds
- FOOD_DEAD_SCORE_LOSS = 0.5
- LIFE_COUNT = 3
- LIFE_SIZE = 20
- LIFE_SIZE_HALF = int(LIFE_SIZE / 2)
- LIFE_OPACITY_STEP = 5
- SCORE_SCALE = 5
- SCORE_SPACING = 5
- SCORE_COLOR_TOP = [48, 57, 26]
- SCORE_COLOR_BOTTOM = [28, 45, 29]
- SCORE_COLOR_BOTTOM_START = 7
- class player:
- def __init__(s):
- s.reset()
- def reset(s):
- global PLATFORM_WIDTH_HALF
- s.x = 5 + PLATFORM_WIDTH_HALF
- s.y = -10
- s.velocityY = 0
- s.currentState = s.falling
- def horizontalKeys(s):
- k = pygame.key.get_pressed()
- if k[pygame.K_a]:
- s.x -= 3
- if k[pygame.K_d]:
- s.x += 3
- def jumpingKey(s):
- k = pygame.key.get_pressed()
- if k[pygame.K_SPACE]:
- s.velocityY = -10
- s.currentState = s.jumping
- def jumping(s):
- s.y += s.velocityY
- s.velocityY += GRAVITY
- if s.velocityY >= 0:
- s.currentState = s.falling
- def falling(s):
- global H
- global GRAVITY
- global SCORE, PLAYER_FALL_SCORE_LOSS
- s.y += s.velocityY
- s.velocityY += GRAVITY
- if s.y >= H:
- SCORE.score -= SCORE.score * PLAYER_FALL_SCORE_LOSS
- s.reset()
- def walking(s):
- global PLATFORMS
- global PLAYER_SIZE_HALF
- s.jumpingKey()
- if s.x + PLAYER_SIZE_HALF < PLATFORMS.activePlatform.x1 or s.x - PLAYER_SIZE_HALF > PLATFORMS.activePlatform.x2:
- PLATFORMS.activePlatform = None
- s.currentState = s.falling
- def draw(s):
- global DS
- global ORANGE
- global PLAYER_SIZE, PLAYER_SIZE_HALF
- pygame.draw.rect(DS, ORANGE, (s.x - PLAYER_SIZE_HALF, int(s.y - PLAYER_SIZE), PLAYER_SIZE, PLAYER_SIZE), 0)
- def do(s):
- s.draw()
- s.horizontalKeys()
- s.currentState()
- class platformBlock:
- def __init__(s, x, y, w, staticTime):
- s.x = x
- s.y = y
- s.w = w
- s.velocity = 0
- s.opacity = 255
- s.dead = False
- s.timeCreated = time.time()
- s.staticTime = staticTime
- def draw(s):
- global DS
- global DOLIVE
- global PLATFORM_BLOCK_SIZE
- pygame.gfxdraw.box(DS, (s.x, int(s.y), s.w, PLATFORM_BLOCK_SIZE), DOLIVE + [s.opacity])
- def move(s):
- global H
- global PLATFORM_OPACITY_STEP
- global GRAVITY
- if time.time() - s.timeCreated < s.staticTime: return
- s.velocity += GRAVITY * 1.5
- s.y += s.velocity
- if s.y >= H:
- s.dead = True
- s.opacity -= PLATFORM_OPACITY_STEP
- if s.opacity <= 0:
- s.opacity = 0
- s.dead = True
- def do(s):
- s.draw()
- s.move()
- class platform:
- def __init__(s, x, y):
- global PLATFORM_BLOCK_HEIGHT
- global PLATFORM_WIDTH_HALF
- s.timeStamp = time.time()
- s.x1 = x - PLATFORM_WIDTH_HALF
- s.x2 = x + PLATFORM_WIDTH_HALF
- s.y = y
- s.h = PLATFORM_BLOCK_HEIGHT
- def draw(s, c):
- global DS
- global PLATFORM_WIDTH
- global PLATFORM_BLOCK_SIZE
- pygame.draw.rect(DS, c, (s.x1, s.y, PLATFORM_WIDTH, s.h * PLATFORM_BLOCK_SIZE), 0)
- class platforms:
- def __init__(s):
- global H
- global PLATFORM_WIDTH_HALF, PLATFORM_HEIGHT_HALF
- s.platforms = [platform(PLATFORM_WIDTH_HALF + 5, H - PLATFORM_HEIGHT_HALF - 5)]
- s.blocks = []
- s.activePlatform = None
- def do(s):
- global DS
- global DOLIVE, OLIVE
- global PLATFORM_WIDTH, PLATFORM_HEIGHT
- global PLATFORM_BLOCK_SIZE, PLATFORM_BLOCK_WIDTH
- global PLAYER
- deadPlatforms = []
- for p in s.platforms: #container
- if p != s.activePlatform:
- p.draw(DOLIVE)
- if p == s.platforms[0]: continue
- now = time.time()
- if now - p.timeStamp >= PLATFORM_CRUMBLE_TIME:
- p.timeStamp = now
- p.h -= 1
- if p.h == 0:
- deadPlatforms.append(p)
- s.blocks.append(platformBlock(p.x1, p.y, PLATFORM_WIDTH, 0))
- if p == s.activePlatform and PLAYER.currentState == PLAYER.walking:
- PLAYER.currentState = PLAYER.falling
- s.activePlatform = None
- else:
- for index in range(PLATFORM_BLOCK_WIDTH):
- s.blocks.append(platformBlock(p.x1 + index * PLATFORM_BLOCK_SIZE, p.y + p.h * PLATFORM_BLOCK_SIZE, PLATFORM_BLOCK_SIZE, random.uniform(0, PLATFORM_CRUMBLE_TIME)))
- for dp in deadPlatforms:
- s.platforms.remove(dp)
- deadBlocks = []
- for b in s.blocks:
- b.do()
- if b.dead: deadBlocks.append(b)
- for db in deadBlocks:
- s.blocks.remove(db)
- if s.activePlatform:
- s.activePlatform.draw(OLIVE)
- def add(s, posxy):
- global PLATFORM_HEIGHT_HALF
- s.platforms.append(platform(posxy[0], posxy[1] - PLATFORM_HEIGHT_HALF))
- def doCollision(s):
- global H
- global PLAYER
- if PLAYER.currentState != PLAYER.falling: return
- possiblePlatforms = []
- for p in s.platforms:
- if PLAYER.x >= p.x1 and PLAYER.x <= p.x2:
- if PLAYER.y <= p.y and PLAYER.y + PLAYER.velocityY >= p.y:
- possiblePlatforms.append(p)
- if not possiblePlatforms: return
- y = H
- s.activePlatform = None
- for p in possiblePlatforms:
- if p.y < y:
- s.activePlatform = p
- y = p.y
- PLAYER.y = y
- PLAYER.velocityY = 0
- PLAYER.currentState = PLAYER.walking
- class food:
- def __init__(s, x, y):
- global H, HW
- global FOOD_SIZE_HALF
- global FOOD_MAX_EASE_TIME, FOOD_EXISTS_TIME
- s.x = x
- s.y = -FOOD_SIZE_HALF
- s.py = y
- fallInDuration = FOOD_MAX_EASE_FRAMES * (s.py / H)
- s.fallIn = [-FOOD_SIZE_HALF + round(fallIn(fi, 0, s.py + FOOD_SIZE_HALF, fallInDuration)) for fi in range(int(fallInDuration) + 1)]
- if s.fallIn[-1] != s.py: s.fallIn.append(s.py)
- s.fallInIndex = 0
- dropOutTarget = (H + FOOD_SIZE_HALF - s.py)
- dropOutDuration = FOOD_MAX_EASE_FRAMES * (dropOutTarget / H)
- s.dropOut = [s.py + round(dropOut(fi, 0, dropOutTarget, dropOutDuration)) for fi in range(int(dropOutDuration) + 1)]
- if s.dropOut[-1] != H + FOOD_SIZE_HALF: s.dropOut.append(H + FOOD_SIZE_HALF)
- s.dropOutIndex = 0
- s.timeStamp = time.time()
- s.ready = False
- s.readyTime = None
- s.beaconTime = None
- s.nextBeaconTime = int(FOOD_EXISTS_TIME / 2)
- s.dead = False
- def do(s):
- global DS
- global RED
- global FOOD_SIZE, FOOD_SIZE_HALF
- global FOOD_EXISTS_TIME
- global FOOD_BEACON_END_SIZE, BEACONS
- global SCORE, FOOD_DEAD_SCORE_LOSS
- pygame.draw.rect(DS, RED, (s.x - FOOD_SIZE_HALF, s.y - FOOD_SIZE_HALF, FOOD_SIZE, FOOD_SIZE), 0)
- now = time.time()
- if not s.ready:
- s.y = s.fallIn[s.fallInIndex]
- s.fallInIndex += 1
- if s.fallInIndex == len(s.fallIn):
- s.ready = True
- s.readyTime = now
- s.beaconTime = s.readyTime
- BEACONS.add(s.x, s.y, FOOD_SIZE, FOOD_BEACON_END_SIZE, FOOD_BEACON_DURATION)
- return
- if s.ready and now - s.readyTime > FOOD_EXISTS_TIME:
- s.y = s.dropOut[s.dropOutIndex]
- s.dropOutIndex += 1
- if s.dropOutIndex == len(s.dropOut):
- s.dead = True
- SCORE.score -= SCORE.score * FOOD_DEAD_SCORE_LOSS
- else:
- if now >= s.beaconTime + s.nextBeaconTime:
- s.beaconTime = now
- s.nextBeaconTime /= 2
- if s.nextBeaconTime < 0.25: s.nextBeaconTime = 0.25
- BEACONS.add(s.x, s.y, FOOD_SIZE, FOOD_BEACON_END_SIZE, FOOD_BEACON_DURATION)
- class foods:
- def __init__(s):
- s.container = []
- #s.add()
- def do(s):
- global LIVES
- deadFood = []
- for f in s.container:
- f.do()
- if f.dead:
- deadFood.append(f)
- LIVES.loseLife()
- s.add()
- for df in deadFood:
- s.container.remove(df)
- def add(s):
- global W, H
- global FOOD_SIZE_HALF
- global SCORE
- s.container.append(food(random.randint(FOOD_SIZE_HALF, W - FOOD_SIZE_HALF), random.randint(0, H)))
- def doCollision(s):
- global PLAYER, PLAYER_SIZE_HALF
- global FOOD_SIZE_HALF
- removeFood = []
- for f in s.container:
- if PLAYER.x - PLAYER_SIZE_HALF > f.x + FOOD_SIZE_HALF or PLAYER.x + PLAYER_SIZE_HALF < f.x - FOOD_SIZE_HALF \
- or PLAYER.y - PLAYER_SIZE_HALF > f.y + FOOD_SIZE_HALF or PLAYER.y + PLAYER_SIZE_HALF < f.y - FOOD_SIZE_HALF:
- pass
- else:
- #print(time.time() - f.timeStamp)
- SCORE.score += time.time() - f.timeStamp
- removeFood.append(f)
- for rf in removeFood:
- s.container.remove(rf)
- s.add()
- class beacon:
- def __init__(s, x, y, startSize, endSize, time):
- global FPS
- s.x = x
- s.y = y
- frameCount = FPS * time
- s.growData = [startSize + round(fallIn(fi, 0, endSize, frameCount)) for fi in range(int(frameCount) + 1)]
- if s.growData[-1] != endSize: s.growData.append(endSize)
- s.growIndex = 0
- s.opacityStep = 64 / frameCount
- s.dead = False
- def do(s):
- global DS
- global BEACON_COLOR
- size = int(s.growData[s.growIndex] / 2)
- pygame.gfxdraw.box(DS, (s.x - size, s.y - size, size * 2, size * 2), BEACON_COLOR + [int(64 +s.opacityStep - s.opacityStep * s.growIndex)])
- s.growIndex += 1
- if s.growIndex == len(s.growData):
- s.dead = True
- class beacons:
- def __init__(s):
- s.container = []
- def add(s, x, y, startSize, endSize, time):
- s.container.append(beacon(x, y, startSize, endSize, time))
- def do(s):
- deadBeacons = []
- for b in s.container:
- b.do()
- if b.dead: deadBeacons.append(b)
- for db in deadBeacons:
- s.container.remove(db)
- class life:
- def __init__(s, x):
- global LIFE_SIZE_HALF
- s.x = x
- s.y = 5 + LIFE_SIZE_HALF
- s.velocity = 0
- s.opacity = 255
- s.dead = False
- s.remove = False
- def do(s):
- global DS
- global PURPLE
- global LIFE_SIZE, LIFE_SIZE_HALF
- global LIFE_OPACITY_STEP
- pygame.gfxdraw.box(DS, (s.x - LIFE_SIZE_HALF, int(s.y - LIFE_SIZE_HALF), LIFE_SIZE, LIFE_SIZE), PURPLE + [s.opacity])
- if not s.dead: return
- s.opacity -= LIFE_OPACITY_STEP
- s.y += s.velocity
- s.velocity += GRAVITY
- if s.opacity <=0: s.remove = True
- class lives:
- def __init__(s):
- global W
- global LIFE_COUNT
- global LIFE_SIZE, LIFE_SIZE_HALF
- s.lives = [life(W + LIFE_SIZE_HALF - (x * (LIFE_SIZE + 5))) for x in range(LIFE_COUNT, 0, -1)]
- def do(s):
- removeLives = []
- for l in s.lives:
- l.do()
- if l.remove:
- removeLives.append(l)
- for rl in removeLives:
- s.lives.remove(rl)
- def loseLife(s):
- global game_over
- liveCount = len(s.lives)
- for l in s.lives:
- liveCount -=1
- if l.dead: continue
- l.dead = True
- break
- if liveCount == 0:
- game_over = True
- class bkgdPoint:
- def __init__(s, x):
- global HH
- s.p = [x, 0]
- s.r = random.randint(50, HH - 50)
- s.a = random.uniform(0, 6.283185307179586)
- s.s = random.uniform(0.01, 0.035)
- def calc(s):
- global HH
- s.p[1] = HH + int(math.cos(s.a) * s.r)
- s.a += s.s
- class background:
- def __init__(s):
- global W, H
- s.points = [bkgdPoint(x) for x in range(0, W, int(W / 10))] + [bkgdPoint(W)]
- s.poly1 = [[W, 0], [0, 0]] + [bp.p for bp in s.points]
- s.poly2 = [[W, H], [0, H]] + [bp.p for bp in s.points]
- def draw(s):
- global DS
- global BACKGROUND_COLOR_TOP, BACKGROUND_COLOR_BOTTOM
- pygame.draw.polygon(DS, BACKGROUND_COLOR_TOP, s.poly1)
- pygame.draw.polygon(DS, BACKGROUND_COLOR_BOTTOM, s.poly2)
- def do(s):
- for bp in s.points: bp.calc()
- s.draw()
- class font:
- def __init__(s, img, charMap, spacing):
- s.surface = img
- s.surfaceWidth, s.surfaceHeight = img.get_size()
- s.cols = len(charMap)
- s.charWidth = int(s.surfaceWidth / s.cols)
- s.spacing = spacing
- s.charMap = charMap
- def textSurface(s, text, scale = 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
- surface.blit(s.surface, (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 size(s, text):
- return (len(text) * s.charWidth + s.spacing * (len(text) - 1), s.surfaceHeight)
- class titleBlock:
- def __init__(s, x, y):
- global H
- global BACKGROUND_COLOR_TOP
- global TITLE_BLOCK_SIZE
- global TITLE_BLOCK_MAX_EASE_FRAMES
- s.x = x
- s.y = -TITLE_BLOCK_SIZE
- s.py = y
- fallInDuration = TITLE_BLOCK_MAX_EASE_FRAMES * (y / H)
- s.fallIn = [-TITLE_BLOCK_SIZE + round(fallIn(fi, 0, y + TITLE_BLOCK_SIZE, fallInDuration)) for fi in range(int(fallInDuration) + 1)]
- if s.fallIn[-1] != y: s.fallIn.append(y)
- s.fallInIndex = 0
- s.staticTime = random.uniform(3, 5)
- s.timeStamp = None
- s.velocity = 0
- s.color = BACKGROUND_COLOR_TOP
- s.opacity = 255
- s.dead = False
- def do(s):
- global DS
- global WHITE
- global GRAVITY
- global TITLE_BLOCK_SIZE
- global TITLE_BLOCK_OPACITY_STEP
- if s.dead: return
- pygame.gfxdraw.box(DS, (s.x, int(s.y), TITLE_BLOCK_SIZE, TITLE_BLOCK_SIZE), s.color + [s.opacity])
- if s.fallInIndex < len(s.fallIn):
- s.y = s.fallIn[s.fallInIndex]
- s.fallInIndex += 1
- elif s.timeStamp == None:
- s.timeStamp = time.time()
- if s.timeStamp == None: return
- if time.time() >= s.timeStamp + s.staticTime:
- s.y += s.velocity
- s.velocity += GRAVITY
- s.opacity -= TITLE_BLOCK_OPACITY_STEP
- if s.opacity <= 0:
- s.dead = True
- def reset(s):
- global BACKGROUND_COLOR_TOP
- global TITLE_BLOCK_SIZE
- s.y = -TITLE_BLOCK_SIZE
- s.fallInIndex = 0
- s.timeStamp = None
- s.velocity = 0
- s.opacity = 255
- s.color = BACKGROUND_COLOR_TOP
- s.dead = False
- class titleBlocks:
- def __init__(s):
- global HW, HH
- global FONT
- global TITLE_TEXT
- global TITLE_BLOCK_SIZE
- FONT.spacing = 4
- titleSurface = FONT.textSurface(TITLE_TEXT)
- w, h = FONT.size(TITLE_TEXT)
- FONT.spacing = 1
- cx = HW - int(w / 2) * TITLE_BLOCK_SIZE
- cy = HH - int(h / 2) * TITLE_BLOCK_SIZE
- s.allDead = False
- s.container = []
- pixelArray = pygame.PixelArray(titleSurface)
- for x in range(w):
- for y in range(h):
- if pixelArray[x][y] != 0:
- s.container.append(titleBlock(cx + x * TITLE_BLOCK_SIZE, cy + y * TITLE_BLOCK_SIZE))
- pixelArray.close()
- def do(s):
- global DS
- global BACKGROUND_COLOR_TOP_INT
- global TITLE_COLOR_TOP, TITLE_COLOR_BOTTOM
- pa = pygame.PixelArray(DS)
- for tb in s.container:
- colorInt = pa[tb.x][int(tb.y)]
- if colorInt == BACKGROUND_COLOR_TOP_INT:
- tb.color = TITLE_COLOR_TOP
- else:
- tb.color = TITLE_COLOR_BOTTOM
- pa.close()
- s.deadCount = 0
- for tb in s.container:
- if s.allDead:
- tb.reset()
- tb.do()
- if tb.dead: s.deadCount += 1
- if s.deadCount == len(s.container):
- s.allDead = True
- else:
- s.allDead = False
- class score:
- def __init__(s):
- global FONT_CHARMAP
- global SCORE_COLOR_TOP, SCORE_COLOR_BOTTOM, SCORE_COLOR_BOTTOM_START
- s.reset()
- s.font = font(imgUnzip(FONT_DATA, (BLACK, SCORE_COLOR_TOP)), FONT_CHARMAP, 1)
- scoreColorBottom = s.font.surface.map_rgb(SCORE_COLOR_BOTTOM)
- pa = pygame.PixelArray(s.font.surface)
- for y in range(SCORE_COLOR_BOTTOM_START, s.font.surfaceHeight):
- for x in range(s.font.surfaceWidth):
- if pa[x][y] != 0:
- pa[x][y] = scoreColorBottom
- pa.close()
- def reset(s):
- s.score = 0
- s.x = 0
- s.y = 0
- s.prevScore = -1
- def draw(s):
- global DS
- global W, H
- global BLACK
- global SCORE_SCALE, SCORE_SPACING
- if s.prevScore != s.score:
- if s.score < 0: s.score = 0
- s.prevScore = s.score
- s.scoreSurface = s.font.textSurface(str(round(s.score, 2)), SCORE_SCALE)
- s.scoreSurface.set_colorkey(0)
- s.scoreSurfaceWidth, s.scoreSurfaceHeight = s.scoreSurface.get_size()
- s.x = W - SCORE_SPACING - s.scoreSurfaceWidth
- s.y = H - SCORE_SPACING - s.scoreSurfaceHeight
- DS.blit(s.scoreSurface, (s.x, s.y))
- def imgUnzip(z64, colors):
- colorList = [(colors[0][0] << 16) + (colors[0][1] << 8) + colors[0][2], (colors[1][0] << 16) + (colors[1][1] << 8) + colors[1][2]]
- data = zlib.decompress(base64.b64decode(z64))
- w = int.from_bytes(data[0:2], "big")
- h = int.from_bytes(data[2:4], "big")
- surface = pygame.Surface((w, h))
- pixelArray = pygame.PixelArray(surface)
- pixelOffset = 0
- colorIndex = 0
- for byteIndex in range(4, len(data)):
- colorLength = data[byteIndex]
- for index in range(colorLength + 1):
- x = pixelOffset % w
- y = int(pixelOffset / w)
- pixelOffset += 1
- pixelArray[x][y] = colorList[colorIndex]
- if colorLength != 255:
- colorIndex = 1 - colorIndex
- pixelArray.close()
- return surface
- def fallIn(frameIndex, offset, target, duration):
- frameIndex /= duration
- frameIndex -= 1
- return target * (frameIndex * frameIndex * frameIndex * frameIndex * frameIndex + 1) + offset
- def dropOut(frameIndex, offset, target, duration):
- frameIndex /= duration
- return target * frameIndex * frameIndex + offset
- def drawDashedVLine(surface, color, x, y, height, dashLength):
- for lineY in range(0, height, dashLength * 2):
- pygame.draw.line(surface, color, (x, y + lineY), (x, y + lineY + dashLength), 1)
- def drawDashedHLine(surface, color, x, y, width, dashLength):
- for lineX in range(0, width, dashLength * 2):
- pygame.draw.line(surface, color, (x + lineX, y), (x + lineX + dashLength, y), 1)
- def events():
- for event in pygame.event.get():
- if event.type == pygame.QUIT or (event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE):
- pygame.quit()
- sys.exit()
- def drawPlatformCursor():
- global DS
- global GRAY
- global PLATFORM_WIDTH, PLATFORM_HEIGHT, PLATFORM_WIDTH_HALF, PLATFORM_HEIGHT_HALF
- mx, my = pygame.mouse.get_pos()
- drawDashedHLine(DS, GRAY, mx - PLATFORM_WIDTH_HALF, my - PLATFORM_HEIGHT_HALF, PLATFORM_WIDTH, 4)
- drawDashedHLine(DS, GRAY, mx - PLATFORM_WIDTH_HALF, my + PLATFORM_HEIGHT_HALF, PLATFORM_WIDTH, 4)
- drawDashedVLine(DS, GRAY, mx - PLATFORM_WIDTH_HALF, my - PLATFORM_HEIGHT_HALF, PLATFORM_HEIGHT, 4)
- drawDashedVLine(DS, GRAY, mx + PLATFORM_WIDTH_HALF, my - PLATFORM_HEIGHT_HALF, PLATFORM_HEIGHT, 4)
- def title():
- global BACKGROUND, TITLE, CLOCK
- events()
- BACKGROUND.do()
- TITLE.do()
- pygame.display.update()
- CLOCK.tick(FPS)
- # Initialise screen centering
- if sys.platform == 'win32' or sys.platform == 'win64':
- os.environ['SDL_VIDEO_CENTERED'] = '1'
- # Initialise Pygame
- pygame.init()
- DS = pygame.display.set_mode((W, H), pygame.NOFRAME)
- CLOCK = pygame.time.Clock()
- # set random seed
- random.seed(3767) # 3767 = DROP on telephone keypad
- # Set up game entities
- FONT_SURFACE = imgUnzip(FONT_DATA, (BLACK, WHITE))
- FONT = font(FONT_SURFACE, FONT_CHARMAP, 1)
- SCORE = score()
- PLATFORMS = platforms()
- PLAYER = player()
- BACKGROUND = background()
- TITLE = titleBlocks()
- FOOD = foods()
- LIVES = lives()
- BEACONS = beacons()
- # Game variables
- mouseClick = False
- game_over = False
- # Main loop
- while True:
- while not pygame.mouse.get_pressed()[0]:
- title()
- while pygame.mouse.get_pressed()[0]:
- title()
- FOOD.add()
- while not game_over or len(LIVES.lives):
- events()
- mb = pygame.mouse.get_pressed()
- if mb[0] and not mouseClick:
- PLATFORMS.add(pygame.mouse.get_pos())
- SCORE.score -= 0.1
- mouseClick = True
- if not mb[0] and mouseClick:
- mouseClick = False
- BACKGROUND.do()
- SCORE.draw()
- BEACONS.do()
- drawPlatformCursor()
- PLATFORMS.do()
- LIVES.do()
- PLAYER.do()
- FOOD.do()
- PLATFORMS.doCollision()
- FOOD.doCollision()
- pygame.display.update()
- CLOCK.tick(FPS)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement