俄罗斯方块:Python实现

时间:2022-09-23 14:45:30

网上搜到一个Pygame写的俄罗斯方块(tetris),大部分看懂的前提下增加了注释,Fedora19下运行OK的

主程序:

#coding:utf8
#! /usr/bin/env python
# 注释说明:shape表示一个俄罗斯方块形状 cell表示一个小方块
import sys
from random import choice
import pygame
from pygame.locals import *
from block import O, I, S, Z, L, J, T COLS = 16
ROWS = 20
CELLS = COLS * ROWS
CELLPX = 32 # 每个cell的像素宽度
POS_FIRST_APPEAR = COLS / 2
SCREEN_SIZE = (COLS * CELLPX, ROWS * CELLPX)
COLOR_BG = (0, 0, 0) def draw(grid, pos=None):
# grid是一个list,要么值为None,要么值为'Block'
# 非空值在eval()的作用下,用于配置颜色
if pos: # 6x5
s = pos - 3 - 2 * COLS # upper left position
for p in range(0, COLS):
q = s + p * COLS
for i in range(q, q + 6):
if 0 <= i < CELLS:
# 0 <=i < CELLS:表示i这个cell在board内部。
c = eval(grid[i] + ".color") if grid[i] else COLOR_BG
# 执行着色。shape的cell涂对应的class设定好的颜色,否则涂黑(背景色)
a = i % COLS * CELLPX
b = i / COLS * CELLPX
screen.fill(c, (a, b, CELLPX, CELLPX))
else: # all
screen.fill(COLOR_BG)
for i, occupied in enumerate(grid):
if occupied:
c = eval(grid[i] + ".color") # 获取方块对应的颜色
a = i % COLS * CELLPX # 横向长度
b = i / COLS * CELLPX # 纵向长度
screen.fill(c, (a, b, CELLPX, CELLPX))
# fill:为cell上色, 第二个参数表示rect
pygame.display.flip()
# 刷新屏幕 def phi(grid1, grid2, pos): # 4x4
# 两个grid之4*4区域内是否会相撞(冲突)
s = pos - 2 - 1 * COLS # upper left position
for p in range(0, 4):
q = s + p * COLS
for i in range(q, q + 4):
try:
if grid1[i] and grid2[i]:
return False
except:
pass
return True def merge(grid1, grid2):
# 合并两个grid
grid = grid1[:]
for i, c in enumerate(grid2):
if c:
grid[i] = c
return grid def complete(grid):
# 减去满行
n = 0
for i in range(0, CELLS, COLS):
# 步长为一行。
if not None in grid[i:i + COLS]:
#这一句很容易理解错误。
#实际含义是:如果grid[i:i + COLS]都不是None,那么执行下面的语句
grid = [None] * COLS + grid[:i] + grid[i + COLS:]
n += 1
return grid, n
#n表示减去的行数,用作统计分数 pygame.init()
pygame.event.set_blocked(None)
pygame.event.set_allowed((KEYDOWN, QUIT))
pygame.key.set_repeat(75, 0)
pygame.display.set_caption('Tetris')
screen = pygame.display.set_mode(SCREEN_SIZE)
pygame.display.update() grid = [None] * CELLS
speed = 500
screen.fill(COLOR_BG)
while True: # spawn a block
block = choice([O, I, S, Z, L, J, T])()
pos = POS_FIRST_APPEAR
if not phi(grid, block.grid(pos), pos): break # you lose
pygame.time.set_timer(KEYDOWN, speed)
# repeatedly create an event on the event queue
# speed是时间间隔。。。speed越小,方块下落的速度越快。。。speed应该换为其他名字 while True: # move the block
draw(merge(grid, block.grid(pos)), pos)
event = pygame.event.wait()
if event.type == QUIT: sys.exit()
try:
aim = {
K_UNKNOWN: pos+COLS,
K_UP: pos,
K_DOWN: pos+COLS,
K_LEFT: pos-1,
K_RIGHT: pos+1,
}[event.key]
except KeyError:
continue
if event.key == K_UP:
# 变形
block.rotate() elif event.key in (K_LEFT, K_RIGHT) and pos / COLS != aim / COLS:
# pos/COLS表示当前位置所在行
# aim/COLS表示目标位置所在行
# 此判断表示,当shape在左边界时,不允许再向左移动(越界。。),在最右边时向右也禁止
continue grid_aim = block.grid(aim)
if grid_aim and phi(grid, grid_aim, aim):
pos = aim
else:
if event.key == K_UP:
block.rotate(times=3)
elif not event.key in (K_LEFT, K_RIGHT):
break grid = merge(grid, block.grid(pos))
grid, n = complete(grid)
if n:
draw(grid)
speed -= 5 * n
if speed < 75: speed = 75

调用的模块:

#coding:utf-8
#! /usr/bin/env python
COLS = 16
ROWS = 20 class Block():
color = (255,255,255)
def __init__(self):
self._state = 0
def __str__(self):
return self.__class__.__name__
def _orientations(self):
raise NotImplementedError()
def rotate(self, times=1):
for i in range(times):
if len(self._orientations())-1 == self._state:
self._state = 0
#只要_state比_orientations长度-1还要小,就让_state加1 else:
self._state += 1
def blades(self):
# 返回对应形状的一种旋转形状。(返回一个list,list中每个元素是一个(x,y))
return self._orientations()[self._state] def grid(self, pos, cols=COLS, rows=ROWS):
# grid()函数:对于一个形状,从它的cell中的pos位置,按照orientations的位置提示,把所有cell涂色
# pos表示的是shape中的一个cell,也就是(0,0)
if cols*rows <= pos:
return None
# 这种情况应该不可能出现吧。如果出现<=的情况
# 那么,pos都跑到界外了。。 grid = [None] * cols * rows
grid[pos] = str(self)
for b in self.blades():
x, y = b
# pos/cols表示pos处于board的第几行
if pos/cols != (pos+x)/cols:
return None
i = pos + x + y * cols
if i < 0:
continue
elif cols*rows <= i:
return None
grid[i] = str(self)
# 给相应的其他位置都“涂色”,比如对于方块,是O型的,那么pos肯定是有值的,pos位于有上角。。
return grid # 以下每个形状class,_orientations()都返回形状的列表。(0,0)一定被包含在其中,为了省略空间所以都没有写出.
class O(Block):
color = (207,247,0)
def _orientations(self):
return (
[(-1,0), (-1,1), (0,1)],
)
class I(Block):
color = (135,240,60)
def _orientations(self):
return (
[(-2,0), (-1,0), (1,0)],
[(0,-1), (0,1), (0,2)],
)
class S(Block):
color = (171,252,113)
def _orientations(self):
return (
[(1,0), (-1,1), (0,1)],
[(0,-1), (1,0), (1,1)],
)
class Z(Block):
color = (243,61,110)
def _orientations(self):
return (
[(-1,0), (0,1), (1,1)],
[(1,-1), (1,0), (0,1)],
)
class L(Block):
color = (253,205,217)
def _orientations(self):
return (
[(-1,1), (-1,0), (1,0)],
[(0,-1), (0,1), (1,1)],
[(-1,0), (1,0), (1,-1)],
[(-1,-1), (0,-1), (0,1)],
)
class J(Block):
color = (140,180,225)
def _orientations(self):
return (
[(-1,0), (1,0), (1,1)],
[(0,1), (0,-1), (1,-1)],
[(-1,-1), (-1,0), (1,0)],
[(-1,1), (0,1), (0,-1)],
)
class T(Block):
color = (229,251,113)
def _orientations(self):
return (
[(-1,0), (0,1), (1,0)],
[(0,-1), (0,1), (1,0)],
[(-1,0), (0,-1), (1,0)],
[(-1,0), (0,-1), (0,1)],
)

俄罗斯方块:Python实现的更多相关文章

  1. Python:游戏:扫雷(附源码)

    这次我们基于 pygame 来做一个扫雷,上次有园友问我代码的 python 版本,我说明一下,我所有的代码都是基于 python 3.6 的. 先看截图,仿照 XP 上的扫雷做的,感觉 XP 上的样 ...

  2. pyqt4&lowbar;应用例子(计算器,对话框,进度条,日历等等)

    sklearn实战-乳腺癌细胞数据挖掘(博客主亲自录制视频教程) https://study.163.com/course/introduction.htm?courseId=1005269003&a ...

  3. 趣味python编程之经典俄罗斯方块

    国庆期间闲不住,用python把经典俄罗斯方块实现了一遍,找到了些儿时的乐趣.因此突发奇想,打算用python写点经典又确实有趣的小程序形成系列.正统编程之余也给自己找点儿乐趣,换个角度写程序. 原计 ...

  4. Python:游戏:300行代码实现俄罗斯方块

    本文代码基于 python3.6 和 pygame1.9.4. 俄罗斯方块是儿时最经典的游戏之一,刚开始接触 pygame 的时候就想写一个俄罗斯方块.但是想到旋转,停靠,消除等操作,感觉好像很难啊, ...

  5. python游戏开发之俄罗斯方块&lpar;一&rpar;:简版

    编程语言:python(3.6.4) 主要应用的模块:pygame (下面有源码,但是拒绝分享完整的源码,下面的代码整合起来就是完整的源码) 首先列出我的核心思路: 1,图像由"核心变量&q ...

  6. Python 写一个俄罗斯方块游戏

    使用 Python 的 PyGame 库写一个俄罗斯方块游戏的逐步指南 很多人学习python,不知道从何学起.很多人学习python,掌握了基本语法过后,不知道在哪里寻找案例上手.很多已经做案例的人 ...

  7. 用Python实现童年的回忆:俄罗斯方块!别说还挺好玩

    在那个电子产品比较匮乏的年代,小游戏机

  8. python tkinter实现俄罗斯方块 基础版

    本项目最终效果见本人b站投稿av81480858 简介部分 本项目最终代码已上传github: https://github.com/BigShuang/Tetris 的1_BASIC文件夹 .其中1 ...

  9. 吴裕雄--天生自然python学习笔记:python 用pygame模块开发俄罗斯方块游戏

    俄罗斯方块游戏 多年前,游戏机中最流行的游戏就是“俄罗斯方块”了.时至今日,虽然网络 游戏日新月异 ,但“俄罗斯方块”这款小游戏仍在许多人心中 占有一席之地.本例中, 我们将亲手设计一个简单的俄罗斯方 ...

随机推荐

  1. struts2 提供的校验器列表

  2. zw版【转发&&num;183&semi;*nvp系列Delphi例程】HALCON HWindowX 02

    zw版[转发·*nvp系列Delphi例程]HALCON HWindowX 02 procedure TForm1.Button1Click(Sender: TObject);var img : H ...

  3. c&plus;&plus; 字符串流 sstream(常用于格式转换) 分类: C&sol;C&plus;&plus; 2014-11-08 17&colon;20 150人阅读 评论&lpar;0&rpar; 收藏

    使用stringstream对象简化类型转换 C++标准库中的<sstream>提供了比ANSI C的<stdio.h>更高级的一些功能,即单纯性.类型安全和可扩展性.在本文中 ...

  4. vue-cli如何引入bootstrap工具

    以下操作以正常安装node环境为前提. 1.引入jq: 在npm控制台中,进入项目目录,然后输入指令npm install jquery --save-dev(npm换成cnpm更好,国内环境下使用c ...

  5. sqlserver 全库查询 带架构

    网上现有的全库查询,无法识别自定义架构的数据库结构: ) ) ) declare @counts int )--以上定义变量 declare cur1 cursor for select a.name ...

  6. ABP官方文档翻译 8&period;1 通知系统

    通知系统 介绍 发送模型 通知类型 通知数据 通知严重性 关于通知持久化 订阅通知 发布通知 用户通知管理 实时通知 客户端 通知存储 通知定义 介绍 在系统中通知用来基于特定的事件告知用户.ABP提 ...

  7. Error&colon; Cannot fit requested classes in a single dex file &lpar;&num; methods&colon; 149346 &gt&semi; 65536&rpar;

    引用第三方库的jar文件,都有可能会触发这个错误.解决方案如下: 参考<[Android]Cannot fit requested classes in a single dex file. T ...

  8. Iview的小经验

    1.动态控制form验证的小红星 HTML部分: <FormItem :class="{requireStar:bankFlag1}"> CSS部分: /*动态必填项c ...

  9. win10远程桌面连接提示身份验证错误,要求的函数不受支持的解决方案

    转自https://www.baidu.com/link?url=67JXh4h79mN47mEenuH_ElGkSh9_GdOiY-Xp9Ihw0_mQIZHrPx-HxY3EIm_nTZKPoRZ ...

  10. Performance testing test scenarios

    1 check if page load time is within acceptable range2 check page load on slow connections 3 check re ...