diff --git a/tile.py b/tile.py new file mode 100644 index 0000000..6f69e3a --- /dev/null +++ b/tile.py @@ -0,0 +1,20 @@ +import pygame +from settings import * + +class Tile(pygame.sprite.Sprite): + def __init__(self,pos,groups,sprite_type,surface = pygame.Surface((TILESIZE,TILESIZE))): + # 调用父类(pygame.sprite.Sprite)的构造函数 + super().__init__(groups) + # 设置瓷砖类型(如 'player'、'object'、'grass' 等) + self.sprite_type = sprite_type + # 获取瓷砖类型对应的 Y 偏移量 + y_offset = HITBOX_OFFSET[sprite_type] + # 设置瓷砖的表面(Surface),默认为 TILESIZE x TILESIZE 的空表面 + self.image = surface + # 根据瓷砖类型设置矩形位置和碰撞框 + if sprite_type == 'object': + self.rect = self.image.get_rect(topleft = (pos[0],pos[1] - TILESIZE)) + else: + self.rect = self.image.get_rect(topleft = pos) + # 根据 Y 偏移量设置碰撞框大小 + self.hitbox = self.rect.inflate(0,y_offset) \ No newline at end of file