If I blit an image onto the screen how can I find the location of that image on the screen after. I'm using pygame in python
Asked
Active
Viewed 518 times
0
-
1Please show us your [code](https://stackoverflow.com/help/mcve). When you call [`blit`](http://www.pygame.org/docs/ref/surface.html#pygame.Surface.blit) you need to pass the top left coordinates, so you already know the position. – skrx Oct 06 '18 at 07:18
2 Answers
1
When you blit one surface onto another, they become the same object. With this understood, you can't retrieve the original surface you blitted onto another's position. However, if you stored the position where you blitted the surface you can retrieve the original object's position within the new surface.
example:
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()
This program will give:
(330, 167)

lakam99
- 575
- 4
- 9
- 24
0
The answer to my own question:
class Objects:
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