def load_interpreter(keyEvent): #I really need to figure out a way to turn this code into its own function... global loadName playerInput = pygame.key.name(keyEvent.key) if keyEvent.key in NUMBER_KEYS: if len(saveFiles) < 10: num = int(playerInput) - 1 try: loadName = saveFiles[num] except IndexError: return confirm_load() return else: loadName += playerInput elif keyEvent.key == K_BACKSPACE: if loadName == '': returnTo() return else: loadName = loadName[:-1] elif keyEvent.key == K_RETURN: try: loadName = saveFiles[int(loadName)-1] except (ValueError, IndexError): return confirm_load() return universal.set_commands([''.join(['(#) Select a file to load:', loadName, '_']), '<==Back']) return universal.get_command_view()
def title_screen(episode=None): global firstEpisode, loadingGame textSurface = None titleImage = None try: titleImage = pygame.image.load(TITLE_IMAGES[0]).convert() titleImage = pygame.transform.scale(titleImage, (pygame.display.Info().current_w, pygame.display.Info().current_h)) except IOError: textSurface = textrect.render_textrect(get_title(), #+ (":" if get_subtitle() != "" else ""), universal.font, universal.worldView, LIGHT_GREY, DARK_GREY, 1) except IndexError: textSurface = textrect.render_textrect(get_title(), #+ (":" if get_subtitle() != "" else ""), universal.font, universal.worldView, LIGHT_GREY, DARK_GREY, 1) titleImages = [] if os.path.exists(os.path.join(os.getcwd(), 'save')) and '.init.sav' in os.listdir(os.path.join(os.getcwd(), 'save')): #townmode.clear_rooms() townmode.previousMode = None townmode.load_game('.init.sav', preserveLoadName=False) else: townmode.save_game('.init.sav', preserveSaveName=False) assert(episode is not None or firstEpisode is not None) if episode is not None: firstEpisode = episode screen = universal.get_screen() worldView = universal.get_world_view() background = universal.get_background() screen.fill(universal.DARK_GREY) font = pygame.font.SysFont(universal.FONT_LIST, 50) wvMidLeft = worldView.midleft if loadingGame: for i in range(1, len(TITLE_IMAGES)): try: titleImages.append(pygame.image.load(TITLE_IMAGES[i])) titleImages[-1] = pygame.transform.scale(titleImages[-1], (pygame.display.Info().current_w, pygame.display.Info().current_h)) except IOError: continue opening_crawl() loadingGame = False music.play_music(music.THEME) universal.set_commands(['(S)tart', '(L)oad', '(A)cknowledgments', '(Esc)Quit']) universal.set_command_interpreter(title_screen_interpreter) if not skip: pygame.time.delay(125) for i in range(0, len(titleImages)): screen.blit(titleImages[i], worldView.topleft) pygame.time.delay(25) pygame.display.flip() if titleImage is not None: screen.blit(titleImage, worldView.topleft) else: screen.blit(textSurface, worldView.centerleft) pygame.display.flip() while 1: universal.textToDisplay = '' universal.titleText = '' for event in pygame.event.get(): if event.type == KEYUP: return [universal.get_command_view()]
def save_interpreter(keyEvent): global saveName, saveNum, numLastInput, previousMode playerInput = pygame.key.name(keyEvent.key) if keyEvent.key == K_BACKSPACE: if saveNum == '' and saveName == '': if previousMode is not None: previousMode() else: town_mode() return else: saveName = saveName[:-1] elif keyEvent.key == K_RETURN: if numLastInput: try: saveName = saveFiles[int(saveNum)-1] except (IndexError, ValueError): return confirm_save(saveName) return elif keyEvent.key in NUMBER_KEYS and saveName == '': if len(saveFiles) < 10 and saveName == '': try: saveName = saveFiles[int(playerInput)-1] except IndexError: return confirm_save(saveName) return else: if len(saveNum) < MAX_SAVE_NAME_LENGTH: saveNum += playerInput numLastInput = True elif re.match(re.compile(r'^\w$'), playerInput): if len(saveName) < MAX_SAVE_NAME_LENGTH: if pygame.key.get_pressed()[K_LSHIFT] or pygame.key.get_pressed()[K_RSHIFT]: saveName += str.capitalize(playerInput) else: saveName += playerInput numLastInput = False universal.set_commands([''.join(['Provide a save name:', saveName, '_']), ''.join(['(#) Select a save file:', saveNum, '_']), '<==Back']) return universal.get_command_view()
def begin_game(episode): global displayedText global commands global screen global font import os if '.init.sav' in os.listdir('save'): os.remove(os.path.join('save', '.init.sav')) # Initialise screen #commandText = textrect.render_textrect(' '.join(commands), font, commandView, LIGHT_GREY, DARK_GREY, 1) #commandTextPos = commandText.get_rect() #commandTextPos.centerx, commandTextPos.centery = commandView.center; # Blit everything to the screen defaultFont = pygame.font.SysFont(universal.FONT_LIST, universal.DEFAULT_SIZE) titleFont = pygame.font.SysFont(universal.FONT_LIST_TITLE, universal.TITLE_SIZE) universal.init_game() background = universal.get_background() screen = universal.get_screen() screen.blit(background, (0, 0)) commandView = universal.get_command_view() worldView = universal.get_world_view() #commandSurface = pygame.Surface((commandView.width, commandView.height)) #commandSurface.fill((255, 0, 0)) #commandSurface.fill(DARK_GREY) #commandSurface.fill(LIGHT_GREY) #pygame.display.flip() leftCommandView = universal.get_left_command_view() rightCommandView = universal.get_right_command_view() middleCommandView = universal.get_middle_command_view() dirtyRects = titleScreen.title_screen(episode) #screen.blit(commandSurface, commandView.topleft) #screen.blit(background, (0,)) # Event loop #fps = 30 #clock = pygame.time.Clock() while True: #screen.blit(commandSurface, commandView.topleft) #pygame.draw.rect(commandSurface, LIGHT_GREY, commandView, 5) #Technically, this for loop is silly now, because I'm only allowing one event in #queue at a time. (Search for CLEAR) for event in pygame.event.get(): if event.type == KEYUP: if event.key == K_LSUPER or event.key == K_RSUPER: pygame.display.iconify() else: #CLEAR #This will have to be changed if I ever implement something that requires rapid key combinations. pygame.event.clear() newDirtyRects = universal.get_command_interpreter()(event) try: dirtyRects.extend(newDirtyRects) except TypeError: pass elif event.type == QUIT: music.close_music_files() import os if '.init.sav' in os.listdir('save'): os.remove(os.path.join('save', '.init.sav')) music.clean_up_music() return if universal.get_text_to_display() != '': if universal.get_title_text() != '': position = (worldView.topleft[0], worldView.topleft[1] + titleFont.get_linesize()) textRect = worldView.copy() textRect.height = textRect.height - titleFont.get_linesize() universal.display_text(universal.get_title_text(), worldView, position, isTitle=True) displayPosition = (worldView.topleft[0], worldView.topleft[1] + 2 * titleFont.get_linesize()) textRect = worldView.copy() dirtyRects.append(textRect) textRect.height = textRect.height - 2 * titleFont.get_linesize() universal.display_text(universal.get_text_to_display(), textRect, displayPosition, isTitle=False) universal.clear_text_to_display() #The following is a bit of command position fiddling to make everything look nice and balanced. universal.display_commands() pygame.draw.rect(screen, universal.LIGHT_GREY, pygame.Rect(commandView.topleft, commandView.size), universal.COMMAND_VIEW_LINE_WIDTH) #pygame.display.flip() #clock.tick_busy_loop(fps) newDirtyRects = [] #Bonemouth was having a strange error where the game would crash when trying to save, apparently because the game only takes one list of dirty rects? Not sure, and couldn't replicate it, #and of course the error message doesn't actually print the value of dirty rects :-/, so I can't figure out what dirty rects look like when the bug is thrown. So I'm assuming for some #strange reason I'm ending up with a nested list. So I flatten it first. Note that since every atomic element is a rectangle, and those aren't iterable, we don't need to worry about #accidentally flattening tuples or strings. for maybeList in dirtyRects: if isinstance(maybeList, pygame.Rect): newDirtyRects.append(maybeList) else: newDirtyRects.extend(maybeList) newDirtyRects.append(get_command_view()) #Wine doesn't play nice with the dirty rects approach, so if we are running under Wine, then we update everything forever. if universal.playOnMac: pygame.display.flip() elif dirtyRects: pygame.display.update(newDirtyRects) dirtyRects = []