Python小项目俄罗斯方块代码基于pygame编写

python实习作业或者期末作业,俄罗斯方块,基于pygame编写
有很多小伙伴想要找一些小项目练练手,下面是我在闲暇时写的一个俄罗斯方块的一个小游戏,它是基于pygame板块来实现的
这是它的首页界面
首页
然后这里是它的运行界面
在这里插入图片描述
总共有四个速度等级,分别对应四种不同的速度,可以自行调整

import math
import random
import sys
from copy import deepcopy
import pygame as pg
import pygame.locals as pl

pg.init()        # 初始化pygame模块

pg.display.set_caption('俄罗斯方块')       # 设置当前窗口标题
fclock = pg.time.Clock()               # 创建一个对象来帮助跟踪时间

FPS = 30    # 每秒的传送帧数
FONT = pg.font.SysFont('simhei', 30)         # 从系统字体创建字体对象
WindowX = 30
WindowY = 25
award = 10  # 奖励倍数
BLOCK_SIZE = 30          # 一个方块的大小
LINE_SPACE = 2
MAIN_X, MAIN_Y = MAIN_WINDOW = [30, 25]
GAME_X, GAME_Y = GAME_WINDOW = [20, MAIN_Y]
NEXT_X = MAIN_X - GAME_X

RED = (255, 0, 0)       # 运用RGB颜色转载,RGB的颜色表示方法
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
YELLOW = (255, 255, 0)
GREEN = (0, 255, 0)
BLUE = (0, 0, 255)


class BaseBlock:                 # 创建一个类建立基本的方块
    def __init__(self):
        self.turn_times = 0
        self.x_move = 0
        self.y_move = 0
        self.location = []


class IBlock(BaseBlock):            #  定义一个小类,创建I型方块,#在4*4的小网格内,以中间左上角坐下为原点(0,0),7种方块及其各形态4个方块在小网格的相对坐标
                                    # 移动时记录小网络(0,0)点在游戏网格的(x,y),就知道4个方块在游戏网格中的位置
    def __init__(self):
        super().__init__()
        self.dot = {
            0: [(0, 1), (0, 0), (0, -1), (0, -2)],
            1: [(-1, 0), (0, 0), (1, 0), (2, 0)],
        }


class OBlock(BaseBlock):
    def __init__(self):
        super().__init__()           # 创建o型方块
        self.dot = {
            0: [(0, 0), (1, 0), (1, 1), (0, 1)],
        }


class LBlock(BaseBlock):
    def __init__(self):
        super().__init__()
        self.dot = {
            0: [(0, 0), (0, 1), (0, -1), (-1, 1),(1,1)],           # 创建L型方块
            1: [(0, 0), (-1, 0), (1, 0), (1, 1)],
            2: [(0, 0), (0, 1), (0, -1), (1, -1)],
            3: [(0, 0), (1, 0), (-1, 0), (-1, -1)],
        }


class ULBlock(BaseBlock):
    def __init__(self):                                        # 定义一个小类创建U型方块
        super().__init__()
        self.dot = {
            0: [(0, 0), (0, 1), (0, -1), (1, 1)]
            ,
            1: [(0, 0), (-1, 0), (1, 0), (1, -1)],
            2: [(0, 0), (0, 1), (0, -1), (-1, -1)],
            3: [(0, 0), (1, 0), (-1, 0), (-1, 1)],
        }


class TBlock(BaseBlock):
    def __init__(self):
        super().__init__()
        self.dot = {
            0: [(0, 0), (1, 0), (0, 1), (-1, 0)],
            1: [(0, 0), (1, 0), (0, 1), (0, -1)],
            2: [(0, 0), (1, 0), (0, -1), (-1, 0)],            # 创建T型方块
            3: [(0, 0), (0, -1), (0, 1), (-1, 0)],
        }


class SBlock(BaseBlock):
    def __init__(self, *args, **kwargs):
        super().__init__()                             # 定义一个小类,创建S型方块
        self.dot = {
            0: [(0, 0), (0, 1), (-1, 0), (-1, -1)],
            1: [(0, 0), (1, 0), (0, 1), (-1, 1)],
        }


class ZBlock(BaseBlock):
    def __init__(self):
        super().__init__()
        self.dot = {
            0: [(0, 0), (0, 1), (-1, 0), (-1, -1)],              # 创建Z型方块
            1: [(0, 0), (1, 0), (0, 1), (-1, 1)],
        }


class Game():
    def __init__(self):      # self表示对象本身,谁调用,就表示谁
        self.fps = FPS
        self.screen = pg.display.set_mode([MAIN_X * BLOCK_SIZE, MAIN_Y * BLOCK_SIZE])    # 初始化窗口或屏幕以供显示并设置大小
        self.screen.fill(WHITE)
        self.stop_block = {k: [] for k in range(MAIN_Y)}     # 当k在y轴上循环,当方块在y轴最下方时停止下落;
        self.level_list = ['简单', '一般', '困难', '地狱']    # 设置难度分类
        self.moshi_list = ['单人','双人']
        self.moshi = 1      # 定义初始的单人模式
        self.level = 1      # 定义初始难度等级
        self.score = 0      # 定义初始分数
        self.next_block = self.create_next()     # 调用create_next这个函数生成下一个方块
        self.now_block = None
        self.gaming = False
        self.click_box = []
        self.click_color = RED

    def draw_text(self):
        score_obj = FONT.render('分数: %s' % self.score, True, (0, 0, 0), )   # render(内容,是否抗锯齿,字体颜色,字体背景颜色)
        level_obj = FONT.render('等级: %s' % self.level_list[self.level - 1], True, (0, 0, 0), )
        x, y = self.three.topleft

        self.screen.blit(score_obj, [x + BLOCK_SIZE * 2.5, y + BLOCK_SIZE * 5])   # 在主窗口上建立一个小窗口显示分数
        self.screen.blit(level_obj, [x + BLOCK_SIZE * 1.5, y + BLOCK_SIZE * 7.5])  # 在主窗口上建立一个小窗口显示难度等级

    @property              # 装饰器,让此方法变为私有属性,防止对其修改,可以用调用属性形式来调用方法,后面不需要加();
    def speed(self):
        # print(round(self.level / 10, 1))
        return round(self.level / 10, 1)         # 定义一个速度函数,用等级除以10,保留一位小数。控制着难度的等级,难度越大,速度越快。

    def start(self):
        if self.gaming:
            if not self.now_block:
                self.change_next()
            self.draw_next_block()
            self.draw_now_block()
            self.draw_stop()
            self.draw_wall()
            self.move()
            remove_line = self.check_full_block()        # 消去的行数调用check.full.block函数;
            if remove_line:                             # 分数等于除去的行数乘上倍数;
                self.score += award * remove_line
            self.draw_text()
        else:
            self.choice_level()

    def level_add(self):
        if self.level + 1 <= len(self.level_list):
            self.level += 1
        else:
            self.level = 1

    def level_pop(self):
        if self.level - 1 >= 1:
            self.level -= 1
        else:
            self.level = len(self.level_list)

    def moshi_add(self):
        if self.moshi + 1 <= len(self.moshi_list):
            self.moshi += 1
        else:
            self.moshi = 1

    def moshi_pop(self):
        if self.moshi - 1 <= len(self.moshi_list):
            self.moshi -= 1
        else:
            self.moshi = len(self.moshi_list)

    def to_gaming(self):
        self.gaming = not self.gaming

这是一部分的开头,创建函数的部分
完整代码可以去博主的Github仓库或者Gitee仓库下载
Gitee仓库地址:
点击跳转Gitee仓库
觉得喜欢的可以给博主的仓库加个星哦
Github仓库地址:
点击跳转Github仓库
觉得有用的可以给博主的仓库加个小星星哦
最后,觉得这篇文章对你来说有用的话,给博主点个赞再走吧