[英]Python surface real position coordinates of pygame.mouse.get_pos and Rect.collidepoint
In my python prog i have 2 surfaces : 在我的python prog中我有2个表面:
ScreenSurface
: the screen ScreenSurface
:屏幕 FootSurface
: another surface blited on ScreenSurface
. FootSurface
:另一面blited上ScreenSurface
。 I put some rect blitted on the FootSurface
, the problem is that Rect.collidepoint()
gives me relative coordinates linked to the FootSurface
and pygame.mouse.get_pos()
gives absolute coordinates. 我在FootSurface
上放了一些rect blitted,问题是Rect.collidepoint()
给我链接到FootSurface
相对坐标, pygame.mouse.get_pos()
给出绝对坐标。
for example : 例如 :
pygame.mouse.get_pos()
--> (177, 500) related to the main surface named ScreenSurface
pygame.mouse.get_pos()
- >( ScreenSurface
)与名为ScreenSurface
的主曲面ScreenSurface
Rect.collidepoint()
--> related to the second surface named FootSurface
where the rect is blitted Rect.collidepoint()
- >与名为FootSurface
的第二个表面相关,其中rect是blitted
Then that can't work. 那就行不通了。 Is there an elegant python way to do this things: have the relative position of mouse on the FootSurface
or the absolute position of my Rect
; 是否有一种优雅的python方式来做这件事:鼠标在FootSurface
上的相对位置或我的Rect
的绝对位置; or must I change my code to split Rect
in the ScreenSurface
. 或者我必须更改我的代码以在ScreenSurface
拆分Rect
。
You can calculate the relative mouse position to any surface with a simple subtraction. 您可以使用简单的减法计算任何曲面的相对鼠标位置。
Consider the following example: 请考虑以下示例:
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()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.