Compare commits

...

2 Commits

Author SHA1 Message Date
038b876b15 change Player.move 2025-02-02 01:33:15 +08:00
705b5c746a change get_collision_surface 2025-02-02 01:11:44 +08:00
2 changed files with 18 additions and 28 deletions

View File

@@ -41,18 +41,15 @@ class Block:
top = real_y - (PLAYER_SIZE + BLOCK_SIZE) / 2
bottom = real_y + (PLAYER_SIZE + BLOCK_SIZE) / 2
# 判碰撞:
return left < player.x < right and top < player.y < bottom
return left < player.x < right and top < player.y < bottom and self.collision
def get_collision_surface(self) -> tuple[int, int, int, int]:
# 获得方块表面界限(碰撞面)
# 上、右、下、左
real_x = self.x * BLOCK_SIZE + BLOCK_SIZE / 2
real_y = self.y * BLOCK_SIZE + BLOCK_SIZE / 2
top = real_y - BLOCK_SIZE / 2
bottom = real_y + BLOCK_SIZE / 2
left = real_x - BLOCK_SIZE / 2
right = real_x + BLOCK_SIZE / 2
top = self.y * BLOCK_SIZE
bottom = top + BLOCK_SIZE
left = self.x * BLOCK_SIZE
right = left + BLOCK_SIZE
return top, right, bottom, left
def get_center_vector(self):

View File

@@ -27,32 +27,20 @@ class Player:
# 无碰撞体积时加 dx dy 有碰撞体积时,限制其值
# 向下移动时且下面方块有碰撞体积,与上表面碰撞
if dy > 0 and bottom_block is not None and bottom_block.collision:
if bottom_block.check_collision(self):
new_y = bottom_block.get_collision_surface()[0] - 10
else:
new_y = self.y + dy
if dy > 0 and bottom_block is not None and bottom_block.check_collision(self):
new_y = bottom_block.get_collision_surface()[0] - 10
# 向上移动时且上面方块有碰撞体积,与下表面碰撞
elif dy < 0 and top_block is not None and top_block.collision:
if top_block.check_collision(self):
new_y = top_block.get_collision_surface()[2] + 10
else:
new_y = self.y + dy
elif dy < 0 and top_block is not None and top_block.check_collision(self):
new_y = top_block.get_collision_surface()[2] + 10
else:
new_y = self.y + dy
# 右移动
if dx > 0 and right_block is not None and right_block.collision:
if right_block.check_collision(self):
new_x = right_block.get_collision_surface()[3] - 10
else:
new_x = self.x + dx
if dx > 0 and right_block is not None and right_block.check_collision(self):
new_x = right_block.get_collision_surface()[3] - 10
# 左移动
elif dx < 0 and left_block is not None and left_block.collision:
if left_block.check_collision(self):
new_x = left_block.get_collision_surface()[1] + 10
else:
new_x = self.x + dx
elif dx < 0 and left_block is not None and left_block.check_collision(self):
new_x = left_block.get_collision_surface()[1] + 10
else:
new_x = self.x + dx
@@ -98,5 +86,10 @@ class Player:
rect = pygame.Rect(block_x, block_y, BLOCK_SIZE, BLOCK_SIZE)
pygame.draw.rect(screen, '#ffd5004d', rect)
# 显示玩家坐标
font = pygame.font.SysFont('KaiTi', 24)
text = font.render(f"x: {self.x}, y: {self.y}", True, (255, 255, 255))
screen.blit(text, (200, 34))
def get_pixel_idx(self) -> tuple[int, int]:
return int(self.x) // BLOCK_SIZE, int(self.y) // BLOCK_SIZE