俄罗斯方块

坐标的存储方式:

记录方式有两种:

1,横纵坐标做一个二元元组,再用一个列表装着一堆二元元组

例如:[(20,1),(20,2),(20,3),(20,4)]代表第20行的1~4列的四个方块

2,二维数组,一行是一个列表,用两个索引代表横纵坐标,值为1就代表有方块,0就是没有方块

例如:block[20][1] 值为1就表示20行第1列有方块,block[20][5] 为0表示20行第5列有没有方块

键盘到图像(本质:键盘到核心变量)

下面简略列出需要的函数

名称 内容 发生时机
左右移动 修改y轴坐标 键盘事件
旋转 复杂的坐标转化 键盘事件
下落 修改x轴坐标 键盘事件,计时器
下落检查 检查是否可以继续下落 下落前后
检查方块 逐行检查是否可以消除, 下落检察后
消除方块 检查方块后
生成方块 产生下一个方块 消除方块后
检查存活
生成的方块 无法放置<=>堆到顶端<=>game over堆到顶端<=>game over

import pygame, sys, random, time

def new_draw():
screen.fill(white)

for i in range(1, 21):
    for j in range(10):
        bolck = background[i][j]
        if bolck:
            pygame.draw.rect(screen, blue, (j * 25 + 1, 500 - i * 25 + 1, 23, 23))

x, y = centre
for i, j in active:
    i += x
    j += y
    pygame.draw.rect(screen, blue, (j * 25 + 1, 500 - i * 25 + 1, 23, 23))

pygame.display.update()

def move_LR(n):
"""n=-1代表向左,n=1代表向右"""
x, y = centre
y += n
for i, j in active:
i += x
j += y
if j < 0 or j > 9 or background[i][j]:
break
else:
centre.clear()
centre.extend([x, y])
def direct_down(self):
"手动点击快速下落“
self.drop_speed= 50

def rotate():
x, y = centre
l = [(-j, i) for i, j in active]
for i, j in l:
i += x
j += y
if j < 0 or j > 9 or background[i][j]:
break
else:
active.clear()
active.extend(l)

def move_down():
x, y = centre
x -= 1
for i, j in active:
i += x
j += y
if background[i][j]:
break
else:
centre.clear()
centre.extend([x, y])
return
# 如果新位置未被占用 通过return结束
# 如果新位置被占用则继续向下执行
x, y = centre
for i, j in active:
background[x + i][y + j] = 1

l = []
for i in range(1, 20):
    if 0 not in background[i]:
        l.append(i)
# l装 行号,鉴于删去后,部分索引变化,对其降序排列,倒着删除
l.sort(reverse=True)

for i in l:
    background.pop(i)
    background.append([0 for j in range(10)])
    # 随删随补

score[0] += len(l)
pygame.display.set_caption("分数:%d" % (score[0]))

active.clear()
active.extend(list(random.choice(all_block)))
# all_block保存7种形状的信息
centre.clear()
centre.extend([20, 4])

x, y = centre
for i, j in active:
    i += x
    j += y
    if background[i][j]:
        break
else:
    return
alive.append(1)

pygame.init()
screen = pygame.display.set_mode((250, 500))
pygame.display.set_caption("俄罗斯方块")
fclock = pygame.time.Clock()

all_block = (((0, 0), (0, -1), (0, 1), (0, 2)),
((0, 0), (0, 1), (-1, 0), (-1, 1)),
((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 i in range(10)] for j in range(24)]
background[0] = [1 for i in range(10)]
active = list(random.choice(all_block))
centre = [20, 4]
score = [0]

black = 0, 0, 0
white = 255, 255, 255
blue = 0, 0, 255

times = 0
alive = []
press = False
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
move_LR(-1)
elif event.key == pygame.K_RIGHT:
move_LR(1)
elif event.key == pygame.K_UP:
rotate()
elif event.key == pygame.K_DOWN:
press = True
elif event.type == pygame.KEYUP:
if event.key == pygame.K_DOWN:
press = False
if press:
times += 10

if times >= 50:
    move_down()
    times = 0
else:
    times += 1

if alive:
    pygame.display.set_caption("over分数:%d" % (score[0]))
    time.sleep(3)
    break
new_draw()
fclock.tick(100)
posted @ 2020-11-24 15:16  任峰皓  阅读(87)  评论(0编辑  收藏  举报