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

作者:顾木子吖 时间:2021-12-07 19:38:23 

导语

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游戏实战项目之俄罗斯方块的魅力

总结

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

来源:https://blog.csdn.net/weixin_55822277/article/details/120083675

标签:python,俄罗斯方块,游戏
0
投稿

猜你喜欢

  • python导入导出redis数据的实现

    2023-02-24 11:10:55
  • Python实现一键整理百度云盘中重复无用文件

    2023-09-23 04:33:38
  • Python 利用切片从列表中取出一部分使用的方法

    2021-04-05 14:14:19
  • 利用python写api接口实战指南

    2021-03-29 06:07:18
  • python日志记录模块实例及改进

    2021-03-14 12:49:09
  • SQL 语句中的通配符

    2007-10-11 18:03:00
  • Python学习之日志模块详解

    2022-06-24 09:20:22
  • 关于多域名COOKIES作用域的问题

    2008-11-18 16:12:00
  • PDO::errorInfo讲解

    2023-06-06 01:51:53
  • 如何用python绘制雷达图

    2023-04-19 12:44:09
  • Python基础之字符串常见操作经典实例详解

    2022-08-14 04:40:58
  • python如何正确的操作字符串

    2023-12-28 02:46:30
  • 为FCKeditor2.6添加行距功能(最新修改)

    2008-08-18 21:09:00
  • css2.1实现多重背景和边框效果

    2010-06-23 19:02:00
  • 对抗MySQL数据库解密高手

    2008-12-25 13:14:00
  • Python3使用requests发闪存的方法

    2021-06-09 16:07:20
  • Python使用LRU缓存策略进行缓存的方法步骤

    2023-09-03 05:00:06
  • Python实现朴素贝叶斯的学习与分类过程解析

    2023-08-17 15:15:23
  • python logging类库使用例子

    2023-10-31 11:17:11
  • Access 2003开发者扩展工具集概述

    2009-03-19 18:32:00
  • asp之家 网络编程 m.aspxhome.com