change Player.move
This commit is contained in:
parent
705b5c746a
commit
038b876b15
@ -41,7 +41,7 @@ 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]:
|
||||
# 获得方块表面界限(碰撞面)
|
||||
|
@ -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
|
Reference in New Issue
Block a user