def draw(self): vista.clear() game.state.draw() panels.draw() if self.parttobuild is not None: self.parttobuild.draw() vista.flip()
def draw(self): if self.paused: vista._screen.blit(self.pscreen, (0,0)) pygame.display.flip() return vista.clear() if self.panel.selected is not None or self.status.selected is not None: self.body.tracehexes() self.body.draw() if self.parttobuild is not None: self.parttobuild.draw() for t in self.twinklers: t.draw() for s in self.shots: s.draw() vista.addmask(self.body.mask) self.panel.draw() self.status.draw() vista.flip()
def draw(self): if self.paused: vista._screen.blit(self.pscreen, (0, 0)) pygame.display.flip() return vista.clear() if self.panel.selected is not None or self.status.selected is not None: self.body.tracehexes() self.body.draw() if self.parttobuild is not None: self.parttobuild.draw() for t in self.twinklers: t.draw() for s in self.shots: s.draw() vista.addmask(self.body.mask) self.panel.draw() self.status.draw() vista.flip()
def makeit(self): vista.zoom = settings.mapzoom game.state.body.remakemask() vista.setgrect(game.state.body.mask.bounds()) x0, y0 = vista.worldtoscreen((vista.wx0, vista.wy1)) x1, y1 = vista.worldtoscreen((0, 0)) x2, y2 = vista.worldtoscreen((vista.wx1, vista.wy0)) settings.vx0, settings.vy0 = 0, 0 settings.vx, settings.vy = x, y = x2 - x0, y2 - y0 vista.vrect = pygame.Rect(0, 0, x, y) vista.screen = vista.Surface((x, y), alpha = False) vista.stars = [(random.randint(64, 255), random.randint(-10000, 10000), random.randint(-10000, 10000)) for _ in range(x * y / 2000)] vista.stars.sort() vista.gx0 = x1-x0 vista.gy0 = y1-y0 vista.clear() game.state.draw() pygame.image.save(vista.screen, settings.mapfile)
import pygame import settings, scene, vista, gamescene, buildscene, menuscene pygame.init() vista.init() pygame.display.set_caption(settings.gamename) scene.push(menuscene) #scene.push(gamescene) #scene.push(buildscene) clock = pygame.time.Clock() while True: scene0 = scene.top() if not scene0: break dt = min(clock.tick(settings.maxfps) * 0.001, 1 / settings.minfps) events = pygame.event.get() if settings.DEBUG: pygame.display.set_caption("%s | %.1ffps" % (settings.gamename, clock.get_fps())) if any(event.type == pygame.QUIT for event in events): break scene0.think(dt, events, pygame.mouse.get_pos()) vista.clear() scene0.draw() vista.flip() if any(event.type == pygame.KEYDOWN and event.key == pygame.K_F12 for event in events): vista.screenshot()
def action(): ''' This is part of the gameplay event loop runs when the state is not in a menu, cutscene, or other non-gameplay state. However, the paused state is included here. ''' global level vista.levelinit(level) global prevPCKey global touchEffect global touchEffectPos global touchColor butterflies, goal, timeout = loadlevel.load(level) clock = pygame.time.Clock() facingright = True x, y, g, vy = 200, 0, (250 if settings.easy else 500), 0 leapvx, leapvy = 200., 200. twirlvy = 200. nabvx = 400. runvx = 300. rollvx, rollvy = 250., 250. dartvx, dartvy = 250., 300. boundvx, boundvy = -100., 250. nabtick, nabtime = 0, 0.25 nabradius = 50 twirlradius = 80 rollradius = 80 leaping = 0 runtick = 0 twirltick = 0 rolltick = 0 currentfeat = "" combocount = 0 grounded = True paused = False titleEffect = effect.Effect(["READY", "SET", "COLLECT"]) endtitle = True ending = False effects = [] heffect = effect.HeightIndicator() ceffect = effect.ComboIndicator() peffect = effect.ProgressIndicator(goal) cdeffect = effect.CountdownIndicator(timeout) seffect = effect.StageNameEffect(level, goal, timeout) feat.startlevel() pygame.event.get() prevTitleStr = None pcRect = None pcPos = None while True: dt = clock.tick(60) * 0.001 if settings.printfps and random.random() < dt: print(clock.get_fps()) if paused: for event in pygame.event.get(): result = 0 if event.type == QUIT: sys.exit() else: result = read_event( controller1, event, pcRect=pcRect, mb_sids=('nab', None, 'BACK'), # always_collide_mb=2, ) if result < 2: vista.screen.blit(pausescreen, (0, 0)) pygame.display.flip() # Try to prevent a black screen when switching # away from the game and back. continue if controller1.getBool('BACK'): ending = True endtitle = None feat.checknewfeat(len(record.collected)) if record.catchamount >= goal: unlocked = record.unlocked record.checkhiscore(level) if record.unlocked > unlocked: level = record.unlocked paused = False noise.unpause() elif controller1.getBool('SCREENSHOT'): screenshot(vista.screen) elif controller1.getBool('nab'): paused = False noise.unpause() elif controller1.getBool('FULLSCREEN'): settings.fullscreen = not settings.fullscreen vista.init() vista.screen.blit(pausescreen, (0, 0)) pygame.display.flip() continue # action main event loop (when not paused): up_events = [] for event in pygame.event.get(): result = 0 if event.type == QUIT: sys.exit() elif event.type == MOUSEBUTTONUP: # Process this separately in case the button was # pressed and released in the same frame such as # with the scroll wheel turning. up_events.append(event) else: # pcRect may be None for a frame but read_event # is ok with that. always_collide_mb = None if event.type == MOUSEBUTTONDOWN: if not grounded: # Double-jump if in the air # - 3: Change right-click from 'x' to 'jump' # even if not clicking the character # - 4&5: Change wheel roll to nab to allow roll # ('x' and 'nab' at the same time) with # only a mouse. always_collide_mb = (2, 3, 4, 5) elif (pcRect is not None) and (event.pos[1] < pcRect.top): # Jump if clicking above the character's head # even if on the ground and not clicking in # pcRect. always_collide_mb = (3) if settings.verbose: print("[main.action] pcRect={}" "".format(pcRect)) if pcRect is not None: print("[main.action] pcRect.top={}" "".format(pcRect.top)) print("[main.action] event.pos={}" "".format(event.pos)) print("[main.action] always_collide_mb={}" "".format(always_collide_mb)) result = read_event( controller1, event, pcRect=pcRect, mb_sids=('nab', 'nab', 'jump'), always_collide_mb=always_collide_mb, ) # ^ Set the wheel to nab and add 3, 4 & 5 to # always_collide_mb when in the air to allow # the roll combo using only a mouse but still # allowing both: # - nab would be impossible if left button # were in always_collide_mb--when not, click on # the character to nab, and click away to dart # or turn # - See "Mouse Controls" in readme.md. # 1: left # 2: middle # 3: right # 4: scroll up # 5: scroll down # ^ The first one in mb_sids is used for button 1. controller_changed = result > 0 if controller1.getBool('EXIT') and controller_changed: # The same button for exit is also pause. paused = True noise.pause() pausescreen = pygame.Surface( vista.screen.get_size()).convert_alpha() fade = pygame.Surface(vista.screen.get_size()).convert_alpha() fade.fill((0, 0, 0, 128)) pausescreen.blit(vista.screen, (0, 0)) pausescreen.blit(fade, (0, 0)) pausetitle = effect.PauseTitle(["PAUSED"]) pauseFmt = "Press [nab] to resume|or [BACK] to exit level" pauseStr = control_format(pauseFmt, controller1) pauseinfo = effect.PauseInfo([pauseStr]) pausetitle.position(pausescreen) pauseinfo.position(pausescreen) pausetitle.draw(pausescreen) pauseinfo.draw(pausescreen) if controller1.getBool('SCREENSHOT') and controller_changed: screenshot(vista.screen) if controller1.getBool('feat') and controller_changed: settings.hidefeatnames = not settings.hidefeatnames feat.startlevel(False) if controller1.getBool('FULLSCREEN') and controller_changed: settings.fullscreen = not settings.fullscreen vista.init() # k = pygame.key.get_pressed() # k = controller1.toKeys() kcombo = combo.get_combo(controller1) if settings.verbose: if (kcombo is not None) and (kcombo != ""): print("[main] kcombo={}".format(kcombo)) # else: # print("[main] controller1._states={}" # "".format(controller1._states)) for event in up_events: # Process these after combos in case down and up were on # the same frame. For further details about # read_event see the first call (this one should match it, # but always_collide_mb doesn't matter on MOUSEBUTTONUP. always_collide_mb = None result = read_event( controller1, event, pcRect=pcRect, mb_sids=('nab', 'nab', 'jump', 'nab', 'nab'), always_collide_mb=always_collide_mb, ) # print("PRESSED:{}".format(controller1.getTrues())) if grounded: # print("ground combo:{}".format(kcombo)) dx = 0 if controller1.getInt('x') > 0: dx += runvx * dt elif controller1.getInt('x') < 0: dx -= runvx * dt if nabtick: dx = 0 if not is_active(cdeffect): dx = 0 kcombo = "" if dx: facingright = dx > 0 currentfeat = "run" x += dx elif not nabtick: currentfeat = "" if kcombo == "leap" and "leap" in feat.known: if feat.attempt("leap"): vx = leapvx if facingright else -leapvx vy = leapvy combocount = 1 grounded = False currentfeat = "leap" noise.play("hop") elif kcombo == "twirl" and "twirl" in feat.known: if feat.attempt("twirl"): combocount = 1 grounded = False currentfeat = "twirl" vx = 0 vy = twirlvy twirltick = 1 noise.play("rotor") elif kcombo == "nab" and "nab" in feat.known: if feat.attempt("nab"): currentfeat = "nab" nabtick = nabtime noise.play("woosh") elif kcombo == ("roll-r" if facingright else "roll-l") and "roll" in feat.known: if feat.attempt("roll"): grounded = False currentfeat = "roll" vx = (rollvx if facingright else -rollvx) vy = rollvy combocount = 1 rolltick = 1 twirltick = 0 noise.play("rotor") elif kcombo == ("dart-r" if facingright else "dart-l") and "dart" in feat.known: if feat.attempt("dart"): grounded = False currentfeat = "dart" vx = (dartvx if facingright else -dartvx) vy = dartvy combocount = 1 rolltick = 0 twirltick = 0 noise.play("hop") elif kcombo == ("dart-l" if facingright else "dart-r") and "bound" in feat.known: if feat.attempt("bound"): grounded = False currentfeat = "bound" vx = (boundvx if facingright else -boundvx) vy = boundvy combocount = 1 rolltick = 0 twirltick = 0 noise.play("hop") else: # print("air combo:{}".format(kcombo)) if kcombo == "leap" and "leap" in feat.known: if feat.attempt("leap"): vx = leapvx if facingright else -leapvx vy = leapvy combocount += 1 currentfeat = "leap" twirltick = 0 noise.play("hop") elif kcombo == ("turn-r" if facingright else "turn-l") and "turn" in feat.known: if feat.attempt("turn"): facingright = not facingright vx = leapvx if facingright else -leapvx vy = leapvy combocount += 1 twirltick = 0 rolltick = 0 currentfeat = "leap" noise.play("hop") elif kcombo == "nab" and "nab" in feat.known: if feat.attempt("nab"): currentfeat = "nab" nabtick = nabtime vx = nabvx if facingright else -nabvx combocount += 1 twirltick = 0 rolltick = 0 noise.play("woosh") elif kcombo == "twirl" and "twirl" in feat.known: if feat.attempt("twirl"): currentfeat = "twirl" vx = 0 vy = twirlvy combocount += 1 twirltick = 1 rolltick = 0 noise.play("rotor") elif kcombo == ("roll-r" if facingright else "roll-l") and "roll" in feat.known: if feat.attempt("roll"): currentfeat = "roll" vx = (rollvx if facingright else -rollvx) vy = rollvy combocount += 1 rolltick = 1 twirltick = 0 noise.play("rotor") elif kcombo == ("dart-r" if facingright else "dart-l") and "dart" in feat.known: if feat.attempt("dart"): currentfeat = "dart" vx = (dartvx if facingright else -dartvx) vy = dartvy combocount += 1 rolltick = 0 twirltick = 0 noise.play("hop") elif kcombo == ("dart-l" if facingright else "dart-r") and "bound" in feat.known: if feat.attempt("bound"): currentfeat = "bound" vx = (boundvx if facingright else -boundvx) vy = boundvy combocount += 1 rolltick = 0 twirltick = 0 noise.play("hop") if nabtick: x += vx * dt else: x += vx * dt y += vy * dt - 0.5 * g * dt**2 vy -= g * dt if y < 0: y = 0 vy = 0 grounded = True combocount = 0 feat.land() ach = record.getrecords() if ach: effects.append(effect.AchievementEffect(ach)) currentfeat = "" twirltick = 0 rolltick = 0 x, y = vista.constrain(x, y, 30) nx, ny, nr = None, None, None if nabtick: nabtick = max(nabtick - dt, 0) if not nabtick: currentfeat = "" if grounded else "leap" nx, ny = x + (40 if facingright else -40), y + 80 nr = nabradius elif currentfeat == "twirl": nx, ny = x, y + 80 nr = twirlradius elif currentfeat == "roll": nx, ny = x, y + 50 nr = rollradius if nx is not None: for b in list(butterflies): adx, ady = b.x - nx, b.y - ny if adx**2 + ady**2 < nr**2: b.nabbed = True butterflies.remove(b) value = 3 * b.value if settings.easy else b.value effects.append(effect.NabBonusIndicator(value, (b.x, b.y))) if grounded: ach = record.checknabgrounded(b) if ach: effects.append(effect.AchievementEffect(ach)) else: record.checknab(b) heffect.update(y / 25.) heffect.position(vista.screen) ceffect.update(combocount) ceffect.position(vista.screen) peffect.update(record.catchamount) cdeffect.position(vista.screen) hbonus = record.checkheightrecord(y / 25.) if hbonus: effects.append(effect.HeightBonusIndicator(hbonus)) cbonus = record.checkcomborecord(combocount) if cbonus: effects.append(effect.ComboBonusIndicator(cbonus)) if currentfeat == "leap": picname = "run2" elif currentfeat == "run": picname = ("run0", "run1", "run2", "run1")[int(4 * runtick / 0.5)] runtick += dt runtick %= 0.5 elif currentfeat == "nab": picname = ("nab3", "nab2", "nab1", "nab0")[int(4. * nabtick / nabtime)] if not grounded: picname = "sky" + picname elif currentfeat == "twirl": twirltick += dt picname = ("twirl0", "twirl1", "twirl2", "twirl3")[int(4 * twirltick / 0.25) % 4] elif currentfeat == "roll": rolltick += dt picname = "roll%s" % (int(8 * rolltick / 0.35) % 8) elif currentfeat == "dart": picname = "dart" elif currentfeat == "bound": picname = "bound" else: picname = "stand" if "twirl" not in picname and not facingright: picname = picname + "-b" vista.position((x, y), facingright, vy) butterflies += loadlevel.newbutterflies(level, dt) feat.think(dt) vista.think(dt) for b in butterflies: b.think(dt) # seffect.set_verbose(True) seffect.think(dt) # if not seffect: # never occurs :( # if not bool(seffect): # never occurs :( if not is_active(seffect): # ^ __bool__() must be called manually-- # The reason seffect is always True even when is the # self.texts evaluates to False in the method is unknown. # Redefining __bool__ in the subclass doesn't help. See # <https://github.com/poikilos/lepidopterist/issues/12>. titleEffect.think(dt) for fx in effects: fx.think(dt) effects = [fx for fx in effects if is_active(fx)] ceffect.think(dt) if not is_active(titleEffect): cdeffect.think(dt) # titleEffect.set_verbose(True) # if prevTitleStr != titleEffect.debug(): # print("titleEffect: '{}'".format(titleEffect.debug())) # prevTitleStr = titleEffect.debug() if grounded and not is_active(cdeffect) and not effects and not ending: ending = True if record.catchamount >= goal: w = ["Stage complete!"] unlocked = record.unlocked w += record.checkhiscore(level) if record.unlocked > unlocked: level = record.unlocked else: w = ["Stage incomplete"] with open('contestdata.txt', 'a') as myfile: now = datetime.now() myfile.write(str(now) + '\n') myfile.write('level=' + str(level) + '\n') myfile.write('record.catchamount=' + str(record.catchamount) + '\n') myfile.write('record.collected=' + str(record.collected) + '\n') w += feat.checknewfeat(len(record.collected)) endtitle = effect.EndEffect(w) if ending and is_active(endtitle): endtitle.think(dt) vista.clear() sprite.frames[picname].draw((x, y)) # ^ draw uses vista so x,y is modifies by camera panning # Player Character: if prevPCKey is None: prevPCKey = "stand" ''' pcPos = vista.get_screen_pos((x, y)) pcRect = sprite.frames[prevPCKey].image.get_rect() pcRect.width = pcRect.width * 1.5 pcRect.center = pcPos pcRect.height = pcRect.height * .4 pcRect.top -= pcRect.height / 2 ''' pcInternalRect = sprite.frames[prevPCKey].image.get_rect() pcRect = vista.get_frame_screen_rect( sprite.frames[prevPCKey], (x, y), width=pcInternalRect.width * 1.5, height=pcInternalRect.height / 2, ) pcRect.height *= 6 # ^ allow clicking below the character for nab prevPCKey = picname for b in butterflies: b.draw() if nr is not None and settings.showdots: vista.circle((int(nx), int(ny)), int(nr)) feat.draw(facingright=facingright) heffect.draw(vista.screen) ceffect.draw(vista.screen) cdeffect.draw(vista.screen) peffect.draw(vista.screen) for fx in effects: fx.draw(vista.screen) seffect.draw(vista.screen) if not is_active(seffect): titleEffect.draw(vista.screen) if ending and is_active(endtitle): endtitle.draw(vista.screen) if is_active(touchEffect): if pcRect is not None: if settings.visualDebug: pygame.draw.rect(vista.screen, idleColor, pcRect) pygame.draw.circle(vista.screen, whiteColor, (int(pcPos[0]), int(pcPos[1])), 10) touchEffect.draw(vista.screen) pygame.draw.circle(vista.screen, touchColor, (touchEffect.x0, touchEffect.y0), 10) pygame.display.flip() if not is_active(endtitle): return
def action(): global level vista.levelinit(level) butterflies, goal, timeout = loadlevel.load(level) clock = pygame.time.Clock() facingright = True x, y, g, vy = 200, 0, (250 if settings.easy else 500), 0 leapvx, leapvy = 200., 200. twirlvy = 200. nabvx = 400. runvx = 300. rollvx, rollvy = 250., 250. dartvx, dartvy = 250., 300. boundvx, boundvy = -100., 250. nabtick, nabtime = 0, 0.25 nabradius = 50 twirlradius = 80 rollradius = 80 leaping = 0 runtick = 0 twirltick = 0 rolltick = 0 currentfeat = "" combocount = 0 grounded = True paused = False title = effect.Effect(["READY", "SET", "COLLECT"]) endtitle = True ending = False effects = [] heffect = effect.HeightIndicator() ceffect = effect.ComboIndicator() peffect = effect.ProgressIndicator(goal) cdeffect = effect.CountdownIndicator(timeout) seffect = effect.StageNameEffect(level, goal, timeout) feat.startlevel() pygame.event.get() while True: dt = clock.tick(60) * 0.001 if settings.printfps and random.random() < dt: print clock.get_fps() if paused: for event in pygame.event.get(): if event.type == QUIT: exit() elif event.type == KEYDOWN and event.key == K_ESCAPE: ending = True endtitle = None feat.checknewfeat(len(record.collected)) if record.catchamount >= goal: unlocked = record.unlocked record.checkhiscore(level) if record.unlocked > unlocked: level = record.unlocked paused = False elif event.type == KEYDOWN and event.key == K_F12: pygame.image.save(vista.screen, "screenshot.png") elif event.type == KEYDOWN and event.key == K_RETURN: paused = False vista.screen.blit(pausescreen, (0,0)) pygame.display.flip() continue for event in pygame.event.get(): if event.type == QUIT: exit() elif event.type == KEYDOWN and event.key == K_ESCAPE: paused = True pausescreen = pygame.Surface(vista.screen.get_size()).convert_alpha() fade = pygame.Surface(vista.screen.get_size()).convert_alpha() fade.fill((0,0,0,128)) pausescreen.blit(vista.screen,(0,0)) pausescreen.blit(fade,(0,0)) pausetitle = effect.PauseTitle(["PAUSED"]) pauseinfo = effect.PauseInfo(["Press Enter to resume|or Esc to exit level"]) pausetitle.position(pausescreen) pauseinfo.position(pausescreen) pausetitle.draw(pausescreen) pauseinfo.draw(pausescreen) elif event.type == KEYDOWN and event.key == K_F12: pygame.image.save(vista.screen, "screenshot.png") elif event.type == KEYDOWN and event.key == K_TAB: settings.hidefeatnames = not settings.hidefeatnames feat.startlevel(False) k = pygame.key.get_pressed() kcombo = combo.check(k) if grounded: dx = 0 if k[K_RIGHT]: dx += runvx * dt if k[K_LEFT]: dx -= runvx * dt if nabtick: dx = 0 if not cdeffect: dx = 0 kcombo = "" if dx: facingright = dx > 0 currentfeat = "run" x += dx elif not nabtick: currentfeat = "" if kcombo == "leap" and "leap" in feat.known: if feat.attempt("leap"): vx = leapvx if facingright else -leapvx vy = leapvy combocount = 1 grounded = False currentfeat = "leap" noise.play("hop") elif kcombo == "twirl" and "twirl" in feat.known: if feat.attempt("twirl"): combocount = 1 grounded = False currentfeat = "twirl" vx = 0 vy = twirlvy twirltick = 1 noise.play("rotor") elif kcombo == "nab" and "nab" in feat.known: if feat.attempt("nab"): currentfeat = "nab" nabtick = nabtime noise.play("woosh") elif kcombo == ("roll-r" if facingright else "roll-l") and "roll" in feat.known: if feat.attempt("roll"): grounded = False currentfeat = "roll" vx = (rollvx if facingright else -rollvx) vy = rollvy combocount = 1 rolltick = 1 twirltick = 0 noise.play("rotor") elif kcombo == ("dart-r" if facingright else "dart-l") and "dart" in feat.known: if feat.attempt("dart"): grounded = False currentfeat = "dart" vx = (dartvx if facingright else -dartvx) vy = dartvy combocount = 1 rolltick = 0 twirltick = 0 noise.play("hop") elif kcombo == ("dart-l" if facingright else "dart-r") and "bound" in feat.known: if feat.attempt("bound"): grounded = False currentfeat = "bound" vx = (boundvx if facingright else -boundvx) vy = boundvy combocount = 1 rolltick = 0 twirltick = 0 noise.play("hop") else: if kcombo == "leap" and "leap" in feat.known: if feat.attempt("leap"): vx = leapvx if facingright else -leapvx vy = leapvy combocount += 1 currentfeat = "leap" twirltick = 0 noise.play("hop") elif kcombo == ("turn-r" if facingright else "turn-l") and "turn" in feat.known: if feat.attempt("turn"): facingright = not facingright vx = leapvx if facingright else -leapvx vy = leapvy combocount += 1 twirltick = 0 rolltick = 0 currentfeat = "leap" noise.play("hop") elif kcombo == "nab" and "nab" in feat.known: if feat.attempt("nab"): currentfeat = "nab" nabtick = nabtime vx = nabvx if facingright else -nabvx combocount += 1 twirltick = 0 rolltick = 0 noise.play("woosh") elif kcombo == "twirl" and "twirl" in feat.known: if feat.attempt("twirl"): currentfeat = "twirl" vx = 0 vy = twirlvy combocount += 1 twirltick = 1 rolltick = 0 noise.play("rotor") elif kcombo == ("roll-r" if facingright else "roll-l") and "roll" in feat.known: if feat.attempt("roll"): currentfeat = "roll" vx = (rollvx if facingright else -rollvx) vy = rollvy combocount += 1 rolltick = 1 twirltick = 0 noise.play("rotor") elif kcombo == ("dart-r" if facingright else "dart-l") and "dart" in feat.known: if feat.attempt("dart"): currentfeat = "dart" vx = (dartvx if facingright else -dartvx) vy = dartvy combocount += 1 rolltick = 0 twirltick = 0 noise.play("hop") elif kcombo == ("dart-l" if facingright else "dart-r") and "bound" in feat.known: if feat.attempt("bound"): currentfeat = "bound" vx = (boundvx if facingright else -boundvx) vy = boundvy combocount += 1 rolltick = 0 twirltick = 0 noise.play("hop") if nabtick: x += vx * dt else: x += vx * dt y += vy * dt - 0.5 * g * dt ** 2 vy -= g * dt if y < 0: y = 0 vy = 0 grounded = True combocount = 0 feat.land() ach = record.getrecords() if ach: effects.append(effect.AchievementEffect(ach)) currentfeat = "" twirltick = 0 rolltick = 0 x, y = vista.constrain(x, y, 30) nx, ny, nr = None, None, None if nabtick: nabtick = max(nabtick - dt, 0) if not nabtick: currentfeat = "" if grounded else "leap" nx, ny = x + (40 if facingright else -40), y + 80 nr = nabradius elif currentfeat == "twirl": nx, ny = x, y + 80 nr = twirlradius elif currentfeat == "roll": nx, ny = x, y + 50 nr = rollradius if nx is not None: for b in list(butterflies): adx, ady = b.x - nx, b.y - ny if adx ** 2 + ady ** 2 < nr ** 2: b.nabbed = True butterflies.remove(b) value = 3 * b.value if settings.easy else b.value effects.append(effect.NabBonusIndicator(value, (b.x, b.y))) if grounded: ach = record.checknabgrounded(b) if ach: effects.append(effect.AchievementEffect(ach)) else: record.checknab(b) heffect.update(y/25.) heffect.position(vista.screen) ceffect.update(combocount) ceffect.position(vista.screen) peffect.update(record.catchamount) cdeffect.position(vista.screen) hbonus = record.checkheightrecord(y/25.) if hbonus: effects.append(effect.HeightBonusIndicator(hbonus)) cbonus = record.checkcomborecord(combocount) if cbonus: effects.append(effect.ComboBonusIndicator(cbonus)) if currentfeat == "leap": picname = "run2" elif currentfeat == "run": picname = ("run0", "run1", "run2", "run1")[int(4 * runtick / 0.5)] runtick += dt runtick %= 0.5 elif currentfeat == "nab": picname = ("nab3", "nab2", "nab1", "nab0")[int(4. * nabtick / nabtime)] if not grounded: picname = "sky" + picname elif currentfeat == "twirl": twirltick += dt picname = ("twirl0", "twirl1", "twirl2", "twirl3")[int(4 * twirltick / 0.25) % 4] elif currentfeat == "roll": rolltick += dt picname = "roll%s" % (int(8 * rolltick / 0.35) % 8) elif currentfeat == "dart": picname = "dart" elif currentfeat == "bound": picname = "bound" else: picname = "stand" if "twirl" not in picname and not facingright: picname = picname + "-b" vista.position((x, y), facingright, vy) butterflies += loadlevel.newbutterflies(level, dt) feat.think(dt) vista.think(dt) for b in butterflies: b.think(dt) seffect.think(dt) if not seffect: title.think(dt) for e in effects: e.think(dt) effects = [e for e in effects if e] ceffect.think(dt) if not title: cdeffect.think(dt) if grounded and not cdeffect and not effects and not ending: ending = True if record.catchamount >= goal: w = ["Stage complete!"] unlocked = record.unlocked w += record.checkhiscore(level) if record.unlocked > unlocked: level = record.unlocked else: w = ["Stage incomplete"] w += feat.checknewfeat(len(record.collected)) endtitle = effect.EndEffect(w) if ending and endtitle: endtitle.think(dt) vista.clear() sprite.frames[picname].draw((x, y)) for b in butterflies: b.draw() if nr is not None and settings.showdots: vista.circle((int(nx), int(ny)), int(nr)) feat.draw(facingright=facingright) heffect.draw(vista.screen) ceffect.draw(vista.screen) cdeffect.draw(vista.screen) peffect.draw(vista.screen) for e in effects: e.draw(vista.screen) seffect.draw(vista.screen) if not seffect: title.draw(vista.screen) if ending and endtitle: endtitle.draw(vista.screen) pygame.display.flip() if not endtitle: return
def draw(self): vista.clear()
def action(): global level vista.levelinit(level) butterflies, goal, timeout = loadlevel.load(level) clock = pygame.time.Clock() facingright = True x, y, g, vy = 200, 0, (250 if settings.easy else 500), 0 leapvx, leapvy = 200., 200. twirlvy = 200. nabvx = 400. runvx = 300. rollvx, rollvy = 250., 250. dartvx, dartvy = 250., 300. boundvx, boundvy = -100., 250. nabtick, nabtime = 0, 0.25 nabradius = 50 twirlradius = 80 rollradius = 80 leaping = 0 runtick = 0 twirltick = 0 rolltick = 0 currentfeat = "" combocount = 0 grounded = True paused = False title = effect.Effect(["READY", "SET", "COLLECT"]) endtitle = True ending = False effects = [] heffect = effect.HeightIndicator() ceffect = effect.ComboIndicator() peffect = effect.ProgressIndicator(goal) cdeffect = effect.CountdownIndicator(timeout) seffect = effect.StageNameEffect(level, goal, timeout) feat.startlevel() pygame.event.get() while True: dt = clock.tick(60) * 0.001 if settings.printfps and random.random() < dt: print clock.get_fps() if paused: for event in pygame.event.get(): if event.type == QUIT: exit() elif event.type == KEYDOWN and event.key == K_ESCAPE: ending = True endtitle = None feat.checknewfeat(len(record.collected)) if record.catchamount >= goal: unlocked = record.unlocked record.checkhiscore(level) if record.unlocked > unlocked: level = record.unlocked paused = False elif event.type == KEYDOWN and event.key == K_F12: pygame.image.save(vista.screen, "screenshot.png") elif event.type == KEYDOWN and event.key == K_RETURN: paused = False vista.screen.blit(pausescreen, (0, 0)) pygame.display.flip() continue for event in pygame.event.get(): if event.type == QUIT: exit() elif event.type == KEYDOWN and event.key == K_ESCAPE: paused = True pausescreen = pygame.Surface( vista.screen.get_size()).convert_alpha() fade = pygame.Surface(vista.screen.get_size()).convert_alpha() fade.fill((0, 0, 0, 128)) pausescreen.blit(vista.screen, (0, 0)) pausescreen.blit(fade, (0, 0)) pausetitle = effect.PauseTitle(["PAUSED"]) pauseinfo = effect.PauseInfo( ["Press Enter to resume|or Esc to exit level"]) pausetitle.position(pausescreen) pauseinfo.position(pausescreen) pausetitle.draw(pausescreen) pauseinfo.draw(pausescreen) elif event.type == KEYDOWN and event.key == K_F12: pygame.image.save(vista.screen, "screenshot.png") elif event.type == KEYDOWN and event.key == K_TAB: settings.hidefeatnames = not settings.hidefeatnames feat.startlevel(False) k = pygame.key.get_pressed() kcombo = combo.check(k) if grounded: dx = 0 if k[K_RIGHT]: dx += runvx * dt if k[K_LEFT]: dx -= runvx * dt if nabtick: dx = 0 if not cdeffect: dx = 0 kcombo = "" if dx: facingright = dx > 0 currentfeat = "run" x += dx elif not nabtick: currentfeat = "" if kcombo == "leap" and "leap" in feat.known: if feat.attempt("leap"): vx = leapvx if facingright else -leapvx vy = leapvy combocount = 1 grounded = False currentfeat = "leap" noise.play("hop") elif kcombo == "twirl" and "twirl" in feat.known: if feat.attempt("twirl"): combocount = 1 grounded = False currentfeat = "twirl" vx = 0 vy = twirlvy twirltick = 1 noise.play("rotor") elif kcombo == "nab" and "nab" in feat.known: if feat.attempt("nab"): currentfeat = "nab" nabtick = nabtime noise.play("woosh") elif kcombo == ("roll-r" if facingright else "roll-l") and "roll" in feat.known: if feat.attempt("roll"): grounded = False currentfeat = "roll" vx = (rollvx if facingright else -rollvx) vy = rollvy combocount = 1 rolltick = 1 twirltick = 0 noise.play("rotor") elif kcombo == ("dart-r" if facingright else "dart-l") and "dart" in feat.known: if feat.attempt("dart"): grounded = False currentfeat = "dart" vx = (dartvx if facingright else -dartvx) vy = dartvy combocount = 1 rolltick = 0 twirltick = 0 noise.play("hop") elif kcombo == ("dart-l" if facingright else "dart-r") and "bound" in feat.known: if feat.attempt("bound"): grounded = False currentfeat = "bound" vx = (boundvx if facingright else -boundvx) vy = boundvy combocount = 1 rolltick = 0 twirltick = 0 noise.play("hop") else: if kcombo == "leap" and "leap" in feat.known: if feat.attempt("leap"): vx = leapvx if facingright else -leapvx vy = leapvy combocount += 1 currentfeat = "leap" twirltick = 0 noise.play("hop") elif kcombo == ("turn-r" if facingright else "turn-l") and "turn" in feat.known: if feat.attempt("turn"): facingright = not facingright vx = leapvx if facingright else -leapvx vy = leapvy combocount += 1 twirltick = 0 rolltick = 0 currentfeat = "leap" noise.play("hop") elif kcombo == "nab" and "nab" in feat.known: if feat.attempt("nab"): currentfeat = "nab" nabtick = nabtime vx = nabvx if facingright else -nabvx combocount += 1 twirltick = 0 rolltick = 0 noise.play("woosh") elif kcombo == "twirl" and "twirl" in feat.known: if feat.attempt("twirl"): currentfeat = "twirl" vx = 0 vy = twirlvy combocount += 1 twirltick = 1 rolltick = 0 noise.play("rotor") elif kcombo == ("roll-r" if facingright else "roll-l") and "roll" in feat.known: if feat.attempt("roll"): currentfeat = "roll" vx = (rollvx if facingright else -rollvx) vy = rollvy combocount += 1 rolltick = 1 twirltick = 0 noise.play("rotor") elif kcombo == ("dart-r" if facingright else "dart-l") and "dart" in feat.known: if feat.attempt("dart"): currentfeat = "dart" vx = (dartvx if facingright else -dartvx) vy = dartvy combocount += 1 rolltick = 0 twirltick = 0 noise.play("hop") elif kcombo == ("dart-l" if facingright else "dart-r") and "bound" in feat.known: if feat.attempt("bound"): currentfeat = "bound" vx = (boundvx if facingright else -boundvx) vy = boundvy combocount += 1 rolltick = 0 twirltick = 0 noise.play("hop") if nabtick: x += vx * dt else: x += vx * dt y += vy * dt - 0.5 * g * dt**2 vy -= g * dt if y < 0: y = 0 vy = 0 grounded = True combocount = 0 feat.land() ach = record.getrecords() if ach: effects.append(effect.AchievementEffect(ach)) currentfeat = "" twirltick = 0 rolltick = 0 x, y = vista.constrain(x, y, 30) nx, ny, nr = None, None, None if nabtick: nabtick = max(nabtick - dt, 0) if not nabtick: currentfeat = "" if grounded else "leap" nx, ny = x + (40 if facingright else -40), y + 80 nr = nabradius elif currentfeat == "twirl": nx, ny = x, y + 80 nr = twirlradius elif currentfeat == "roll": nx, ny = x, y + 50 nr = rollradius if nx is not None: for b in list(butterflies): adx, ady = b.x - nx, b.y - ny if adx**2 + ady**2 < nr**2: b.nabbed = True butterflies.remove(b) value = 3 * b.value if settings.easy else b.value effects.append(effect.NabBonusIndicator(value, (b.x, b.y))) if grounded: ach = record.checknabgrounded(b) if ach: effects.append(effect.AchievementEffect(ach)) else: record.checknab(b) heffect.update(y / 25.) heffect.position(vista.screen) ceffect.update(combocount) ceffect.position(vista.screen) peffect.update(record.catchamount) cdeffect.position(vista.screen) hbonus = record.checkheightrecord(y / 25.) if hbonus: effects.append(effect.HeightBonusIndicator(hbonus)) cbonus = record.checkcomborecord(combocount) if cbonus: effects.append(effect.ComboBonusIndicator(cbonus)) if currentfeat == "leap": picname = "run2" elif currentfeat == "run": picname = ("run0", "run1", "run2", "run1")[int(4 * runtick / 0.5)] runtick += dt runtick %= 0.5 elif currentfeat == "nab": picname = ("nab3", "nab2", "nab1", "nab0")[int(4. * nabtick / nabtime)] if not grounded: picname = "sky" + picname elif currentfeat == "twirl": twirltick += dt picname = ("twirl0", "twirl1", "twirl2", "twirl3")[int(4 * twirltick / 0.25) % 4] elif currentfeat == "roll": rolltick += dt picname = "roll%s" % (int(8 * rolltick / 0.35) % 8) elif currentfeat == "dart": picname = "dart" elif currentfeat == "bound": picname = "bound" else: picname = "stand" if "twirl" not in picname and not facingright: picname = picname + "-b" vista.position((x, y), facingright, vy) butterflies += loadlevel.newbutterflies(level, dt) feat.think(dt) vista.think(dt) for b in butterflies: b.think(dt) seffect.think(dt) if not seffect: title.think(dt) for e in effects: e.think(dt) effects = [e for e in effects if e] ceffect.think(dt) if not title: cdeffect.think(dt) if grounded and not cdeffect and not effects and not ending: ending = True if record.catchamount >= goal: w = ["Stage complete!"] unlocked = record.unlocked w += record.checkhiscore(level) if record.unlocked > unlocked: level = record.unlocked else: w = ["Stage incomplete"] w += feat.checknewfeat(len(record.collected)) endtitle = effect.EndEffect(w) if ending and endtitle: endtitle.think(dt) vista.clear() sprite.frames[picname].draw((x, y)) for b in butterflies: b.draw() if nr is not None and settings.showdots: vista.circle((int(nx), int(ny)), int(nr)) feat.draw(facingright=facingright) heffect.draw(vista.screen) ceffect.draw(vista.screen) cdeffect.draw(vista.screen) peffect.draw(vista.screen) for e in effects: e.draw(vista.screen) seffect.draw(vista.screen) if not seffect: title.draw(vista.screen) if ending and endtitle: endtitle.draw(vista.screen) pygame.display.flip() if not endtitle: return