2048游戏 算法+图形化实现

用python实现2048, 借助pygame图形化展示

代码运用MVC模型写成

运行main.py()就可以玩了。

     model.py 是数据模型,用见名知意的变量名代替数字,增强代码可读性。也就是Model。

     bll.py是business logic layer,也就是Controller. 写好了所有计算、数据处理的算法。

     ui.py是图形化界面。接受用户输入,调用bll.py里面的算法,借助pygame的方法来实现游戏图形化展示。


以下文件可以从我的github上下载:

https://github.com/7429/2048-Game-/


model.py()

"""
    数据模型
        用于封装数据,使得代码阅读者知道数据背后的意义。
"""
class Location:
    """
        位置
    """
    def __init__(self,r,c):
        self.r_index = r
        self.c_index = c

bll.py

"""
    游戏逻辑控制器,负责处理游戏核心算法.
    Business Logic Layer
"""
from model import Location
import random


class GameCoreController:
    def __init__(self):
        self.__list_merge = None
        self.__map = [
            [0, 0, 0, 0],
            [0, 0, 0, 0],
            [0, 0, 0, 0],
            [0, 0, 0, 0],
        ]
        self.__list_empty_location = []
        self.score = 0

    @property
    def map(self):
        return self.__map

    def __zero_to_end(self):
        """
            零元素移动到末尾.
        """
        for i in range(-1, -len(self.__list_merge) - 1, -1):
            if self.__list_merge[i] == 0:
                del self.__list_merge[i]
                self.__list_merge.append(0)

    def __merge(self):
        """
            合并
        """
        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)
                # 这里self.__list_merge[i]已经是合并之后的那个了,直接加上。
                self.score += self.__list_merge[i]

    # def __move_left(self):
    def move_left(self):
        """
            向左移动
        """
        for line in self.__map:
            self.__list_merge = line
            self.__merge()

    # def __move_right(self):
    def move_right(self):
        """
            向右移动
        """
        for line in self.__map:
            self.__list_merge = line[::-1]
            self.__merge()
            line[::-1] = self.__list_merge

    # def __move_up(self):
    def move_up(self):
        self.__square_matrix_transpose()
        self.move_left()
        self.__square_matrix_transpose()

    # def __move_down(self):
    def move_down(self):
        self.__square_matrix_transpose()
        self.move_right()
        self.__square_matrix_transpose()

    def __square_matrix_transpose(self):
        """
            方阵转置
        :param sqr_matrix: 二维列表类型的方阵
        """
        for c in range(1, len(self.__map)):
            for r in range(c, len(self.__map)):
                self.__map[r][c - 1], self.__map[c - 1][r] = self.__map[c - 1][r], self.__map[r][c - 1]

    # def move(self, dir):
    #     """
    #         移动
    #     :param dir: 方向,DirectionModel类型
    #     :return:
    #     """
    #     if dir == DirectionModel.UP:
    #         self.__move_up()
    #     elif dir == DirectionModel.DOWN:
    #         self.__move_down()
    #     elif dir == DirectionModel.LEFT:
    #         self.__move_left()
    #     elif dir == DirectionModel.RIGHT:
    #         self.__move_right()
    """
    2. 在GameCoreController类中,定义产生随机数功能.
   需求:在空白的位置上
       可能是2(90%),也可能是4(10%).
       1 - 10  --> 随机数 是1的概率是10%
       1 --100  --> 1 <=随机数<=38 的概率是38%

    """
    # 我的思路1:一个列表9个2,1个4,随机抽
    # 我的思路2:random.randint 结果如果是1-90就2, 91-100就是4

    # 产生每一格数字的,并把数字放进格子中为0 的位置,一次放一个

    def generate_new_number(self):
        # 获取空白位置
        self.__get_empty_location()
        # 列表为空会报错,return终止加数的代码
        if len(self.__list_empty_location) == 0:
            return
        loc = random.choice(self.__list_empty_location)

        self.__map[loc.r_index][loc.c_index] = self.__create_random_num()
        # 填完数字以后,在记录空格的列表中删除这一项
        self.__list_empty_location.remove(loc)

    def __get_empty_location(self):
        # 避免每一次找空位置的时候都创建一个新的列表,把创建空列表写进init
        # 每一次寻找空位置前,先清空原列表
        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:
                    # 把行号列号用对象封装好,放进列表
                    # 直接修改了实例变量(可以看做全局变量),不需要return了。
                    self.__list_empty_location.append(Location(r,c))


    def __create_random_num(self):
        # 非等概率生成数字
        #   方法1
        return 4 if random.randint(1,10) == 1 else 2

        #   方法2
        # random.choice
        #   方法3:
        #   random.randint(0, 3) > 0 and 2 or 4

    """
    3. 在GameCoreController类中,定义判断游戏是否结束的方法.
        是否具有空位置
        横向竖向没有相同的元素
    """
    def is_game_over(self):
        """
        :return: True代表退出游戏, False代表不退出游戏
        """
        # 借助上面已经写好的代码,看有无空
        if len(self.__list_empty_location) > 0:
            return False
        # 0-1 1-2 2-3,
        # 看是否每一列有相同的元素:取第0行,0-1 1-2 2-3 逐列取数字,定位元素比较。后面依次取第1,2,3行
        # [0][0]->[0][1];[0][1]->[0][2];[0][2]->[0][3]
        # [1][0]->[1][1];[1][1]->[1][2];[1][2]->[1][3]
        # 看是否每一行有相同的元素:取第0列,0-1 1-2 2-3 逐行取数字,定位元素比较。后面依次取第1,2,3列
        # [0][0]->[1][0];[1][0]->[2][0];[2][0]->[3][0]
        # [0][1]->[1][1];[1][1]->[2][1];[2][1]->[3][1]

        # 两个对比的索引值在数值上正好是互换
        # r = 1 第二次循环:第三位:[1][2]->[1][3]     [2][1]->[3][1]
        #                          [r][c]->[r][c+1]   [c][r]->[c+1][r]
        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] or self.__map[c][r] == self.__map[c+1][r]:
                    # 不结束
                    return False
        return True
if __name__ == "__main__":
    controller = GameCoreController()

ui.py

"""
2048 控制台界面
"""
from bll import GameCoreController
# import os
import pygame
from pygame.locals import *
import sys


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()
            # def __draw_map(self):
            #     # 目的:为了让每次绘制界面的时候覆盖原来的,而不是每次显示一个新的
            #     # 让python调用Windows的命令执行操作
            #     os.system("cls")
            #     for line in self.__controller.map:
            #         for item in line:
            #             print(item, end=' ')
            #         print()


if __name__ == "__main__":
    view = GameConsoleView()
    view.main()

 

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

德彪稳坐倒骑驴

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值