脚本之家,脚本语言编程技术及教程分享平台!
分类导航

Python|VBS|Ruby|Lua|perl|VBA|Golang|PowerShell|Erlang|autoit|Dos|bat|

服务器之家 - 脚本之家 - Python - python游戏实战项目之俄罗斯方块的魅力

python游戏实战项目之俄罗斯方块的魅力

2022-01-07 10:58顾木子吖 Python

迟早一定会挂掉的俄罗斯方块,为什么至今仍是世界游戏之王?它是怎么编写的?本文将给大家详细的介绍,对大家的学习或工作具有一定的参考借鉴价值

 

导语

python游戏实战项目之俄罗斯方块的魅力

为什么有这么一个简单的游戏?这个游戏如此受欢迎?

python游戏实战项目之俄罗斯方块的魅力

仅仅是因为它在游戏行业异常匮乏的年代出现,从而成为了一代人的记忆吗?恐怕并不是。

玩过俄罗斯方块的人都明白,它给人的感觉就像是嗑瓜子一样,一旦开始就会像上瘾一样难以停下来,绞尽脑汁只想填满空缺的地方。

python游戏实战项目之俄罗斯方块的魅力

哈哈哈!小编每周的话基本上都会整理一些游戏代码的哈!

这一期文章就带大家来开发一款俄罗斯方块小游戏!

 

正文

游戏规则:由小方块组成的不同形状的板块陆续从屏幕上方落下来,玩家通过调整板块的位置和方向,使它们在屏幕底部拼出完整的一条或几条。

这些完整的横条会随即消失,给新落下来的板块腾出空间,与此同时,玩家得到分数奖励。没有被消除掉的方块不断堆积起来,一旦堆到屏幕顶端,玩家便告输,游戏结束。

(1)游戏定义,俄罗斯方块儿的不同的类型:

class tetrisShape():
    def __init__(self, shape=0):
        # 空块
        self.shape_empty = 0
        # 一字型块
        self.shape_I = 1
        # L型块
        self.shape_L = 2
        # 向左的L型块
        self.shape_J = 3
        # T型块
        self.shape_T = 4
        # 田字型块
        self.shape_O = 5
        # 反向Z型块
        self.shape_S = 6
        # Z型块
        self.shape_Z = 7

(2)​获得该形状当前旋转状态的四个小方块的相对坐标分布:

def getRotatedRelativeCoords(self, direction):
        # 初始分布
        if direction == 0 or self.shape == self.shape_O:
            return self.relative_coords
        # 逆时针旋转90度
        if direction == 1:
            return [[-y, x] for x, y in self.relative_coords]
        # 逆时针旋转180度
        if direction == 2:
            if self.shape in [self.shape_I, self.shape_Z, self.shape_S]:
                return self.relative_coords
            else:
                return [[-x, -y] for x, y in self.relative_coords]
        # 逆时针旋转270度
        if direction == 3:
            if self.shape in [self.shape_I, self.shape_Z, self.shape_S]:
                return [[-y, x] for x, y in self.relative_coords]
            else:
                return [[y, -x] for x, y in self.relative_coords]

(3)游戏的方块儿可以向不同方向移动:​

"""向右移动"""
    def moveRight(self):
        if self.ableMove([self.current_coord[0] + 1, self.current_coord[1]]):
            self.current_coord[0] += 1
    """向左移动"""
    def moveLeft(self):
        if self.ableMove([self.current_coord[0] - 1, self.current_coord[1]]):
            self.current_coord[0] -= 1
    """顺时针转"""
    def rotateClockwise(self):
        if self.ableMove(self.current_coord, (self.current_direction - 1) % 4):
            self.current_direction = (self.current_direction-1) % 4
    """逆时针转"""
    def rotateAnticlockwise(self):
        if self.ableMove(self.current_coord, (self.current_direction + 1) % 4):
            self.current_direction = (self.current_direction+1) % 4
    """向下移动"""
    def moveDown(self):
        removed_lines = 0
        if self.ableMove([self.current_coord[0], self.current_coord[1] + 1]):
            self.current_coord[1] += 1
        else:
            x_min, x_max, y_min, y_max = self.current_tetris.getRelativeBoundary(self.current_direction)
            # 简单起见, 有超出屏幕就判定游戏结束
            if self.current_coord[1] + y_min < 0:
                self.is_gameover = True
                return removed_lines
            self.mergeTetris()
            removed_lines = self.removeFullLines()
            self.createNewTetris()
        return removed_lines
    """坠落"""
    def dropDown(self):
        removed_lines = 0
        while self.ableMove([self.current_coord[0], self.current_coord[1] + 1]):
            self.current_coord[1] += 1
        x_min, x_max, y_min, y_max = self.current_tetris.getRelativeBoundary(self.current_direction)
        # 简单起见, 有超出屏幕就判定游戏结束
        if self.current_coord[1] + y_min < 0:
            self.is_gameover = True
            return removed_lines
        self.mergeTetris()
        removed_lines = self.removeFullLines()
        self.createNewTetris()
        return removed_lines

(4)合并俄罗斯方块(最下面定型不能再动的那些):

    def mergeTetris(self):
        for x, y in self.current_tetris.getAbsoluteCoords(self.current_direction, self.current_coord[0], self.current_coord[1]):
            self.board_data[x + y * self.width] = self.current_tetris.shape
        self.current_coord = [-1, -1]
        self.current_direction = 0
        self.current_tetris = tetrisShape()

(5)当每行铺满之后会得分,相应的消失一行:

python游戏实战项目之俄罗斯方块的魅力

"""移出整行都有小方块的"""
    def removeFullLines(self):
        new_board_data = [0] * self.width * self.height
        new_y = self.height - 1
        removed_lines = 0
        for y in range(self.height - 1, -1, -1):
            cell_count = sum([1 if self.board_data[x + y * self.width] > 0 else 0 for x in range(self.width)])
            if cell_count < self.width:
                for x in range(self.width):
                    new_board_data[x + new_y * self.width] = self.board_data[x + y * self.width]
                new_y -= 1
            else:
                removed_lines += 1
        self.board_data = new_board_data
        return removed_lines

 

效果图:

​​​python游戏实战项目之俄罗斯方块的魅力

 

​总结

哈哈哈!好啦!按住方向键也可以变形的哈!赶快试试~

到此这篇关于python游戏实战项目之俄罗斯方块的魅力的文章就介绍到这了,更多相关python 俄罗斯方块内容请搜索服务器之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持服务器之家!

原文链接:https://blog.csdn.net/weixin_55822277/article/details/120083675

延伸 · 阅读

精彩推荐