python游戏实战项目之俄罗斯方块的魅力
作者:顾木子吖 发布时间:2021-12-07 19:38:23
标签: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)当每行铺满之后会得分,相应的消失一行:
'''移出整行都有小方块的'''
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
效果图:
总结
哈哈哈!好啦!按住方向键也可以变形的哈!赶快试试~
来源:https://blog.csdn.net/weixin_55822277/article/details/120083675


猜你喜欢
- 环境准备好了!我们怎么使用这些东东?IIS用组件初始化是用这个过程Public Sub OnStartPage给个使用asp组件的例子:数字
- 本节讲述单选框/下拉菜单/添加文件,综合css,html和JavaScript实现的,具体详情如下所示:单选框:实现的功能是:(类似平时的性
- 一、所需工具**Python版本:**3.5.4(64bit)二、相关模块opencv_python模块sklearn模块numpy模块dl
- 本文主要解决两个问题,第一个,在element-ui中,直接设置参数排序,达不到预期效果,预期是按照数字的大小进行排序;第二个,想对表格中某
- 前言排序是数据库中的一个基本功能,MySQL也不例外。用户通过Order by语句即能达到将指定的结果集排序的目的,其实不仅仅是Order
- 本文实例分析了AngularJS框架的ng-app指令与自动加载实现方法。分享给大家供大家参考,具体如下:ng-app是angular的一个
- 目录简介快速使用格式时区cli总结参考简介不管什么时候,处理时间总是让人头疼的一件事情。因为时间格式太多样化了,再加上时区,夏令时,闰秒这些
- vuex状态刷新网页时数据被清空问题vuex状态管理,在网页刷新数据被清空的解决方法。在main.js中写入下面的代码段(亲测有效)//刷新
- 题目:用 JavaScript 代码实现空位补零,比如 pad(12, 3) => 012实现一:/* 平淡无奇法 */functio
- 1.zip用法简介在python 3.x系列中,zip方法返回的为一个zip object可迭代对象。class zip(object):&
- 图例如下https://github.com/Dongvdong/python_Smartvoice上电后,只要周围声音超过 2000,开始
- 列表的添加1)+ 添加2)append 追加一次只能添加一个元素到列表中,适合用于循环里3)extend 拉伸可一次添加多个元素到列表中4)
- 功能介绍 (需要版本5.0.45)大数据操作ORM性能瓶颈在实体转换上面,并且不能使用常规的Sql去实现当列越多转换越慢,SqlSugar将
- 在CSS中,模式(pattern)匹配规则决定那种样式规则应用于文档树(document tree)的哪个元素。这些模式叫着选择符(sele
- SQL Update常见写法Oralce和DB2都支持的语法:update test1?set (test1.name,test1.age)
- 现在公布方法:替换editor.js 函数 // Toolbar button onmouseup
- 一、备份数据库1、打开SQL企业管理器,在控制台根目录中依次点开Microsoft SQL Server2、SQL Server组-->
- 本文实例展示了Python Tkinter基础控件的用法,分享给大家供大家参考之用。具体方法如下:# -*- coding: utf-8 -
- I. 前言联邦学习(Federated Learning) 是人工智能的一个新的分支,这项技术是谷歌2016年于论文Communicatio
- 需求背景:进行分值计算。如下图,如果只是一两个还好说,写写判断,但是如果有几十个,几百个,会不会惨不忍睹。而且,下面的还是三种情况。例如:解