Ejemplo n.º 1
0
    def wait_for_input(self):
        ''' Controls all user input and keeps menu displayed unless an exit has occured '''
        no_exit = True

        while no_exit:
            pc_input = pygwrap.wait_event()

            if pc_input.type == pygwrap.TIMEREVENT:
                # Redraw the menu on each timer event.
                self.render()
                pygame.display.flip()

            # Deal with mouse stuff here? needed if need to click on dev console.
            # probably true if we need to click on input section of dev console, but can
            # just always have this active. or may need if want to copy output

            # may need to check to see if we are actively in focus with the text-enter box,
            # if we ever allow it to not be in focus

            elif pc_input.type == pygame.KEYDOWN:
                if pc_input.key == pygame.K_UP:
                    # recall last command? here
                    print("Pressed up key")
                elif pc_input.key == pygame.K_RETURN:
                    # execute dev console command here
                    print("Pressed return key")
                elif pc_input.unicode == u"`":
                    # just exit menu
                    no_exit = False

                # may need to add mouse button up/down events here

                elif pc_input.type == pygame.QUIT:
                    no_exit = False
Ejemplo n.º 2
0
def choose_terrain( levelmap , screen ):
    # Instead of going back and forth, just pick a terrain from the sprite image.
    keep_going = True

    myrect = levelmap.sprite.bitmap.get_rect( center = ( screen.get_width() / 2 , screen.get_height() / 2 ) )
    terrain = -1

    while keep_going:
            ev = pygwrap.wait_event()

            if ev.type == pygwrap.TIMEREVENT:
                levelmap.render( screen , show_special = True )
                screen.blit( levelmap.sprite.bitmap , myrect )
                pygame.display.flip()

            elif ( ev.type == pygame.MOUSEBUTTONDOWN ) and myrect.collidepoint( ev.pos ):
                x,y = ev.pos
                x -= myrect.left
                y -= myrect.top

                terrain = x / levelmap.tile_size + ( y / levelmap.tile_size ) * 10

                keep_going = False

            elif ( ev.type == pygame.KEYDOWN ) and ( ev.key == pygame.K_ESCAPE ):
                keep_going = False

    return terrain
Ejemplo n.º 3
0
    def do_player_action(self, explo, chara):
        #Start by making a hotmap centered on PC, to see how far can move.
        hm = hotmaps.MoveMap(self.scene, chara)

        tacred = TacticsRedraw(chara, self, explo, hm)

        while self.can_act(chara) and self.still_fighting():
            # Get input and process it.
            gdi = pygwrap.wait_event()

            if gdi.type == pygwrap.TIMEREVENT:
                explo.view.overlays.clear()
                explo.view.overlays[chara.pos] = maps.OVERLAY_CURRENTCHARA
                explo.view.overlays[
                    explo.view.mouse_tile] = maps.OVERLAY_CURSOR

                tacred(explo.screen)
                pygame.display.flip()

            else:
                if gdi.type == pygame.KEYDOWN:
                    if gdi.unicode == u"Q":
                        self.camp.save(explo.screen)
                        self.no_quit = False
                    elif gdi.unicode == u"i":
                        explo.view_party(self.camp.party.index(chara),
                                         can_switch=False)
                        self.end_turn(chara)
                    elif gdi.unicode == u"c":
                        explo.view.focus(explo.screen, *chara.pos)
                    elif gdi.unicode == u" ":
                        self.end_turn(chara)
                elif gdi.type == pygame.MOUSEBUTTONUP:
                    if gdi.button == 1:
                        # Left mouse button.
                        if (explo.view.mouse_tile !=
                                chara.pos) and self.scene.on_the_map(
                                    *explo.view.mouse_tile):
                            tacred.hmap = None
                            target = explo.view.modelmap.get(
                                explo.view.mouse_tile, None)
                            if target and target.is_hostile(self.camp):
                                if chara.can_attack():
                                    self.move_to_attack(
                                        explo, chara, target, tacred)
                                else:
                                    explo.alert("You are out of ammunition!")
                            else:
                                self.move_player_to_spot(
                                    explo, chara, explo.view.mouse_tile,
                                    tacred)
                            tacred.hmap = hotmaps.MoveMap(self.scene, chara)
                    else:
                        self.pop_combat_menu(explo, chara)
Ejemplo n.º 4
0
    def play( self , pc , screen ):
        keep_playing = True
        while keep_playing:
            # Get some input...
            ev = pygwrap.wait_event()

            if ev.type == pygwrap.TIMEREVENT:
                self.update()

                set_offset( self , pc )

                self.render( screen )
                pc.render_health( screen )
                pygame.display.flip()

                if pc.health < 1:
                    if pc.is_really_dead(self):
                        keep_playing = False

            elif ev.type == pygame.KEYDOWN:
                if ev.key == pygame.K_UP:
                    pc.move_up = True
                elif ev.key == pygame.K_DOWN:
                    pc.move_down = True
                elif ev.key == pygame.K_LEFT:
                    pc.move_left = True
                elif ev.key == pygame.K_RIGHT:
                    pc.move_right = True
                elif ev.key == pygame.K_SPACE:
                    pc.fire_button = True
                elif ev.key == pygame.K_ESCAPE:
                    keep_playing = False

            elif ev.type == pygame.KEYUP:
                if ev.key == pygame.K_UP:
                    pc.move_up = False
                elif ev.key == pygame.K_DOWN:
                    pc.move_down = False
                elif ev.key == pygame.K_LEFT:
                    pc.move_left = False
                elif ev.key == pygame.K_RIGHT:
                    pc.move_right = False
                elif ev.key == pygame.K_SPACE:
                    pc.fire_button = False

            elif ev.type == pygame.QUIT:
                keep_playing = False
Ejemplo n.º 5
0
    def do_player_action( self, explo, chara ):
        #Start by making a hotmap centered on PC, to see how far can move.
        hm = hotmaps.MoveMap( self.scene, chara )

        tacred = TacticsRedraw( chara, self, explo, hm )

        while self.can_act( chara ) and self.still_fighting():
            # Get input and process it.
            gdi = pygwrap.wait_event()

            if gdi.type == pygwrap.TIMEREVENT:
                explo.view.overlays.clear()
                explo.view.overlays[ chara.pos ] = maps.OVERLAY_CURRENTCHARA
                explo.view.overlays[ explo.view.mouse_tile ] = maps.OVERLAY_CURSOR

                tacred( explo.screen )
                pygame.display.flip()

            else:
                if gdi.type == pygame.KEYDOWN:
                    if gdi.unicode == u"Q":
                        self.camp.save(explo.screen)
                        self.no_quit = False
                    elif gdi.unicode == u"i":
                        explo.view_party( self.camp.party.index(chara), can_switch=False )
                        self.end_turn( chara )
                    elif gdi.unicode == u"c":
                        explo.view.focus( explo.screen, *chara.pos )
                    elif gdi.unicode == u" ":
                        self.end_turn( chara )
                elif gdi.type == pygame.MOUSEBUTTONUP:
                    if gdi.button == 1:
                        # Left mouse button.
                        if ( explo.view.mouse_tile != chara.pos ) and self.scene.on_the_map( *explo.view.mouse_tile ):
                            tacred.hmap = None
                            target = explo.view.modelmap.get( explo.view.mouse_tile, None )
                            if target and target.is_hostile( self.camp ):
                                if chara.can_attack():
                                    self.move_to_attack( explo, chara, target, tacred )
                                else:
                                    explo.alert( "You are out of ammunition!" )
                            else:
                                self.move_player_to_spot( explo, chara, explo.view.mouse_tile, tacred )
                            tacred.hmap = hotmaps.MoveMap( self.scene, chara )
                    else:
                        self.pop_combat_menu( explo, chara )
Ejemplo n.º 6
0
    def select_area( self, origin, aoegen, caption = None ):
        # Start by determining the possible target tiles.
        legal_tiles = aoegen.get_targets( self.camp, origin )
        target = None
        aoe = set()

        # Keep processing until a target is selected.
        while not target:
            # Get input and process it.
            gdi = pygwrap.wait_event()

            if gdi.type == pygwrap.TIMEREVENT:
                # Set the mouse cursor on the map.
                self.view.overlays.clear()
                self.view.overlays[ origin ] = maps.OVERLAY_CURRENTCHARA
                self.view.overlays[ self.view.mouse_tile ] = maps.OVERLAY_CURSOR
                if self.view.mouse_tile in legal_tiles:
                    aoe = aoegen.get_area( self.camp, origin, self.view.mouse_tile )
                    for p in aoe:
                        self.view.overlays[ p ] = maps.OVERLAY_AOE

                self.view( self.screen )
                if caption:
                    pygwrap.default_border.render( self.screen, self.SELECT_AREA_CAPTION_ZONE )
                    pygwrap.draw_text( self.screen, pygwrap.SMALLFONT, caption, self.SELECT_AREA_CAPTION_ZONE )

                pygame.display.flip()
            elif gdi.type == pygame.KEYDOWN and gdi.key == pygame.K_F1:
                caption = "Record Anim"
                self.record_anim = True
            elif gdi.type == pygame.QUIT:
                self.no_quit = False
                break
            elif gdi.type == pygame.MOUSEBUTTONUP:
                if gdi.button == 1 and self.view.mouse_tile in legal_tiles:
                    target = self.view.mouse_tile
                else:
                    break
        self.view.overlays.clear()
        return target
Ejemplo n.º 7
0
    def select_area( self, origin, aoegen, caption = None ):
        # Start by determining the possible target tiles.
        legal_tiles = aoegen.get_targets( self.camp, origin )
        target = None
        aoe = set()

        # Keep processing until a target is selected.
        while not target:
            # Get input and process it.
            gdi = pygwrap.wait_event()

            if gdi.type == pygwrap.TIMEREVENT:
                # Set the mouse cursor on the map.
                self.view.overlays.clear()
                self.view.overlays[ origin ] = maps.OVERLAY_CURRENTCHARA
                self.view.overlays[ self.view.mouse_tile ] = maps.OVERLAY_CURSOR
                if self.view.mouse_tile in legal_tiles:
                    aoe = aoegen.get_area( self.camp, origin, self.view.mouse_tile )
                    for p in aoe:
                        self.view.overlays[ p ] = maps.OVERLAY_AOE

                self.view( self.screen )
                if caption:
                    pygwrap.default_border.render( self.screen, self.SELECT_AREA_CAPTION_ZONE )
                    pygwrap.draw_text( self.screen, pygwrap.SMALLFONT, caption, self.SELECT_AREA_CAPTION_ZONE )

                pygame.display.flip()
            elif gdi.type == pygame.KEYDOWN and gdi.key == pygame.K_F1:
                caption = "Record Anim"
                self.record_anim = True
            elif gdi.type == pygame.QUIT:
                self.no_quit = False
                break
            elif gdi.type == pygame.MOUSEBUTTONUP:
                if gdi.button == 1 and self.view.mouse_tile in legal_tiles:
                    target = self.view.mouse_tile
                else:
                    break
        self.view.overlays.clear()
        return target
Ejemplo n.º 8
0
    def go( self ):
        self.no_quit = True
        self.order = None
        if self.scene.name:
            caption = pygwrap.SMALLFONT.render(self.scene.name, True, pygwrap.TEXT_COLOR )
            caption_rect = caption.get_rect(topleft=(32,32))
            caption_timer = 35
        else:
            caption = None

        self.update_scene()

        # Do one view first, just to prep the model map and mouse tile.
        self.view( self.screen )
        pygame.display.flip()

        # Do a start trigger, unless we're in combat.
        if not self.camp.fight:
            self.check_trigger( "START" )

        while self.keep_exploring():
            first_pc_pos=self.camp.first_living_pc().pos
            if self.camp.fight:
                self.order = None
                self.camp.fight.go( self )
                if pygwrap.GOT_QUIT or not self.camp.fight.no_quit:
                    self.no_quit = False
                    break
                self.camp.fight = None

            # Get input and process it.
            gdi = pygwrap.wait_event()

            if not self.keep_exploring():
                pass
            elif gdi.type == pygwrap.TIMEREVENT:
                self.view( self.screen, first_pc_pos=first_pc_pos )
                if caption and caption_timer > 0:
                    pygwrap.default_border.render( self.screen, caption_rect )
                    self.screen.blit( caption, caption_rect )
                    caption_timer += -1
                    if caption_timer < 1:
                        caption = None
                pygame.display.flip()

                self.time += 1

                if self.order:
                    if not self.order( self ):
                        self.order = None

                self.update_monsters()

                if self.time % 150 == 0:
                    self.update_enchantments()

            elif not self.order:
                # Set the mouse cursor on the map.
                self.view.overlays.clear()
                self.view.overlays[ self.view.mouse_tile ] = maps.OVERLAY_CURSOR

                if gdi.type == pygame.KEYDOWN:
                    if gdi.unicode == u"1":
                        self.view_party(0)
                    elif gdi.unicode == u"2":
                        self.view_party(1)
                    elif gdi.unicode == u"3":
                        self.view_party(2)
                    elif gdi.unicode == u"4":
                        self.view_party(3)
                    elif gdi.unicode == u"Q":
                        self.camp.save(self.screen)
                        self.no_quit = False
                    elif gdi.unicode == u"c":
                        self.view.focus( self.screen, *self.camp.first_living_pc().pos )
                    elif gdi.unicode == u"m":
                        self.cast_explo_spell(0)
                    elif gdi.unicode == u"M":
                        MiniMap( self )
                    elif gdi.unicode == u"R":
                        self.field_camp()
                    elif gdi.unicode == u"s":
                        services.SpellManager()(self)
                    elif gdi.unicode == u"h" or gdi.unicode == u"?" or gdi.key == pygame.K_F1:
                        self.alert("HELP\n ==== \n 1-4 View party member\n Q Quit and save\n c Center view\n M View map\n R Rest\n s Manage spells",False)

                    elif gdi.unicode == u"*":
                        for pc in self.camp.party:
                            pc.xp += 1000
                    elif gdi.unicode == u"&":
                        for x in range( self.scene.width ):
                            for y in range( self.scene.height ):
                                self.scene.map[x][y].visible = True
                    elif gdi.unicode == u"_":
                        self.camp.known_spells = spells.SPELL_LIST[:]
                    elif gdi.unicode == u"!":
                        self.flatten_world()

                elif gdi.type == pygame.QUIT:
                    self.camp.save(self.screen)
                    self.no_quit = False
                elif gdi.type == pygame.MOUSEBUTTONUP:
                    if gdi.button == 1:
                        # Left mouse button.
                        if ( self.view.mouse_tile != self.camp.first_living_pc().pos ) and self.scene.on_the_map( *self.view.mouse_tile ):
                            self.order = MoveTo( self, self.view.mouse_tile )
                            self.view.overlays.clear()
                        else:
                            self.pick_up( self.view.mouse_tile )
                    else:
                        self.pop_explo_menu()
Ejemplo n.º 9
0
    def query(self):
        # A return of False means selection was cancelled. 
        if not self.items :
            return False
        elif self.selected_item >= len( self.items ):
            self.selected_item = 0
        no_choice_made = True
        choice = False

        menu_height = self.menu_height()

        mouse_button_down = False
        first_mouse_selection = None
        first_mouse_y = 0
        current_mouse_selection = None
 
        while no_choice_made:
            pc_input = pygwrap.wait_event()

            if pc_input.type == pygwrap.TIMEREVENT:
                # Redraw the menu on each timer event.
                self.render()
                pygame.display.flip()

                # Also deal with mouse stuff then...
                if mouse_button_down:
                    pos = pygame.mouse.get_pos()
                    dy = pos[1] - first_mouse_y

                    if dy > 10 and self.top_item > 0:
                        self.top_item += -1
                        first_mouse_selection = None
                    elif dy < -10 and self.top_item < len( self.items ) - menu_height:
                        self.top_item += 1
                        first_mouse_selection = None

                    current_mouse_selection = self.get_mouseover_item( pos )
                    if current_mouse_selection != None:
                        self.selected_item = current_mouse_selection

            elif pc_input.type == pygame.KEYDOWN:
                # A key was pressed, oh happy day! See what key it was and act
                # accordingly.
                if pc_input.key == pygame.K_UP:
                    self.selected_item -= 1
                    if self.selected_item < 0:
                        self.selected_item = len( self.items ) - 1
                    if ( self.selected_item < self.top_item ) or ( self.selected_item >= self.top_item + menu_height ):
                        self.top_item = self.selected_item
                elif pc_input.key == pygame.K_DOWN:
                    self.selected_item += 1
                    if self.selected_item >= len( self.items ):
                        self.selected_item = 0
                    if ( self.selected_item < self.top_item ) or ( self.selected_item >= self.top_item + menu_height ):
                        self.top_item = self.selected_item
                elif pc_input.key == pygame.K_SPACE or pc_input.key == pygame.K_RETURN:
                    choice = self.items[ self.selected_item ].value
                    no_choice_made = False
                elif ( pc_input.key == pygame.K_ESCAPE or pc_input.key == pygame.K_BACKSPACE ) and self.can_cancel:
                    no_choice_made = False
                elif pc_input.key >= 0 and pc_input.key < 256 and chr( pc_input.key ) in self.quick_keys:
                    choice = self.quick_keys[ chr(pc_input.key) ]
                    no_choice_made = False
                elif pc_input.key > 255 and pc_input.key in self.quick_keys:
                    choice = self.quick_keys[ pc_input.key ]
                    no_choice_made = False

            elif pc_input.type == pygame.MOUSEBUTTONDOWN and not mouse_button_down:
                # Mouse down does nothing but set the first mouse selection, and a
                # counter telling that the button is down.
                first_mouse_selection = self.get_mouseover_item( pc_input.pos )
                first_mouse_y = pc_input.pos[1]
                if first_mouse_selection != None:
                    self.selected_item = first_mouse_selection
                mouse_button_down = True
            elif pc_input.type == pygame.MOUSEBUTTONUP:
                # Mouse button up makes a selection, as long as your finger is still
                # on the first item selected.
                mouse_button_down = False

                current_mouse_selection = self.get_mouseover_item( pc_input.pos )
                if current_mouse_selection == first_mouse_selection and first_mouse_selection != None:
                    self.selected_item = current_mouse_selection
                    choice = self.items[ current_mouse_selection ].value
                    no_choice_made = False

            elif pc_input.type == pygame.QUIT:
                no_choice_made = False


        return( choice )
Ejemplo n.º 10
0
    def go( self ):
        self.no_quit = True
        self.order = None
        if self.scene.name:
            caption = pygwrap.SMALLFONT.render(self.scene.name, True, pygwrap.TEXT_COLOR )
            caption_rect = caption.get_rect(topleft=(32,32))
            caption_timer = 35
        else:
            caption = None

        self.update_scene()

        # Do one view first, just to prep the model map and mouse tile.
        self.view( self.screen )
        pygame.display.flip()

        #import enchantments
        #self.camp.party[0].condition.append( enchantments.CurseEn() )

        # Do a start trigger, unless we're in combat.
        if not self.camp.fight:
            self.check_trigger( "START" )

        while self.keep_exploring():
            first_pc_pos=self.camp.first_living_pc().pos
            if self.camp.fight:
                self.order = None
                self.camp.fight.go( self )
                if pygwrap.GOT_QUIT or not self.camp.fight.no_quit:
                    self.no_quit = False
                    break
                self.camp.fight = None

            # Get input and process it.
            gdi = pygwrap.wait_event()

            if gdi.type == pygwrap.TIMEREVENT:
                self.view( self.screen, first_pc_pos=first_pc_pos )
                if caption and caption_timer > 0:
                    pygwrap.default_border.render( self.screen, caption_rect )
                    self.screen.blit( caption, caption_rect )
                    caption_timer += -1
                    if caption_timer < 1:
                        caption = None
                pygame.display.flip()

                self.time += 1

                if self.order:
                    if not self.order( self ):
                        self.order = None

                self.update_monsters()

                if self.time % 150 == 0:
                    self.update_enchantments()

            elif not self.order:
                # Set the mouse cursor on the map.
                self.view.overlays.clear()
                self.view.overlays[ self.view.mouse_tile ] = maps.OVERLAY_CURSOR

                if gdi.type == pygame.KEYDOWN:
                    if gdi.unicode == u"1":
                        self.view_party(0)
                    elif gdi.unicode == u"2":
                        self.view_party(1)
                    elif gdi.unicode == u"3":
                        self.view_party(2)
                    elif gdi.unicode == u"4":
                        self.view_party(3)
                    elif gdi.unicode == u"Q":
                        self.camp.save(self.screen)
                        self.no_quit = False
                    elif gdi.unicode == u"c":
                        self.view.focus( self.screen, *self.camp.first_living_pc().pos )
                    elif gdi.unicode == u"m":
                        self.cast_explo_spell(0)
                    elif gdi.unicode == u"M":
                        MiniMap( self )
                    elif gdi.unicode == u"R":
                        self.field_camp()
                    elif gdi.unicode == u"s":
                        services.SpellManager()(self)
                    elif gdi.unicode == u"*":
                        for pc in self.camp.party:
                            pc.xp += 1000
                    elif gdi.unicode == u"&":
                        for x in range( self.scene.width ):
                            for y in range( self.scene.height ):
                                self.scene.map[x][y].visible = True

                elif gdi.type == pygame.QUIT:
                    self.camp.save(self.screen)
                    self.no_quit = False
                elif gdi.type == pygame.MOUSEBUTTONUP:
                    if gdi.button == 1:
                        # Left mouse button.
                        if ( self.view.mouse_tile != self.camp.first_living_pc().pos ) and self.scene.on_the_map( *self.view.mouse_tile ):
                            self.order = MoveTo( self, self.view.mouse_tile )
                            self.view.overlays.clear()
                        else:
                            self.pick_up( self.view.mouse_tile )
                    else:
                        self.pop_explo_menu()
Ejemplo n.º 11
0
def edit_map( levelmap , screen ):
    # Edit this map in place.
    edit_cursor = image.Image( "edit_cursors.png" , 32 , 32 )

    curs_x = 0
    curs_y = 0

    terrain = -1

    keep_going = True

    while keep_going:
            ev = pygwrap.wait_event()

            if ev.type == pygwrap.TIMEREVENT:
                levelmap.center_on( curs_x * levelmap.tile_size + 16 , curs_y * levelmap.tile_size + 16 )
                levelmap.render( screen , show_special = True )
                edit_cursor.render( screen , ( screen.get_width() / 2 - 16 , screen.get_height() / 2 - 16 ) , 0 )
                pygwrap.draw_text( screen , pygwrap.SMALLFONT , str( curs_x ) + "," + str( curs_y ) + '  things:' + str( len( levelmap.contents ) ) , pygame.Rect( 8 , 42 , 256 , 32 ) )

                if terrain > -1:
                    levelmap.sprite.render( screen , ( 8 , 8 ) , terrain )
                edit_cursor.render( screen , ( 8 , 8 ) , 1 )

                pygame.display.flip()

            elif ev.type == pygame.KEYDOWN:
                if ( ev.key == pygame.K_UP ) and ( curs_y > 0 ):
                    curs_y += -1
                elif ( ev.key == pygame.K_DOWN ) and ( curs_y < ( levelmap.height - 1 ) ):
                    curs_y += 1
                elif ( ev.key == pygame.K_LEFT ) and ( curs_x > 0 ):
                    curs_x += -1
                elif ( ev.key == pygame.K_RIGHT ) and ( curs_x < ( levelmap.width - 1 ) ):
                    curs_x += 1
                elif ev.key == pygame.K_LEFTBRACKET:
                    terrain += -1
                    if terrain < -1:
                        terrain = levelmap.sprite.num_frames() - 1
                elif ev.key == pygame.K_RIGHTBRACKET:
                    terrain += 1
                    if terrain >= levelmap.sprite.num_frames():
                        terrain = -1
                elif ev.key == pygame.K_SPACE:
                    levelmap.map[curs_x][curs_y] = terrain
                    if terrain in TERRAIN_NEXT:
                        terrain = TERRAIN_NEXT[ terrain ]

                elif ev.unicode == u"_":
                    for x in range( 0 , levelmap.width ):
                        levelmap.map[x][curs_y] = terrain
                        if terrain in TERRAIN_NEXT:
                            terrain = TERRAIN_NEXT[ terrain ]

                elif ev.key == pygame.K_DELETE:
                    # If there's a thing in this tile, delete it.
                    # If not, delete the map terrain.
                    deleted_thing = False
                    for t in levelmap.contents:
                        if ( t.x == curs_x * levelmap.tile_size ) and ( t.y == curs_y * levelmap.tile_size ):
                            levelmap.contents.remove( t )
                            deleted_thing = True
                    if not deleted_thing:
                        levelmap.map[curs_x][curs_y] = -1
                elif ev.key == pygame.K_b:
                    select_backdrop( levelmap , screen )
                elif ev.key == pygame.K_p:
                    levelmap.pc_start_x = curs_x
                    levelmap.pc_start_y = curs_y

                elif ev.key == pygame.K_F1:
                    lm2 = copy.deepcopy( levelmap )
                    pc = player.Player()
                    lm2.enter( pc , screen )

                elif ev.key == pygame.K_TAB:
                    terrain = choose_terrain( levelmap , screen )

                elif ev.key == pygame.K_m:
                    monster = select_monster( levelmap , screen )
                    if monster:
                        levelmap.contents.append( monster( x = curs_x * levelmap.tile_size , y = curs_y * levelmap.tile_size ) )

                elif ev.key == pygame.K_i:
                    item = select_item( levelmap , screen )
                    if item:
                        levelmap.contents.append( item( x = curs_x * levelmap.tile_size , y = curs_y * levelmap.tile_size ) )

                elif ev.key == pygame.K_u:
                    item = select_usable( levelmap , screen )
                    if item:
                        levelmap.contents.append( item( x = curs_x * levelmap.tile_size , y = curs_y * levelmap.tile_size ) )

                elif ev.key == pygame.K_g:
                    monster = select_monster( levelmap , screen , prompt = "Select Monster to Generate" )
                    n = select_number( levelmap , screen , 1, 11, 1, prompt = "How many at once?" )
                    freq = select_number( levelmap , screen , 30, 301 , 30, prompt = "How often to generate?" )
                    if monster and n:
                        levelmap.contents.append( generator.Generator(x = curs_x * levelmap.tile_size , y = curs_y * levelmap.tile_size, product=monster, number = n , frequency = freq ) )

                elif ev.key == pygame.K_s:
                    if levelmap.fname == "":
                        levelmap.fname = "gorch.dat"
                    f = open( "level/" + levelmap.fname , "wb" )
                    pickle.dump( levelmap , f , -1 )
                    f.close()

                elif ev.key == pygame.K_ESCAPE:
                    keep_going = False

            elif ev.type == pygame.QUIT:
                keep_going = False
Ejemplo n.º 12
0
    def query(self):
        # A return of False means selection was cancelled. 
        if not self.items :
            return False
        elif self.selected_item >= len( self.items ):
            self.selected_item = 0
        no_choice_made = True
        choice = False

        menu_height = self.menu_height()

        mouse_button_down = False
        first_mouse_selection = None
        first_mouse_y = 0
        current_mouse_selection = None
 
        while no_choice_made:
            pc_input = pygwrap.wait_event()

            if pc_input.type == pygwrap.TIMEREVENT:
                # Redraw the menu on each timer event.
                self.render()
                pygame.display.flip()

                # Also deal with mouse stuff then...
                if mouse_button_down:
                    pos = pygame.mouse.get_pos()
                    dy = pos[1] - first_mouse_y

                    if dy > 10 and self.top_item > 0:
                        self.top_item += -1
                        first_mouse_selection = None
                    elif dy < -10 and self.top_item < len( self.items ) - menu_height:
                        self.top_item += 1
                        first_mouse_selection = None

                    current_mouse_selection = self.get_mouseover_item( pos )
                    if current_mouse_selection != None:
                        self.selected_item = current_mouse_selection

            elif pc_input.type == pygame.KEYDOWN:
                # A key was pressed, oh happy day! See what key it was and act
                # accordingly.
                if pc_input.key == pygame.K_UP:
                    self.selected_item -= 1
                    if self.selected_item < 0:
                        self.selected_item = len( self.items ) - 1
                    if ( self.selected_item < self.top_item ) or ( self.selected_item >= self.top_item + menu_height ):
                        self.top_item = self.selected_item
                elif pc_input.key == pygame.K_DOWN:
                    self.selected_item += 1
                    if self.selected_item >= len( self.items ):
                        self.selected_item = 0
                    if ( self.selected_item < self.top_item ) or ( self.selected_item >= self.top_item + menu_height ):
                        self.top_item = self.selected_item
                elif pc_input.key == pygame.K_SPACE or pc_input.key == pygame.K_RETURN:
                    choice = self.items[ self.selected_item ].value
                    no_choice_made = False
                elif ( pc_input.key == pygame.K_ESCAPE or pc_input.key == pygame.K_BACKSPACE ) and self.can_cancel:
                    no_choice_made = False
                elif pc_input.key >= 0 and pc_input.key < 256 and chr( pc_input.key ) in self.quick_keys:
                    choice = self.quick_keys[ chr(pc_input.key) ]
                    no_choice_made = False
                elif pc_input.key > 255 and pc_input.key in self.quick_keys:
                    choice = self.quick_keys[ pc_input.key ]
                    no_choice_made = False
            elif pc_input.type == pygame.MOUSEBUTTONDOWN and not mouse_button_down:
                # Mouse down does nothing but set the first mouse selection, and a
                # counter telling that the button is down.
                if pc_input.button == 1:
                    first_mouse_selection = self.get_mouseover_item( pc_input.pos )
                    first_mouse_y = pc_input.pos[1]
                    if first_mouse_selection != None:
                        self.selected_item = first_mouse_selection
                    mouse_button_down = True
            elif pc_input.type == pygame.MOUSEBUTTONUP:
                # Mouse button up makes a selection, as long as your finger is still
                # on the first item selected.
                mouse_button_down = False

                current_mouse_selection = self.get_mouseover_item( pc_input.pos )
                if current_mouse_selection == first_mouse_selection and first_mouse_selection != None:
                    self.selected_item = current_mouse_selection
                    choice = self.items[ current_mouse_selection ].value
                    no_choice_made = False

            elif pc_input.type == pygame.QUIT:
                no_choice_made = False


        return( choice )