如何查找在屏幕上变色的图像的位置(python,pygame)

时间:2018-10-05 22:38:19

标签: pygame pygame-surface

如果我在屏幕上显示图像,之后如何在屏幕上找到该图像的位置。我在python中使用pygame

2 个答案:

答案 0 :(得分:1)

将一个表面蒙皮到另一个表面时,它们成为同一对象。了解了这一点后,您将无法检索被涂抹到其他位置的原始曲面。但是,如果您存储了在曲面上消光的位置,则可以在新曲面内检索原始对象的位置。

示例:

import pygame
white, red = (255, 255, 255), (255, 0, 0)
screen = pygame.display.set_mode((500, 500))
a = pygame.Surface((100, 100))
b = pygame.Surface((10, 10))
b_pos = (30, 42)
a_pos = (300, 125)
a.fill(white)
b.fill(red)
a.blit(b, b_pos)
screen.blit(a, a_pos)
new_b_pos = a_pos[0] + b_pos[0], a_pos[1] + b_pos[1]
print(new_b_pos)
pygame.quit()

该程序将提供:

 (330, 167)

答案 1 :(得分:0)

我自己的问题的答案:

类对象:

def __init__(self, screen, image_location, xposition, yposition):
    image = pygame.image.load(image_location)
    screen.blit(image, [xposition, yposition])
    self.height = image.get_height()
    self.width = image.get_width()
    self.xposition = xposition
    self.yposition = yposition

def collision(object1, object2):
    if object1.xposition <= object2.xposition <= object1.xposition + object1.width:
        if object1.yposition <= object2.yposition <= object1.yposition + object1.height:
            return True
    if object1.xposition <= object2.xposition + object2.width <= object1.xposition + object1.width:
        if object1.yposition <= object2.yposition <= object1.yposition + object1.height:
            return True
    if object1.xposition <= object2.xposition <= object1.xposition + object1.width:
        if object1.yposition <= object2.yposition + object2.height <= object1.yposition + object1.height:
            return True
    if object1.xposition <= object2.xposition + object2.width <= object1.xposition + object1.width:
        if object1.yposition <= object2.yposition + object2.height <= object1.yposition + object1.height:
            return True
    if object2.xposition <= object1.xposition <= object2.xposition + object2.width:
        if object2.yposition <= object1.yposition <= object2.yposition + object2.height:
            return True
    if object2.xposition <= object1.xposition + object1.width <= object2.xposition + object2.width:
        if object2.yposition <= object1.yposition <= object2.yposition + object2.height:
            return True
    if object2.xposition <= object1.xposition <= object2.xposition + object2.width:
        if object2.yposition <= object1.yposition + object1.height <= object2.yposition + object2.height:
            return True
    if object2.xposition <= object1.xposition + object1.width <= object2.xposition + object2.width:
        if object2.yposition <= object1.yposition + object1.height <= object2.yposition + object2.height:
            return True
    return False