我在更新鼠标位置和检查由于等级滚动导致的实体碰撞(鼠标和实体之间)时遇到问题。我已经使用了摄像头功能,从这个问题:How to add scrolling to platformer in pygame在pygame中使用鼠标获取世界坐标
我曾尝试使用相机功能上的鼠标是这样的:
def update(self, target, target_type, mouse):
if target_type != "mouse":
self.state = self.camera_func(self.state, target.rect)
else:
new_pos = self.camera_func(mouse.rect, target.rect)
mouse.update((new_pos[0], new_pos[1]))
print mouse.rect
但mouse.rect始终设置为608, 0
。有人可以帮我弄这个吗?鼠标类看起来是这样的:
class Mouse(Entity):
def __init__(self, pos):
Entity.__init__(self)
self.x = pos[0]
self.y = pos[1]
self.rect = Rect(pos[0], pos[1], 32, 32)
def update(self, pos, check=False):
self.x = pos[0]
self.y = pos[1]
self.rect.top = pos[1]
self.rect.left = pos[0]
if check:
print "Mouse Pos: %s" %(self.rect)
print self.x, self.y
每次我点击屏幕,并使之通过碰撞试验,它总是使用屏幕上的点,但是我需要在地图上的点(如果是有道理的)。例如,屏幕尺寸为640x640
。如果我点击左上角,鼠标位置将始终为0,0
但是,实际地图坐标可能在屏幕的顶部角落320,180
。我试图使用相机和鼠标更新所有内容,唯一真正的结果是当我将camera.update
功能应用于鼠标时,但这会阻止播放器成为滚动的原因,因此我试图用mouse.rect
更新mouse.rect
这个功能。
尝试代码:
mouse_pos = pygame.mouse.get_pos()
mouse_offset = camera.apply(mouse)
pos = mouse_pos[0] + mouse_offset.left, mouse_pos[1] + mouse_offset.top
mouse.update(mouse_pos)
if hit_block:
print "Mouse Screen Pos: ", mouse_pos
print "Mouse Pos With Offset: ", pos
print "Mouse Offset: ", mouse_offset
replace_block(pos)
我说我的代码到实际的答案,似乎有一个问题,即当屏幕高度或宽度超过屏幕高度或宽度时,偏移量开始减小 – ReallyGoodPie
如果将完整代码粘贴到pastebin(所以我可以运行它)我可以看一看。 – ninMonkey
http://pastebin.com/Sx1KKBU2这是我的完整代码。 – ReallyGoodPie