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
|
top = real_y - (PLAYER_SIZE + BLOCK_SIZE) / 2
|
||||||
bottom = 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]:
|
def get_collision_surface(self) -> tuple[int, int, int, int]:
|
||||||
# 获得方块表面界限(碰撞面)
|
# 获得方块表面界限(碰撞面)
|
||||||
|
@ -27,32 +27,20 @@ class Player:
|
|||||||
|
|
||||||
# 无碰撞体积时加 dx dy 有碰撞体积时,限制其值
|
# 无碰撞体积时加 dx dy 有碰撞体积时,限制其值
|
||||||
# 向下移动时且下面方块有碰撞体积,与上表面碰撞
|
# 向下移动时且下面方块有碰撞体积,与上表面碰撞
|
||||||
if dy > 0 and bottom_block is not None and bottom_block.collision:
|
if dy > 0 and bottom_block is not None and bottom_block.check_collision(self):
|
||||||
if bottom_block.check_collision(self):
|
new_y = bottom_block.get_collision_surface()[0] - 10
|
||||||
new_y = bottom_block.get_collision_surface()[0] - 10
|
|
||||||
else:
|
|
||||||
new_y = self.y + dy
|
|
||||||
# 向上移动时且上面方块有碰撞体积,与下表面碰撞
|
# 向上移动时且上面方块有碰撞体积,与下表面碰撞
|
||||||
elif dy < 0 and top_block is not None and top_block.collision:
|
elif dy < 0 and top_block is not None and top_block.check_collision(self):
|
||||||
if top_block.check_collision(self):
|
new_y = top_block.get_collision_surface()[2] + 10
|
||||||
new_y = top_block.get_collision_surface()[2] + 10
|
|
||||||
else:
|
|
||||||
new_y = self.y + dy
|
|
||||||
else:
|
else:
|
||||||
new_y = self.y + dy
|
new_y = self.y + dy
|
||||||
|
|
||||||
# 右移动
|
# 右移动
|
||||||
if dx > 0 and right_block is not None and right_block.collision:
|
if dx > 0 and right_block is not None and right_block.check_collision(self):
|
||||||
if right_block.check_collision(self):
|
new_x = right_block.get_collision_surface()[3] - 10
|
||||||
new_x = right_block.get_collision_surface()[3] - 10
|
|
||||||
else:
|
|
||||||
new_x = self.x + dx
|
|
||||||
# 左移动
|
# 左移动
|
||||||
elif dx < 0 and left_block is not None and left_block.collision:
|
elif dx < 0 and left_block is not None and left_block.check_collision(self):
|
||||||
if left_block.check_collision(self):
|
new_x = left_block.get_collision_surface()[1] + 10
|
||||||
new_x = left_block.get_collision_surface()[1] + 10
|
|
||||||
else:
|
|
||||||
new_x = self.x + dx
|
|
||||||
else:
|
else:
|
||||||
new_x = self.x + dx
|
new_x = self.x + dx
|
||||||
|
|
||||||
@ -98,5 +86,10 @@ class Player:
|
|||||||
rect = pygame.Rect(block_x, block_y, BLOCK_SIZE, BLOCK_SIZE)
|
rect = pygame.Rect(block_x, block_y, BLOCK_SIZE, BLOCK_SIZE)
|
||||||
pygame.draw.rect(screen, '#ffd5004d', rect)
|
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]:
|
def get_pixel_idx(self) -> tuple[int, int]:
|
||||||
return int(self.x) // BLOCK_SIZE, int(self.y) // BLOCK_SIZE
|
return int(self.x) // BLOCK_SIZE, int(self.y) // BLOCK_SIZE
|
Reference in New Issue
Block a user