主要思想
1、将2048核心算法定义到核心类中GameCoreController,作为实例成员
2、产生新数字 –随机2(90%)或者4(10%)
3、判定游戏是否结束 –如果有空位置,游戏不能结束 –如果横向可以移动,游戏不能结束 –纵向 –以上条件不满足,游戏结束
4、创建控制台游戏视图GameConsoleView –游戏开始时,产生2个新数字,绘制界面 –游戏逻辑,获取玩家输入wsad移动地图,产生新数字,判断游戏是否结束
GameCoreController.py
"""
游戏核心逻辑控制器
"""
import random
from model import LocationModel
class GameCoreController:
def __init__(self):
self.__list_merge = None
self.__map = [
[2, 0, 0, 2],
[2, 4, 4, 2],
[0, 4, 2, 0],
[2, 0, 2, 0],
]
self.__list_empty_location = []
@property
def map(self):
return self.__map
# 1、将列表零元素移动到末尾
def __zero_to_end(self):
"""
零元素移动到末尾
思路:从后向前依次判断,如果是零元素,则删除零元素后追加零
:return:
"""
for i in range(len(self.__list_merge) - 1, -1, -1):
if self.__list_merge[i] == 0:
del self.__list_merge[i]
self.__list_merge.append(0)
# 2、将list_merge中的元素进行合并(相邻且相同)
def __merge(self):
"""
合并
思路:
将零元素后移
判断如果相邻且相同则合并
:return:
"""
self.__zero_to_end()
for i in range(len(self.__list_merge) - 1):
if self.__list_merge[i] == self.__list_merge[i + 1]:
self.__list_merge[i] += self.__list_merge[i + 1]
del self.__list_merge[i + 1]
self.__list_merge.append(0)
# merge()
# print(list_merge)
# 3、将二维列表map中的元素向左移动
def move_left(self):
"""
向左移动
思路:
将每行(一维列表)赋值给全职变量list_merge
再通过merge函数操作数据
:return:
"""
for line in self.map:
self.__list_merge = line
self.__merge()
# move_left()
# print(map)
# 4、将二维列表map中的元素向右移动
def move_right(self):
"""
向右移动
思路:
将每行(反向切片)赋值给全局变量list_merge
再通过merge函数操作数据
再对list_merge(反向切片)
:return:
"""
for line in self.map:
self.__list_merge = line[::-1] # 因为切片,所以创建了新列表
self.__merge() # 操作的是新列表
line[::-1] = self.__list_merge
# 矩阵转置
def __square_matrix_tranpose(self):
for c in range(len(self.map) - 1):
for r in range(c + 1, len(self.map)):
self.map[r][c], self.map[c][r] = self.map[c][r], self.map[r][c]
# 5、将二维列表map中元素向上移动
def move_up(self):
"""
思想:
方阵转置
调用向左移动
方阵转置
:return:
"""
self.__square_matrix_tranpose()
self.move_left()
self.__square_matrix_tranpose()
# 6、将二维列表map中元素向下移动
def move_down(self):
"""
思想:
方阵转置
调用向右移动
方阵转置
:return:
"""
self.__square_matrix_tranpose()
self.move_right()
self.__square_matrix_tranpose()
# 产生新数字
def generate_new_number(self):
#选择位置
#计算所有空位置
self.__calculate_empty_location()
if len(self.__list_empty_location) == 0:return
#随机选择一个
loc = random.choice(self.__list_empty_location)
#计算数字
# self.map[loc[0][loc[1]]] = self.__select_random_number()
self.map[loc.r][loc.c] = self.__select_random_number()
def __select_random_number(self):
return 4 if random.randint(1, 10) == 1 else 2
def __calculate_empty_location(self):
self.__list_empty_location.clear()
for r in range(len(self.map)):
for c in range(len(self.map[r])):
if self.map[r][c] == 0:
# self.__list_empty_location.append((r, c))
self.__list_empty_location.append(LocationModel(r,c))
def is_game_over(self):
if len(self.__list_empty_location) > 0:
return False
for r in range(len(self.map)):
for c in range(len(self.map[r])-1):
if self.map[r][c] == self.map[r][c+1] and self.map[r][c] == self.map[c+1][r]:
return False
# for r in range(len(self.map)):
# for c in range(len(self.map[r])-1):
# if self.map[r][c] == self.map[r][c+1]:
# return False
# for c in range(len(self.map)):
# for r in range(len(self.map)-1):
# if self.map[r][c] == self.map[r+1][c]
# return False
return True
if __name__ == '__main__':
controller = GameCoreController()
controller.generate_new_number()
controller.generate_new_number()
controller.move_down()
print(controller.map)
model.py
class LocationModel:
"""
位置模型
"""
def __init__(self,r,c):
self.r = r
self.c = c
usl.py
"""
游戏界面逻辑模块
"""
import os
from GameCoreController import GameCoreController
class GameConsoleView:
"""
控制台界面类
"""
def __init__(self):
self.__controller = GameCoreController()
def __start(self):
self.__controller.generate_new_number()
self.__controller.generate_new_number()
self.__draw_map()
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("游戏结束!")
def __move_map_for_input(self):
dir = input("请输入方向:")
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()
def main(self):
self.__start()
self.__update()
def __draw_map(self):
# os.system("clear")#linux清空
for line in self.__controller.map:
for item in line:
print(item,end="\t")
print()
main.py
"""
游戏入口
"""
from usl import GameConsoleView
if __name__ == '__main__':
view = GameConsoleView()
view.main()
|