Python Pygame实现俄罗斯方块
作者:一个超会写Bug的安太狼 发布时间:2023-03-17 07:18:03
标签:python,pygame,俄罗斯方块
本文实例为大家分享了Python Pygame实现俄罗斯方块的具体代码,供大家参考,具体内容如下
源码:
# coding : utf-8
#: pip install pygame
import random
import sys
import pygame
#: 颜色定义
COLOR_WHITE = (255, 255, 255)
COLOR_BLACK = (0, 0, 0)
class Block:
"""小块"""
width = 24
height = 24
@staticmethod
def draw(s, left, top, color, bg_color):
pygame.draw.rect(s, bg_color, pygame.Rect(left, top, Block.width, Block.height))
pygame.draw.rect(s, color, pygame.Rect(left, top, Block.width - 1, Block.height - 1))
class Building:
"""积木"""
def __init__(self):
"""
方块的7种基本形状
每次初始化随机选择一个形状
@:return True / False
"""
self.form = random.choice(
[
[
[0, 0, 0, 0, 0],
[0, 0, 1, 0, 0],
[0, 1, 1, 1, 0],
[0, 0, 0, 0, 0],
[0, 0, 0, 0, 0]
],
[
[0, 0, 0, 0, 0],
[0, 0, 0, 0, 0],
[1, 1, 1, 1, 0],
[0, 0, 0, 0, 0],
[0, 0, 0, 0, 0]
],
[
[0, 0, 0, 0, 0],
[0, 1, 1, 0, 0],
[0, 0, 1, 1, 0],
[0, 0, 0, 0, 0],
[0, 0, 0, 0, 0]
],
[
[0, 0, 0, 0, 0],
[0, 0, 1, 1, 0],
[0, 1, 1, 0, 0],
[0, 0, 0, 0, 0],
[0, 0, 0, 0, 0]
],
[
[0, 0, 0, 0, 0],
[0, 1, 1, 0, 0],
[0, 0, 1, 0, 0],
[0, 0, 1, 0, 0],
[0, 0, 0, 0, 0]
],
[
[0, 0, 0, 0, 0],
[0, 0, 1, 1, 0],
[0, 0, 1, 0, 0],
[0, 0, 1, 0, 0],
[0, 0, 0, 0, 0]
],
[
[0, 0, 0, 0, 0],
[0, 1, 1, 0, 0],
[0, 1, 1, 0, 0],
[0, 0, 0, 0, 0],
[0, 0, 0, 0, 0]
]
])
def __getitem__(self, pos):
return self.form[pos]
def __setitem__(self, key, value):
self.form[key] = value
class Layout:
"""棋盘"""
def __init__(self):
self.block_x_count = 16;
self.block_y_count = 22;
self.layout = [[0 if 1 < i < self.block_x_count - 2 and j < self.block_y_count - 2 else 1
for i in range(self.block_x_count)] for j in range(self.block_y_count)]
@property
def size(self):
"""返回棋盘屏幕大小(width,height)"""
return (self.block_x_count * Block.width, self.block_y_count * Block.height)
def create_new_building(self):
"""
创建新的积木,初始化位置为第5,0格, 速度为4
:return: 返回是否无空间创建了
"""
self.building = Building()
self.building_left, self.building_top = 5, 0 #
self.drop_speed = 3
print(self.test_building_touch_wall())
return self.test_building_touch_wall()
@property
def speed(self):
return self.drop_speed
def test_building_touch_wall(self, x_offset=0, y_offset=0):
"""
积木是否已经触底/墙壁
具体操作:
判断积木最后一排的1,是否在当前棋牌对应的位置是也是1
@:param x_offset: x的偏移量 移动时可以传入1/-1来判断
@:param y_offset: y的偏移量 正常下落时可以传入1来判断
"""
for i in range(4, -1, -1):
for j in range(5):
if self.building[i][j]:
if self.layout[i + self.building_top + y_offset][j + self.building_left + x_offset]:
return True
return False
def move_left_right(self, x):
"""
左右移动
@:param x: 移动量 x_offset
"""
#: 移动时不能撞墙
if not self.test_building_touch_wall(x_offset=x):
self.building_left += x
def down_build(self):
""" 盒子的自动下移 """
self.building_top += 1
def direct_down(self):
""" 手动快速降落 """
self.drop_speed = 50
def convert_building(self):
"""
* 扭转盒子的总方位 (右转)
具体操作:
把第一竖排的倒序给第一横排的
把第二竖排的倒序给第二横排的
后面同理.
"""
new_box = [[0 for i in range(5)] for j in range(5)]
for i in range(5):
for j in range(4, -1, -1):
new_box[i][j] = self.building[4 - j][i]
self.building = new_box
def clear_full_lines(self):
"""消除满行的所有行"""
new_layout = [[0 if 1 < i < self.block_x_count - 2 and j < self.block_y_count - 2 else 1
for i in range(self.block_x_count)] for j in range(self.block_y_count)]
row_len = self.block_x_count - 4
new_row = self.block_y_count - 2 - 1
for cur_row in range(self.block_y_count - 2 - 1, 0, -1):
if sum(self.layout[cur_row][2:self.block_x_count - 2]) < row_len:
new_layout[new_row] = self.layout[cur_row]
new_row -= 1
self.layout = new_layout
def put_building_to_layout(self):
"""将积木放到棋盘里"""
for i in range(4, -1, -1):
for j in range(5):
if self.building[i][j]:
self.layout[i + self.building_top][j + self.building_left] = 1
#: 这里会调用消除函数
self.clear_full_lines()
def draw_building(self, s):
"""
显示积木
@:param s : pygame = screen
"""
cur_left, cur_top = self.building_left * Block.width, self.building_top * Block.height
for i in range(5):
for j in range(5):
# 只画积木实体,不管盒子本身
if self.building[j][i]:
Block.draw(s, cur_left + i * Block.width, cur_top + j * Block.height, COLOR_BLACK, COLOR_WHITE)
def draw(self, s):
"""
显示棋盘
@:param s : pygame = screen
"""
for i in range(self.block_x_count):
for j in range(self.block_y_count):
if self.layout[j][i] == 0:
Block.draw(s, i * Block.width, j * Block.height, COLOR_WHITE, COLOR_BLACK)
else:
Block.draw(s, i * Block.width, j * Block.height, COLOR_BLACK, COLOR_WHITE)
# -------------------------------------------------------------------
# Main
# -------------------------------------------------------------------
def main():
#: 初始化
while True:
layout = Layout()
layout.create_new_building()
pygame.init()
pygame.display.set_caption('俄罗斯方块')
screen = pygame.display.set_mode((layout.size), 0, 32)
is_over = False
#: 单局游戏循环开始 [结束后直接重新开始]
while not is_over:
#: 处理游戏消息
for e in pygame.event.get():
if e.type == pygame.QUIT:
sys.exit()
#: 处理按键
if e.type == pygame.KEYDOWN:
if e.key == pygame.K_UP:
layout.convert_building()
if e.key == pygame.K_DOWN:
layout.direct_down()
if e.key == pygame.K_LEFT:
layout.move_left_right(-1)
if e.key == pygame.K_RIGHT:
layout.move_left_right(1)
#: 是否碰触底部地面了,是 -> 融合背景 否 -> 继续下落
if layout.test_building_touch_wall(y_offset=1):
layout.put_building_to_layout()
is_over = layout.create_new_building()
else:
layout.down_build()
#: 绘制
layout.draw(screen)
layout.draw_building(screen)
pygame.display.update()
#: 速度
pygame.time.Clock().tick(layout.speed)
if __name__ == '__main__':
main()
效果:
更多俄罗斯方块精彩文章请点击专题:俄罗斯方块游戏集合 进行学习。
来源:https://blog.csdn.net/m0_46278037/article/details/113851443
0
投稿
猜你喜欢
- MD5(Message-Digest Algorithm 5) 模块用于计算信息密文(信息摘要),得出一个128位的密文。sha模块跟md5
- 往期学习:python数据类型: python数据结构:数据类型.python的输入输出: python数据结构之输入输出及控制和异常.py
- 在SQL Server中进行开发会让你身处险地,并且寻找快速解决方案。我们编辑了前十名关于SQL Server开发的常见问题。对常见的针对表
- MySQL的本地备份和双机相互备份脚本:首先,我们需要修改脚本进行必要的配置,然后以root用户执行。◆1. 第一执行远程备份时先用 fir
- opendir – 打开一个目录句柄,可用于之后的 closedir(),readdir() 和 rewinddir()
- 纳什均衡是一种博弈论中的概念,它描述了一种平衡状态,其中每个参与者都不能通过独立改变其决策来提高自己的利益。在 Python 中,可以使用一
- matplotlib官方除了提供了鼠标十字光标的示例,还提供了同一图像内多子图共享光标的示例,其功能主要由widgets模块中的MultiC
- 写在前面的话:Part 1记得刚毕业那时,常幻想着自己是个大艺术家,满怀憧憬的想找一份理想的工作。后来入了行,慢慢的发现自己好像不是这块料;
- MySQL数据库由于它本身的小巧和操作的高效, 在数据库应用中越来越多的被采用.本文中列举了一个P2P应用开发实例,实例中使用了MySQL来
- 说明和代码如下:<%@ language = vbscript%><% 
- 设计师不等于美工设计无所不在,但大多数企业不知道如何使用它。现代设计进入中国大概是二十多年的时间,而在国外,尤其在美国在欧洲,大概有一百年的
- Python 如何转换string到float?简单几步,让你轻松解决。打开软件,新建python项目,如图所示右键菜单中创建.py文件,如
- 首先你要确保你机器上面安装了python,其次,你还要确保你上面安装了Django。接下来,才能进入到搭建第一个Django应用程序很简单的
- 如何用表单在线建立目录?很简单,两个文件就搞定了:creatfolder.htm' 表单文件<form n
- ansible 简介ansible 是什么?ansible是新出现的自动化运维工具,基于Python开发,集合了众多运维工具(puppet、
- 让我们重温一下JavaScript的一些基础知识,请先写出以下代码中问号处的答案,再运行比较!<script type=&q
- str_replace — 子字符串替换 [str_replace]mixed str_replace ( mixed
- 本文实例讲述了Python数据分析之获取双色球历史信息的方法。分享给大家供大家参考,具体如下:每个人都有一颗中双色球大奖的心,对于技术人员来
- 这次我主要讲解如何用Python基于Flask的登录和注册,验证方式采用Basic Auth 主要用以下库import os#Flask的基
- theme: channing-cyan网页伪静态将 * 页伪装成静态网页,可以提升网页被搜索引擎检索道德概率表现形式为:网址看着像是一个具