def dissolve_transition(old_screen, new_screen, time, rect=None): # initialize values alpha = 0 # calculate how much we will increase alpha incAlpha = 255/float(time) while alpha < 255: lag = clock.tick(MAX_FPS) if handleEvents() == "quit": exit() if Input.up(K_ESCAPE): new_screen.set_alpha(None) screen.blit(new_screen, (0,0)) updateScreen() return # blit the old and new screens screen.blit(old_screen, (0,0)) new_screen.set_alpha(int(alpha)) screen.blit(new_screen, (0,0)) updateScreen() # increase alpha alpha += incAlpha * lag # blit the new screen with full opacity new_screen.set_alpha(None) screen.blit(new_screen, (0,0)) updateScreen()
def credits_screen(): goscreen = core.load_image("credits.png", directory="gfx", auto_alpha=False) core.dissolve_transition( core.screen.copy().convert(), goscreen.copy().convert(), 1000 ) while True: core.clock.tick(core.MAX_FPS) events = pygame.event.get() if core.handleEvents(events) == "quit": exit() for event in events: if event.type == KEYUP: core.play_sound("button_click") Input.update() core.next_screen = main_menu return core.screen.blit(goscreen, (0,0)) core.updateScreen()
def game_over(score, rank): core.play_music("game over", queue=True) # load game over screen goscreen = core.load_image("game_over.png", directory="gfx", auto_alpha=False) core.dissolve_transition( core.screen.copy().convert(), goscreen.copy().convert(), 1000 ) while True: core.clock.tick(core.MAX_FPS) events = pygame.event.get() if core.handleEvents(events) == "quit": exit() for event in events: if event.type == KEYUP: core.next_screen = core.curry(hiscores_screen, score, rank) Input.update() return core.screen.blit(goscreen, (0,0)) core.updateScreen()
def level_clear_screen(mc, killed_bugs, ltime, lives, lost_lives, score): core.play_music("stage clear", queue=True) last_screen = core.screen.copy().convert() ls = last_screen.copy().convert() bg = core.load_image("stage_clear.png", directory="gfx") stars = core.load_image("rank_star.png", directory="gfx") stars = [ core.im_crop(stars, pygame.Rect(0,0,55,51)), core.im_crop(stars, pygame.Rect(55,0,55,51)) ] ltime = int(ltime) used_feathers, wasted_feathers, waste_penalty = getFeatherStats(mc) rank = getRank(mc, lost_lives, lives, ltime, killed_bugs) state = 0 skilled_bugs = oStatsDisp(0, (465,155), useNumbers=True, xalign=1.0, width=100, formatValue=format_level_time) stime = oStatsDisp(0, (465,190), useNumbers=True, xalign=1.0, width=100, formatValue=format_level_time) slives = oStatsDisp(0, (465,225), useNumbers=True, xalign=1.0, width=100, formatValue=format_level_time) sfeathers = oStatsDisp(0, (465,260), useNumbers=True, xalign=1.0, width=100, formatValue=format_level_time) stotal = oStatsDisp(score, (235,370), useNumbers=True, xalign=1.0, width=330, formatValue=format_score) last_total = score forceUpdate = True vel = .3 vel2 = 100 while True: lag = core.clock.tick(core.MAX_FPS) if lag <= 0: lag = 1 dirty_rects = [] if core.handleEvents() == "quit": exit() vel = 0.2 if Input.up(K_RETURN): if state < 8: vel = sys.maxint else: core.dissolve_transition(core.screen.copy().convert(), ls, 1000) return stotal.value, rank core.screen.blit(ls, (0,0)) core.screen.blit(bg, (0,0)) # points if state == 0: skilled_bugs.update( skilled_bugs.value + vel*lag ) if skilled_bugs.value >= killed_bugs: skilled_bugs.value = killed_bugs state += 1 elif state == 1: stime.update( stime.value + vel*lag ) if stime.value >= ltime: stime.value = ltime state += 1 elif state == 2: slives.update( slives.value + vel*lag ) if slives.value >= lives: slives.value = lives state += 1 elif state == 3: sfeathers.update( sfeathers.value + vel*lag ) if sfeathers.value >= wasted_feathers: sfeathers.value = wasted_feathers state += 1 elif state == 4: stotal.update( stotal.value + vel2*lag ) if stotal.value >= last_total + killed_bugs*10: stotal.value = last_total + killed_bugs*10 last_total = stotal.value state += 1 elif state == 5: stotal.update( stotal.value + vel2*lag ) if stotal.value >= last_total + ltime*100: stotal.value = last_total + ltime*100 last_total = stotal.value state += 1 elif state == 6: stotal.update( stotal.value + vel2*lag ) if stotal.value >= last_total + lives*1000: stotal.value = last_total + lives*1000 last_total = stotal.value state += 1 elif state == 7: stotal.update( stotal.value - vel2*lag ) if stotal.value <= last_total - waste_penalty: stotal.value = last_total - waste_penalty last_total = stotal.value state += 1 # draw dirty_rects.extend( skilled_bugs.draw(core.screen) ) if state > 0: dirty_rects.extend( stime.draw(core.screen) ) if state > 1: dirty_rects.extend( slives.draw(core.screen) ) if state > 2: dirty_rects.extend( sfeathers.draw(core.screen) ) if state > 3: dirty_rects.extend( stotal.draw(core.screen) ) if state > 7: def draw_rank_star(pos, rank_need): if rank >= rank_need: return core.screen.blit(stars[1], pos) else: return core.screen.blit(stars[0], pos) dirty_rects.append( draw_rank_star((405,430), 3) ) dirty_rects.append( draw_rank_star((460,430), 2) ) dirty_rects.append( draw_rank_star((515,430), 1) ) if last_screen is not None: core.dissolve_transition(last_screen, core.screen.copy().convert(), 1000) last_screen = None if forceUpdate: dirty_rects = None forceUpdate = False core.updateScreen(dirty_rects)
def hiscores_screen(score=None, rank=0): last_screen = core.screen.copy().convert() scores = [] import xdrlib if not os.path.exists("high_scores"): # fill with default scores default_scores = [(100000,2),(50000,2),(30000,2),(20000,2),(10000,2),(8000,1), (4000,1),(500,0)] for i in range(8): s, r = default_scores[i] scores.append( ("Nozomi", s, r) ) else: # load from file data = open("high_scores", "rb").read() loader = xdrlib.Unpacker(data) for i in range(8): scores.append( (loader.unpack_string(), loader.unpack_int(), loader.unpack_int()) ) # verify if we have a high score =D if score is not None: for index, _score in enumerate(scores): name, _score, _rank = _score if (score > _score) or (score == _score and rank > _rank): player_name = core.InputPrompt("Nice timing! You've scored high, Mr/Mrs...", "Player") scores.insert(index, (player_name, score, rank)) break # save scores p = xdrlib.Packer() for name, score, rank in scores: p.pack_string(name) p.pack_int(score) p.pack_int(rank) f = open("high_scores", "wb") f.write(p.get_buffer()) f.close() # load graphics =D bg = core.load_image("hiscores.png", directory="gfx") stars = pygame.transform.smoothscale(core.load_image("rank_star.png", directory="gfx"), (86,40)) stars = [ core.im_crop(stars, pygame.Rect(0,0,43,40)), core.im_crop(stars, pygame.Rect(43,0,43,40)) ] core.screen.blit(bg, (0,0)) # render high scores! for index, score in enumerate(scores): name, score, rank = score # 260, 100 y = 100 + 40*index # name core.fonts.Draw(core.screen, None, 36, name, pygame.Rect(260,y,200,40), (255,255,255,255),alignVert='center',antialias=True) # points score = str(score) w = 160-core.numbers.getWidth(score) core.numbers.draw(core.screen, (470+w,y+8), score) # rank if rank > 2: core.screen.blit(stars[1], (660,y)) else: core.screen.blit(stars[0], (660,y)) if rank > 1: core.screen.blit(stars[1], (703,y)) else: core.screen.blit(stars[0], (703,y)) if rank > 0: core.screen.blit(stars[1], (746,y)) else: core.screen.blit(stars[0], (746,y)) core.dissolve_transition(last_screen, core.screen.copy().convert(), 1000) while True: events = pygame.event.get() if core.handleEvents(events) == "quit": exit() for event in events: if event.type == KEYUP: core.next_screen = main_menu core.play_sound("button_click") Input.update() return
def main_menu(): core.play_music("main menu") # load bg bg = core.load_image("main_menu.png", directory="gfx", auto_alpha=False) selected = 0 def genButton(images, pos): button = sprite.oSprite() button.x = pos[0] button.y = pos[1] button.animations = { "active": { "N": [0,True,[sprite.oFrame(images[0])]] }, "inactive": { "N": [0,True,[sprite.oFrame(images[1])]] } } core.render_group.add(button) return button selected = 0 buttons = [] core.render_group.clear() for i, button in enumerate(["new game","help","credits","exit"]): y = 180 + i*40 x = 75 buttons.append( genButton(core.game_images[button], (x,y)) ) menu_screens = [ game_intro, # new game hiscores_screen,# high scores credits_screen, # credits exit_screen, # exit ] forceUpdate = True last_screen = core.screen.copy().convert() while True: lag = core.clock.tick(core.MAX_FPS) if core.handleEvents() == "quit": exit() if Input.up(K_UP): core.play_sound("cursor_move") selected -= 1 if selected < 0: selected = len(buttons)-1 if Input.up(K_DOWN): core.play_sound("cursor_move") selected += 1 if selected >= len(buttons): selected = 0 if Input.up(K_RETURN): core.next_screen = menu_screens[selected] core.play_sound("button_click") return for i, button in enumerate(buttons): button.changeState("active" if i == selected else "inactive") dirty_rects = core.render_group.update(lag) core.screen.blit(bg, (0,0)) core.render_group.draw(core.screen) if last_screen is not None: core.dissolve_transition(last_screen, core.screen.copy().convert(), 1000) last_screen = None if forceUpdate: dirty_rects = None core.updateScreen(dirty_rects)
def yesno_prompt(text): last_screen = core.screen.copy().convert() ls = last_screen.copy().convert() bg = core.load_image("yesno_prompt.png", directory="gfx") selected = 0 button_group = sprite.oSpriteGroup() def genButton(images, pos): button = sprite.oSprite() button.x = pos[0] button.y = pos[1] button.animations = { "active": { "N": [0,True,[sprite.oFrame(images[0])]] }, "inactive": { "N": [0,True,[sprite.oFrame(images[1])]] } } button_group.add(button) return button selected = 0 buttons = [] buttons.append( genButton(core.game_images["no"], (200,300)) ) buttons.append( genButton(core.game_images["yes"], (400,300)) ) forceUpdate = True while True: lag = core.clock.tick(core.MAX_FPS) if core.handleEvents() == "quit": exit() if Input.up(K_LEFT): core.play_sound("cursor_move") selected -= 1 if selected < 0: selected = len(buttons)-1 if Input.up(K_RIGHT): core.play_sound("cursor_move") selected += 1 if selected >= len(buttons): selected = 0 if Input.up(K_RETURN): core.play_sound("button_click") core.dissolve_transition(core.screen.copy().convert(), ls, 1000) return bool(selected) for i, button in enumerate(buttons): button.changeState("active" if i == selected else "inactive") dirty_rects = button_group.update(lag) core.screen.blit(ls, (0,0)) core.screen.blit(bg, (0,0)) button_group.draw(core.screen) dirty_rects.append(core.fonts.Draw(core.screen, None, 36, text, pygame.Rect(50,250,700,50), (255,255,255,255), alignHoriz='center', antialias=True)) if last_screen is not None: core.dissolve_transition(last_screen, core.screen.copy().convert(), 1000) last_screen = None core.updateScreen(dirty_rects)
def handlePlayerControls(char, lag): if char.animState == "dying": return d = "" ly = char.y lx = char.x if Input.motion(CONTROLS["up"]): char.y -= char.speed*char.speedMul * lag d += "N" if Input.motion(CONTROLS["down"]): char.y += char.speed*char.speedMul * lag d += "S" if Input.motion(CONTROLS["left"]): char.x -= char.speed*char.speedMul * lag d += "W" if Input.motion(CONTROLS["right"]): char.x += char.speed*char.speedMul * lag d += "E" if d == "": char.changeState("stopped") else: char.changeState("walking") char.changeDirection(d) if not char.canWalk(): char.x = lx char.y = ly if Input.motion(CONTROLS["attack"]): char.attacks[char.use_attack].attack(char) if Input.up(CONTROLS["prev_feather"]): char.use_attack -= 1 if char.use_attack < 0: char.use_attack = len(char.attacks)-1 if Input.up(CONTROLS["next_feather"]): char.use_attack += 1 if char.use_attack >= len(char.attacks): char.use_attack = 0 # if Input.motion(CONTROLS["run"]): # char.speedMul = 2 # else: # char.speedMul = 1 if Input.up(CONTROLS["action"]): w, h = level.tileSize x = int( math.floor( char.x / w ) ) y = int( math.floor( (char.y-33) / h ) )# fix tiles have a blank area on top o.o tile = level.getTile(x, y) if tile.portal and tile.active: play_sound("portal_activate") level.portalActive = True if tile.op > -1: tile.active = not tile.active if tile.active: play_sound("block_activate") else: play_sound("block_deactivate") tile.changed = True mathExp.activateTerm(x, y) level.activateTiles(mathExp.getResult()) level.genBitmask()
def handleEvents(events=None, escape_exits=True): global next_screen, forceUpdate, quitPrompt # update last events Input.update() # Update our mouse position and 3 button state Input.update_mouse(pygame.mouse.get_pressed(), pygame.mouse.get_pos()) # Add all events from the event queue to the input module # for this loop cycle. if events is None: events = pygame.event.get() Input.add_events(events) if (Input.down(K_ESCAPE) and escape_exits) or Input.isset("QUIT"): if not quitPrompt: quitPrompt = True if game.yesno_prompt("Exit RED -spider lily-?"): next_screen = None return "quit" quitPrompt = False forceUpdate = True Input.update() # if Input.up(K_F9): # level = int(getInput("Goto level #:", pygame.Rect(0, 550, 800, 50), default="0")) # forceUpdate = True # game.load_level(level, game.mc) if Input.up(K_F8): mapeditor.map_editor() forceUpdate = True
def show_texts(self, texts, char=0): rv = True self.char = char x, y = self.x, self.y if self.background is not None: last_screen = core.screen.copy().convert() ls = last_screen.copy().convert() core.screen.blit( self.background, self.bg_pos ) core.dissolve_transition( last_screen, core.screen.copy().convert(), self.intime ) last_screen = core.screen.copy().convert() try: for text in texts: text = "\n".join( self.wrap_multi_line(text, self.font, self.width) ) + ' ' self.dismissed = False start = 0 while not self.dismissed: lag = core.clock.tick(core.MAX_FPS) if lag <= 0: lag = 1 if core.handleEvents(escape_exits = not K_ESCAPE in self.skip_keys) == "quit": exit() core.screen.blit(last_screen, (0,0)) for skip_key in self.skip_keys: if Input.up(skip_key): rv = False raise eSkipText if start < len(text): for dismiss in self.dismiss_keys: if Input.up(dismiss): self.char = len(text) self.char += self.speed * lag chars = text[start:int(self.char)] start += len(chars) update_rects = [] for char in chars: if char in ["\r","\n"]: y += self.font.size("W")[1] + self.line_spacing x = self.x else: text_surf = self.font.render( char, True, self.color ) update_rects.append( last_screen.blit(text_surf, (x,y)) ) x += text_surf.get_width() core.updateScreen() else: if self.ctc is not None: self.ctc.x = x self.ctc.y = y ctc_rect = self.ctc.update(lag) self.ctc.draw(core.screen) ctc_rect.extend(update_rects) core.updateScreen(ctc_rect) for dismiss in self.dismiss_keys: if Input.up(dismiss): self.dismissed = True self.char -= len(text) x = self.x y += self.font.size("W")[1] + self.line_spacing except eSkipText: pass core.dissolve_transition( core.screen.copy().convert(), ls, self.intime ) return rv
def map_editor(): mapa, filename = new_map() tiles = [ map.oTile(0, 25, True), map.oTile(1, 25, True), map.oTile(2, 25, True), map.oTile(3, 25, True), map.oTile(4, 25, False), map.oTile(5, 25, False), map.oTile(6, 25, True) ] selector = core.load_image("selector.png", directory="gfx") current_tile = 0 number = 0 op = 0 task = 0 ox = 0 oy = 0 tw, th = mapa.tileSize while True: lag = core.clock.tick(core.MAX_FPS) mx, my = pygame.mouse.get_pos() ox = int(math.floor( (mx-mapa.x) / tw )) oy = int(math.floor( (my-mapa.y) / th )) if ox >= mapa.width: ox = mapa.width-1 if oy >= mapa.height: oy = mapa.height-1 if ox < 0: ox = 0 if oy < 0: oy = 0 dx, dy = ox, oy events = pygame.event.get() for event in events: if event.type == QUIT: exit() if event.type == KEYDOWN: if event.key == K_ESCAPE: return if event.key == K_LEFT: if task == 0: # tile current_tile -= 1 if current_tile < 0: current_tile = len(tiles)-1 elif task == 1: # number number -= 1 if number < 0: number = 9 elif task == 2: # op op -= 1 if op < 0: op = 4 if event.key == K_RIGHT: if task == 0: current_tile += 1 if current_tile >= len(tiles): current_tile = 0 if task == 1: number += 1 if number > 9: number = 0 if task == 2: op += 1 if op > 4: op = 0 if event.key == K_UP: task -= 1 if task < 0: task = 3 if event.key == K_DOWN: task += 1 if task > 3: task = 0 if event.key == K_F1: mapa, filename = new_map() if event.key == K_F2: mapa.save(filename) if event.key == K_F3: mapa, filename = load_map() if event.key == K_F4: mapa = edit_map(mapa) core.handleEvents(events) if Input.down("MB1"): p = oy*mapa.width + ox if task == 0: if mapa.mapData[p] is None: mapa.mapData[p] = map.oTile(0,0,False) mapa.mapData[p].imageID = tiles[current_tile].imageID mapa.mapData[p].height = tiles[current_tile].height mapa.mapData[p].walkable = tiles[current_tile].walkable elif task == 1 and mapa.mapData[p] is not None: mapa.mapData[p].number = number elif task == 2 and mapa.mapData[p] is not None: mapa.mapData[p].op = op elif task == 3 and mapa.mapData[p] is not None: mapa.mapData[p].portal = True if Input.down("MB3"): p = oy*mapa.width + ox if task == 0: mapa.mapData[p] = None elif task == 1 and mapa.mapData[p] is not None: mapa.mapData[p].number = -1 elif task == 2 and mapa.mapData[p] is not None: mapa.mapData[p].op = -1 elif task == 3 and mapa.mapData[p] is not None: mapa.mapData[p].portal = False core.screen.fill((180,180,180,255)) mapa.draw(core.screen) if task == 0: core.screen.blit( mapa.tileset[tiles[current_tile].imageID], (dx*tw,dy*th), special_flags=BLEND_RGBA_MULT ) elif task == 1: core.screen.blit(mapa.tileset[10+number], (dx*tw, dy*th-25)) elif task == 2: core.screen.blit(mapa.tileset[20+op], (dx*tw, dy*th-25)) elif task == 3: core.screen.blit(mapa.tileset[8], (dx*tw, dy*th-25)) core.screen.blit( selector, (dx*tw,dy*th-25) ) core.fonts.Draw(core.screen, None, 24, 'x, y: ' + str(ox) + ', ' + str(oy), (10, 10), (255,255,255,255)) core.updateScreen()