예제 #1
0
파일: ui.py 프로젝트: superspma/game_2048
class GameConsoleView:
    """
    定义实例变量
    """
    def __init__(self):
        self.__controller = GameCoreController()

    def game_start(self):
        """
        游戏开始
        :return:
        """
        self.__controller.generate_new_number()
        self.__controller.generate_new_number()
        self.__print_map()

    def __print_map(self):
        """
        打印地图
        :return:
        """
        os.system("clear")  # 清空控制台
        for item in self.__controller.map:
            for obj in item:
                print("\033[32;1m%d\033[0m" % obj, end="\t")
            print()

    # ------------测试----------------
    # a = GameConsoleView()
    # a.game_start()

    def update(self):
        """
        移动并跟新地图
        :return:
        """
        while True:
            self.__move_map()
            if self.__controller.is_change:
                self.__controller.generate_new_number()
                self.__print_map()
                if self.__controller.is_game_over():
                    print("\033[31;1m游戏结束!\033[0m")
                    break

    def __move_map(self):
        """
        根据输入的指令,进行移动
        :return:
        """
        move_dir = input("\033[34;1m请输入移动方向(wasd):\033[0m")
        if move_dir == "w":
            self.__controller.move(Direction.up)
        if move_dir == "s":
            self.__controller.move(Direction.down)
        if move_dir == "a":
            self.__controller.move(Direction.left)
        if move_dir == "d":
            self.__controller.move(Direction.right)
예제 #2
0
파일: ui.py 프로젝트: kaiforone/game2048
class GameView:
    """
        界面视图类
    """
    def __init__(self):
        self.__controller = GameCoreController()

    def __print_map(self):
        """
            显示二维列表
        :return:
        """
        # os.system("clear")
        for r in range(len(self.__controller.map)):
            for c in range(len(self.__controller.map[r])):
                print(self.__controller.map[r][c], end="\t")
            print()
            print()

    def __move_map(self):
        """
            移动二维列表
        :return:
        """
        direction = input("请输入移动方向(提示:wasd):")
        if direction == "w":
            self.__controller.move(Direction.up)
        if direction == "s":
            self.__controller.move(Direction.down)
        if direction == "a":
            self.__controller.move(Direction.left)
        if direction == "d":
            self.__controller.move(Direction.right)

    def start(self):
        """
            开始游戏,随机生成两个新数字,打印二维列表
        :return:
        """
        self.__controller.generate_new_number()
        self.__controller.generate_new_number()
        self.__print_map()

    def update(self):
        """
            更新地图,判定游戏是否结束
        :return:
        """
        while True:
            self.__move_map()
            if self.__controller.is_change:
                self.__controller.generate_new_number()
                self.__print_map()
            if self.__controller.is_game_over():
                print("游戏结束")
                break
예제 #3
0
파일: ui.py 프로젝트: jhjh6143/python
class GameConsoleView:
    """
    控制台视图
    """
    def __init__(self):
        #创建核心类对象
        self.__controller=GameCoreController()

    def start(self):
        """
            游戏开始
        :return: 
        """
        self.__controller.generate_new_number()
        self.__controller.generate_new_number()
        self.__print_map()

    def __print_map(self):
        """
            打印界面
        :return: 
        """
        #清空控制台
        # os.system("clear")
        # print("----------------------")
        for r in range(len(self.__controller.map)):
            for c in range(len(self.__controller.map[r])):
                print(self.__controller.map[r][c],end="\t")
            print()

    def update(self):
        """
            更新(游戏逻辑)
        :return: 
        """
        while True:
            self.__move_map()
            #if 界面发生变化
            if self.__controller.is_change:
                self.__controller.generate_new_number()
                self.__print_map()
                if self.__controller.is_game_over():
                    print("游戏结束")
                    break

    def __move_map(self):
        dir=input("请输入移动方向(wsad)")
        if dir == "w":
            self.__controller.move(Direction.up)
        if dir == "a":
            self.__controller.move(Direction.left)
        if dir == "d":
            self.__controller.move(Direction.right)
        if dir == "s":
            self.__controller.move(Direction.down)
예제 #4
0
class GameConsoleView:
    """
        控制台视图
    """
    def __init__(self):
        #  创建核心类对象
        self.__controller = GameCoreController()

    def start(self):
        """
            游戏开始
        """
        self.__controller.generate_new_number()
        self.__controller.generate_new_number()
        self.__print_map()

    def __print_map(self):
        # 清空控制台
        os.system("clear")
        for r in range(len(self.__controller.map)):
            for c in range(len(self.__controller.map[r])):
                print(self.__controller.map[r][c], end="\t")
            print()

    def update(self):
        """
            更新(游戏逻辑)
        :return:
        """
        while True:
            self.__move_map()
            # if 界面发生变化
            #这个判断用于避免玩家在一个方向已经不能合成,
            #通过不断使用那个方向键来生成新的随机数,
            # 避免出现,比如一直按‘上’明明已经没法合成了,却能产生随机数
            if self.__controller.is_change:
                self.__controller.generate_new_number()
                self.__print_map()
                #下边步鄹用于正确操作后,系统直接判断是否存在再次合成的可能
                #如果没有相邻合成的可能确没有
                if self.__controller.is_game_over():
                    print("游戏结束")
                    break

    def __move_map(self):
        dir = input("请输入移动方向(wsad)")
        if dir == "w":
            self.__controller.move(Direction.up)
        elif dir == "s":
            self.__controller.move(Direction.down)
        elif dir == "a":
            self.__controller.move(Direction.left)
        elif dir == "d":
            self.__controller.move(Direction.right)
예제 #5
0
class GameView:
    """
        控制台视图
    """


    def __init__(self):
        self.__controller = GameCoreController()

    def __print_map(self):

        for i in range(len(self.__controller.map)):
            for j in self.__controller.map[i]:
                print(j, end='\t')
            print()

    def start(self):
        """
            游戏开始
        """
        self.__controller.generate_new_number()
        self.__controller.generate_new_number()
        self.__print_map()

    def move_map(self):
        dir = input('请输入移动方向(wasd):')
        if dir == 'w':
            self.__controller.move(Direction.up)
        elif dir == 'a':
            self.__controller.move(Direction.left)
        elif dir == 's':
            self.__controller.move(Direction.down)
        elif dir == 'd':
            self.__controller.move(Direction.right)

    def update(self):
        """
            更新游戏逻辑
        :return:
        """
        while True:


            self.move_map()

            if self.__controller.is_change:
                self.__controller.generate_new_number()
                self.__print_map()

            if self.__controller.is_game_over():
                print('Game Over')
                break
예제 #6
0
파일: ui.py 프로젝트: Dython-sky/AID1908
class GameConsoleView:
    def __init__(self):
        self.__controller = GameCoreController()

    def main(self):
        self.__Start()
        self.__update()

    def __Start(self):
        # 产生两个数字
        self.__controller.generate_new_number()
        self.__controller.generate_new_number()
        # 绘制界面
        self.__draw_map()

    def __draw_map(self):
        # 清空控制台
        os.system("cls")
        for row in self.__controller.map:
            for col in row:
                print(col, end=" ")
            print()

    def __update(self):
        # 循环
        while True:
            # 判断玩家的输入 --> 移动地图
            self.__move_map_for_input()
            # 产生新数字
            self.__controller.generate_new_number()
            # 绘制界面
            self.__draw_map()
            # 游戏结束判断 --> 提示
            if self.__controller.is_game_over():
                print("游戏结束")
                break

    def __move_map_for_input(self):
        dir = input("请输入方向(wsad):")
        dict_dir = {
            "w": DirectionModel.UP,
            "W": DirectionModel.UP,
            "s": DirectionModel.DOWN,
            "S": DirectionModel.DOWN,
            "a": DirectionModel.LEFT,
            "A": DirectionModel.LEFT,
            "d": DirectionModel.RIGHT,
            "D": DirectionModel.RIGHT,
        }
        if dir in dict_dir:
            self.__controller.move(dict_dir[dir])
예제 #7
0
class GameConsoleView:
    """
        控制台视图
    """
    def __init__(self):
        #  创建核心类对象
        self.__controller = GameCoreController()  #视窗链接逻辑控制台

    def start(self):  #启动器
        """
            游戏开始
        """
        self.__controller.generate_new_number()  #产生一个随机数
        self.__controller.generate_new_number()
        self.__print_map()  #打印结果到控制台

    def __print_map(self):
        # 清空控制台
        os.system("clear")
        for r in range(len(self.__controller.map)):
            for c in range(len(self.__controller.map[r])):
                print(self.__controller.map[r][c], end="\t")
            print()

    def update(self):
        """
            更新(游戏逻辑)
        :return:
        """
        while True:
            self.__move_map()
            # if 界面发生变化
            if self.__controller.is_change:
                self.__controller.generate_new_number()
                self.__print_map()
                if self.__controller.is_game_over():
                    print("游戏结束")
                    break

    def __move_map(self):
        dir = input("请输入移动方向(wsad)")
        if dir == "w":
            self.__controller.move(Direction.up)
        elif dir == "s":
            self.__controller.move(Direction.down)
        elif dir == "a":
            self.__controller.move(Direction.left)
        elif dir == "d":
            self.__controller.move(Direction.right)
예제 #8
0
파일: ui.py 프로젝트: RRCHcc/python_base
class GameConsoleView:
    """
        控制台视图
    """
    def __init__(self):
        #  创建核心类对象
        self.__controller = GameCoreController()

    def start(self):
        """
            游戏开始
        """
        self.__controller.generate_new_number()
        self.__controller.generate_new_number()
        self.print_map()

    def print_map(self):
        """
            打印界面
        :return:
        """
        print("----------------")
        for r in range(len(self.__controller.map)):
            for c in range(len(self.__controller.map[r])):
                print(self.__controller.map[r][c], end=" ")
            print()

    def update(self):
        """
            更新(游戏逻辑)
        :return:
        """
        while True:
            self.move_map()
            # if 界面发生变化
            if self.__controller.is_change:
                self.__controller.generate_new_number()
                self.print_map()

    def move_map(self):
        dir = input("请输入移动方向(wsad)")
        if dir == "w":
            self.__controller.move(Direction.up)
        elif dir == "s":
            self.__controller.move(Direction.down)
        elif dir == "a":
            self.__controller.move(Direction.left)
        elif dir == "d":
            self.__controller.move(Direction.right)
예제 #9
0
파일: ui.py 프로젝트: Molly-l/1209
class GameConsoleView:
    def __init__(self):
        self.__controller = GameCoreController()

    def main(self):
        self.__start()
        self.__update()

    #开始
    def __start(self):
        # 产生两个数字
        self.__controller.generate_new_number()
        self.__controller.generate_new_number()
        self.__draw_map()  # 绘制界面

    #绘制界面
    def __draw_map(self):
        os.system("clear")  # 清空控制台
        for line in self.__controller.map:
            for item in line:
                print(item, end=" ")
            print()

    #更新
    def __update(self):
        while True:  # 循环
            # 判断玩家的输入 --> 移动地图
            self.__move_map_for_input()
            # 产生新数字
            self.__controller.generate_new_number()
            # 绘制界面
            self.__draw_map()
            # 游戏结束判断 --> 提示
            if self.__controller.is_game_over():
                print("游戏结束")
                break

    # 判断玩家的输入 --> 移动地图
    def __move_map_for_input(self):
        dir = input("请输入方向(wsas)")
        dict_dir = {
            "w": DirectionModel.UP,
            "s": DirectionModel.DOWN,
            "a": DirectionModel.LEFT,
            "d": DirectionModel.RIGHT,
        }
        if dir in dict_dir:
            self.__controller.move(dict_dir[dir])
예제 #10
0
class GameConsoleView:
    def __init__(self):
        self.__controller = GameCoreController()

    def main(self):
        self.__start()
        self.__update()

    def __start(self):
        # 产生两个数字
        self.__controller.generate_new_number()
        self.__controller.generate_new_number()
        # 绘制界面
        self.__draw_map()

    def __draw_map(self):
        # 清空控制台
        os.system("clear")
        for line in self.__controller.map:
            for item in line:
                print(item, end=" ")
            print()

    def __update(self):
        # 循环
        while True:
            # 判断玩家的输入 --> 移动地图
            self.__move_map_for_input()
            # 产生新数字
            self.__controller.generate_new_number()
            # 绘制界面
            self.__draw_map()
            # 游戏结束判断 --> 提示
            if self.__controller.is_game_over():
                print("游戏结束")
                break

    def __move_map_for_input(self):
        dir = input("请输入方向(wsas)")
        if dir == "w":
            self.__controller.move_up()
        elif dir == "s":
            self.__controller.move_down()
        elif dir == "a":
            self.__controller.move_left()
        elif dir == "d":
            self.__controller.move_right()
예제 #11
0
class GameConsoleView:
    def __init__(self):
        self.__controller = GameCoreController()

    def print_list(self):
        for item in self.__controller.map:
            for i in item:
                print(i, end="\t")
            print()

    def main(self):
        self.__controller.generate_new_number()
        self.__controller.generate_new_number()
        self.print_list()
        while True:
            self.display_options()
            self.update()

    def display_options(self):
        print(""""A"向左,"D"向右,"W"向上,"S"向下""")

    def move_map_input(self):
        str_input = input("请输入选项:")
        if str_input == "A":
            self.__controller.move_left()
        elif str_input == "D":
            self.__controller.move_right()
        elif str_input == "W":
            self.__controller.move_up()
        elif str_input == "S":
            self.__controller.move_down()

    def update(self):
        self.move_map_input()
        self.__controller.generate_new_number()
        self.print_list()
        if self.__controller.is_game_over():
            print("游戏结束")
예제 #12
0
파일: usl.py 프로젝트: Jeremy277/exercise
class GameConsoleView:
    def __init__(self):
        self.__console = GameCoreController()

    def main(self):
        self.__start()
        self.__update()

    def __start(self):
        self.__console.generate_new_number()
        self.__console.generate_new_number()
        self.__draw_map()

    def __draw_map(self):
        for item in self.__console.map:
            for i in item:
                print(i, end=' ')
            print()

    def __move_map_for_input(self):
        choice = input('请输入选择wsad:')
        if choice == 'w':
            self.__console.move_up()
        elif choice == 's':
            self.__console.move_down()
        elif choice == 'a':
            self.__console.move_left()
        elif choice == 'd':
            self.__console.move_right()

    def __update(self):
        while True:
            self.__move_map_for_input()
            self.__console.generate_new_number()
            self.__draw_map()
            if self.__console.is_game_over():
                print('游戏结束')
                break
예제 #13
0
class GameConsoleView:
    def __init__(self):
        self.__controller = GameCoreController()

    def start(self):
        self.__controller.generate_new_number()
        self.__controller.generate_new_number()
        self.print_map()

    def print_map(self):
        # os.system("clear")
        for r in range(len(self.__controller.map)):
            for c in range(len(self.__controller.map[r])):
                print(self.__controller.map[r][c], end="\t")
            print()

    def update(self):
        while True:
            self.__move_map()
            if self.__controller.is_change:
                self.__controller.generate_new_number()
                self.print_map()
                if self.__controller.is_game_over():
                    print("游戏结束")
                    break

    def __move_map(self):
        dir = input("请输入移动方向(wsad):")
        if dir == "w":
            self.__controller.move(Direction.up)
        elif dir == "s":
            self.__controller.move(Direction.down)
        elif dir == "a":
            self.__controller.move(Direction.left)
        elif dir == "d":
            self.__controller.move(Direction.right)
예제 #14
0
파일: ui.py 프로젝트: 7429/2048-Game-
class GameConsoleView:
    def __init__(self):
        self.__controller = GameCoreController()
        self.__PIXEL = 150
        self.__SIZE = 4
        self.__SCORE_PIXEL = 100

    def main(self):
        self.__start()
        self.__update()

    # 开始游戏
    def __start(self):
        # 随机产生两个数字并插入空白
        self.__controller.generate_new_number()
        self.__controller.generate_new_number()
        # self.__draw_map()
        # 开始界面绘制
        self.__show_init()

    def __show_init(self):
        # 初始化pygame
        pygame.init()
        # pygame.display.set_mode,初始化一个窗口。写多少就创建多大的窗口,width和height中哪一个写的0,就按照系统窗口的长或宽来创建窗口
        self.screen = pygame.display.set_mode(
            (self.__PIXEL * self.__SIZE,
             self.__PIXEL * self.__SIZE + self.__SCORE_PIXEL))
        # 游戏界面左上角的文字
        pygame.display.set_caption("2048")
        # 把前面这个对象pygame.Surface((PIXEL, PIXEL))创建4次
        # pygame.Surface(),pygame中用来代表image的对象
        self.block = [
            pygame.Surface((self.__PIXEL, self.__PIXEL)) for i in range(4)
        ]
        # 设置2048每个方格的颜色
        self.block[0].fill((238, 228, 218))
        self.block[1].fill((237, 224, 200))
        self.block[2].fill((242, 177, 121))
        self.block[3].fill((205, 193, 180))
        # (0, 0, 0)是黑色,(255, 255, 255)是白色,
        # surface((width, height).显示的是score背景的那个浅棕色的部分的大小。
        self.score_block = pygame.Surface(
            (self.__PIXEL * self.__SIZE, self.__SCORE_PIXEL))
        # 对于分数条区域填充颜色
        self.score_block.fill((250, 248, 239))
        # 设置字体:通过None访问内建默认字体,第二个参数为size-小过每个格子的大小PIXEL
        self.map_font = pygame.font.Font(None, int(self.__PIXEL * 2 / 3))
        self.score_font = pygame.font.Font(None,
                                           int(self.__SCORE_PIXEL * 2 / 3))
        self.clock = pygame.time.Clock()
        self.show()

    # 游戏不断随着操作刷新
    def __update(self):
        # 一直循环-刷新
        while not self.__controller.is_game_over():
            # 每次循环访问你的目前的事件,r如果点叉号会退出
            for event in pygame.event.get():
                if event.type == QUIT:
                    sys.exit()
            # clock.tick()他会计算距离上一次调用这个程序过去的second,限制一秒钟调用程序的次数。
            self.clock.tick(12)
            # 判断玩家的输入,移动地图
            self.__move_map_by_keyboard()
            # self.__draw_map()
            self.show()
        # 游戏结束后界面保留的时间
        pygame.time.delay(3000)

    def show(self):
        for i in range(self.__SIZE):
            for j in range(self.__SIZE):
                # print(True and "Score: "),输出Score:,逻辑运算符:and or 一旦整体为True,把非逻辑运算符的部分代表整体
                # 这个值如果是0,那就从self.block的第0个和1个的颜色块中挑一个作为区域颜色。反之从第2个和第三个中挑。
                self.screen.blit(
                    self.__controller.map[i][j] == 0
                    and self.block[(i + j) % 2] or self.block[2 + (i + j) % 2],
                    (self.__PIXEL * j, self.__PIXEL * i))

                # 数值显示
                if self.__controller.map[i][j] != 0:
                    # 制作图片
                    #   取出第i行j列的数字,str了,RGB设置颜色
                    #   pygame.font.Font().render()是在一个新的Surface对象上绘制文本。写True字体就没有锯齿。
                    map_text = self.map_font.render(
                        str(self.__controller.map[i][j]), True, (38, 38, 38))
                    # 生成图片放置的坐标
                    text_rect = map_text.get_rect()
                    text_rect.center = (self.__PIXEL * j + self.__PIXEL / 2,
                                        self.__PIXEL * i + self.__PIXEL / 2)
                    # 图片显示
                    self.screen.blit(map_text, text_rect)
        # 分数条显示
        #   分数条放在(0,600)。图片和背景都是以左上角的点为原点,向下和向右为正方向。600分数条处正好和4*4的格子擦边。
        self.screen.blit(self.score_block, (0, self.__SIZE * self.__PIXEL))

        # 生成分数图片
        #   pygame.font.Font().render(text, antialias, color, background=None)
        #   print(False or"Score: ")结果是 Score。
        score_text = self.score_font.render(
            (self.__controller.is_game_over() and "Game over with score "
             or "Score: ") + str(self.__controller.score), True,
            (119, 110, 101))
        # 生成分数位置
        #   .get_rect()获取文字图片的length和width
        score_rect = score_text.get_rect()
        #   (300, 650)。向右向下为正方向。300保证了在屏幕横向中间。650保证在600-700分数条的中间。
        #   但是这个点的坐标是整个文字图的左上角那个点。不写.center会左上角的点在中心,图片偏了。
        #   写了以后pygame自动为我们寻找能够使得图片中心在你想要的位置的坐标并return出来。
        score_rect.center = (self.__PIXEL * self.__SIZE / 2,
                             self.__PIXEL * self.__SIZE +
                             self.__SCORE_PIXEL / 2)
        # 分数图片显示在指定位置
        self.screen.blit(score_text, score_rect)
        # 让我们绘制的东西显示在屏幕上
        pygame.display.update()

    def __move_map_by_keyboard(self):
        # 接收玩家操作
        pressed_keys = pygame.key.get_pressed()
        if pressed_keys[K_w] or pressed_keys[K_UP]:
            self.__controller.move_up()
            # 产生随机数字,插入空白
            self.__controller.generate_new_number()
        elif pressed_keys[K_s] or pressed_keys[K_DOWN]:
            self.__controller.move_down()
            self.__controller.generate_new_number()
        elif pressed_keys[K_a] or pressed_keys[K_LEFT]:
            self.__controller.move_left()
            self.__controller.generate_new_number()
        elif pressed_keys[K_d] or pressed_keys[K_RIGHT]:
            self.__controller.move_right()
            self.__controller.generate_new_number()