计算机网络学习笔记】初始网络之网络发展和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))

http://my.tv.sohu.com/us/224496667/707343547.shtml

http://my.tv.sohu.com/us/135594753/707343489.shtml

http://my.tv.sohu.com/us/210318298/707343546.shtml

http://my.tv.sohu.com/us/296307329/707343383.shtml

http://my.tv.sohu.com/us/441549359/707343635.shtml

http://my.tv.sohu.com/us/296698768/707343487.shtml

http://my.tv.sohu.com/us/270458475/707343634.shtml

http://my.tv.sohu.com/us/135594753/707343486.shtml

http://my.tv.sohu.com/us/260257928/707343544.shtml

http://my.tv.sohu.com/us/210318298/707343543.shtml

http://my.tv.sohu.com/us/224496667/707343483.shtml

http://my.tv.sohu.com/us/296307329/707343379.shtml

http://my.tv.sohu.com/us/441549359/707343629.shtml

http://my.tv.sohu.com/us/270458475/707343627.shtml

http://my.tv.sohu.com/us/245853407/707343628.shtml

http://my.tv.sohu.com/us/135594753/707343378.shtml

http://my.tv.sohu.com/us/305889970/707343624.shtml

http://my.tv.sohu.com/us/296698768/707343480.shtml

http://my.tv.sohu.com/us/260257928/707343625.shtml

http://my.tv.sohu.com/us/210318298/707343626.shtml

http://my.tv.sohu.com/us/224496667/707343482.shtml

http://my.tv.sohu.com/us/245853407/707343620.shtml

http://my.tv.sohu.com/us/296307329/707343481.shtml

http://my.tv.sohu.com/us/441549359/707343621.shtml

http://my.tv.sohu.com/us/270458475/707343376.shtml

http://my.tv.sohu.com/us/135594753/707343623.shtml

http://my.tv.sohu.com/us/210318298/707343541.shtml

http://my.tv.sohu.com/us/260257928/707343619.shtml

http://my.tv.sohu.com/us/224496667/707343622.shtml

http://my.tv.sohu.com/us/296307329/707343540.shtml

http://my.tv.sohu.com/us/305889970/707343374.shtml

http://my.tv.sohu.com/us/135594753/707343372.shtml

http://my.tv.sohu.com/us/270458475/707343373.shtml

http://my.tv.sohu.com/us/296698768/707343538.shtml

http://my.tv.sohu.com/us/210318298/707343371.shtml

http://my.tv.sohu.com/us/224496667/707343617.shtml

http://my.tv.sohu.com/us/441549359/707343618.shtml

http://my.tv.sohu.com/us/305889970/707343370.shtml

http://my.tv.sohu.com/us/245853407/707343478.shtml

http://my.tv.sohu.com/us/296307329/707343369.shtml

http://my.tv.sohu.com/us/270458475/707343615.shtml

http://my.tv.sohu.com/us/135594753/707343616.shtml

http://my.tv.sohu.com/us/260257928/707343367.shtml

http://my.tv.sohu.com/us/210318298/707343476.shtml

http://my.tv.sohu.com/us/296698768/707343475.shtml

http://my.tv.sohu.com/us/441549359/707343477.shtml

http://my.tv.sohu.com/us/224496667/707343614.shtml

http://my.tv.sohu.com/us/305889970/707343368.shtml

http://my.tv.sohu.com/us/245853407/707343474.shtml

http://my.tv.sohu.com/us/296307329/707343473.shtml

http://my.tv.sohu.com/us/441549359/707343366.shtml

http://my.tv.sohu.com/us/210318298/707343472.shtml

http://my.tv.sohu.com/us/296698768/707343613.shtml

http://my.tv.sohu.com/us/135594753/707343365.shtml

http://my.tv.sohu.com/us/305889970/707343535.shtml

http://my.tv.sohu.com/us/245853407/707343471.shtml

http://my.tv.sohu.com/us/296307329/707343612.shtml

http://my.tv.sohu.com/us/260257928/707343611.shtml

http://my.tv.sohu.com/us/441549359/707343364.shtml

http://my.tv.sohu.com/us/210318298/707343470.shtml

http://my.tv.sohu.com/us/224496667/707343610.shtml

http://my.tv.sohu.com/us/270458475/707343608.shtml

http://my.tv.sohu.com/us/296698768/707343606.shtml

http://my.tv.sohu.com/us/296307329/707343363.shtml

http://my.tv.sohu.com/us/441549359/707343607.shtml

http://my.tv.sohu.com/us/210318298/707343468.shtml

http://my.tv.sohu.com/us/305889970/707343530.shtml

http://my.tv.sohu.com/us/260257928/707343532.shtml

http://my.tv.sohu.com/us/224496667/707343605.shtml

http://my.tv.sohu.com/us/245853407/707343604.shtml

http://my.tv.sohu.com/us/135594753/707343361.shtml

http://my.tv.sohu.com/us/270458475/707343603.shtml

http://my.tv.sohu.com/us/296307329/707343362.shtml

http://my.tv.sohu.com/us/441549359/707343602.shtml

http://my.tv.sohu.com/us/260257928/707343528.shtml

http://my.tv.sohu.com/us/224496667/707343466.shtml

http://my.tv.sohu.com/us/210318298/707343525.shtml

http://my.tv.sohu.com/us/305889970/707343465.shtml

http://my.tv.sohu.com/us/245853407/707343467.shtml

http://my.tv.sohu.com/us/270458475/707343524.shtml

http://my.tv.sohu.com/us/441549359/707343527.shtml

http://my.tv.sohu.com/us/135594753/707343462.shtml

http://my.tv.sohu.com/us/296698768/707343464.shtml

http://my.tv.sohu.com/us/296307329/707343299.shtml

http://my.tv.sohu.com/us/260257928/707343358.shtml

http://my.tv.sohu.com/us/245853407/707343297.shtml

http://my.tv.sohu.com/us/224496667/707343357.shtml

http://my.tv.sohu.com/us/305889970/707343356.shtml

http://my.tv.sohu.com/us/441549359/707343461.shtml

http://my.tv.sohu.com/us/296307329/707343296.shtml

http://my.tv.sohu.com/us/210318298/707343295.shtml

http://my.tv.sohu.com/us/296698768/707343521.shtml

http://my.tv.sohu.com/us/270458475/707343457.shtml

http://my.tv.sohu.com/us/260257928/707343456.shtml

http://my.tv.sohu.com/us/224496667/707343520.shtml

http://my.tv.sohu.com/us/135594753/707343458.shtml

http://my.tv.sohu.com/us/245853407/707343294.shtml

http://my.tv.sohu.com/us/441549359/707343293.shtml

http://my.tv.sohu.com/us/305889970/707343354.shtml

http://my.tv.sohu.com/us/296307329/707343291.shtml

http://my.tv.sohu.com/us/210318298/707343353.shtml

http://my.tv.sohu.com/us/296698768/707343352.shtml

http://my.tv.sohu.com/us/135594753/707343455.shtml

http://my.tv.sohu.com/us/305889970/707343351.shtml

http://my.tv.sohu.com/us/270458475/707343290.shtml

http://my.tv.sohu.com/us/245853407/707343518.shtml

http://my.tv.sohu.com/us/210318298/707343519.shtml

http://my.tv.sohu.com/us/296698768/707343454.shtml

http://my.tv.sohu.com/us/260257928/707343516.shtml

http://my.tv.sohu.com/us/441549359/707343350.shtml

http://my.tv.sohu.com/us/296307329/707343517.shtml

http://my.tv.sohu.com/us/135594753/707343453.shtml

http://my.tv.sohu.com/us/305889970/707343288.shtml

http://my.tv.sohu.com/us/210318298/707343287.shtml

http://my.tv.sohu.com/us/296698768/707343286.shtml

http://my.tv.sohu.com/us/270458475/707343451.shtml

http://my.tv.sohu.com/us/441549359/707343450.shtml

http://my.tv.sohu.com/us/296307329/707343449.shtml

http://my.tv.sohu.com/us/245853407/707343515.shtml

http://my.tv.sohu.com/us/260257928/707343348.shtml

http://my.tv.sohu.com/us/305889970/707343346.shtml

http://my.tv.sohu.com/us/135594753/707343347.shtml

http://my.tv.sohu.com/us/210318298/707343514.shtml

http://my.tv.sohu.com/us/270458475/707343513.shtml

http://my.tv.sohu.com/us/245853407/707343282.shtml

http://my.tv.sohu.com/us/260257928/707343512.shtml

http://my.tv.sohu.com/us/441549359/707343445.shtml

http://my.tv.sohu.com/us/296698768/707343284.shtml

http://my.tv.sohu.com/us/135594753/707343285.shtml

http://my.tv.sohu.com/us/305889970/707343280.shtml

http://my.tv.sohu.com/us/296307329/707343510.shtml

http://my.tv.sohu.com/us/210318298/707343447.shtml

http://my.tv.sohu.com/us/270458475/707343283.shtml

http://my.tv.sohu.com/us/260257928/707343343.shtml

http://my.tv.sohu.com/us/135594753/707343342.shtml

http://my.tv.sohu.com/us/296698768/707343444.shtml

http://my.tv.sohu.com/us/210318298/707343505.shtml

http://my.tv.sohu.com/us/224496667/707343443.shtml

http://my.tv.sohu.com/us/270458475/707343341.shtml

http://my.tv.sohu.com/us/296307329/707343508.shtml

http://my.tv.sohu.com/us/441549359/707343276.shtml

http://my.tv.sohu.com/us/260257928/707343278.shtml

http://my.tv.sohu.com/us/245853407/707343506.shtml

http://my.tv.sohu.com/us/296698768/707343340.shtml

http://my.tv.sohu.com/us/135594753/707343277.shtml

http://my.tv.sohu.com/us/224496667/707343439.shtml

http://my.tv.sohu.com/us/270458475/707343198.shtml

http://my.tv.sohu.com/us/296307329/707343502.shtml

http://my.tv.sohu.com/us/305889970/707343438.shtml

http://my.tv.sohu.com/us/296698768/707343273.shtml

http://my.tv.sohu.com/us/260257928/707343437.shtml

http://my.tv.sohu.com/us/245853407/707343272.shtml

http://my.tv.sohu.com/us/210318298/707343271.shtml

http://my.tv.sohu.com/us/135594753/707343269.shtml

http://my.tv.sohu.com/us/224496667/707343337.shtml

http://my.tv.sohu.com/us/441549359/707343435.shtml

http://my.tv.sohu.com/us/296307329/707343436.shtml

http://my.tv.sohu.com/us/305889970/707343196.shtml

http://my.tv.sohu.com/us/296698768/707343268.shtml

http://my.tv.sohu.com/us/245853407/707343434.shtml

http://my.tv.sohu.com/us/210318298/707343193.shtml

http://my.tv.sohu.com/us/296307329/707343430.shtml

http://my.tv.sohu.com/us/224496667/707343336.shtml

http://my.tv.sohu.com/us/270458475/707343429.shtml

http://my.tv.sohu.com/us/260257928/707343266.shtml

http://my.tv.sohu.com/us/135594753/707343192.shtml

http://my.tv.sohu.com/us/441549359/707343264.shtml

http://my.tv.sohu.com/us/296698768/707343190.shtml

http://my.tv.sohu.com/us/245853407/707343335.shtml

http://my.tv.sohu.com/us/305889970/707343262.shtml

http://my.tv.sohu.com/us/224496667/707343427.shtml

http://my.tv.sohu.com/us/270458475/707343426.shtml

http://my.tv.sohu.com/us/260257928/707343424.shtml

http://my.tv.sohu.com/us/296307329/707343261.shtml

http://my.tv.sohu.com/us/441549359/707343425.shtml

http://my.tv.sohu.com/us/210318298/707343259.shtml

http://my.tv.sohu.com/us/135594753/707343183.shtml

http://my.tv.sohu.com/us/245853407/707343421.shtml

http://my.tv.sohu.com/us/296698768/707343333.shtml

http://my.tv.sohu.com/us/305889970/707343186.shtml

http://my.tv.sohu.com/us/224496667/707343187.shtml

http://my.tv.sohu.com/us/270458475/707343420.shtml

http://my.tv.sohu.com/us/296307329/707343419.shtml

http://my.tv.sohu.com/us/441549359/707343184.shtml

http://my.tv.sohu.com/us/260257928/707343182.shtml

http://my.tv.sohu.com/us/210318298/707343330.shtml

http://my.tv.sohu.com/us/245853407/707343256.shtml

http://my.tv.sohu.com/us/224496667/707343418.shtml

http://my.tv.sohu.com/us/305889970/707343180.shtml

http://my.tv.sohu.com/us/296698768/707343416.shtml

http://my.tv.sohu.com/us/270458475/707343252.shtml

http://my.tv.sohu.com/us/296307329/707343328.shtml

http://my.tv.sohu.com/us/441549359/707343324.shtml

http://my.tv.sohu.com/us/135594753/707343254.shtml

http://my.tv.sohu.com/us/260257928/707343415.shtml

http://my.tv.sohu.com/us/210318298/707343178.shtml

http://my.tv.sohu.com/us/245853407/707343414.shtml

http://my.tv.sohu.com/us/224496667/707343253.shtml

http://my.tv.sohu.com/us/296698768/707343413.shtml

http://my.tv.sohu.com/us/296307329/707343248.shtml

http://my.tv.sohu.com/us/305889970/707343250.shtml

http://my.tv.sohu.com/us/135594753/707343412.shtml

http://my.tv.sohu.com/us/260257928/707343403.shtml

http://my.tv.sohu.com/us/210318298/707343247.shtml

http://my.tv.sohu.com/us/224496667/707343410.shtml

http://my.tv.sohu.com/us/270458475/707343320.shtml

http://my.tv.sohu.com/us/441549359/707343318.shtml

http://my.tv.sohu.com/us/245853407/707343173.shtml

http://my.tv.sohu.com/us/296698768/707343319.shtml

http://my.tv.sohu.com/us/135594753/707343174.shtml

http://my.tv.sohu.com/us/305889970/707343405.shtml

http://my.tv.sohu.com/us/296307329/707343404.shtml

http://my.tv.sohu.com/us/210318298/707343244.shtml

http://my.tv.sohu.com/us/224496667/707343317.shtml

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

相关推荐
AOwhisky16 小时前
Python 学习笔记(第一期与第二期)——基础语法——核心知识点自测与详解
开发语言·笔记·python·学习·云原生·运维开发
STLearner17 小时前
ICML 2026 | LLM×Graph论文总结[1]【图基础模型,文本属性图,多模态属性图,图对齐,图提示学习,关系深度学习
论文阅读·人工智能·python·深度学习·学习·机器学习·数据挖掘
泰晶科技17 小时前
【前装供应链实操笔记:车规晶振背后AEC-Q100与Q200的绑定关系】
笔记
神明不懂浪漫18 小时前
【第四章】CSS(二)——文本外观属性与复合选择器
前端·css·经验分享·笔记
什巳18 小时前
JAVA练习278- 和为 K 的子数组
java·学习·算法·leetcode
碎光拾影19 小时前
CPU与MCU核心区别揭秘
单片机·学习·51单片机
栈溢出了20 小时前
Redis 分片集群与哈希槽笔记
java·redis·笔记·spring
我命由我1234521 小时前
复利极简理解
经验分享·笔记·学习·职场和发展·求职招聘·职场发展·学习方法
AOwhisky21 小时前
Python 学习笔记(第三期)——流程控制核心知识点自测与详解
开发语言·笔记·python·学习·云原生·运维开发·流程控制
tyqtyq2221 小时前
药品说明书解读 —— AI 安全用药助手,鸿蒙原生应用深度解析
人工智能·学习·华为·生活·harmonyos