如何从单击图像的点而不是python中的中心点单击并拖动图像
这里我有一张比屏幕还大的地图图片。我将其设置为,当我单击图像时,图像会移动,使图像的中心位于鼠标下方,然后我可以拖动图像。我想知道我怎样才能使图像从我点击的点拖动,而不是让它跳到中心。我希望这不会太令人困惑 正如sshashank124所说,您需要用图像的宽度、高度和尺寸偏移图像的x、y位置。例如:如何从单击图像的点而不是python中的中心点单击并拖动图像,python,python-2.7,pygame,Python,Python 2.7,Pygame,这里我有一张比屏幕还大的地图图片。我将其设置为,当我单击图像时,图像会移动,使图像的中心位于鼠标下方,然后我可以拖动图像。我想知道我怎样才能使图像从我点击的点拖动,而不是让它跳到中心。我希望这不会太令人困惑 正如sshashank124所说,您需要用图像的宽度、高度和尺寸偏移图像的x、y位置。例如: import pygame pygame.init() width = 272 height = 552 white = 255,255,255 gameDisplay = pygame.d
import pygame
pygame.init()
width = 272
height = 552
white = 255,255,255
gameDisplay = pygame.display.set_mode((width,height))
pygame.display.set_caption('War Games')
Map = pygame.image.load("SPEMap.jpg"); pygame.Surface((800,552))
rect = Map.get_rect(); rect.center = (400,286)
stop = False
while not stop:
gameDisplay.blit(Map, rect)
if pygame.mouse.get_pressed()[0]:
if rect.collidepoint(pygame.mouse.get_pos()):
rect.center = pygame.mouse.get_pos()
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
pygame.display.update()
我希望我没有看错你的问题
编辑:上面的代码片段将在您的程序中转换为类似下面的代码
imageX = mouseX - (imageWidth / 2)
imageY = mouseY - (imageHeight / 2)
如果有价值,请选择最佳答案表示感谢。:) pygame.mouse.get_pos()返回鼠标x和y位置的元组
您可以分别获得鼠标位置,如下所示:
import pygame
pygame.init()
gameDisplay = pygame.display.set_mode((272, 552))
mapWidth = 0 # MUST FILL IN SIZE
mapHeight = 0 # MUST FILL IN SIZE
gameMap = pygame.image.load("SPEMap.jpg")
mapRect = map.get_rect()
stop = False
while not stop:
if pygame.mouse.get_pressed()[0]:
if mapRect.collidepoint(pygame.mouse.get_pos()):
mapRect.x = pygame.mouse.get_pos()[0] - (mapWidth / 2)
mapRect.x = pygame.mouse.get_pos()[0] - (mapWidth / 2)
for event in pygame.event.get():
if event.type == pygame.QUIT:
stop = True
gameDisplay.blit(gameMap, mapRect)
pygame.display.update()
pygame.quit()
exit()
获取中心相对于单击位置的偏移,然后使用它“取消偏移”。如何获取鼠标x坐标和鼠标y坐标seperately@RomanFormicola:您已经有了单独的值(
pygame.mouse.get_pos()[0/1]
)。关于我添加的更多细节,请参见上面的答案。这不是重点吗?我一定是误会了什么,恐怕我没能成功地申请你的帮助。我对python非常陌生,如果有人能更直截了当地提出建议,那就太好了。给您带来的不便,我深表歉意。我是个新手。
mouse_x = pygame.mouse.get_pos()[0]
mouse_y = pygame.mouse.get_pos()[1]