简体   繁体   中英

How do i find out the x and positions of something on screen (python)

I am making a game in python and I am trying to get the x and y values of something put on screen when it has been put on.

The code i have made as an example is,

def runGame():
    self.RocketImage = pygame.image.load('LaserBase.png')
    self.screen.blit(self.RocketImage, (112, 650))
    pygame.display.flip()
    self.getPos()

def getPos(self, posx, posy):
    print("The position is", posx)

My question is how do i get the x and y coordinates of the RocketImage which is blit / displayed on screen.

Any information would be helpfull / codes on how to do this.

Thank you

As far as I know there is no such method in Pygame.

But you can write your own method for that purpose. How it should works:

  1. Get the pixels colors of your image ('LaserBase.png' in your case)
  2. Search for the same pixels colors on the screen
  3. If pixels matched, that means that you find your image

This is actually quite simple, but only requires Python, no methods in Pygame. Just have 2 variables (or a 2 element array) to keep track of the rocket's location. Instead of moving the image, change the variables, and blit the surface at those coordinates. There is no easy way to detect the location of an already-blitted image.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM