Commit 0e8661bb by BellCodeEditor

save project

parent 24107989
Showing with 153 additions and 0 deletions
File added
bg.png

46.1 KB

import pygame,random
from pygame import locals
pygame.init() # 初始化
score = 0
grid_size = 20 # 格子大小
grid_num_width = 15 # 横向格子数量
grid_num_height = 25 # 纵向格子数量
FPS = 30
count=0
# 创建窗口
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) # 字体\
#形状设计
# 俄罗斯方块所有形状
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)]
state=False
num_list=[]
a=[0]
for i in range(25):
num_list.append(a*15)
#检测每个方块的边缘
def check(center):
for c in current_shape:
c=(c[0]+center[0],c[1]+center[1])
if c[0]<1 or c[1]<1 or c[0]>25 or c[1]>15:
return False
if num_list[c[0]-1][c[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 event.key==locals.K_RIGHT:
center[1]+=1
if check(center)==False:
center[1]-=1
if event.key==locals.K_LEFT:
center[1]-=1
if check(center)==False:
center[1]+=1
if event.key==locals.K_DOWN:
center[0]+=1
if check(center)==False:
center[0]-=1
if event.key==locals.K_UP:
old_index=index
index+=1
if index>=len(shape):
index=0
current_shape=shape[index]
if check(center)==False:
current_shape=shape[old_index]
if state==False:
state=True
#中心方块的初始坐标
center=[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]+=1
if check(center)==False:
center[0]-=1
state=False
for cube in current_pos:
num_list[cube[0]-1][cube[1]-1]=color
# 将背景图画上去
screen.blit(background,(0,0))
#画方块(surface,color,rect,width)
current_pos=[]
for c in current_shape:
pos_x=c[1]+center[1]
pos_y=c[0]+center[0]
current_pos.append([pos_y,pos_x])
for c in current_pos:
pygame.draw.rect(screen,color,(c[1]*20-20,c[0]*20-20,20,20),0)
pygame.draw.rect(screen,(255,255,255),(c[1]*20-20,c[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),0)
pygame.draw.rect(screen,(255,255,255),(j*20-20,i*20-20,20,20),1)
#将分数渲染到右边分数窗口(350,70),内容,锯齿弱化,颜色
score_text=font.render(str(score),True,(0,0,0))
screen.blit(score_text,(350,70))
num_list5=[]
ad=[0]
for i in range(25):
num_list5.append(ad*15)
x=24
for i in range(24,-1,-1):
is_full=True
for j in range(15):
if num_list[i][j]==0:
is_full=False
if is_full==False:
num_list5[x]=num_list[i]
x-=1
else:
score+=1
num_list=num_list5
pygame.display.update()
clock.tick(FPS)
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment