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])
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)
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])
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
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)
class GameConsoleView: def __init__(self): self.__controller = GameCoreController() def main(self): self._start() self.__update() def _start(self): self.__controller.generate_random_number() self.__controller.generate_random_number() self.__draw_list_map() def __update(self): while True: self.__move_list_map() self.__controller.generate_random_number() self.__draw_list_map() if self.__controller.is_game_over(): print("GameOver") break def __draw_list_map(self): os.system("clear") for line in self.__controller.list_map: for item in line: print(item, end=" ") print() def __move_list_map(self): dir = input("Plear input your direction(wsda):") 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])
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)
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
class GameConsoleView: """ 控制台视图 """ def __init__(self): self.__controller = GameCoreController() def start(self): """ 游戏开始 :return: """ self.__controller.gen_random_number() self.__controller.gen_random_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 self.__controller.is_change: self.__controller.gen_random_number() os.system("cls") self.print_map() def move_map(self): dir = input("请输入移动方向(WSAD):").upper() 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) else: pass
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)
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)
class GameConsoleView: """ 控制台视图 """ def __init__(self): self.__game_controller = GameCoreController() def start(self): self.__game_controller.generate_new_num() self.__game_controller.generate_new_num() self.__print_interface() def update(self): """ 更新 @return: """ while True: self.__move_map() if self.__game_controller.is_change: self.__game_controller.generate_new_num() self.__print_interface() if self.__game_controller.is_game_over(): print("Game Over") break def __move_map(self): str_dire = input("up: w, down: s, left: a, right: d") if str_dire == "w": self.__game_controller.move(Direction.up) elif str_dire == "s": self.__game_controller.move(Direction.down) elif str_dire == "a": self.__game_controller.move(Direction.left) elif str_dire == "d": self.__game_controller.move(Direction.right) def __print_interface(self): """ 打印界面 @return: """ # 清空控制台 os.system("clear") for r in range(len(self.__game_controller.map)): for column in range(len(self.__game_controller.map[r])): print(self.__game_controller.map[r][column], end="\t ") print()
class Game2048View: def __init__(self): self.__control = GameCoreController() def print_map(self): os.system("clear") for i in range(len(self.__control.map)): for n in range(len(self.__control.map[i])): print("%4s" % self.__control.map[i][n], end=" ") print() print() print("得分:%d".center(22) % self.__control.total_score) print() def start(self): self.__control.generat_random_new_number() self.__control.generat_random_new_number() self.print_map() def move_map(self): """ 方向选择 :return: """ direction = input("请输入(w-s-a-d)选择移动方向:") if direction == "w": self.__control.move(Direction.up) elif direction == "s": self.__control.move(Direction.down) elif direction == "a": self.__control.move(Direction.left) elif direction == "d": self.__control.move(Direction.right) def update(self): while True: self.move_map() if self.__control.is_change: self.__control.generat_random_new_number() self.print_map() if self.__control.is_game_over(): print("Game Over") break
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)