本篇文章为大家展示了python是怎么实现简单俄罗斯方块游戏,内容简明扼要并且容易理解,绝对能使你眼前一亮,通过这篇文章的详细介绍希望你能有所收获。
1、简单易用,与C/C++、Java、C# 等传统语言相比,Python对代码格式的要求没有那么严格;2、Python属于开源的,所有人都可以看到源代码,并且可以被移植在许多平台上使用;3、Python面向对象,能够支持面向过程编程,也支持面向对象编程;4、Python是一种解释性语言,Python写的程序不需要编译成二进制代码,可以直接从源代码运行程序;5、Python功能强大,拥有的模块众多,基本能够实现所有的常见功能。
import pygame,sys,random,time all_block = [[[0,0],[0,-1],[0,1],[0,2]], [[0,0],[0,1],[1,1],[1,0]], [[0,0],[0,-1],[-1,0],[-1,1]], [[0,0],[0,1],[-1,-1],[-1,0]], [[0,0],[0,1],[1,0],[0,-1]], [[0,0],[1,0],[-1,0],[1,-1]], [[0,0],[1,0],[-1,0],[1,1]]] background = [[0 for column in range(0,10)] for row in range(0,22)] background[0] = [1 for column in range(0,10)] select_block = list(random.choice(all_block)) block_initial_position = [21,5] times = 0 score = [0] gameover = [] press = False pygame.init() screen = pygame.display.set_mode((250,500)) title = pygame.display.set_caption("俄罗斯方块") #下落、位置、数组检测、得分、屏幕信息 def block_move_down(): y_drop=block_initial_position[0] x_move=block_initial_position[1] y_drop-=1 for row,column in select_block: row+=y_drop column+=x_move if background[row][column]==1: break else: block_initial_position.clear() block_initial_position.extend([y_drop,x_move]) return y_drop,x_move=block_initial_position for row,column in select_block: background[y_drop+row][x_move+column]=1 complete_row=[] for row in range(1,21): if 0 not in background[row]: complete_row.append(row) complete_row.sort(reverse=True) for row in complete_row: background.pop(row) background.append([0 for column in range(0,10)]) score[0]+=len(complete_row) pygame.display.set_caption(str(score[0])+'分') select_block.clear() select_block.extend(list(random.choice(all_block))) block_initial_position.clear() block_initial_position.extend([20,5]) y_drop,x_move=block_initial_position for row,column in select_block: row+=y_drop column+=x_move if background[row][column]: gameover.append(1) #方块设置、变化、背景改变 def new_draw(): y_drop,x_move=block_initial_position for row,column in select_block: row+=y_drop column+=x_move pygame.draw.rect(screen,(255,165,0),(column*25,500-row*25,23,23)) for row in range(0,20): for column in range(0,10): bottom_block=background[row][column] if bottom_block: pygame.draw.rect(screen,(0,0,255),(column*25,500-row*25,23,23)) #方块的移动,防止出界,碰撞 def move_left_right(n): y_drop,x_move=block_initial_position x_move+=n for row,column in select_block: row+=y_drop column+=x_move if column<0 or column>9 or background[row][column]: break else: block_initial_position.clear() block_initial_position.extend([y_drop,x_move]) #旋转,位置都进行变化 def rotate(): y_drop,x_move=block_initial_position rotating_position=[(-column,row)for row,column in select_block] for row,column in rotating_position: row+=y_drop column+=x_move if column<0 or column>9 or background[row][column]: break else: select_block.clear() select_block.extend(rotating_position) while True: screen.fill((255,255,255)) for event in pygame.event.get(): if event.type==pygame.QUIT: sys.exit() elif event.type==pygame.KEYDOWN and event.key==pygame.K_LEFT: move_left_right(-1) elif event.type==pygame.KEYDOWN and event.key==pygame.K_RIGHT: move_left_right(1) elif event.type==pygame.KEYDOWN and event.key==pygame.K_UP: rotate() elif event.type==pygame.KEYDOWN and event.key==pygame.K_DOWN: press=True elif event.type==pygame.KEYUP and event.key==pygame.K_DOWN: press=False if press: times+=10 if times>=50: block_move_down() times=0 else: times+=1 if gameover: sys.exit() new_draw() pygame.time.Clock().tick(200) pygame.display.flip()
效果:
上述内容就是python是怎么实现简单俄罗斯方块游戏,你们学到知识或技能了吗?如果还想学到更多技能或者丰富自己的知识储备,欢迎关注亿速云行业资讯频道。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。