python实现简单俄罗斯方块游戏
时间:2023-02-19 10:53:08|栏目:Python代码|点击: 次
本文实例为大家分享了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中使用Counter进行字典创建以及key数量统计的方法
栏 目:Python代码
下一篇:Python实现归一化算法详情
本文标题:python实现简单俄罗斯方块游戏
本文地址:http://www.codeinn.net/misctech/226038.html