用Python做一个俄罗斯方块
2022/2/5 14:43:22
本文主要是介绍用Python做一个俄罗斯方块,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
全部组件
俄罗斯方块python小程序(免费)
学习前的准备
- python编译器
- pygame库和random库
- 清醒的头脑
- 一双能
三连实践的双手
让我们开始吧
导入
import pygame from pygame import locals import random pygame.init() # 初始化
pygame库可以做一些小游戏,如果报这个错的话(见#1)就打开cmd,输入(见#2)就可以解决了。
#1:
ModuleNotFoundError: No module named 'pygame'
#2:
pip install pygame
创建窗口
# 创建窗口 screen = pygame.display.set_mode((460, 500)) pygame.display.set_caption("俄罗斯方块") clock = pygame.time.Clock() # pygame时钟,控制游戏速度(帧数)
载入
素材
# 载入素材 background = pygame.image.load('bg.png') font = pygame.font.Font('STKAITI.TTF', 60) # 字体 fontovor = pygame.font.Font('STKAITI.TTF', 100)
素材都打包进了俄罗斯方块python小程序(免费)-Python文档类资源-CSDN下载
形状
# 俄罗斯方块所有形状 O = [[(0, 0), (0, 1), (1, 0), (1, 1)]] I = [[(0, -1), (0, 0), (0, 1), (0, 2)], [(-1, 0), (0, 0), (1, 0), (2, 0)]] Z = [[(0, -1), (0, 0), (1, 0), (1, 1)], [(-1, 0), (0, 0), (0, -1), (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)]] J = [[(-1, 0), (0, 0), (1, 0), (1, -1)], [(0, -1), (0, 0), (0, 1), (-1, -1)], [(-1, 0), (0, 0), (1, 0), (-1, 1)], [(0, -1), (0, 0), (0, 1), (1, 1)]] L = [[(-1, 0), (0, 0), (1, 0), (1, 1)], [(0, -1), (0, 0), (0, 1), (1, -1)], [(-1, 0), (0, 0), (1, 0), (-1, -1)], [(0, -1), (0, 0), (0, 1), (-1, 1)]] shape_list = [I, J, L, O, S, T, Z] # 7种类型俄罗斯方块
颜色
# 一些RGB颜色 cube_colors = [ (204, 153, 153), (102, 102, 153),(153, 0, 102), (255, 204, 0), (204, 0, 51),(255, 0, 51), (0, 102, 153), (153, 0, 51), (204, 255, 102), (255, 153, 0)]
画地图
num_list=[] for i in range(25): num_list.append([0]*15)
防虫网 防Bug函数
def check(center): for cube in current_shape: cube = (cube[0] + center[0], cube[1] + center[1]) # 当前的小方块超出网格的行、列,就返回False if cube[0] < 1 or cube[1] < 1 or cube[0] > grid_num_height \ or cube[1] >grid_num_width: return False if num_list[cube[0]-1][cube[1]-1] != 0: return False
用来防止方块起飞
主体
循环
while True:
侦测
for event in pygame.event.get(): if event.type == locals.QUIT: exit() if event.type == locals.KEYDOWN: if gameovor==True: gameovor=False if event.key == locals.K_LEFT: # 向左 center[1] = center[1] - 1 # 左移1列 if check(center) == False: center[1] = center[1] + 1 # 右移1列 elif event.key == locals.K_RIGHT: # 向右 center[1] = center[1] + 1 if check(center) == False: center[1] = center[1] - 1 elif event.key == locals.K_DOWN: # 向下 center[0] = center[0] + 1 if check(center) == False: center[0] = center[0] - 1 elif event.key == locals.K_UP: # 向上键 old_index = index index += 1 if index >= len(shape): index = 0 current_shape = shape[index] if check(center) == False: index = old_index current_shape = shape[index]
此处左右键移动,下键加速,上键旋转。
游玩
游戏结束为假
if gameovor==False:
方块掉落
if states == False: states = True center = [2, 8] # 第2行第8列 shape = random.choice(shape_list) index = random.randint(0, len(shape)-1) # 随机形状索引 current_shape = shape[index] color = random.choice(cube_colors) # 随机选取一种颜色
掉落速度
count += 1 if count % FPS == 0: # 降落速度的算式 center[0] = center[0] + 1 if check(center) == False: center[0] = center[0] - 1 states = False for cube in current_pos: num_list[cube[0]-1][cube[1]-1]=color
覆盖背景图
# 将背景图画上去 screen.blit(background, (0, 0))
计算方块位置
# 计算出所有小方块的行、列位置 current_pos = [] for cube in current_shape: pos = (cube[0] + center[0], cube[1] + center[1]) current_pos.append(pos)
绘制方块
# 取出所有小方块的行、列位置,计算坐标,绘制俄罗斯方块 for cube in current_pos: pygame.draw.rect(screen, color, (cube[1] * 20-20, cube[0] * 20-20, 20, 20), 0) pygame.draw.rect(screen, (255, 255, 255), (cube[1] * 20-20, cube[0] * 20-20, 20, 20), 1) for i,row in zip(range(1,26),num_list): for j,colors in zip(range(1,16),row): if colors!=0: pygame.draw.rect(screen,colors,(j*20-20,i*20-20,20,20)) pygame.draw.rect(screen,(255,255,255),(j*20-20,i*20-20,20,20),1) new_list=[] for i in range(25): new_list.append([0]*15) row_index=24 for i in range(24,-1,-1): is_full=True for k in range(grid_num_width): if num_list[i][k]==0: is_full=False if is_full==False: new_list[row_index]=num_list[i] row_index-=1 else: score+=1 num_list=new_list if num_list[1][7]!=0: gameovor=True
得分
# 得分 text_surface = font.render(str(score), True, (0, 0, 0)) screen.blit(text_surface, (350,70))
游戏结束
if gameovor==True: text=fontovor.render('菜'*3,False,(0,0,0)) screen.blit(text,(0,0)) score=0 num_list=[] for i in range(25): num_list.append([0]*15)
画面刷新
# 刷新画面 pygame.display.update() clock.tick(FPS)
总结
现在游戏就做完了,敬请大家一键扇脸。
这篇关于用Python做一个俄罗斯方块的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-12-20Python编程入门指南
- 2024-12-20Python编程基础与进阶
- 2024-12-19Python基础编程教程
- 2024-12-19python 文件的后缀名是什么 怎么运行一个python文件?-icode9专业技术文章分享
- 2024-12-19使用python 把docx转为pdf文件有哪些方法?-icode9专业技术文章分享
- 2024-12-19python怎么更换换pip的源镜像?-icode9专业技术文章分享
- 2024-12-19Python资料:新手入门的全面指南
- 2024-12-19Python股票自动化交易实战入门教程
- 2024-12-19Python股票自动化交易入门教程
- 2024-12-18Python量化入门教程:轻松掌握量化交易基础知识