如何找到 blit 到屏幕上的图像的位置 (python,pygame)
How to find location of an image that was blit onto screen(python,pygame)
如果我将一个图像 blit 到屏幕上,之后如何找到该图像在屏幕上的位置。我在 python
中使用 pygame
当你将一个表面 blit 到另一个表面时,它们就变成了同一个对象。了解了这一点,您就无法取回您 blit 到另一个位置的原始表面。但是,如果您存储了块化表面的位置,您可以在新表面中检索原始对象的位置。
示例:
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)
我自己问题的答案:
class 对象:
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
如果我将一个图像 blit 到屏幕上,之后如何找到该图像在屏幕上的位置。我在 python
中使用 pygame当你将一个表面 blit 到另一个表面时,它们就变成了同一个对象。了解了这一点,您就无法取回您 blit 到另一个位置的原始表面。但是,如果您存储了块化表面的位置,您可以在新表面中检索原始对象的位置。
示例:
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)
我自己问题的答案:
class 对象:
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