def loadLevel(self, map="map1", stage = -1): """ Loads current map which is specified by the self.current_map var. @param string map - Map Pack Filename @param int stage - Map Pack Stage """ # Find Map File self.map_dir = levelLoader.load(map) # Load current map self.parser = iniget.iniGet(self.map_dir + "maps") # Get selected stage. if stage > 0: self.current_map = stage # Get map hash (Used in save file) self.maphash = hashlib.sha224(open(self.map_dir + "maps").read()).hexdigest() # Current map self.packMaps = self.parser.get("pack","order").split(",") # Load theme self.themeparser = iniget.iniGet(self.map_dir + self.parser.get(self.packMaps[self.current_map],"theme")) # Load Music music = self.themeparser.get("music","file") if music: self.sound.playSfx(self.map_dir + music, 1, 1) screensize = pygame.display.get_surface() screensize = screensize.get_size() size = screensize[0] if screensize[1] > size: size = screensize[1] self.resizeTiles( math.ceil(size / 16) )
def __init__(self): """Inits the game.""" pygame.init() # Set size here so Android can overwrite it. size=[640,480] # Do some android stuff if android: android.init() # Map back key to to escape android.map_key(android.KEYCODE_BACK, pygame.K_ESCAPE) # Get display size and set game size to it disp_info = pygame.display.Info() size = [disp_info.current_w, disp_info.current_h] # Activate accelerometer if settings.getBool("android","accelerometer_enable"): android.accelerometer_enable(1) # Screen/Dialog stuff self.screen=pygame.display.set_mode(size, pygame.RESIZABLE) pygame.display.set_caption("UpChomp") # Used to manage how fast the screen updates self.clock=pygame.time.Clock() # Game Save self.save = iniget.iniGet(app_path + "game.sav") # Sprites self.all_sprites_list = pygame.sprite.RenderPlain() # Initalize Sound self.sound = sound.Sound() # Init da Chomp self.chomp = chompy.Chompy(self.screen, self.sound) self.all_sprites_list.add(self.chomp) # Init Menu self.menu = menu.Menu(self.screen, self.sound, self.clock, self.save) # Make a dialog object. self.dlogbox = dialog.Dialog(self.screen, self.sound) # Setup Hud self.hud = hud.Hud(self.screen, self.sound) # Setup Transition self.transition = transition.Transition() # Game State: 0-Playing, 1-Main Menu, 2-Pack Select, 3 - Load New Level self.state = 1 # Init Game Map self.level = gamemap.Gamemap(self.sound, self.save) # Frame rate self.frames = 1 # Set Current Map self.map_file = [] # Opening a map... install it. if len(sys.argv) > 1: size = self.screen.get_size() # Unzip Map if not zipfile.is_zipfile(sys.argv[1]): self.gameLoop() return None # Make Temporary Storage Directory if os.path.exists(app_path + "temp"): shutil.rmtree(app_path +"temp") time.sleep(.25) os.mkdir(app_path + "temp") zip_file = zipfile.ZipFile(sys.argv[1]) zip_file.extract("maps", app_path + "temp/") zip_file.close() mappack = iniget.iniGet(app_path + "temp/maps") # Get map hash (Used in save file) maphash = hashlib.sha224(open(sys.argv[1]).read()).hexdigest() if mappack.get("pack","name") and mappack.get("pack", "order"): oldmaphash = "" if os.path.isfile(app_path + "maps/" + mappack.get("pack","name").replace(" ", "_") + ".ucm"): oldmaphash = hashlib.sha224(open(app_path + "maps/" + mappack.get("pack","name").replace(" ", "_") + ".ucm").read()).hexdigest() if maphash == oldmaphash: print "Failed to install new map pack." self.dlogbox.setMessageBox(size, "The map you are trying to install appears to already be installed.", "Already Installed!", [['OK',self.menu.dialog.closeMessageBox]] ) else: shutil.copy (sys.argv[1], app_path + "maps/" + mappack.get("pack","name").replace(" ", "_") + ".ucm") if os.path.isfile(app_path + "maps/" + mappack.get("pack","name").replace(" ", "_") + ".ucm"): print "Installed map pack '"+ str(mappack.get("pack","name").replace(" ", "_")) + ".ucm'." self.dlogbox.setMessageBox(size, "New map pack has been installed!", "New Map Pack!", [['OK',self.menu.dialog.closeMessageBox]] ) else: print "Failed to install new map pack." self.dlogbox.setMessageBox(size, "Unable to install map pack.", "Error", [['OK',self.menu.dialog.closeMessageBox]] ) events = pygame.event.get() while self.dlogbox.drawBox(size, events): events = pygame.event.get() for event in events: if event.type == pygame.QUIT: # If user clicked close pygame.quit() sys.exit() elif event.type == pygame.VIDEORESIZE: size = event.size # Android events if android: if android.check_pause(): android.wait_for_resume() pygame.display.flip() # Enter game loop self.gameLoop()
def stageSelect(self, mappack): """ Map pack stage select state. @param string mappack - Map pack file. @return int - Selected stage. """ selected_stage = 0 current_selection = 0 # self.screen size size = self.screen.get_size() # Get Sizes of Buttons... btnsize = self.dialog.getButtonSize("Play") btnsize2 = self.dialog.getButtonSize("Back") scroll_right_collide = 0 scroll_left_collide = 0 mouse = [0, 0] # Static Vars LIST_SPACING = 32 LIST_START_POS = 92 # Unzip Map map_dir = levelLoader.load(mappack, ["maps"]) # Get map pack info pack = iniget.iniGet(map_dir + "maps") maplist = pack.get("pack","order").split(",") maphash = hashlib.sha224(open(map_dir + "maps").read()).hexdigest() current_selection = self.save.getInt(maphash, "progress") progress = current_selection map = [] for i in maplist: cmap = pack.get(i, "map").replace(" ", "").split("\n") width = 0 height = len(cmap) tiles = [] for x in cmap: if len(x) > width: width = len(x) for y in range(len(x)): tiles.append(x[y]) # Unzip Theme map_dir = levelLoader.load(mappack, ["maps", pack.get(i, "theme").strip()]) # Get Theme Date theme = iniget.iniGet(map_dir + pack.get(i, "theme")) tile_draw = [] for x in tiles: if theme.getBool(x, "collide") and not theme.get(x, "type"): # Floor/Wall Block tile_draw.append(1) elif theme.get(x, "type") == "goal": # Goal Block tile_draw.append(2) else: # Air tile_draw.append(0) map.append({ 'tiles' : tile_draw, 'name' : pack.get(i, "name"), 'startpos' : pack.get(i, "startpos").split(","), 'width' : width, 'height' : height, 'record' : self.save.getFloat(maphash, i), 'toprank' : pack.getFloat(i, "arank") }) # Upon completing the last map the current selection will become one higher, fix it! if current_selection > len(map) - 1: current_selection = len(map) - 1 draw_map = 1 while not selected_stage: # Set frame rate to 30. self.clock.tick(30) events = pygame.event.get() # Android events if android: if android.check_pause(): android.wait_for_resume() mouse_click = [0, 0] for event in events: # User did something if event.type == pygame.QUIT: # If user clicked close pygame.quit() sys.exit() elif event.type == pygame.VIDEORESIZE: self.resizeTitle(event.size) size = event.size draw_map = 1 elif event.type == pygame.MOUSEMOTION: mouse = event.pos elif event.type == pygame.MOUSEBUTTONDOWN: mouse_click = event.pos elif event.type == pygame.KEYDOWN: if event.key == pygame.K_UP: current_selection += 1 if current_selection > len(map) - 1: current_selection = len(map) - 1 # Can't go past maps you haven't completed. if current_selection > progress: current_selection = progress draw_map = 1 elif event.key == pygame.K_DOWN: current_selection -= 1 if current_selection < 0: current_selection = 0 draw_map = 1 if draw_map: map_render_size = 20 map_w, map_h = [9999, 9999] while map_w > size[0] - (TILE_SIZE[0] * 4) or map_h > size[1] - 256: map_render_size -= 1 if map_render_size <= 1: break map_w = map[current_selection]['width'] * map_render_size + (map_render_size * 2) map_h = map[current_selection]['height'] * map_render_size + (map_render_size * 2) map_surface = pygame.Surface((map_w, map_h)) x = 0 y = 0 for i in map[current_selection]['tiles']: # Draw Floor/Wall if i == 1: pygame.draw.rect(map_surface, [255, 255, 255], pygame.Rect((x * map_render_size) + map_render_size, (y * map_render_size) + map_render_size, map_render_size, map_render_size)) # Draw Goal elif i == 2: pygame.draw.rect(map_surface, [0, 0, 255], pygame.Rect((x * map_render_size) + map_render_size, (y * map_render_size) + map_render_size, map_render_size, map_render_size)) # Draw Start Pos elif x == int(map[current_selection]['startpos'][0]) and y == int(map[current_selection]['startpos'][1]): pygame.draw.rect(map_surface, [255, 0, 0], pygame.Rect((x * map_render_size) + map_render_size, (y * map_render_size) + map_render_size, map_render_size, map_render_size)) x += 1 if x >= map[current_selection]['width']: x = 0 y += 1 draw_map = 0 # Render the background self.renderBg(size) # Title Text self.screen.blit( self.titlefont.render("Stage Select", 0, [0,0,0]), (34,34) ) self.screen.blit( self.titlefont.render("Stage Select", 0, [255,179,0]), (32,32) ) # Render Map Preview self.screen.blit(map_surface, ( (size[0] / 2) - ((((map[current_selection]['width'] * map_render_size) / 2) + (map_render_size) ) ) , (size[1] / 2) - ((map[current_selection]['height'] * map_render_size) / 2))) # Render Map Name mapnamesize = self.font.size(map[current_selection]['name']) map_title_pos = [(size[0] / 2) - (mapnamesize[0] / 2), (size[1] / 2) - ((map[current_selection]['height'] * map_render_size) / 2) - mapnamesize[1] * 1.5 ] self.screen.blit( self.font.render(map[current_selection]['name'], 0, [0,0,0]), ( map_title_pos[0] + 1, map_title_pos[1] + 1 ) ) self.screen.blit( self.font.render(map[current_selection]['name'], 0, [255,179,0]), ( map_title_pos[0], map_title_pos[1])) # Render Stats if map[current_selection]['record']: timesize = self.font.size("Best Time: " + str(map[current_selection]['record'])) timepos = [(size[0] / 2) - (timesize[0] / 2), (size[1] / 2) - ((map[current_selection]['height'] * map_render_size) / 2) + map_h + 16 ] self.screen.blit( self.font.render("Best Time: " + str(map[current_selection]['record']), 0, [0,0,0]), ( timepos[0] + 1, timepos[1] + 1 )) self.screen.blit( self.font.render("Best Time: " + str(map[current_selection]['record']), 0, [255,179,0]), ( timepos[0], timepos[1])) if map[current_selection]['record'] <= map[current_selection]['toprank']: self.screen.blit(self.highrank, (timepos[0] + timesize[0] + (TILE_SIZE[0] / 4), timepos[1] ) ) # Buttons if self.dialog.makeButton("Back", [ size[0] - btnsize2[0] - btnsize[0] - (LIST_SPACING * 1.5) , size[1] - (LIST_SPACING * 1.5) ], size, events, 0): selected_stage = -1 elif self.dialog.makeButton("Play", [ size[0] - btnsize[0] - (LIST_SPACING * 1.5) , size[1] - (LIST_SPACING * 1.5) ], size, events, 1): selected_stage = current_selection + 1 # Render scroll right arrow if current_selection < len(map) - 1 and current_selection < progress: pos = [size[0] - (TILE_SIZE[0] * 2), (size[1] / 2) - (TILE_SIZE[1] / 2) ] rect = pygame.Rect(pos[0] - TILE_SIZE[0], pos[1] - TILE_SIZE[1], TILE_SIZE[0] * 4, TILE_SIZE[1] * 4) if not rect.collidepoint(mouse[0], mouse[1]): self.screen.blit( self.scrollarrows_horizontal[0][0], (pos[0], pos[1]) ) else: self.screen.blit( self.scrollarrows_horizontal[1][0], (pos[0], pos[1]) ) if rect.collidepoint(mouse_click[0], mouse_click[1]): if current_selection < len(map) - 1: current_selection += 1 draw_map = 1 # Render scroll left arrow if current_selection > 0: pos = [TILE_SIZE[0], (size[1] / 2) - (TILE_SIZE[1] / 2)] rect = pygame.Rect(pos[0] - TILE_SIZE[0], pos[1] - TILE_SIZE[1], TILE_SIZE[0] * 4, TILE_SIZE[1] * 4) if not rect.collidepoint(mouse[0], mouse[1]): self.screen.blit( self.scrollarrows_horizontal[0][1], (pos[0], pos[1]) ) else: self.screen.blit( self.scrollarrows_horizontal[1][1], (pos[0], pos[1]) ) if rect.collidepoint(mouse_click[0], mouse_click[1]): if current_selection > 0: current_selection -= 1 draw_map = 1 # Go ahead and update the self.screen with what we've drawn. pygame.display.flip() return selected_stage
if hasattr(sys, 'frozen'): app_path = os.path.dirname(sys.executable) elif __file__: app_path = os.path.dirname(__file__) app_path = app_path.replace('\\', '/') + "/" try: import android, android_mixer print "[OS] Running on Android." except ImportError: print "[OS] Running on PC." android = None settings = iniget.iniGet(app_path + "settings.ini") error_log = open(app_path + 'error.log', 'w') class Game(object): def __init__(self): """Inits the game.""" pygame.init() # Set size here so Android can overwrite it. size=[640,480] # Do some android stuff if android:
def mapSelect(self): """ Map pack select state. @return int - Next game state. """ done = 0 # self.screen size size = self.screen.get_size() # Vars to Use bgoffset = 0 map_list_scroll = 0 map_selected = 0 stage_selected = 0 # Static Vars LIST_SPACING = 32 LIST_START_POS = 92 # Map Select Arrow maparrow = pygame.image.load(app_path + "gfx/map_select_arrow.png").convert_alpha() # Load Map List maps = levelLoader.levelList() mapList = [] for i in maps: map_dir = levelLoader.load(i, ['maps']) ini = iniget.iniGet(map_dir + "maps") totalmaps = len(str(ini.get("pack","order")).split(",")) maphash = hashlib.sha224(open(map_dir + "maps").read()).hexdigest() highrank = 1 for x in ini.get("pack","order").split(","): record = self.save.getFloat(maphash, x) ranktime = ini.getFloat(x, "arank") if not record <= ranktime: highrank = 0 if self.save.getInt(maphash, "progress") == totalmaps: complete = 1 else: complete = 0 mapList.append([str(i), ini.get("pack", "name"), complete, highrank]) returnVal = -1 # Get Sizes of Buttons... btnsize = self.dialog.getButtonSize("Next") btnsize2 = self.dialog.getButtonSize("Back") mouse = [0, 0] # Update BG Size self.resizeTitle(size) # Play Menu Music self.sound.playSfx(app_path + "sfx/danosongs.com-helium-hues.ogg", -1, 1) while not done: # Set frame rate to 30. self.clock.tick(30) events = pygame.event.get() # Android events if android: if android.check_pause(): android.wait_for_resume() mouse_click = [0, 0] for event in events: # User did something if event.type == pygame.QUIT: # If user clicked close done=True # Flag that we are done so we exit this loop elif event.type == pygame.VIDEORESIZE: self.resizeTitle(event.size) size = event.size map_list_scroll = 0 map_selected = 0 elif event.type == pygame.MOUSEMOTION: mouse = event.pos elif event.type == pygame.MOUSEBUTTONDOWN: mouse_click = event.pos x = 0 for i in mapList: fontSize = self.font.size(i[1]) rect = pygame.Rect(64, x * LIST_SPACING + LIST_START_POS - (map_list_scroll * LIST_SPACING), fontSize[0], fontSize[1]) if rect.collidepoint(event.pos[0], event.pos[1]): map_selected = x self.sound.playSfx(app_path + "sfx/beep.wav", 0) x += 1 elif event.type == pygame.KEYDOWN: if event.key == pygame.K_UP: map_selected -= 1 self.sound.playSfx(app_path + "sfx/beep.wav", 0) if map_selected < 0: map_selected = 0 if scroll_down and map_selected < map_list_scroll: map_list_scroll = map_selected elif event.key == pygame.K_DOWN: map_selected += 1 self.sound.playSfx(app_path + "sfx/beep.wav", 0) if map_selected > len(mapList) - 1: map_selected = len(mapList) - 1 if scroll_down and map_selected > map_list_scroll: map_list_scroll = map_selected # Render the background self.renderBg(size) # Title Text self.screen.blit( self.titlefont.render("Pack Select", 0, [0,0,0]), (34,34) ) self.screen.blit( self.titlefont.render("Pack Select", 0, [255,179,0]), (32,32) ) # Map List x = 0 scroll_down = 0 max_text_width = 0 cut_off = 0 for i in mapList: pos = [64, x * LIST_SPACING + LIST_START_POS - (map_list_scroll * LIST_SPACING) ] if self.font.size(i[1])[0] > max_text_width: max_text_width = self.font.size(i[1])[0] if not pos[1] > size[1] - 64 and not pos[1] < 64: self.screen.blit( self.font.render(i[1], 0, [0,0,0]), (pos[0] + 2, pos[1] + 2) ) self.screen.blit( self.font.render(i[1], 0, [255,255,255]), (pos[0], pos[1]) ) # If map pack completed, display emblem if i[2]: textsize = self.font.size(i[1]) self.screen.blit( self.checkmark, (pos[0] + textsize[0] + (TILE_SIZE[0] / 4), pos[1]) ) # If all maps completed with high rank, display emblem if i[3]: self.screen.blit( self.highrank, (pos[0] + textsize[0] + 16 + (TILE_SIZE[0] / 2), pos[1]) ) else: scroll_down = 1 cut_off += 1 x += 1 # Render scroll down arrow if scroll_down and not map_list_scroll >= cut_off: pos = [max_text_width + 78, size[1] - 92] rect = pygame.Rect(pos[0], pos[1], TILE_SIZE[0], TILE_SIZE[1]) if not rect.collidepoint(mouse[0], mouse[1]): self.screen.blit( self.scrollarrows_vertical[0][0], (pos[0], pos[1]) ) else: self.screen.blit( self.scrollarrows_vertical[1][0], (pos[0], pos[1]) ) if rect.collidepoint(mouse_click[0], mouse_click[1]): map_list_scroll += 1 if map_list_scroll > cut_off: map_list_scroll = cut_off # Render scroll up arrow if map_list_scroll > 0: pos = [max_text_width + 78, 64] rect = pygame.Rect(pos[0], pos[1], TILE_SIZE[0], TILE_SIZE[1]) if not rect.collidepoint(mouse[0], mouse[1]): self.screen.blit( self.scrollarrows_vertical[0][1], (pos[0], pos[1]) ) else: self.screen.blit( self.scrollarrows_vertical[1][1], (pos[0], pos[1]) ) if rect.collidepoint(mouse_click[0], mouse_click[1]) and map_list_scroll > 0: map_list_scroll -= 1 # Map Selected Arrow if map_selected < map_list_scroll: map_selected = map_list_scroll self.screen.blit(maparrow, ( 32, LIST_START_POS + (LIST_SPACING * map_selected) - (LIST_SPACING * map_list_scroll) ) ) # Dialog Box if not self.dialog.drawBox(size, events): # If next clicked load selected level if self.dialog.makeButton("Play", [ size[0] - btnsize[0] - (LIST_SPACING * 1.5) , size[1] - (LIST_SPACING * 1.5) ], size, events, 1): stage_selected = self.stageSelect(mapList[map_selected][0]) if stage_selected > 0: returnVal = mapList[map_selected][0] done = 1 else: size = self.screen.get_size() self.resizeTitle(size) elif not stage_selected and self.dialog.makeButton("Quit", [ size[0] - btnsize2[0] - btnsize[0] - (LIST_SPACING * 1.5) , size[1] - (LIST_SPACING * 1.5) ], size, events, 0): pygame.quit() sys.exit() else: stage_selected = 0 # Go ahead and update the self.screen with what we've drawn. pygame.display.flip() return [returnVal, stage_selected - 1]