依然是基于pygame库

import pygame
import random
import ospygame.init()GRID_WIDTH = 20
GRID_NUM_WIDTH = 15
GRID_NUM_HEIGHT = 25
WIDTH, HEIGHT = GRID_WIDTH * GRID_NUM_WIDTH, GRID_WIDTH * GRID_NUM_HEIGHT
SIDE_WIDTH = 200
SCREEN_WIDTH = WIDTH + SIDE_WIDTH
WHITE = (0xff, 0xff, 0xff)
BLACK = (0, 0, 0)
LINE_COLOR = (0x33, 0x33, 0x33)CUBE_COLORS = [(0xcc, 0x99, 0x99), (0xff, 0xff, 0x99), (0x66, 0x66, 0x99),(0x99, 0x00, 0x66), (0xff, 0xcc, 0x00), (0xcc, 0x00, 0x33),(0xff, 0x00, 0x33), (0x00, 0x66, 0x99), (0xff, 0xff, 0x33),(0x99, 0x00, 0x33), (0xcc, 0xff, 0x66), (0xff, 0x99, 0x00)
]screen = pygame.display.set_mode((SCREEN_WIDTH, HEIGHT))
pygame.display.set_caption("俄罗斯方块")
clock = pygame.time.Clock()
FPS = 30score = 0
level = 1screen_color_matrix = [[None] * GRID_NUM_WIDTH for i in range(GRID_NUM_HEIGHT)]# 设置游戏的根目录为当前文件夹
base_folder = os.path.dirname(__file__)def show_text(surf, text, size, x, y, color=WHITE):font_name = os.path.join(base_folder, 'Fonts\simsun.ttc')font = pygame.font.Font(font_name, size)text_surface = font.render(text, True, color)text_rect = text_surface.get_rect()text_rect.midtop = (x, y)surf.blit(text_surface, text_rect)
class CubeShape(object):SHAPES = ['I', 'J', 'L', 'O', 'S', 'T', 'Z']I = [[(0, -1), (0, 0), (0, 1), (0, 2)],[(-1, 0), (0, 0), (1, 0), (2, 0)]]J = [[(-2, 0), (-1, 0), (0, 0), (0, -1)],[(-1, 0), (0, 0), (0, 1), (0, 2)],[(0, 1), (0, 0), (1, 0), (2, 0)],[(0, -2), (0, -1), (0, 0), (1, 0)]]L = [[(-2, 0), (-1, 0), (0, 0), (0, 1)],[(1, 0), (0, 0), (0, 1), (0, 2)],[(0, -1), (0, 0), (1, 0), (2, 0)],[(0, -2), (0, -1), (0, 0), (-1, 0)]]O = [[(0, 0), (0, 1), (1, 0), (1, 1)]]S = [[(-1, 0), (0, 0), (0, 1), (1, 1)],[(1, -1), (1, 0), (0, 0), (0, 1)]]T = [[(0, -1), (0, 0), (0, 1), (-1, 0)],[(-1, 0), (0, 0), (1, 0), (0, 1)],[(0, -1), (0, 0), (0, 1), (1, 0)],[(-1, 0), (0, 0), (1, 0), (0, -1)]]Z = [[(0, -1), (0, 0), (1, 0), (1, 1)],[(-1, 0), (0, 0), (0, -1), (1, -1)]]SHAPES_WITH_DIR = {'I': I, 'J': J, 'L': L, 'O': O, 'S': S, 'T': T, 'Z': Z}def __init__(self):self.shape = self.SHAPES[random.randint(0, len(self.SHAPES) - 1)]# 骨牌所在的行列self.center = (2, GRID_NUM_WIDTH // 2)self.dir = random.randint(0, len(self.SHAPES_WITH_DIR[self.shape]) - 1)self.color = CUBE_COLORS[random.randint(0, len(CUBE_COLORS) - 1)]def get_all_gridpos(self, center=None):curr_shape = self.SHAPES_WITH_DIR[self.shape][self.dir]if center is None:center = [self.center[0], self.center[1]]return [(cube[0] + center[0], cube[1] + center[1])for cube in curr_shape]def conflict(self, center):for cube in self.get_all_gridpos(center):# 超出屏幕之外,说明不合法if cube[0] < 0 or cube[1] < 0 or cube[0] >= GRID_NUM_HEIGHT or \cube[1] >= GRID_NUM_WIDTH:return True# 不为None,说明之前已经有小方块存在了,也不合法if screen_color_matrix[cube[0]][cube[1]] is not None:return Truereturn Falsedef rotate(self):new_dir = self.dir + 1new_dir %= len(self.SHAPES_WITH_DIR[self.shape])old_dir = self.dirself.dir = new_dirif self.conflict(self.center):self.dir = old_dirreturn Falsedef down(self):# import pdb; pdb.set_trace()center = (self.center[0] + 1, self.center[1])if self.conflict(center):return Falseself.center = centerreturn Truedef left(self):center = (self.center[0], self.center[1] - 1)if self.conflict(center):return Falseself.center = centerreturn Truedef right(self):center = (self.center[0], self.center[1] + 1)if self.conflict(center):return Falseself.center = centerreturn Truedef draw(self):for cube in self.get_all_gridpos():pygame.draw.rect(screen, self.color,(cube[1] * GRID_WIDTH, cube[0] * GRID_WIDTH,GRID_WIDTH, GRID_WIDTH))pygame.draw.rect(screen, WHITE,(cube[1] * GRID_WIDTH, cube[0] * GRID_WIDTH,GRID_WIDTH, GRID_WIDTH),1)
def draw_grids():for i in range(GRID_NUM_WIDTH):pygame.draw.line(screen, LINE_COLOR,(i * GRID_WIDTH, 0), (i * GRID_WIDTH, HEIGHT))for i in range(GRID_NUM_HEIGHT):pygame.draw.line(screen, LINE_COLOR,(0, i * GRID_WIDTH), (WIDTH, i * GRID_WIDTH))pygame.draw.line(screen, WHITE,(GRID_WIDTH * GRID_NUM_WIDTH, 0),(GRID_WIDTH * GRID_NUM_WIDTH, GRID_WIDTH * GRID_NUM_HEIGHT))
def draw_matrix():for i, row in zip(range(GRID_NUM_HEIGHT), screen_color_matrix):for j, color in zip(range(GRID_NUM_WIDTH), row):if color is not None:pygame.draw.rect(screen, color,(j * GRID_WIDTH, i * GRID_WIDTH,GRID_WIDTH, GRID_WIDTH))pygame.draw.rect(screen, WHITE,(j * GRID_WIDTH, i * GRID_WIDTH,GRID_WIDTH, GRID_WIDTH), 2)
def draw_score():show_text(screen, u'得分:{}'.format(score), 20, WIDTH + SIDE_WIDTH // 2, 100)
def remove_full_line():global screen_color_matrixglobal scoreglobal levelnew_matrix = [[None] * GRID_NUM_WIDTH for i in range(GRID_NUM_HEIGHT)]index = GRID_NUM_HEIGHT - 1n_full_line = 0for i in range(GRID_NUM_HEIGHT - 1, -1, -1):is_full = Truefor j in range(GRID_NUM_WIDTH):if screen_color_matrix[i][j] is None:is_full = Falsecontinueif not is_full:new_matrix[index] = screen_color_matrix[i]index -= 1else:n_full_line += 1score += n_full_linelevel = score // 20 + 1screen_color_matrix = new_matrix
def show_welcome(screen):show_text(screen, u'俄罗斯方块', 30, WIDTH / 2, HEIGHT / 2)show_text(screen, u'按任意键开始游戏', 20, WIDTH / 2, HEIGHT / 2 + 50)
running = True
gameover = True
counter = 0
live_cube = None
while running:clock.tick(FPS)for event in pygame.event.get():if event.type == pygame.QUIT:running = Falseelif event.type == pygame.KEYDOWN:if gameover:gameover = Falselive_cube = CubeShape()breakif event.key == pygame.K_LEFT:live_cube.left()elif event.key == pygame.K_RIGHT:live_cube.right()elif event.key == pygame.K_DOWN:live_cube.down()elif event.key == pygame.K_UP:live_cube.rotate()elif event.key == pygame.K_SPACE:while live_cube.down() == True:passremove_full_line()# level 是为了方便游戏的难度,level 越高 FPS // level 的值越小# 这样屏幕刷新的就越快,难度就越大if gameover is False and counter % (FPS // level) == 0:# down 表示下移骨牌,返回False表示下移不成功,可能超过了屏幕或者和之前固定的# 小方块冲突了if live_cube.down() == False:for cube in live_cube.get_all_gridpos():screen_color_matrix[cube[0]][cube[1]] = live_cube.colorlive_cube = CubeShape()if live_cube.conflict(live_cube.center):gameover = Truescore = 0live_cube = Nonescreen_color_matrix = [[None] * GRID_NUM_WIDTH for i in range(GRID_NUM_HEIGHT)]# 消除满行remove_full_line()counter += 1# 更新屏幕screen.fill(BLACK)draw_grids()draw_matrix()draw_score()if live_cube is not None:live_cube.draw()if gameover:show_welcome(screen)pygame.display.update()

 关于可能报错的地方:

'Fonts\simsun.ttc' 代表Fonts 文件夹下的字体文件simsun.ttc,Fonts文件夹与俄罗斯方块游戏代码文件在同一目录下

simsun.ttc字体文件在C:\Windows\Fonts文件夹下可以自由选择,文中使用新宋体常规,查看属性显示详细文件名

运行结果: 

操作方式:经典方向键操作;快速单次点击方向键可加快移动速度,空格键可以实现一键落下

详细参考:

https://www.jb51.net/article/234740.htm

python快速实现简易俄罗斯方块小游戏相关推荐

  1. python快速实现数字华容道小游戏

    华容道,中国历史地名.据<资治通鉴>注释中的说法,就是"从此道可至华容也".这里所说的华容,当然是指华容县城.华容道也就是赤壁战争中曹军逃入华容县界后向华容县城逃跑的路 ...

  2. python快速实现简易中国象棋游戏

    游戏所需所有图片资源如下: 游戏完整代码如下(依然主要依赖于pygame库): 如果运行报错就只可能是由于你的项目放置在其它文件夹下或IDE内部原因导致部分图片资源路径问题,将多个pygame.ima ...

  3. Python编写俄罗斯方块小游戏

    俄罗斯方块是俄罗斯人发明的一款休闲类的小游戏,这款小游戏可以说是很多人童年的主打电子游戏了,本文我们使用 Python 来实现这款小游戏. 很多人学习python,不知道从何学起. 很多人学习pyth ...

  4. 用 Python 写个俄罗斯方块小游戏

    前言 本文的文字及图片来源于网络,仅供学习.交流使用,不具有任何商业用途,版权归原作者所有,如有问题请及时联系我们以作处理. 作者:python技术 PS:如有需要Python学习资料的小伙伴可以加点 ...

  5. Python编写人机对战小游戏(抓狐狸)(2)

    封面图片:<中学生可以这样学Python>,董付国.应根球著,清华大学出版社 =========== 很久很久以前,在公众号里推送过一个抓狐狸游戏,详见Python编写人机对战小游戏(抓小 ...

  6. pyqt5制作俄罗斯方块小游戏-----源码解析

    一.前言 最近学习pyqt5中文教程时,最后一个例子制作了一个俄罗斯方块小游戏,由于解释的不是很清楚,所以源码有点看不懂,查找网上资料后,大概弄懂了源码的原理. 二.绘制主窗口 将主窗口居中,且设置了 ...

  7. c++ 小游戏_C/C++编程新手入门基础系列:俄罗斯方块小游戏制作源代码

    这篇文章主要为大家详细介绍了C语言实现俄罗斯方块小游戏,具有一定的参考价值,感兴趣的小伙伴们可以参考一下. 1.要先下载一个 graphics.h 的头文件来绘图. 2.初始化窗口:initgraph ...

  8. 基于stm32、0.96寸OLED实现的俄罗斯方块小游戏(详细源码注释)

    概述:本实验基于stm32最小系统.0.96寸OLED(68*128)和摇杆实现一个经典的俄罗斯方块小游戏.项目源码地址:点击下载. 硬件要求: 普通摇杆,两个电位器和一个开关组成,左右摇动控制一个电 ...

  9. 3d游戏编程大师技巧 源代码_C/C++编程入门基础系列:俄罗斯方块小游戏制作,直接源代码分享...

    这篇文章主要为大家详细介绍了C语言实现俄罗斯方块小游戏,具有一定的参考价值,感兴趣的小伙伴们可以参考一下 1.要先下载一个 graphics.h 的头文件来绘图. 2.初始化窗口:initgraph( ...

最新文章

  1. python中degree什么意思_解读Python中degrees()方法的使用
  2. 用select 语句中的START WITH...CONNECT BY PRIOR子句实现递归查询
  3. POI的入门:单元格样式处理
  4. css改变指针形状,css 指针样式
  5. mysql datetime month不走索引_like百分号加前面一定不走索引吗?一不小心就翻车,关于mysql索引那些容易错的点...
  6. 好玩gan_效果超赞服务器挤爆!用GAN生成人像油画火了,带你一秒回到文艺复兴...
  7. javascript小技巧(转)
  8. 1014-新浪微博(数据 cell )
  9. 【UML】使用环境(转)
  10. Java基础学习总结(79)——Java本地接口JNI详解
  11. 微信小程序 自定义顶部导航栏标题 navigationStyle
  12. 恶意软件清理助手V2.6.3 build 005 2007-07-05
  13. Kali 渗透测试:基于结构化异常处理的渗透-使用Python编写渗透模块
  14. 【平面图理论】平面图学习笔记
  15. 算法笔记(胡凡)刷题收获@Kaysen
  16. 2017 码云最火开源项目 TOP 50
  17. 使用EDAS投稿系统进行论文投稿时常遇到的问题及解决方法
  18. 人机大战(类和对象)
  19. 程序员相亲 满屏尴尬
  20. 博时基金数据开发面经

热门文章

  1. sqlserver直接取整_SQLSERVER取整并“看到要害处”
  2. 希腊字母的发音(希腊人的发音)
  3. [二维区间DP?] Atcoder ARC004E. Salvage Robots
  4. ESXI 6.7 环境 centos7.6 虚拟机安装tesla k80 显卡驱动失败问题解决
  5. 干货 I 用数据分析进行“无死角”的复盘?
  6. (已更新)新版帝国cms内核试玩佣金WAP手机版网站源码,可打包APP
  7. redisson究极爽文-手把手带你实现redisson的发布订阅,消息队列,延迟队列(死信队列),(模仿)分布式线程池
  8. 心脏滴血漏洞(CVE-2014-0160)
  9. 分布式CAP精彩故事
  10. 使用word2vec训练词向量