基于pygame的简易游戏框架.zip
立即下载
资源介绍:
基于pygame的简易游戏框架.zip
import pygame
pygame.init()
blocklong = 16
w, h = screen_size = (30 * blocklong, 20 * blocklong)
# 1个方块需要 <坐标> <材质> <形态(固\液\气\虚无态)> <碰撞箱坐标角1> <碰撞箱坐标角2> [可否被覆盖] [可否发光] [阻力]
class Block:
def __init__(self, pos: tuple[int, int], texture: str, morphology: int,
hitbox_pos1: tuple[int, int], hitbox_pos2: tuple[int, int],
can_overwrite: bool = False, can_luminous: bool = False, resistance: int = 1):
# 这里的参数与上文一一对应, 其中 morphology 表示的是形态, 我们规定1为固态, 2为液态, 3为气态
self.x, self.y = self.pos = pos
self.texture = pygame.image.load(texture)
self.morphology = morphology
self.hitbox = pygame.Rect(hitbox_pos1[0], hitbox_pos1[1], hitbox_pos2[0], hitbox_pos2[1])
self.can_overwrite = can_overwrite
self.can_luminous = can_luminous
self.resistance = resistance
# 这是实体的相关代码
class Entity:
def __init__(self, pos: tuple[int, int], texture: str, type: int,
go: tuple[tuple, tuple, tuple, tuple] = (
(pygame.K_LEFT,), (pygame.K_RIGHT,), (pygame.K_UP,), (pygame.K_DOWN,))):
# 目前我还没想好具体的变量, 就写了点基础的(不可或缺的)变量awa
# type 表示类型, 1为玩家, 2为其他实体
self.x, self.y = self.pos = pos
self.texture = pygame.image.load(texture)
self.hitbox = pygame.Rect(pos[0], pos[1], pos[0] + blocklong, pos[1] + blocklong)
self.type = type
self.go = go
def move(self, x, y):
# 这是实体移动的代码, x和y是相对于实体的坐标
if w - 16 >= self.x + x >= 0:
self.x += x
if h - 16 >= self.y + y >= 0:
self.y += y
self.pos = self.x, self.y
can = True
for block in blocklist:
block: Block
if self.pos == block.pos and block.morphology == 1:
can = False
break
if not can:
self.x -= x
self.y -= y
self.pos = self.x, self.y
self.hitbox.move_ip(x, y)
entitylist = []
blocklist = []
资源文件列表:
基于pygame的简易游戏框架.zip 大约有994个文件