|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
import sys, pygame ,time,random
from pygame.locals import *
rows=22#表示多少行
columns=12#表示多少列
side=25#一个小方块的边长
###定义俄罗斯方块的数据结构
T_shape= [[".x..",
"xxx.",
"....",
"...."],
[".x..",
"xx..",
".x..",
"...."],
["xxx.",
".x..",
"....",
"...."],
["x...",
"xx..",
"x...",
"...."]]
S_shape= [[".xx.",
"xx..",
"....",
"...."],
["x...",
"xx..",
".x..",
"...."]]
R_shape= [["xx..",
".xx.",
"....",
"...."],
[".x..",
"xx..",
"x...",
"...."]]
I_shape= [["x...",
"x...",
"x...",
"x..."],
["xxxx",
"....",
"....",
"...."]]
O_shape= [["xx..",
"xx..",
"....",
"...."]]
J_shape= [["xx..",
"x...",
"x...",
"...."],
["x...",
"xxx.",
"....",
"...."],
[".x..",
".x..",
"xx..",
"...."],
["xxx..",
"..x..",
"....",
"...."]]
L_shape= [["xx..",
".x..",
".x..",
"...."],
["xxx.",
"x...",
"....",
"...."],
["x...",
"x...",
"xx..",
"...."],
["..x.",
"xxx.",
"....",
"...."]]
shapes_set=[L_shape,J_shape,S_shape,R_shape,T_shape,O_shape,I_shape]
#创建俄罗斯方块的函数,用字典记录信息,用R,C,shape,n表示起始行列形态及某形态中一个形状
def create_piece():
piece={}
piece["r"]=0
piece["c"]=4
piece["shape"]=random.choice(shapes_set)
piece["n"]=0
return piece
#初始化界面22*12小方块,用.来填充表示。二维矩阵结构。
def create_matrix():
matrix=[["." for i in range(columns)] for j in range(rows)]
return matrix
#画一个小方块
def draw_singleblock(screen,x,y):
pygame.draw.rect(screen,"grey",(y*side+4,x*side+52,side,side))
pygame.draw.rect(screen,"white",(y*side+4,x*side+52,side-2,side-2))
##画一个俄罗斯方块
def draw_piece(screen,piece):
shape=piece["shape"][piece["n"]]
for i in range(4):
for j in range(4):
if shape[i][j] !=".":
draw_singleblock(screen,piece["r"]+i,piece["c"]+j)
#检测冲突(小方块在边界范围内)
def is_block_conflict(x,y):
if y>=0 and y<columns and x<rows:return False
return True
#检测俄罗斯方块移动后,位置是否有效(没有冲突,返回真,)。
def is_position_valid(matrix,piece,adjrow=0,adjcol=0):
shape=piece["shape"][piece["n"]]
for i in range(4):
for j in range(4):
if shape[i][j]==".":continue
if is_block_conflict(i+piece["r"]+adjrow,j+piece["c"]+adjcol):return False#检测俄罗斯方块中每一块是否在界面内
if matrix[i+piece["r"]+adjrow][j+piece["c"]+adjcol]=="x":return False#检测下落的俄罗斯方块中每一块到达matrix中是否已有俄罗斯方块
return True
def update_matrix(matrix,piece):
shape=piece["shape"][piece["n"]]
for i in range(4):
for j in range(4):
if shape[i][j]=="x":
matrix[piece["r"]+i][piece["c"]+j]="x"
return matrix
def draw_matrix(screen,matrix):
for i in range(rows):
for j in range(columns):
if matrix[i][j] !=".":
draw_singleblock(screen,i,j)
def is_line_completed(row):
for i in range(columns):
if matrix[row][i]==".":return False
return True
def remove_completed_line(matrix):
lines_removed=0
for i in range(rows):
if is_line_completed(i):
#满行后,以上的行都掉落一行
for j in range(i,0,-1):
for k in range(columns):
matrix[j][k]=matrix[j-1][k]
for j in range(columns):
matrix[0][j]="."
lines_removed+=1
return lines_removed
pygame.init()
screen=pygame.display.set_mode((columns*side+4,rows*side+50))
pygame.display.set_caption("俄罗斯方块")
piece=create_piece()
lasttime=time.time()
matrix=create_matrix()
scores=0
while True:
for event in pygame.event.get():
if event.type==QUIT:
pygame.quit()
sys.exit()
elif event.type==KEYDOWN:
if event.key==K_LEFT and is_position_valid(matrix,piece,adjcol=-1):
piece["c"]-=1
if event.key==K_RIGHT and is_position_valid(matrix,piece,adjcol=1):
piece["c"]+=1
if event.key==K_DOWN and is_position_valid(matrix,piece,adjrow=1):
piece["r"]+=1
if event.key==K_UP:
piece["n"]=(piece["n"]+1)%len(piece["shape"])
#翻转超出界面,返回上个形状。也就是翻转不了。
if not is_position_valid(matrix,piece):
piece["n"]=(piece["n"]-1)%len(piece["shape"])
# 统计时间差大于0.2秒,下移一格。
if time.time()-lasttime>0.3:
piece["r"]+=1
lasttime=time.time()
screen.fill("black")
pygame.draw.rect(screen,"blue",(2,48,columns*side,rows*side),4)
draw_piece(screen,piece)
#俄罗斯方块下落时,碰撞到底或已在底的俄罗斯方块
if not is_position_valid(matrix,piece,adjrow=1):
matrix=update_matrix(matrix,piece)#更新大二维矩阵中,与俄罗斯方块显示图形位置,标记在大二维矩阵中。
scores+=remove_completed_line(matrix)*10
piece=create_piece()#创建新的俄罗斯方块
draw_matrix(screen,matrix)#画上已碰撞俄罗斯方块
myfont=pygame.font.Font(None,25)
score_img=myfont.render("Score分数:"+str(scores),True,"white")
screen.blit(score_img,(20,15))
pygame.display.update()
|
评分
-
查看全部评分
|