示例#1
0
文件: city.py 项目: btdevel/bt
    def __init__(self, map, location=""):
        EventHandler.__init__(self, location=location)
        self.map = map
        self.pos = Vector([0, 0])
        self.dir = Direction()

        self.show_pos = app.config.debug.show_pos(default=False, type=bool)

        self.add_key_event((pygame.K_UP, 0), self.forward)
        self.add_key_event((pygame.K_DOWN, 0), self.reverse)
        self.add_key_event((pygame.K_LEFT, 0), self.turn_left)
        self.add_key_event((pygame.K_RIGHT, 0), self.turn_right)
        self.add_key_event("?", self.print_location)
示例#2
0
文件: city.py 项目: btdevel/bt
class CityHandler(EventHandler):
    def __init__(self, map, location=""):
        EventHandler.__init__(self, location=location)
        self.map = map
        self.pos = Vector([0, 0])
        self.dir = Direction()

        self.show_pos = app.config.debug.show_pos(default=False, type=bool)

        self.add_key_event((pygame.K_UP, 0), self.forward)
        self.add_key_event((pygame.K_DOWN, 0), self.reverse)
        self.add_key_event((pygame.K_LEFT, 0), self.turn_left)
        self.add_key_event((pygame.K_RIGHT, 0), self.turn_right)
        self.add_key_event("?", self.print_location)

    def isbuildingat(self, forw, left=0):
        cell = self.map[self.pos +
                        forw * self.dir.forward_vec +
                        left * self.dir.left_vec]
        if cell is not None and cell.is_building():
            return cell
        return None


    def blit_building_at(self, state, forw, left=0):
        cell = self.isbuildingat(forw, left)
        if cell is None:
            return
        if forw == 1 and left == 0 and cell.front is not None:
            filename = cell.front
        else:
            base = 'FLR'[left] + str(forw - (left == 0))
            filename = os.path.join(cell.type, base + '.png')
        state.ui.blit_image(filename)

    def redraw(self, state):
        with state.ui.world_view.noupdate():
            surf = state.ui.world_view.get_surf()
            if self.isbuildingat(1):
                pygame.draw.rect(surf, pygame.Color(0, 0, 119),
                                 pygame.Rect(0, 0, 224, 92 + 84))
            else:
                pygame.draw.rect(surf, pygame.Color(0, 0, 119),
                                 pygame.Rect(0, 0, 224, 92))
                pygame.draw.rect(surf, pygame.Color(204, 119, 85),
                                 pygame.Rect(0, 0 + 92, 224, 84))

            # naming for building images with location relative to xx  
            #    F2
            # L2 F1 R2
            # L1 F0 R1
            # L0 xx R0
            if self.isbuildingat(1):
                self.blit_building_at(state, 1)
            else:
                if self.isbuildingat(2):
                    self.blit_building_at(state, 2)
                else:
                    self.blit_building_at(state, 3)
                    self.blit_building_at(state, 2, 1)
                    self.blit_building_at(state, 2, -1)

                self.blit_building_at(state, 1, 1)
                self.blit_building_at(state, 1, -1)
                self.blit_building_at(state, 0, 1)
                self.blit_building_at(state, 0, -1)


    def set_position(self, pos):
        self.pos = Vector(pos)

    def set_direction(self, dir):
        if not isinstance(dir, Direction):
            dir = Direction(dir)
        self.dir = dir


    def forward(self, state):
        with state.ui.message_view.noupdate():
            cell = self.map[self.pos + self.dir.forward_vec]
            state.ui.clear_message()

            if not cell.is_building():
                self.pos = self.pos + self.dir.forward_vec
                self.redraw(state)
                if self.show_pos:
                    self.print_location(state)
        if cell.action:
            cell.action(state)

    def reverse(self, state):
        self.dir.reverse()
        self.redraw(state)
        state.ui.clear_message()

    def turn_left(self, state):
        self.dir.left()
        self.redraw(state)
        state.ui.clear_message()

    def turn_right(self, state):
        self.dir.right()
        self.redraw(state)
        state.ui.clear_message()

    def print_location(self, state):
        with state.ui.message_view.noupdate() as msg:
            msg.clear()
            msg.message("You are on %s facing %s." % (self.map[self.pos].name, str(self.dir)))
            msg.message("\n\nYou are on: %dE, %dN" % tuple(self.pos))