pygame.mouse.get_pos和Rect.collidepoint的Python曲面真实位置坐标
在python程序中,我有两个曲面:pygame.mouse.get_pos和Rect.collidepoint的Python曲面真实位置坐标,python,pygame,mouse,pygame-surface,Python,Pygame,Mouse,Pygame Surface,在python程序中,我有两个曲面: 屏幕表面:屏幕 脚面:另一个表面在屏幕表面上闪烁 我在脚面上放置了一些矩形块,问题是rect.collidepoint()提供了链接到脚面的相对坐标,而pygame.mouse.get_pos()提供了绝对坐标 例如: pygame.mouse.get_pos()-->(177500)与名为screensface的主曲面相关 Rect.collidepoint()-->与名为FootSurface的第二个曲面相关,在该曲面上闪烁矩形 那就不行了。有没有一
:屏幕屏幕表面
:另一个表面在脚面
上闪烁屏幕表面
脚面上放置了一些矩形块,问题是rect.collidepoint()
提供了链接到脚面的相对坐标,而pygame.mouse.get_pos()
提供了绝对坐标
例如:
pygame.mouse.get_pos()
-->(177500)与名为screensface的主曲面相关
Rect.collidepoint()
-->与名为FootSurface
的第二个曲面相关,在该曲面上闪烁矩形
那就不行了。有没有一种优雅的python方法可以做到这一点:在脚面上设置鼠标的相对位置
,或者设置myRect的绝对位置
;或者我必须在屏幕表面
中将代码更改为splitRect
,您可以通过简单的减法计算鼠标相对于任何表面的相对位置
考虑以下示例:
import pygame
pygame.init()
screen = pygame.display.set_mode((400, 400))
rect = pygame.Rect(180, 180, 20, 20)
clock = pygame.time.Clock()
d=1
while True:
for e in pygame.event.get():
if e.type == pygame.QUIT:
raise
screen.fill((0, 0, 0))
pygame.draw.rect(screen, (255, 255, 255), rect)
rect.move_ip(d, 0)
if not screen.get_rect().contains(rect):
d *= -1
pos = pygame.mouse.get_pos()
# print the 'absolute' mouse position (relative to the screen)
print 'absoulte:', pos
# print the mouse position relative to rect
print 'to rect:', pos[0] - rect.x, pos[1] - rect.y
clock.tick(100)
pygame.display.flip()