代码及配套资源以上传到百度网盘
链接:https://pan.baidu.com/s/1xeh7StIxFCPp7lUswItEFQ
提取码:cms5
代码如下:
import pygame
import random
#导入系统库
import os
import sys
pygame.init()
# 设置窗口的位置
os.environ['SDL_VIDEO_WINDOW_POS'] = "%d,%d" % (100, 20)
#初始化混合音响
pygame.mixer.init()
pygame.mixer.music.load("俄罗斯方块背景音.wav")
pygame.mixer.music.set_volume(0.2)
pygame.mixer.music.play(loops=-1)
# 创建游戏主窗口
screen = pygame.display.set_mode((400, 800))
#实例化指针
clock = pygame.time.Clock()
def main():
pygame.display.set_caption("俄罗斯方块!")
# 添加背景图
bg = pygame.image.load("图片-背景.png")
# 设置全屏矩阵,横排和竖排每个元素代表十个像素点,
marx = [
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
]
# 创建六个图形,四个方向
# 图形0
#表示40x40正方形方块,资源中所用小方块为10x10个像素点,列表中四个元素代表四个方向
shape00 = [
[(0, 0), (0, 1), (1, 0), (1, 1)],
[(0, 0), (0, 1), (1, 0), (1, 1)],
[(0, 0), (0, 1), (1, 0), (1, 1)],
[(0, 0), (0, 1), (1, 0), (1, 1)]
]
shape01 = [
[(0, 0), (1, 0), (1, 1), (2, 1)],
[(1, 0), (0, 1), (1, 1), (0, 2)],
[(0, 0), (1, 0), (1, 1), (2, 1)],
[(1, 0), (0, 1), (1, 1), (0, 2)]
]
shape02 = [
[(0, 1), (1, 1), (1, 0), (2, 0)],
[(0, 0), (0, 1), (1, 1), (1, 2)],
[(0, 1), (1, 1), (1, 0), (2, 0)],
[(0, 0), (0, 1), (1, 1), (1, 2)]
]
shape03 = [
[(0, 1), (1, 1), (2, 1), (3, 1)],
[(1, 0), (1, 1), (1, 2), (1, 3)],
[(0, 1), (1, 1), (2, 1), (3, 1)],
[(1, 0), (1, 1), (1, 2), (1, 3)]
]
shape04 = [
[(1, 0), (0, 1), (1, 1), (2, 1)],
[(0, 0), (0, 1), (1, 1), (0, 2)],
[(0, 0), (1, 0), (2, 0), (1, 1)],
[(1, 1), (2, 0), (2, 1), (2, 2)]
]
shape05 = [
[(2, 0), (0, 1), (1, 1), (2, 1)],
[(0, 0), (0, 1), (1, 2), (0, 2)],
[(0, 0), (1, 0), (2, 0), (0, 1)],
[(1, 0), (2, 0), (2, 1), (2, 2)]
]
shapes = [shape00, shape01, shape02, shape03, shape04, shape05]
chosed = False
#用于控制方块落下的速度
a = 0
#加载方块图片
r = pygame.image.load("图片-方块00.png")
shape, x, y = None, 0, 0
# 形状的方向
toward = 0
# 加速
freq = 60
# 游戏循环
while True:
#利用时针,减少内存的占用率
clock.tick(60)
# 绘制在屏幕
screen.blit(bg, (0, 0))
# 记录上一次的X
lastX = x
for event in pygame.event.get():
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_a or event.key == pygame.K_LEFT:
x -= 1
elif event.key == pygame.K_d or event.key == pygame.K_RIGHT:
x += 1
elif event.key == pygame.K_SPACE:
# 修改方向
toward += 1
if toward == 4:
toward = 0
# K_DOWN与KEYDOWN不同
elif event.key == pygame.K_DOWN:
freq = 1
elif event.type == pygame.KEYUP:
# 如果被抬起来的是下键,freq恢复为60,即方块下落速度减慢
if event.key == pygame.K_DOWN:
freq = 60
if event.type==pygame.QUIT:
sys.exit()
if not chosed:
# 随机取出shapes列表中的一个图形
shape = shapes[random.randint(0, 5)]
x = 4
y = -4
chosed = True
# 判断一下左右移动
for s in shape[toward]:
if x + s[0] < 0:
x += 1
if x + s[0] > 9:
x -= 1
if marx[y + s[1]][x + s[0]] == 1:
x = lastX
for s in shape[toward]:
# 判断shape[toward]是否在屏幕上,如果在,画出来
if 0 <= x + s[0] <= 9 and 0 <= y + s[1] <= 19:
# 对应格子的位置上画上方块
screen.blit(r, ((x + s[0]) * 40, (y + s[1]) * 40))
#判断方块是否落到底
if y + s[1] == 19:
chosed = False
# 坐标添加到大矩阵
for sh in shape[toward]:
marx[y + sh[1]][x + sh[0]] = 1
break
# 与正下方的方块紧挨着
elif marx[y + s[1] + 1][x + s[0]] == 1:
chosed = False
for sh in shape[toward]:
marx[y + sh[1]][x + sh[0]] = 1
break
a += 1
if a % freq == 0:
y += 1
a = 0
# 遍历大矩阵,绘制落地的图像
for yy in range(0, 20):
for xx in range(0, 10):
if marx[yy][xx] == 1:
screen.blit(r, (xx * 40, yy * 40))
# 检测消除
for yy in range(19, -1, -1):
if marx[yy] == [1, 1, 1, 1, 1, 1, 1, 1, 1, 1]:
for i in range(yy, 0, -1):
marx[i] = marx[i - 1]
#如果屏幕满了,就结束游戏
for first in marx[0]:
if first==1:
game_over()
# 更新显示
pygame.display.update()
#加载游戏结束后的界面
def game_over():
pygame.display.set_caption("Game over!")
while True:
game_over_img = pygame.image.load('game_over界面1.jpg')
for event in pygame.event.get():
if event.type==pygame.QUIT:
sys.exit()
mouse_press=pygame.mouse.get_pressed()
mouse_pos=pygame.mouse.get_pos()
#print(mouse_pos[0],mouse_pos[1])
if 90<=mouse_pos[0]<=290 and 440<=mouse_pos[1]<=480:
game_over_img = pygame.image.load('game_over界面2.jpg')
if mouse_press[0]:
main()
screen.blit(game_over_img, (0, 0))
pygame.display.update()
main()