Commit 205a6837 by BellCodeEditor

save project

parent c1a29d62
Showing with 169 additions and 155 deletions
import pygame import pygame
from pygame import locals from pygame import locals
import random import random
pygame.init() # 初始化 pygame.init() # 初始化
score = 0 score = 0
grid_size = 20 # 格子大小 grid_size = 20 # 格子大小
grid_num_width = 15 # 横向格子数量 grid_num_width = 15 # 横向格子数量
grid_num_height = 25 # 纵向格子数量 grid_num_height = 25 # 纵向格子数量
FPS = 30 # 帧率 FPS = 30 # 帧率
count = 0 count = 0
states = False states = False
gameover = False # gameover:True,游戏结束;False,游戏正常运行
# 创建窗口
screen = pygame.display.set_mode((460, 500)) # 创建窗口
pygame.display.set_caption("俄罗斯方块") screen = pygame.display.set_mode((460, 500))
clock = pygame.time.Clock() # pygame时钟,控制游戏速度(帧数) pygame.display.set_caption("俄罗斯方块")
# 载入素材 clock = pygame.time.Clock() # pygame时钟,控制游戏速度(帧数)
background = pygame.image.load('bg.png') # 载入素材
font = pygame.font.Font('STKAITI.TTF', 60) # 字体 background = pygame.image.load('bg.png')
font = pygame.font.Font('STKAITI.TTF', 60) # 字体
# 俄罗斯方块所有形状 font_restart = pygame.font.Font('STKAITI.TTF', 25)
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)]] O = [[(0, 0), (0, 1), (1, 0), (1, 1)]]
Z = [[(0, -1), (0, 0), (1, 0), (1, 1)], I = [[(0, -1), (0, 0), (0, 1), (0, 2)],
[(-1, 0), (0, 0), (0, -1), (1, -1)]] [(-1, 0), (0, 0), (1, 0), (2, 0)]]
S = [[(-1, 0), (0, 0), (0, 1), (1, 1)], Z = [[(0, -1), (0, 0), (1, 0), (1, 1)],
[(1, -1), (1, 0), (0, 0), (0, 1)]] [(-1, 0), (0, 0), (0, -1), (1, -1)]]
T = [[(0, -1), (0, 0), (0, 1), (-1, 0)], S = [[(-1, 0), (0, 0), (0, 1), (1, 1)],
[(-1, 0), (0, 0), (1, 0), (0, 1)], [(1, -1), (1, 0), (0, 0), (0, 1)]]
[(0, -1), (0, 0), (0, 1), (1, 0)], T = [[(0, -1), (0, 0), (0, 1), (-1, 0)],
[(-1, 0), (0, 0), (1, 0), (0, -1)]] [(-1, 0), (0, 0), (1, 0), (0, 1)],
J = [[(-1, 0), (0, 0), (1, 0), (1, -1)], [(0, -1), (0, 0), (0, 1), (1, 0)],
[(0, -1), (0, 0), (0, 1), (-1, -1)], [(-1, 0), (0, 0), (1, 0), (0, -1)]]
[(-1, 0), (0, 0), (1, 0), (-1, 1)], J = [[(-1, 0), (0, 0), (1, 0), (1, -1)],
[(0, -1), (0, 0), (0, 1), (1, 1)]] [(0, -1), (0, 0), (0, 1), (-1, -1)],
L = [[(-1, 0), (0, 0), (1, 0), (1, 1)], [(-1, 0), (0, 0), (1, 0), (-1, 1)],
[(0, -1), (0, 0), (0, 1), (1, -1)], [(0, -1), (0, 0), (0, 1), (1, 1)]]
[(-1, 0), (0, 0), (1, 0), (-1, -1)], L = [[(-1, 0), (0, 0), (1, 0), (1, 1)],
[(0, -1), (0, 0), (0, 1), (-1, 1)]] [(0, -1), (0, 0), (0, 1), (1, -1)],
shape_list = [I, J, L, O, S, T, Z] # 7种类型俄罗斯方块 [(-1, 0), (0, 0), (1, 0), (-1, -1)],
[(0, -1), (0, 0), (0, 1), (-1, 1)]]
# 一些RGB颜色 shape_list = [I, J, L, O, S, T, Z] # 7种类型俄罗斯方块
cube_colors = [
(204, 153, 153), (102, 102, 153),(153, 0, 102), # 一些RGB颜色
(255, 204, 0), (204, 0, 51),(255, 0, 51), (0, 102, 153), cube_colors = [
(153, 0, 51), (204, 255, 102), (255, 153, 0)] (204, 153, 153), (102, 102, 153),(153, 0, 102),
(255, 204, 0), (204, 0, 51),(255, 0, 51), (0, 102, 153),
def check(center): (153, 0, 51), (204, 255, 102), (255, 153, 0)]
for cube in current_shape:
cube = (cube[0] + center[0], cube[1] + center[1]) def check(center):
# 当前的小方块超出网格的行、列,就返回False for cube in current_shape:
if cube[0] < 1 or cube[1] < 1 or cube[0] > grid_num_height \ cube = (cube[0] + center[0], cube[1] + center[1])
or cube[1] >grid_num_width: # 当前的小方块超出网格的行、列,就返回False
return False if cube[0] < 1 or cube[1] < 1 or cube[0] > grid_num_height \
if num_list[cube[0]-1][cube[1]-1]!=0: or cube[1] >grid_num_width:
return False return False
if num_list[cube[0]-1][cube[1]-1] != 0:
num_list=[] return False
for i in range(25):
num_list.append([0]*15) # 格子基础数字设为0
num_list = []
while True: for i in range(25):
for event in pygame.event.get(): num_list.append([0] * 15)
if event.type == locals.QUIT:
exit() while True:
if event.type == locals.KEYDOWN: for event in pygame.event.get():
if event.key == locals.K_LEFT: # 向左 if event.type == locals.QUIT:
center[1] = center[1] - 1 # 左移1列 exit()
if check(center) == False: if event.type == locals.KEYDOWN:
center[1] = center[1] + 1 # 右移1列 if gameover == True:
gameover = False
elif event.key == locals.K_RIGHT: # 向右 if event.key == locals.K_LEFT: # 向左
center[1] = center[1] + 1 center[1] = center[1] - 1 # 左移1列
if check(center) == False: if check(center) == False:
center[1] = center[1] - 1 center[1] = center[1] + 1 # 右移1列
elif event.key == locals.K_DOWN: # 向下 elif event.key == locals.K_RIGHT: # 向右
center[0] = center[0] + 1 center[1] = center[1] + 1
if check(center) == False: if check(center) == False:
center[0] = center[0] - 1 center[1] = center[1] - 1
elif event.key == locals.K_UP: # 向上键 elif event.key == locals.K_DOWN: # 向下
old_index = index center[0] = center[0] + 1
index += 1 if check(center) == False:
if index >= len(shape): center[0] = center[0] - 1
index = 0
current_shape = shape[index] elif event.key == locals.K_UP: # 向上键
if check(center) == False: old_index = index
index = old_index index += 1
current_shape = shape[index] if index >= len(shape):
index = 0
if states == False: current_shape = shape[index]
states = True if check(center) == False:
center = [2, 8] # 第2行第8列 index = old_index
shape = random.choice(shape_list) current_shape = shape[index]
index = random.randint(0, len(shape)-1) # 随机形状索引
current_shape = shape[index] if gameover == False:
color = random.choice(cube_colors) # 随机选取一种颜色 # 生成新俄罗斯方块
if states == False:
count += 1 states = True
center = [2, 8] # 第2行第8列
if count % FPS == 0: # 降落速度的算式 shape = random.choice(shape_list)
center[0] = center[0] + 1 index = random.randint(0, len(shape)-1) # 随机形状索引
if check(center) == False: current_shape = shape[index]
center[0] = center[0] - 1 color = random.choice(cube_colors) # 随机选取一种颜色
states = False count += 1
for cube in current_pos: if count % FPS == 0: # 降落速度的算式
num_list[cube[0]-1][cube[1]-1]=color center[0] = center[0] + 1
if check(center) == False:
# 将背景图画上去 center[0] = center[0] - 1
screen.blit(background, (0, 0)) states = False
# 计算出所有小方块的行、列位置 for cube in current_pos:
current_pos = [] num_list[cube[0]-1][cube[1]-1] = color
for cube in current_shape: # 将背景图画上去
pos = (cube[0] + center[0], cube[1] + center[1]) screen.blit(background, (0, 0))
current_pos.append(pos) # 计算出所有小方块的行、列位置
# 取出所有小方块的行、列位置,计算坐标,绘制俄罗斯方块 current_pos = []
for cube in current_pos: for cube in current_shape:
pygame.draw.rect(screen, color, pos = (cube[0] + center[0], cube[1] + center[1])
(cube[1] * 20-20, cube[0] * 20-20, 20, 20), 0) current_pos.append(pos)
pygame.draw.rect(screen, (255, 255, 255), # 取出所有小方块的行、列位置,计算坐标,绘制俄罗斯方块
(cube[1] * 20-20, cube[0] * 20-20, 20, 20), 1) for cube in current_pos:
pygame.draw.rect(screen, color,
for i,row in zip(range(1,126),num_list): (cube[1] * 20-20, cube[0] * 20-20, 20, 20), 0)
for j,colors in zip(range(1,16),row): pygame.draw.rect(screen, (255, 255, 255),
if colors != 0: (cube[1] * 20-20, cube[0] * 20-20, 20, 20), 1)
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) for i, row in zip(range(1,26), num_list):
for j, colors in zip(range(1,16), row):
if colors != 0:
new_list=[] pygame.draw.rect(screen, colors,
for i in range(25): (j * 20-20, i * 20-20, 20, 20))
new_list.append([0]*15) pygame.draw.rect(screen, (255, 255, 255),
row_index=24 (j * 20-20, i * 20-20, 20, 20), 1)
for i in range(24,-1,-1):
is_full=True new_list = [] # 新的地图列表
for j in range(grid_num_width): for i in range(25):
if new_list[i][j]==0: new_list.append([0] * 15)
is_full=False row_index = 24
if is_full == False: for i in range(24, -1, -1):
num_list[row_index]=num_list[i] is_full = True
row_index-=1 for j in range(grid_num_width):
else: if num_list[i][j] == 0:
score+=1 is_full = False
num_list = new_list if is_full == False:
new_list[row_index] = num_list[i]
row_index -= 1
# 得分 else:
text_surface = font.render(str(score), True, (0, 0, 0)) score += 1
screen.blit(text_surface, (350,70)) num_list = new_list
# 刷新画面 # 小方块超出高度
pygame.display.update() if num_list[1][7] != 0:
clock.tick(FPS) gameover = True
# 得分
text_surface = font.render(str(score), True, (0, 0, 0))
screen.blit(text_surface, (350,70))
if gameover == True:
text = font_restart.render("游戏失败,按任意键开始",True,(0,0,0))
screen.blit(text, (20, 250))
score = 0
num_list = []
for i in range(25):
num_list.append([0] * 15)
# 刷新画面
pygame.display.update()
clock.tick(FPS)
\ No newline at end of file
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