计算机网络学习笔记】初始网络之网络发展和OSI七层模型

以下是基于 Python + Pygame 实现的完整俄罗斯方块游戏代码,包含核心功能(方块生成、移动、旋转、消除、计分),注释详细可直接运行:

第一步:安装依赖

先安装 Pygame 库:

pip install pygame

第二步:完整代码

import pygame

import random

初始化Pygame

pygame.init()

游戏常量

SCREEN_WIDTH = 300 # 屏幕宽度

SCREEN_HEIGHT = 600 # 屏幕高度

BLOCK_SIZE = 30 # 方块大小(像素)

GRID_WIDTH = SCREEN_WIDTH // BLOCK_SIZE # 网格列数(10列)

GRID_HEIGHT = SCREEN_HEIGHT // BLOCK_SIZE # 网格行数(20行)

颜色定义(RGB)

BLACK = (0, 0, 0)

WHITE = (255, 255, 255)

GRAY = (128, 128, 128)

COLORS = [

(0, 255, 255), # 青色(I型)

(0, 0, 255), # 蓝色(J型)

(255, 165, 0), # 橙色(L型)

(255, 255, 0), # 黄色(O型)

(0, 255, 0), # 绿色(S型)

(128, 0, 128), # 紫色(T型)

(255, 0, 0) # 红色(Z型)

]

俄罗斯方块7种形状(0=空,1=方块)

SHAPES = [

\[1, 1, 1, 1], # I型(横)

\[1, 0, 0, 1, 1, 1], # J型

\[0, 0, 1, 1, 1, 1], # L型

\[1, 1, 1, 1], # O型(正方形)

\[0, 1, 1, 1, 1, 0], # S型

\[0, 1, 0, 1, 1, 1], # T型

\[1, 1, 0, 0, 1, 1] # Z型

]

屏幕设置

screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))

pygame.display.set_caption("俄罗斯方块")

时钟(控制游戏帧率)

clock = pygame.time.Clock()

FPS = 10

字体设置(计分板)

font = pygame.font.Font(None, 36)

class Tetromino:

"""方块类:管理单个下落的俄罗斯方块"""

def init(self):

self.shape = random.choice(SHAPES) # 随机选择形状

self.color = random.choice(COLORS) # 随机选择颜色

self.x = GRID_WIDTH // 2 - len(self.shape0) // 2 # 初始X位置(居中)

self.y = 0 # 初始Y位置(顶部)

def rotate(self):

"""旋转方块(矩阵转置+逆序)"""

转置矩阵

rotated = list(zip(*self.shape::-1))

转换为列表格式

self.shape = list(row) for row in rotated

def draw(self):

"""绘制方块到屏幕"""

for y, row in enumerate(self.shape):

for x, cell in enumerate(row):

if cell:

计算方块在屏幕上的实际坐标

screen_x = (self.x + x) * BLOCK_SIZE

screen_y = (self.y + y) * BLOCK_SIZE

绘制方块(带边框)

pygame.draw.rect(screen, self.color, (screen_x, screen_y, BLOCK_SIZE - 1, BLOCK_SIZE - 1))

class Game:

"""游戏主类:管理网格、碰撞检测、计分"""

def init(self):

self.grid = \[BLACK for _ in range(GRID_WIDTH) for _ in range(GRID_HEIGHT)] # 游戏网格(初始全黑)

self.current_tetromino = Tetromino() # 当前下落的方块

self.score = 0 # 分数

self.game_over = False # 游戏结束标志

def draw_grid(self):

"""绘制游戏网格(已落地的方块)"""

for y in range(GRID_HEIGHT):

for x in range(GRID_WIDTH):

pygame.draw.rect(screen, self.gridyx, (x * BLOCK_SIZE, y * BLOCK_SIZE, BLOCK_SIZE - 1, BLOCK_SIZE - 1))

def check_collision(self, tetromino, dx=0, dy=0, rotated=False):

"""检测碰撞:dx=X偏移,dy=Y偏移,rotated=是否旋转后的形状"""

shape = tetromino.shape

if rotated:

临时计算旋转后的形状

shape = list(row) for row in zip(\*shape\[::-1)]

for y, row in enumerate(shape):

for x, cell in enumerate(row):

if cell:

计算偏移后的坐标

new_x = tetromino.x + x + dx

new_y = tetromino.y + y + dy

碰撞条件:超出左右边界、超出下边界、碰到已落地的方块

if (new_x < 0 or new_x >= GRID_WIDTH or

new_y >= GRID_HEIGHT or

(new_y >= 0 and self.gridnew_ynew_x != BLACK)):

return True

return False

def lock_tetromino(self):

"""将落地的方块锁定到网格中"""

for y, row in enumerate(self.current_tetromino.shape):

for x, cell in enumerate(row):

if cell:

grid_y = self.current_te

https://www.cnblogs.com/xxcsc523/articles/19846241

相关推荐
小O的算法实验室4 分钟前
AAAI-26,全局相关性学习:通过谱图神经网络增强进化算法
神经网络·学习·算法
宁风NF6 分钟前
JavaScript:网络请求与前端通信
开发语言·前端·javascript·网络·学习·ecmascript
xqqxqxxq8 分钟前
DML 表数据:插入、删除、修改
笔记·mysql
多巴胺梦想家32 分钟前
计算机系统概述
计算机网络
不会代码的小猴33 分钟前
Linux note2
linux·笔记
神神道呵he1 小时前
[深度学习] 大模型学习-RAG技术全景解析
人工智能·深度学习·学习
MartinYeung52 小时前
[论文学习]AgentSafetyBench:大语言模型智能体安全评估基准深度分析
学习·安全·语言模型
xqqxqxxq2 小时前
MySQL 数据类型笔记
数据库·笔记·mysql
艾莉丝努力练剑3 小时前
【Linux:动静态库】Linux 动静态库与可执行文件
linux·运维·服务器·学习·面试·文件系统·动静态库
软件技术NINI3 小时前
盗墓笔记html+css+js 2页
css·笔记·html