def Enhancement_Interface(self, surf, num, rect, margin): if (self.pictures.has_key(num)): img = resource.Load_Image(self.pictures[num]) img_r = img.get_rect() img_r.center = rect.center img_r.left = rect.left + margin surf.blit(img, img_r.topleft)
def __init__(self): self.radius = 4.0 + ( random.random() * 1.8 ) # eye of storm radius = 4. self.angle = random.random() * TWO_PI self.dr = abs(random.Random().normalvariate(0.0,0.15)) + 0.01 # Colour comes from an authentic set of alien storm colours. stormsample = resource.Load_Image("stormsample.png") x = random.randint(0, stormsample.get_rect().width - 1) y = random.randint(0, stormsample.get_rect().height - 1) self.c = stormsample.get_at((x,y))
def __Draw(self, xxx_todo_changeme): (width_hint, height_hint) = xxx_todo_changeme surf = pygame.Surface((width_hint, height_hint)) bbox = Rect(0, 0, width_hint, height_hint) extra.Tile_Texture(surf, "006metal.jpg", surf.get_rect()) margin = 8 w = bbox.width - (margin * 2) th = None y = margin + bbox.top control_rects = [] max_width = 0 first_item = True for (num, name, hotkeys) in self.options: if (name == None): # a gap if (first_item): img = resource.Load_Image("header.jpg") img_r = img.get_rect() img_r.center = bbox.center img_r.top = y surf.blit(img, img_r.topleft) extra.Edge_Effect(surf, img_r) max_width = img_r.width + (margin * 2) y += img_r.height y += margin * 2 continue txt = render.Render(name, 18, (50, 200, 20), (200, 200, 0)) if (th == None): th = txt.get_rect().height + (margin * 2) tw = txt.get_rect().width + (margin * 2) if (tw > max_width): max_width = tw x = bbox.left + margin r = Rect(x, y, w, th) x += self.Justify(w, txt.get_rect().width) extra.Tile_Texture(surf, "greenrust.jpg", r) extra.Edge_Effect(surf, r) self.Enhancement_Interface(surf, num, r, margin) surf.blit(txt, (x, y + margin - 1)) y += th + margin control_rects.append((num, r)) first_item = False # Finalise drawing extra.Line_Edging(surf, bbox, True) return (surf, control_rects, (max_width, y))
def Tile_Texture(output, name, rect): cr = output.get_clip() output.set_clip(rect) img = resource.Load_Image(name) img_r = img.get_rect() for x in range(0, rect.width, img_r.width): for y in range(0, rect.height, img_r.height): output.blit(img, (x + rect.left, y + rect.top)) output.set_clip(cr)
def __init__(self, key): (img_name, grid_size) = key img = resource.Load_Image(img_name) (w, h) = img.get_rect().bottomright new_width = Get_Grid_Size() * grid_size new_height = (new_width * h) / w img = pygame.transform.scale(img, (new_width, new_height)) r = img.get_rect() r.center = (0, 0) self.offset_x = r.left self.offset_y = r.top self.frames = [] for c in [0, 0, 50, 100, 150, 200, 250, 250, 150]: self.frames.append(self.__Colour_Substitute(c, img))
def __init__(self, key): (src_image_name, src_image_rect, grid_size) = key # Load the complete image file img = resource.Load_Image(src_image_name) # Crop out the required rectangle if any if src_image_rect != None: assert type(src_image_rect) == tuple assert len(src_image_rect) == 4 # x,y,w,h img = img.subsurface(Rect(src_image_rect)) if grid_size >= 0: # Rescale for grid new_size = Get_Grid_Size() * grid_size grid_rect = Rect(0, 0, new_size, new_size) self.grid_mode = True else: grid_rect = Rect(0, 0, -grid_size, -grid_size) self.grid_mode = False img_rect = img.get_rect().fit(grid_rect) img = pygame.transform.scale(img, img_rect.size) # Calculate offsets img_rect.center = (0, 0) self.offset_x = img_rect.left self.offset_y = img_rect.top # Make frames self.frames = [] if self.grid_mode: #for c in [ 0, 0, 50, 100, 150, 200, 250, 250, 150 ]: # self.frames.append(self.__Colour_Substitute(c, img)) self.frames.append(img) else: self.frames.append(img)
def Main_Loop(screen, clock, xxx_todo_changeme, restore_pos, challenge): # Initialisation of screen things. (width, height) = xxx_todo_changeme menu_margin = height screen.fill((0, 0, 0)) # screen is black during init pygame.display.flip() tutor.Off() draw_obj.Flush_Draw_Obj_Cache() # in case of resize # Grid setup (w, h) = GRID_SIZE assert w == h Set_Grid_Size(height / h) # Windows.. game_screen_rect = Rect(0, 0, menu_margin, height) game_screen_surf = screen.subsurface(game_screen_rect) menu_area = screen.subsurface( Rect(menu_margin, 0, width - menu_margin, height)) menu_width = width - menu_margin # Constraint on resolution applied here: assert menu_width >= 100 def Sc(v): # Original values were for 800x600 - scale that # for whatever the user's screen res happens to be. return (v * height) / 600 margin = Sc(10) x1 = menu_margin + margin menu_width1 = menu_width - (margin * 2) picture = resource.Load_Image("headersm.jpg") picture_rect = picture.get_rect().inflate(10, 10) picture_rect.center = (x1 + (menu_width1 / 2), 0) picture_rect.top = margin picture_surf = screen.subsurface(picture_rect) stats_rect = Rect(x1, picture_rect.bottom + margin, menu_width1, Sc(120)) stats_surf = screen.subsurface(stats_rect) global_stats_rect = Rect(x1, stats_rect.bottom + margin, menu_width1, Sc(110)) global_stats_surf = screen.subsurface(global_stats_rect) controls_rect = Rect(x1, global_stats_rect.bottom + margin, menu_width1, height - (margin + global_stats_rect.bottom + margin)) controls_surf = screen.subsurface(controls_rect) def Special_Refresh(): extra.Tile_Texture( screen, "rivets.jpg", Rect(menu_margin, 0, menu_width, screen.get_rect().height)) edge = Rect(menu_margin, -10, menu_width + 10, screen.get_rect().height + 10) for r in [stats_rect, global_stats_rect, edge]: extra.Line_Edging(screen, r, False) r = picture.get_rect() r.center = picture_surf.get_rect().center extra.Line_Edging(picture_surf, r, False) picture_surf.blit(picture, r.topleft) Special_Refresh() stats_surf.fill((0, 0, 0)) FRAME_RATE = 35 alarm_sound = sound.Persisting_Sound("emergency") teaching = (challenge == MENU_TUTORIAL) # Game data holder g = Game_Data() g.version = startup.Get_Game_Version() g.sysinfo = extra.Get_System_Info() # Steam network initialisation g.net = Network(teaching) DIFFICULTY.Set(MENU_INTERMEDIATE) # Establish equilibrium with initial network. for i in range(300): g.net.Steam_Think() if (g.net.hub.Get_Pressure() >= PRESSURE_GOOD): if (DEBUG): print(i, 'steps required for equilibrium') break assert g.net.hub.Get_Pressure() >= PRESSURE_GOOD # UI setup ui = User_Interface(g.net, (width, height)) inputs = [(controls_rect, ui.Control_Mouse_Down, ui.Control_Mouse_Move), (game_screen_rect, ui.Game_Mouse_Down, ui.Game_Mouse_Move)] exit_options = [(MENU_MENU, "Exit to Main Menu", []), (MENU_QUIT, "Exit to " + extra.Get_OS(), [K_F10])] save_available = [(MENU_SAVE, "Save Game", []), (MENU_LOAD, "Restore Game", []), (None, None, [])] if (challenge == MENU_TUTORIAL): save_available = [] in_game_menu = menu.Menu([(None, None, []), (MENU_MUTE, "Toggle Sound", []), (None, None, [])] + save_available + [(MENU_HIDE, "Return to Game", [K_ESCAPE])] + exit_options) current_menu = in_game_menu flash = True loop_running = True quit = False stats_review = False in_game_menu.Select(None) mail.Initialise() rt_then = time.time() fps_count = 0 fps_time = rt_then autosave_timer = 0 if (restore_pos == None): DIFFICULTY.Set(challenge) # Game variables g.season = SEASON_START g.season_ends = 0 g.season_effect = 0 g.season_fx = Quiet_Season(g.net) g.work_units_used = 0 g.challenge = challenge g.difficulty_level = 1.0 g.work_timer = 0.1 g.game_ends_at = None g.game_running = True g.game_time = gametime.Game_Time() g.historian = [] g.historian_time = 0 g.win = False g.warning_given = False g.wu_integral = 0 mail.Set_Day(g.game_time.Get_Day()) def Summary(g): lev = dict() lev[MENU_TUTORIAL] = "a Tutorial" lev[MENU_BEGINNER] = "a Beginner" lev[MENU_INTERMEDIATE] = "an Intermediate" lev[MENU_EXPERT] = "an Expert" lev[MENU_PEACEFUL] = "a Peaceful" assert g.challenge != None assert g.challenge in lev New_Mail("You are playing " + lev[g.challenge] + " game.") New_Mail("Win the game by upgrading your city to tech level %u." % DIFFICULTY.CITY_MAX_TECH_LEVEL) # Almost ready to start... but are we starting # from a savegame? def Restore(g, cmd): (g2, result) = save_game.Load(g, cmd) if (result == None): g = g2 ui.net = g.net mail.Initialise() mail.Set_Day(g.game_time.Get_Day()) assert g.challenge != None DIFFICULTY.Set(g.challenge) New_Mail( f"Game restored. It is currently {g.season_fx.name} season.") else: New_Mail(result) return g if (restore_pos != None): g.challenge = MENU_INTERMEDIATE g = Restore(g, restore_pos) assert g.challenge != None Summary(g) if (g.challenge == MENU_TUTORIAL): tutor.On((menu_margin * 40) / 100) cur_time = g.game_time.time() # Main loop while (loop_running): if (g.game_running): flash = not flash menu_inhibit = ui.Is_Menu_Open() or not g.game_running # Insert delay... # Hmm, I'm not sure if I know what this does. clock.tick(FRAME_RATE) rt_now = time.time() rt_frame_length = rt_now - rt_then rt_then = rt_now fps_count += 1 if (fps_count > 100): if (DEBUG): print( f"{math.floor(float(fps_count) / (rt_now - fps_time))} fps" ) fps_time = rt_now fps_count = 0 if (not menu_inhibit): if (not tutor.Frozen()): g.game_time.Advance(rt_frame_length) draw_obj.Next_Frame() # Flashing lights on the various items cur_time = g.game_time.time() mail.Set_Day(g.game_time.Get_Day()) ui.Draw_Game(game_screen_surf, g.season_fx) # if ( flash ): # ui.Draw_Selection(picture_surf) if (g.challenge == MENU_TUTORIAL): until_next = [] elif (g.challenge == MENU_PEACEFUL): until_next = [((128, 128, 128), 12, "Peaceful mode")] else: until_next = [( (128, 128, 128), 12, f"{math.floor((g.season_ends - cur_time) + 1)} days until next season)" )] ui.Draw_Stats(stats_surf, [((128, 0, 128), 18, f"Day {g.game_time.Get_Day()}"), ((128, 128, 0), 18, g.season_fx.name + " season")] + until_next + g.season_fx.Get_Extra_Info()) ui.Draw_Controls(controls_surf) if (menu_inhibit): current_menu.Draw(screen) alarm_sound.Set(0.0) stats_back = (0, 0, 0) supply = g.net.hub.Get_Steam_Supply() demand = g.net.hub.Get_Steam_Demand() if (g.net.hub.Get_Pressure() < PRESSURE_DANGER): # You'll lose the game if you stay in this zone # for longer than a timeout. Also, an # alarm will sound. if (g.game_ends_at == None): sound.FX("steamcrit") g.warning_given = True New_Mail("Danger! The City needs more steam!", (255, 0, 0)) g.game_ends_at = cur_time + DIFFICULTY.GRACE_TIME New_Mail( f"Game will end on Day {g.game_ends_at} unless supplies are increased.", (255, 0, 0)) if (flash): demand_colour = (255, 0, 0) if (not menu_inhibit): alarm_sound.Set(0.6) else: demand_colour = (128, 0, 0) stats_back = (100, 0, 0) elif (g.net.hub.Get_Pressure() < PRESSURE_WARNING): g.game_ends_at = None if (flash): demand_colour = (255, 100, 0) if (not menu_inhibit): alarm_sound.Set(0.3) else: demand_colour = (128, 50, 0) stats_back = (50, 25, 0) else: if (g.warning_given): sound.FX("steamres") g.warning_given = False if (g.net.hub.Get_Pressure() < PRESSURE_OK): demand_colour = (128, 128, 0) else: demand_colour = (0, 128, 0) g.game_ends_at = None alarm_sound.Set(0.0) avw = g.net.hub.Get_Avail_Work_Units() wu_unused = avw - g.work_units_used if (not menu_inhibit): global_stats_surf.fill(stats_back) stats.Draw_Stats_Window( global_stats_surf, [(CITY_COLOUR, 18, "Steam Pressure"), (CITY_COLOUR, 18, "Supply : Demand"), (demand_colour, 24, f"{round(supply, 1)} : {round(demand, 1)}"), (None, None, g.net.hub.Get_Pressure_Meter()), (CITY_COLOUR, 0, ""), (CITY_COLOUR, 12, f"{wu_unused} of {avw} work units available"), (None, None, (wu_unused, (255, 0, 255), avw, (0, 0, 0)))]) if (g.challenge == MENU_TUTORIAL): tutor.Draw(screen, g) pygame.display.flip() if (not menu_inhibit): g.season_fx.Per_Frame(rt_frame_length) ui.Frame_Advance(rt_frame_length) # Timing effects if (g.work_timer <= cur_time): # Fixed periodic effects g.work_timer = cur_time + 0.1 g.wu_integral += wu_unused g.work_units_used = g.net.Work_Pulse( g.net.hub.Get_Avail_Work_Units()) g.net.Steam_Think() g.net.Expire_Popups() tutor.Examine_Game(g) if (g.season_effect <= cur_time): # Seasonal periodic effects g.season_effect = cur_time + g.season_fx.Get_Period() g.season_fx.Per_Period() if (((not tutor.Permit_Season_Change()) and (g.season == SEASON_QUIET)) or (g.challenge == MENU_PEACEFUL)): g.season_ends = cur_time + 2 if (g.season_ends <= cur_time): # Season change if (g.season == SEASON_START): g.season = SEASON_QUIET g.season_fx = Quiet_Season(g.net) elif ((g.season == SEASON_QUIET) or (g.season == SEASON_STORM)): g.season = SEASON_ALIEN g.season_fx = Alien_Season(g.net, g.difficulty_level) sound.FX("aliensappr") elif (g.season == SEASON_ALIEN): g.season = SEASON_QUAKE g.season_fx = Quake_Season(g.net, g.difficulty_level) if (not tutor.Active()): # hack... sound.FX("quakewarn") elif (g.season == SEASON_QUAKE): g.season = SEASON_STORM g.season_fx = Storm_Season(g.net, g.difficulty_level) g.difficulty_level *= 1.2 # 20% harder.. sound.FX("stormwarn") else: assert False g.season_ends = cur_time + LENGTH_OF_SEASON g.season_effect = cur_time + (g.season_fx.Get_Period() / 2) if (g.challenge != MENU_PEACEFUL): New_Mail(f"The {g.season_fx.name} season has started.", (200, 200, 200)) just_ended = False if ((g.game_ends_at != None) and (g.game_ends_at <= cur_time) and (g.game_running)): # Game over - you lose g.game_running = False New_Mail("The City ran out of steam.", (255, 0, 0)) New_Mail("Game Over!", (255, 255, 0)) sound.FX("krankor") just_ended = True elif ((g.net.hub.tech_level >= DIFFICULTY.CITY_MAX_TECH_LEVEL) and (g.game_running)): # Game over - you win! g.game_running = False g.win = True New_Mail("The City is now fully upgraded!", (255, 255, 255)) New_Mail("You have won the game!", (255, 255, 255)) sound.FX("applause") just_ended = True if (just_ended): current_menu = in_game_menu = menu.Menu( [(None, None, []), (MENU_REVIEW, "Review Statistics", [])] + exit_options) in_game_menu.Select(None) # Events e = pygame.event.poll() while (e.type != NOEVENT): if e.type == QUIT: loop_running = False quit = True elif e.type == MOUSEBUTTONDOWN or e.type == MOUSEMOTION: if e.type == MOUSEBUTTONDOWN and e.button != 1: if not menu_inhibit: ui.Right_Mouse_Down() elif not menu_inhibit: for rect, click, move in inputs: if rect.collidepoint(e.pos): (x, y) = e.pos x -= rect.left y -= rect.top if e.type == MOUSEMOTION: move((x, y)) else: click((x, y)) elif menu_inhibit: if e.type == MOUSEMOTION: current_menu.Mouse_Move(e.pos) else: current_menu.Mouse_Down(e.pos) elif e.type == KEYDOWN: if not menu_inhibit: ui.Key_Press(e.key) elif menu_inhibit: current_menu.Key_Press(e.key) if DEBUG: # Cheats. if e.key == K_F10: New_Mail("Cheat used: Advanced to next season.") g.season_ends = 0 elif e.key == K_F9: New_Mail("Cheat used: Screen filled with white.") screen.fill((255, 255, 255)) elif e.key == K_F8: # Lose the game cheat # Heh, worst cheat ever. New_Mail("Cheat used: Game ended.") g.game_ends_at = cur_time e = pygame.event.poll() # Any commands from the menu? if menu_inhibit: cmd = current_menu.Get_Command() current_menu.Select(None) # consume command if current_menu == in_game_menu: # It's the normal menu. if cmd == MENU_QUIT: loop_running = False quit = True ui.Reset() # makes menu disappear elif cmd == MENU_MENU: loop_running = False ui.Reset() elif cmd == MENU_SAVE: if (g.game_running): # Switch to alternate menu current_menu = save_menu.Save_Menu(True) elif cmd == MENU_LOAD: current_menu = save_menu.Save_Menu(False) elif cmd == MENU_MUTE: config.cfg.mute = not config.cfg.mute ui.Reset() elif cmd == MENU_REVIEW: loop_running = False stats_review = True ui.Reset() elif cmd != None: # Default option - back to game if (not g.game_running): New_Mail("Sorry - the game has finished") ui.Reset() else: # It's another menu! That means it's the save menu. if cmd != None and cmd >= 0: if not current_menu.Is_Saving(): g = Restore(g, cmd) else: label = f"Day {math.floor(g.game_time.Get_Day())} - {g.season_fx.name} season - {time.asctime()}" g.net.Make_Ready_For_Save() result = save_game.Save(g, cmd, label) if result == None: New_Mail("Game saved.") else: New_Mail(result) if cmd != None: # Back to game. Special_Refresh() current_menu = in_game_menu ui.Reset() if autosave_timer <= cur_time and g.game_running and DEBUG and not menu_inhibit: # Autosave is slow, so it's really a debugging feature. save_game.Save(g, 11, "Autosave") autosave_timer = cur_time + 60 if (g.historian_time <= cur_time) and (g.game_running) and (not menu_inhibit): g.historian.append(review.Analyse_Network(g)) g.historian_time = cur_time + 4 tutor.Off() # About to exit. Blackout. screen.fill((0, 0, 0)) if (stats_review): review.Review(screen, (width, height), g, g.historian) return quit
class Menu: def __init__(self, menu_options, force_width=0): self.options = menu_options self.control_rects = [] self.hover = None self.bbox = None self.selection = None self.update_required = True width_hint = height_hint = 10 if (force_width > 0): width_hint = force_width # Two attempts at drawing required. (discard1, discard2, (width_hint, height_hint)) = self.__Draw( (width_hint, height_hint)) if (width_hint < 150): width_hint = 150 if (force_width > 0): width_hint = force_width (self.surf_store, self.control_rects, (discard1, discard2)) = self.__Draw((width_hint, height_hint)) self.bbox = Rect(0, 0, width_hint, height_hint) def Get_Command(self): return self.selection def Select(self, snum): self.update_required = True self.selection = snum def Mouse_Move(self, spos): if ((spos == None) or (not self.bbox.collidepoint(spos))): self.hover = None return self.update_required = True (x, y) = spos old_sel = self.hover self.hover = None x -= self.bbox.left y -= self.bbox.top for (num, r) in self.control_rects: if (r.collidepoint(x, y)): self.hover = num if (old_sel != self.hover): sound.FX("click_s") return def Mouse_Down(self, spos): self.Mouse_Move(spos) if (self.hover != None): self.selection = self.hover sound.FX("click") def Key_Press(self, k): for (num, name, hotkeys) in self.options: if ((hotkeys != None) and (k in hotkeys)): self.selection = num self.update_required = True sound.FX("click") return def Draw(self, output, centre=None): if (self.update_required): self.update_required = False if (centre == None): self.bbox.center = output.get_rect().center else: self.bbox.center = centre self.bbox.clamp_ip(output.get_rect()) output.blit(self.surf_store, self.bbox.topleft) for (num, r) in self.control_rects: r = Rect(r) r.top += self.bbox.top r.left += self.bbox.left if (num == self.selection): pygame.draw.rect(output, (255, 255, 255), r, 1) elif (num == self.hover): pygame.draw.rect(output, (0, 180, 0), r, 1) def __Draw(self, (width_hint, height_hint)): surf = pygame.Surface((width_hint, height_hint)) bbox = Rect(0, 0, width_hint, height_hint) extra.Tile_Texture(surf, "006metal.jpg", surf.get_rect()) margin = 8 w = bbox.width - (margin * 2) th = None y = margin + bbox.top control_rects = [] max_width = 0 first_item = True for (num, name, hotkeys) in self.options: if (name == None): # a gap if (first_item): img = resource.Load_Image("header.jpg") img_r = img.get_rect() img_r.center = bbox.center img_r.top = y surf.blit(img, img_r.topleft) extra.Edge_Effect(surf, img_r) max_width = img_r.width + (margin * 2) y += img_r.height y += margin * 2 continue txt = render.Render(name, 18, (50, 200, 20), (200, 200, 0)) if (th == None): th = txt.get_rect().height + (margin * 2) tw = txt.get_rect().width + (margin * 2) if (tw > max_width): max_width = tw x = bbox.left + margin r = Rect(x, y, w, th) x += self.Justify(w, txt.get_rect().width) extra.Tile_Texture(surf, "greenrust.jpg", r) extra.Edge_Effect(surf, r) self.Enhancement_Interface(surf, num, r, margin) surf.blit(txt, (x, y + margin - 1)) y += th + margin control_rects.append((num, r)) first_item = False # Finalise drawing extra.Line_Edging(surf, bbox, True) return (surf, control_rects, (max_width, y))
def Edge_Effect(output, rect): bolt = resource.Load_Image("bolt.png") margin = 2 for x in [rect.left + margin, rect.right - (margin + 3)]: for y in [rect.top + margin, rect.bottom - (margin + 3)]: output.blit(bolt, (x, y))
Rect(menu_margin, 0, width - menu_margin, height)) menu_width = width - menu_margin # Constraint on resolution applied here: assert menu_width >= 100 def Sc(v): # Original values were for 800x600 - scale that # for whatever the user's screen res happens to be. return (v * height) / 600 margin = Sc(10) x1 = menu_margin + margin menu_width1 = menu_width - (margin * 2) picture = resource.Load_Image("headersm.jpg") picture_rect = picture.get_rect().inflate(10, 10) picture_rect.center = (x1 + (menu_width1 / 2), 0) picture_rect.top = margin picture_surf = screen.subsurface(picture_rect) stats_rect = Rect(x1, picture_rect.bottom + margin, menu_width1, Sc(120)) stats_surf = screen.subsurface(stats_rect) global_stats_rect = Rect(x1, stats_rect.bottom + margin, menu_width1, Sc(110)) global_stats_surf = screen.subsurface(global_stats_rect) controls_rect = Rect(x1, global_stats_rect.bottom + margin, menu_width1, height - (margin + global_stats_rect.bottom + margin)) controls_surf = screen.subsurface(controls_rect) def Special_Refresh():
def Main(data_dir): n = "20,000 Light-Years Into Space: Refueled" print("") print(n) print("Copyright (C) Jack Whitham 2006-11 and Tux Penguin 2020") print("Version", config.CFG_VERSION) print("") resource.DATA_DIR = data_dir config.Initialise("--safe" in sys.argv) # Pygame things flags = 0 if ("--fullscreen" in sys.argv): flags |= FULLSCREEN bufsize = 2048 no_sound = ("--no-sound" in sys.argv) if not no_sound: try: pygame.mixer.pre_init(22050, -16, 2, bufsize) pygame.mixer.init() except pygame.error as message: print('Sound initialization failed. %s' % message) no_sound = True pygame.init() pygame.font.init() if flags & FULLSCREEN: # Ensure that all resolutions are supported by the system for resolution in RESOLUTIONS: if resolution[:2] not in pygame.display.list_modes(): RESOLUTIONS.remove(resolution) if (no_sound): resource.No_Sound() else: pygame.mixer.init(22050, -16, 2, bufsize) clock = pygame.time.Clock() screen = pygame.display.set_mode(config.cfg.resolution, flags) height = screen.get_rect().height width = screen.get_rect().width # Icon # The icon provided in the Debian package is different than the original one # (size and location changed) if os.path.isfile(DEB_ICON): pygame.display.set_icon(resource.Load_Image(DEB_ICON)) else: pygame.display.set_icon(resource.Load_Image("32.png")) # Game begins.. show loading image screen.fill((0, 0, 0)) pygame.display.flip() pygame.display.set_caption(n) storms.Init_Storms() alien_invasion.Init_Aliens() quakes.Init_Quakes() quit = False while (not quit): if (config.cfg.resolution != (width, height)): # As the toggle mode thing doesn't work outside of Unix, # the fallback strategy is to do set_mode again. # But if you set the same mode, then nothing happens. # So: screen = pygame.display.set_mode((640, 480), flags) # not the right mode screen = pygame.display.set_mode(config.cfg.resolution, flags) # right mode! height = screen.get_rect().height width = screen.get_rect().width quit = Main_Menu_Loop(n, clock, screen, (width, height)) config.Save() # Bye bye Pygame. pygame.mixer.quit() pygame.quit()
def Main_Menu_Loop(name, clock, screen, xxx_todo_changeme): # Further initialisation (width, height) = xxx_todo_changeme menu_image = resource.Load_Image("mainmenu.jpg") if (menu_image.get_rect().width != width): menu_image = pygame.transform.scale(menu_image, (width, height)) stats.Set_Font_Scale(config.cfg.font_scale) main_menu = current_menu = menu.Menu([ (None, None, []), (MENU_TUTORIAL, "Play Tutorial", []), (MENU_NEW_GAME, "Play New Game", []), (MENU_LOAD, "Restore Game", []), (None, None, []), (MENU_RES, "Set Graphics Resolution", []), (MENU_MUTE, "Toggle Sound", []), (MENU_MANUAL, "View Manual", []), (MENU_UPDATES, "Check for Updates", []), (None, None, []), (MENU_QUIT, "Exit to " + extra.Get_OS(), [K_ESCAPE, K_F10]) ]) resolution_menu = menu.Menu([(None, None, [])] + [(w, "%u x %u" % (w, h), []) for (w, h, fs) in RESOLUTIONS] + [(None, None, []), (-1, "Cancel", [])]) difficulty_menu = menu.Menu([(None, None, []), (MENU_TUTORIAL, "Tutorial", []), (None, None, []), (MENU_BEGINNER, "Beginner", []), (MENU_INTERMEDIATE, "Intermediate", []), (MENU_EXPERT, "Expert", []), (None, None, []), (MENU_PEACEFUL, "Peaceful", []), (None, None, []), (-1, "Cancel", [])]) copyright = [ name, "Copyright (C) Jack Whitham 2006-11 - website: www.jwhitham.org", None, "Game version " + config.CFG_VERSION ] # off we go. quit = False while (not quit): # Main menu screen.fill((0, 0, 0)) screen.blit(menu_image, (0, 0)) y = 5 sz = 11 for text in copyright: if (text == None): sz = 7 continue img = stats.Get_Font(sz).render(text, True, (200, 200, 128)) img_r = img.get_rect() img_r.center = ((width * 3) / 4, 0) img_r.clamp_ip(screen.get_rect()) img_r.top = y screen.blit(img, img_r.topleft) y += img_r.height (quit, cmd) = extra.Simple_Menu_Loop(screen, current_menu, ((width * 3) / 4, 10 + (height / 2))) if (current_menu == main_menu): if (cmd == MENU_NEW_GAME): current_menu = difficulty_menu elif (cmd == MENU_TUTORIAL): quit = game.Main_Loop(screen, clock, (width, height), None, MENU_TUTORIAL) elif (cmd == MENU_LOAD): current_menu = save_menu.Save_Menu(False) elif (cmd == MENU_QUIT): quit = True elif (cmd == MENU_MUTE): config.cfg.mute = not config.cfg.mute return False # update menu elif (cmd == MENU_RES): current_menu = resolution_menu elif (cmd == MENU_UPDATES): if Update_Feature(screen, menu_image): url = (CGISCRIPT + "v=" + startup.Get_Game_Version()) pygame.display.iconify() try: webbrowser.open(url, True, True) except: pass elif (cmd == MENU_MANUAL): pygame.display.iconify() if os.path.isfile(DEB_MANUAL): # Debian manual present url = 'file://' + DEB_MANUAL else: base = os.path.abspath( resource.Path( os.path.join("..", "manual", "index.html"))) if os.path.isfile(base): # Upstream package manual present url = 'file://' + base else: # No manual? Redirect to website. url = 'http://www.jwhitham.org/20kly/' try: webbrowser.open(url, True, True) except: pass elif (cmd != None): if (current_menu == resolution_menu): for (w, h, fs) in RESOLUTIONS: if (w == cmd): config.cfg.resolution = (w, h) config.cfg.font_scale = fs # change res - don't quit return False elif (current_menu == difficulty_menu): if (cmd >= 0): quit = game.Main_Loop(screen, clock, (width, height), None, cmd) else: # Load menu if (cmd >= 0): # Start game from saved position quit = game.Main_Loop(screen, clock, (width, height), cmd, None) current_menu = main_menu return True
if (no_sound): resource.No_Sound() else: pygame.mixer.init(22050, -16, 2, bufsize) clock = pygame.time.Clock() screen = pygame.display.set_mode(config.cfg.resolution, flags) height = screen.get_rect().height width = screen.get_rect().width # Icon # The icon provided in the Debian package is different than the original one # (size and location changed) if os.path.isfile(DEB_ICON): pygame.display.set_icon(resource.Load_Image(DEB_ICON)) else: pygame.display.set_icon(resource.Load_Image("32.png")) # Game begins.. show loading image screen.fill((0, 0, 0)) pygame.display.flip() pygame.display.set_caption(n) storms.Init_Storms() alien_invasion.Init_Aliens() quakes.Init_Quakes() quit = False while (not quit): if (config.cfg.resolution != (width, height)):
def Main_Menu_Loop(): # Update resolution (width, height) = screen.Update_Resolution() # Further initialisation menu_image = resource.Load_Image("mainmenu.jpg") menu_rect = menu_image.get_rect() sr = screen.surface.get_rect() if menu_rect.size != sr.size: menu_rect = menu_rect.fit(sr) menu_image = pygame.transform.scale(menu_image, menu_rect.size) font.Scale_Font(height) main_menu = current_menu = menu.Menu([ (None, None, []), #(MENU_TUTORIAL, "Play Tutorial", []), (MENU_NEW_GAME, "Play New Game", []), (MENU_LOAD, "Restore Game", []), (None, None, []), (MENU_MUTE, "Toggle Sound", []), (MENU_MANUAL, "View Manual", []), (MENU_UPDATES, "Check for Updates", []), (None, None, []), (MENU_QUIT, "Exit to " + extra.Get_OS(), [K_ESCAPE, K_F10]) ]) difficulty_menu = menu.Menu([ (None, None, []), #(MENU_TUTORIAL, "Tutorial", []), #(None, None, []), (MENU_BEGINNER, "Beginner", []), (MENU_INTERMEDIATE, "Intermediate", []), (MENU_EXPERT, "Expert", []), (None, None, []), (-1, "Cancel", []) ]) copyright = [ NAME, "Copyright (C) Jack Whitham 2006-11 - website: www.jwhitham.org", None, "Game version " + startup.Get_Game_Version() ] # off we go. while True: # Main menu loop screen.surface.fill(BLACK) screen.surface.blit(menu_image, menu_rect.topleft) y = 5 sz = 11 for text in copyright: if (text == None): sz = 7 continue img = font.Get_Font(sz).render(text, True, COPYRIGHT) img_r = img.get_rect() img_r.center = ((width * 3) / 4, 0) img_r.clamp_ip(screen.surface.get_rect()) img_r.top = y screen.surface.blit(img, img_r.topleft) y += img_r.height (quit, cmd) = extra.Simple_Menu_Loop(current_menu, ((width * 3) / 4, 10 + (height / 2))) if (quit): return MENU_QUIT if (cmd == MENU_RESIZE_EVENT): # Sent to outer loop, reinitialisation will be required return MENU_RESIZE_EVENT elif (current_menu == main_menu): if (cmd == MENU_NEW_GAME): current_menu = difficulty_menu elif (cmd == MENU_TUTORIAL): return MENU_TUTORIAL elif (cmd == MENU_LOAD): current_menu = save_menu.Save_Menu(False) elif (cmd == MENU_QUIT): return MENU_QUIT elif (cmd == MENU_MUTE): config.cfg.mute = not config.cfg.mute elif (cmd == MENU_UPDATES): if Update_Feature(screen, menu_image): url = (CGISCRIPT + "v=" + startup.Get_Game_Version()) pygame.display.iconify() try: webbrowser.open(url, True, True) except: pass elif (cmd == MENU_MANUAL): if os.path.isfile(DEB_MANUAL): # Debian manual present url = 'file://' + DEB_MANUAL else: base = os.path.abspath( resource.Path( os.path.join("..", "manual", "index.html"))) if os.path.isfile(base): # Upstream package manual present url = 'file://' + base else: # No manual? Redirect to website. url = 'http://www.jwhitham.org/20kly/' pygame.display.iconify() try: webbrowser.open(url, True, True) except: pass elif (cmd != None): if (current_menu == difficulty_menu): if (cmd >= 0): # Start game at specified difficulty return cmd else: # Load menu if (cmd >= 0): # Start game from saved position return cmd current_menu = main_menu
pygame.init() pygame.font.init() if (no_sound): resource.No_Sound() else: try: pygame.mixer.init(22050, -16, 2, bufsize) except pygame.error, message: no_sound = True screen.Initialise() # Icon & window title if os.path.isfile(DEB_ICON): icon = resource.Load_Image(DEB_ICON) else: icon = resource.Load_Image("lightyears.png") pygame.display.set_icon(icon) pygame.display.set_caption(NAME) # Game begins.. show loading image screen.surface.fill(BLACK) img = font.Get_Font(12).render("Starting...", True, GREY) r = img.get_rect() r.center = screen.surface.get_rect().center screen.surface.blit(img, r.topleft) pygame.display.flip()