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

相关推荐
是一个Bug1 天前
Agent(智能体)应用 的入门学习路径
学习·机器学习
2301_809051141 天前
Linux 网络编程 学习笔记
linux·网络·学习
eggcode1 天前
【Qt学习】Linux(ARM架构)在线安装Qt6.x
linux·qt·学习·arm
鹏北海-RemHusband1 天前
Go 语言进阶笔记 — 面向 JS/TS 前端开发者
笔记·golang
_李小白1 天前
【android opencv学习笔记】Day 26: 滤波算法之低通滤波与图像缩放插值
android·opencv·学习
Bechamz1 天前
大数据开发学习Day43
大数据·学习
nnsix1 天前
Unity QFramework ResKit、UIKit 笔记
笔记
摇滚侠1 天前
Java 零基础全套教程,反射机制,笔记 187-188
java·开发语言·笔记
【云轩】1 天前
如何设计一台能模拟电机的电子负载:一个硬件工程师的实战笔记
笔记·嵌入式硬件