• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

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

python 搞代码 4年前 (2022-01-09) 15次浏览 已收录 0个评论
文章目录[隐藏]

导语

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

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

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

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

正文

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

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

(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 rot<em style="color:transparent">本文来源[email protected]搞@^&代*@码网(</em>ateClockwise(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

搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:python游戏实战项目之俄罗斯方块的魅力

喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址