【发布时间】:2020-04-09 21:29:38
【问题描述】:
我一直在寻找解决方案,但任何其他线程都可以帮助我。
我一直在尝试让精灵在背景图像上移动。背景有透明的街道,应该是精灵的可能路径。
我需要检测精灵何时与不透明的背景图像的其他部分发生碰撞。我尝试了完美的碰撞方法,但我认为这不是我的情况的正确解决方案,因为background.rect 没有任何意义。
我也尝试了重叠方法,但它总是返回 true。
图片为 32 位深度,我打电话给convert_alpha()
class sprites(pygame.sprite.Sprite):
def __init__(self):
pygame.sprite.Sprite.__init__(self)
self.x = 200
self.y = 300
self.img= player_sprite
self.rect = player_sprite.get_rect()
self.mask = pygame.mask.from_surface(player_sprite)
def position(self):
dx = mouse_x-self.x
dy = self.y-mouse_y
d = float((dx**2+dy**2)**0.5)
displ_x = dx/d
displ_y = dy/d
self.x += displ_x
self.y += displ_y
if type(self.mask.overlap(object_background.mask,(0,0))):
self.x -= displ_x
self.y -= displ_y
class object_background_class(pygame.sprite.Sprite):
def __init__(self):
pygame.sprite.Sprite.__init__(self)
self.img = object_background_img
self.rect = object_background_img.get_rect()
self.mask = pygame.mask.from_surface(object_background_img.convert_alpha())
object_background = object_background_class()
player = sprites()
player.position() changes each time the coordinates of the sprite accordind to the mouse(x,y) and check if with the new x,y of the player make it collides with the background
game_state = False
while not game_state:
for e in pygame.event.get():
if e == pygame.QUIT:
game_state = True
if e.type == pygame.KEYDOWN and e.type == pygame.K_ESCAPE:
game_state = True
if e.type == pygame.KEYDOWN:
if e.key == 27:
game_state = True
mouse_x, mouse_y = pygame.mouse.get_pos()
player.position()
DISPLAYSURFACE.blit(color_background, (0, 0))
DISPLAYSURFACE.blit(player.img, (player.x, player.y))
DISPLAYSURFACE.blit(object_background.img, (0, 0))
pygame.display.flip()
【问题讨论】:
-
你必须用街道画(paintshop)一个面具
-
我已经尝试使用街道的像素颜色来检查精灵,但我正在寻找一种替代方法来检测是否有任何精灵边缘与街道的边界发生碰撞,而不仅仅是他的绳索 x,y。
-
是否可以在不分割背景元素的情况下检测精灵和具有透明部分(街道)和不透明部分(障碍物)的背景图像元素之间的碰撞?
-
我需要检测与不透明元素的碰撞,但我不能将它们溢出。