示例#1
0
class Editor(State):
    def __init__(self, resourcemgr, level=None):
        super(Editor, self).__init__(resourcemgr)
        self.tilemgr = TileManager(resourcemgr, level)
        self.lw = self.tilemgr.lw
        self.lh = self.tilemgr.lh
        self.camera = Camera(0, 0, self.lw, self.lh, resourcemgr)
        self.current_tile = 0
        self.font = SysFont(FONTS, int(48 * self.resourcemgr.scale), True)

    def set_tile(self):
        x, y = mouse.get_pos()
        x += self.camera.rect.x
        y += self.camera.rect.y

        for t in self.tilemgr.tiles:
            if t.rect.collidepoint(x, y):
                t.set_tile(self.current_tile)
                break

    def get_tile(self):
        x, y = mouse.get_pos()
        x += self.camera.rect.x
        y += self.camera.rect.y

        for t in self.tilemgr.tiles:
            if t.rect.collidepoint(x, y):
                self.current_tile = t.kind
                break

    def render_kind(self):
        text = "Current Tile: %s" % self.tilemgr.tile_labels[self.current_tile]
        msg = self.font.render(text, 1, WHITE)
        msg_rect = msg.get_rect(
            centerx=self.resourcemgr.screen.get_rect().w / 2, centery=self.resourcemgr.screen.get_rect().h / 6
        )
        self.resourcemgr.screen.blit(msg, msg_rect)

    def input(self, event, next_state):
        next_state = super(Editor, self).input(event, next_state)

        self.camera.input(event)

        if event.type == KEYDOWN:
            if event.key == K_ESCAPE:
                self.tilemgr.save_tiles("map1.map")
                next_state = set_next_state(next_state, STATES.MENU)
        elif event.type == MOUSEBUTTONDOWN:
            if event.button == MOUSE.LEFT:
                self.set_tile()
            elif event.button == MOUSE.RIGHT:
                self.get_tile()
            elif event.button == MOUSE.WHEELUP:
                self.current_tile -= 1
                if self.current_tile < 0:
                    self.current_tile = self.tilemgr.total_tile_kinds - 1
            elif event.button == MOUSE.WHEELDOWN:
                self.current_tile += 1
                if self.current_tile > self.tilemgr.total_tile_kinds - 1:
                    self.current_tile = 0
        elif event.type == MOUSEMOTION and mouse.get_pressed()[0]:
            self.set_tile()

        return next_state

    def logic(self, clock, next_state):
        self.camera.logic(clock.get_time())
        return next_state

    def render(self):
        self.resourcemgr.screen.fill(BLACK)
        self.tilemgr.render(self.camera)
        self.render_kind()
示例#2
0
class InGame(State):
    def __init__(self, resourcemgr, team, level):
        super(InGame, self).__init__(resourcemgr)
        self.tilemgr = TileManager(resourcemgr, level)
        self.unitmgr = UnitManager(resourcemgr, STATES.INGAME, team, level)
        self.lw = self.tilemgr.lw
        self.lh = self.tilemgr.lh
        self.team = team
        # self.bar = Bar(config)
        music.load(os.path.join(resourcemgr.main_path, 'sound/yyz.ogg'))
        self.camera = Camera(0, 0, self.lw, self.lh, resourcemgr)
        font = SysFont(FONTS, int(48 * resourcemgr.scale), True)
        self.win_msg = font.render('YOU WINNN!!! GGGG GGGGGGGGGGGGGGG', 1,
            WHITE)
        self.win_msg_rect = self.win_msg.get_rect(
            centerx=self.camera.rect.w / 2, centery=self.camera.rect.h / 4)
        self.lose_msg = font.render('You lost. What the f**k, man?', 1,
            BLACK)
        self.lose_msg_rect = self.win_msg.get_rect(
            centerx=self.camera.rect.w / 2, centery=self.camera.rect.h / 4)
        self.mouse_rect = Rect(0, 0, 0, 0)
        self.x_first = 0
        self.y_first = 0
        self.won = False
        self.lost = False
        self.gg = load_sound(resourcemgr.main_path, 'gg.ogg')
        self.lose = load_sound(resourcemgr.main_path, 'lose.ogg')

    def input_mouse(self, event):
        x, y = mouse.get_pos()
        x += self.camera.rect.x
        y += self.camera.rect.y

        if event.type == MOUSEMOTION and mouse.get_pressed()[0]:
            self.mouse_rect.x = min(self.x_first, x)
            self.mouse_rect.w = abs(self.x_first - x)
            self.mouse_rect.y = min(self.y_first, y)
            self.mouse_rect.h = abs(self.y_first - y)

        if event.type == MOUSEBUTTONDOWN and event.button == MOUSE.LEFT:
            self.mouse_rect.x = x
            self.mouse_rect.y = y
            self.mouse_rect.w = 0
            self.mouse_rect.h = 0
            self.x_first = x
            self.y_first = y

    def render_mouse_rect(self, camera):
        if mouse.get_pressed()[0] and (self.mouse_rect.w or self.mouse_rect.h):
            pos = Rect(self.mouse_rect.x - camera.rect.x,
                self.mouse_rect.y - camera.rect.y, self.mouse_rect.w,
                self.mouse_rect.h)
            draw.rect(self.resourcemgr.screen, BLUE, pos, 5)

    def input(self, event, next_state):
        next_state = super(InGame, self).input(event, next_state)

        if event.type == KEYDOWN:
            if event.key == K_ESCAPE:
                next_state = set_next_state(next_state, STATES.MENU)
            elif event.key == K_m:
                if music.get_busy():
                    music.stop()
                else:
                    music.play()

        self.camera.input(event)
        self.unitmgr.input(event, self.camera, self.mouse_rect, self.tilemgr)
        self.input_mouse(event)

        return next_state

    def logic(self, clock, next_state):
        self.camera.logic(clock.get_time())
        self.unitmgr.logic(self.camera, clock.get_time(), self.tilemgr)

        if not self.won and not self.lost and self.unitmgr.num_enemies == 0:
            self.gg.play()
            self.won = True

        if not self.won and not self.lost and self.unitmgr.num_units == 0:
            self.lose.play()
            self.lost = True

        return next_state

    def render(self):
        self.resourcemgr.screen.fill(BLACK)
        self.tilemgr.render(self.camera)
        self.unitmgr.render(self.camera)
        self.unitmgr.play_sounds(self.camera)
        self.render_mouse_rect(self.camera)
        # self.bar.render()
        if self.won:
            self.resourcemgr.screen.blit(self.win_msg, self.win_msg_rect)
        if self.lost:
            self.resourcemgr.screen.blit(self.lose_msg, self.lose_msg_rect)