Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/311.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
pygame.mouse.get_pos和Rect.collidepoint的Python曲面真实位置坐标_Python_Pygame_Mouse_Pygame Surface - Fatal编程技术网

pygame.mouse.get_pos和Rect.collidepoint的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的第二个曲面相关,在该曲面上闪烁矩形 那就不行了。有没有一

在python程序中,我有两个曲面:

  • 屏幕表面
    :屏幕
  • 脚面
    :另一个表面在
    屏幕表面
    上闪烁
我在
脚面上放置了一些矩形块,问题是
rect.collidepoint()
提供了链接到
脚面的相对坐标,而
pygame.mouse.get_pos()
提供了绝对坐标

例如:

pygame.mouse.get_pos()
-->(177500)与名为
screensface的主曲面相关

Rect.collidepoint()
-->与名为
FootSurface
的第二个曲面相关,在该曲面上闪烁矩形


那就不行了。有没有一种优雅的python方法可以做到这一点:在
脚面上设置鼠标的相对位置
,或者设置my
Rect的绝对位置
;或者我必须在
屏幕表面
中将代码更改为split
Rect
,您可以通过简单的减法计算鼠标相对于任何表面的相对位置

考虑以下示例:

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()