Example #1
0
class Game:
    def __init__(self, title, width, height, fps=60):
        """
        :param title: 游戏窗口的标题
        :param width: 游戏窗口的宽度
        :param height: 游戏窗口的高度
        :param fps: 游戏每秒刷新次数
        """
        self.title = title
        self.width = width
        self.height = height
        self.screen_surf = None
        self.fps = fps
        self.__init_pygame()
        self.__init_game()
        self.update()

    def __init_pygame(self):
        """
        pygame相关的初始化操作
        """
        pygame.init()
        pygame.display.set_caption(self.title)
        self.screen_surf = pygame.display.set_mode([self.width, self.height])
        self.clock = pygame.time.Clock()

    def __init_game(self):
        """
        我们游戏的一些初始化操作
        """
        self.hero = pygame.image.load(
            './img/character/hero.png').convert_alpha()
        self.map_bottom = pygame.image.load('./img/map/0.png').convert_alpha()
        self.map_top = pygame.image.load('./img/map/0_top.png').convert_alpha()
        self.game_map = GameMap(self.map_bottom, self.map_top, 0, 0)
        self.game_map.load_walk_file('./img/map/0.map')
        self.role = CharWalk(self.hero, 48, CharWalk.DIR_DOWN, 5, 10)

    def update(self):
        while True:
            self.clock.tick(self.fps)
            # 逻辑更新
            self.role.logic()
            self.event_handler()
            # 画面更新
            self.game_map.draw_bottom(self.screen_surf)
            self.role.draw(self.screen_surf, self.game_map.x, self.game_map.y)
            self.game_map.draw_top(self.screen_surf)
            # self.game_map.draw_grid(self.screen_surf)
            pygame.display.update()

    def event_handler(self):
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                sys.exit()
            elif event.type == pygame.MOUSEBUTTONDOWN:
                mouse_x, mouse_y = pygame.mouse.get_pos()
                mx = (mouse_x - self.game_map.x) // 32
                my = (mouse_y - self.game_map.y) // 32
                self.role.find_path(self.game_map, (mx, my))
Example #2
0
class Game:
    def __init__(self, title, width, height, fps=60):
        """
        :param title: 游戏窗口的标题
        :param width: 游戏窗口的宽度
        :param height: 游戏窗口的高度
        :param fps: 游戏每秒刷新次数
        """
        self.title = title
        self.width = width
        self.height = height
        self.screen_surf = None
        self.fps = fps
        self.__init_pygame()
        self.__init_game()
        self.update()

    def __init_pygame(self):
        """
        pygame相关的初始化操作
        """
        pygame.init()
        pygame.display.set_caption(self.title)
        self.screen_surf = pygame.display.set_mode([self.width, self.height])
        self.clock = pygame.time.Clock()

    def __init_game(self):
        """
        我们游戏的一些初始化操作
        """
        self.hero = pygame.image.load('./character/hero.png').convert_alpha()
        self.map_bottom = pygame.image.load('./map/0.png').convert_alpha()
        self.map_top = pygame.image.load('./map/0_top.png').convert_alpha()
        self.game_map = GameMap(self.map_bottom, self.map_top, 0, 0)
        self.game_map.load_walk_file('./map/0.map')
        self.role = CharWalk(self.hero, 48, CharWalk.DIR_DOWN, 5, 10)

    def update(self):
        while True:
            self.clock.tick(self.fps)
            # 逻辑更新
            self.role.logic()
            self.event_handler()
            self.game_map.roll(self.role.x, self.role.y)     # 实现滚动
            # 画面更新
            self.game_map.draw_bottom(self.screen_surf)
            self.role.draw(self.screen_surf, self.game_map.x, self.game_map.y)
            self.game_map.draw_top(self.screen_surf)
            # self.game_map.draw_grid(self.screen_surf)
            pygame.display.update()
Example #3
0
class Game:
    def __init__(self, title, width, height, fps=60):
        """
        :param title: 游戏窗口的标题
        :param width: 游戏窗口的宽度
        :param height: 游戏窗口的高度
        :param fps: 游戏每秒刷新次数
        """
        self.title = title
        self.width = width
        self.height = height
        self.screen_surf = None
        self.fps = fps
        self.__init_pygame()
        self.__init_game()
        self.update()

    def __init_pygame(self):
        pygame.init()
        pygame.display.set_caption(self.title)
        self.screen_surf = pygame.display.set_mode([self.width, self.height])
        self.clock = pygame.time.Clock()

    def __init_game(self):
        self.hero = pygame.image.load(
            './img/character/hero.png').convert_alpha()
        self.map_bottom = pygame.image.load('./img/map/0.png').convert_alpha()
        self.map_top = pygame.image.load('./img/map/0_top.png').convert_alpha()
        self.game_map = GameMap(self.map_bottom, self.map_top, 0, 0)
        self.game_map.load_walk_file('./img/map/0.map')

    def update(self):
        while True:
            self.clock.tick(self.fps)
            # TODO:逻辑更新
            self.event_handler()
            # TODO:画面更新
            self.game_map.draw_bottom(self.screen_surf)
            Sprite.draw(self.screen_surf, self.hero, 100, 100, 0, 0)
            Sprite.draw(self.screen_surf, self.hero, 210, 120, 1, 1)
            Sprite.draw(self.screen_surf, self.hero, 300, 100, 2, 2)
            self.game_map.draw_top(self.screen_surf)
            self.game_map.draw_grid(self.screen_surf)
            pygame.display.update()

    def event_handler(self):
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                sys.exit()
Example #4
0
class GameScene(Scene):
    def __init__(self, scene_id):
        super().__init__(scene_id=scene_id)
        self.hero = pygame.image.load(
            './img/character/hero.png').convert_alpha()
        self.map_bottom = pygame.image.load('./img/map/0.png').convert_alpha()
        self.map_top = pygame.image.load('./img/map/0_top.png').convert_alpha()
        self.game_map = GameMap(self.map_bottom, self.map_top, 0, 0)
        self.game_map.load_walk_file('./img/map/0.map')
        self.role = None
        self.other_player = []
        self.chat_box = pygame.image.load('./img/chat_box.png').convert_alpha()
        self.chat_input = TextBox(145,
                                  20,
                                  75,
                                  550,
                                  color=(0, 0, 0),
                                  no_bg=True,
                                  callback=self.cb_send_chat)
        self.chat_history = []

    def cb_send_chat(self, text):
        # 不予许发空字符串
        if not text:
            return
        g.client.chat(text)
        self.chat_input.reset()

    def logic(self):
        self.role.logic()
        for player in self.other_player:
            player.logic()
        self.game_map.roll(self.role.x, self.role.y)

    def render(self):
        self.game_map.draw_bottom(g.screen)
        self.role.draw(g.screen, self.game_map.x, self.game_map.y)
        # 绘制其他玩家
        for player in self.other_player:
            player.draw(g.screen, self.game_map.x, self.game_map.y)
        self.game_map.draw_top(g.screen)
        # 绘制聊天记录
        for index, history in enumerate(self.chat_history):
            draw_src_outline_text(g.screen, 0, 380 + index * 25, history,
                                  g.font, (255, 0, 0), (0, 0, 0))
        # 绘制聊天框
        Sprite.blit(g.screen, self.chat_box, 0,
                    571 - self.chat_box.get_height())
        self.chat_input.draw(g.screen)
        # self.game_map.draw_grid(g.screen)

    def mouse_down(self, x, y, pressed):
        if self.chat_input.mouse_down(x, y, pressed):
            return

        mx = int((x - self.game_map.x) / 32)
        my = int((y - self.game_map.y) / 32)
        # 寻路
        self.role.find_path(self.game_map, (mx, my))

    def mouse_move(self, x, y):
        pass

    def mouse_up(self, x, y, pressed):
        pass

    def key_down(self, event):
        self.chat_input.safe_key_down(event)
Example #5
0
class Game:
    def __init__(self, title, width, height, fps=60):
        """
        :param title: 游戏窗口的标题
        :param width: 游戏窗口的宽度
        :param height: 游戏窗口的高度
        :param fps: 游戏每秒刷新次数
        """
        self.title = title
        self.width = width
        self.height = height
        self.screen_surf = None
        self.fps = fps
        self.__init_pygame()
        self.__init_game()
        self.update()

    def __init_pygame(self):
        """
        pygame相关的初始化操作
        """
        pygame.init()
        pygame.display.set_caption(self.title)
        self.screen_surf = pygame.display.set_mode([self.width, self.height])
        self.clock = pygame.time.Clock()

    def __init_game(self):
        """
        我们游戏的一些初始化操作
        """
        self.hero = pygame.image.load(
            './img/character/hero.png').convert_alpha()
        self.map_bottom = pygame.image.load('./img/map/0.png').convert_alpha()
        self.map_top = pygame.image.load('./img/map/0_top.png').convert_alpha()
        self.game_map = GameMap(self.map_bottom, self.map_top, 0, 0)
        self.game_map.load_walk_file('./img/map/0.map')
        self.role = None  # CharWalk(self.hero, 48, CharWalk.DIR_DOWN, 5, 10)
        self.other_player = []
        self.game_state = 0  # 0未登录 1已登录
        # 与服务端建立连接
        s = socket.socket()
        s.connect(('127.0.0.1', 6666))  # 与服务器建立连接
        self.client = Client(s, self)
        g.client = self.client  # 把client赋值给全局对象上,以便到处使用
        # 登录
        self.client.login()

    def update(self):
        while True:
            self.clock.tick(self.fps)
            if self.game_state == 0:  # 还未登录的时候,没必要执行这些逻辑
                continue
            # 逻辑更新
            self.role.logic()
            self.event_handler()
            # 其他玩家逻辑(移动逻辑)
            for player in self.other_player:
                player.logic()
            self.game_map.roll(self.role.x, self.role.y)
            # 画面更新
            self.game_map.draw_bottom(self.screen_surf)
            self.role.draw(self.screen_surf, self.game_map.x, self.game_map.y)
            # 绘制其他玩家
            for player in self.other_player:
                player.draw(self.screen_surf, self.game_map.x, self.game_map.y)
            self.game_map.draw_top(self.screen_surf)
            # self.game_map.draw_grid(self.screen_surf)
            pygame.display.update()

    def event_handler(self):
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                sys.exit()
            elif event.type == pygame.MOUSEBUTTONDOWN:
                mouse_x, mouse_y = pygame.mouse.get_pos()
                mx = int((mouse_x - self.game_map.x) / 32)
                my = int((mouse_y - self.game_map.y) / 32)
                # 寻路
                self.role.find_path(self.game_map, (mx, my))