How can I get the x and y coordinates of an image in PyGame? I need this for a game where I have to get the shot right out of an enemy. Here is the code:
# -*- coding: cp1252 -*-
import sys
import random
import pygame
print pygame.init() #inicia todo o módulo pygame (pygame.init() retorna algo, não faz a inicialização completa por isso so print
x_player, y_player = 650, 440
screen = pygame.display.set_mode((700, 500)) # screen's size width and height
pygame.display.set_caption("Space Invaders v1.0.0") # name of the screen
pygame.display.flip()
invaders = pygame.image.load(
"C:\Users\bernardo\Documents\IFC\Programação\SpaceInvaders-my_own\space-invaders.jpeg").convert()
player = pygame.image.load(
"C:\Users\bernardo\Documents\IFC\Programação\SpaceInvaders-my_own\28006.png").convert() # loads the image
# of the player and put it on a variable
mother_ship = pygame.image.load(
"C:\Users\bernardo\Documents\IFC\Programação\SpaceInvaders-my_own\mother_ship.png").convert()
lifes = pygame.image.load(
"C:\Users\bernardo\Documents\IFC\Programação\SpaceInvaders-my_own\28007.png").convert()
shots = pygame.image.load(
"C:\Users\bernardo\Documents\IFC\Programação\SpaceInvaders-my_own\shots_and_bombs2.png").convert()
pygame.font.init()
clock = pygame.time.Clock()
player_size = player.get_size()
move_x = 0
x_invaders, y_invaders = 60, 60
lifes_list = [lifes, lifes, lifes]
x_mother = 0
invaders_matrix = [[invaders] * 11] * 5
existe_nave = False
start = True
while len(lifes_list) != 0:
screen.fill([0, 0, 0]) # color of the screen (rgb)
screen.blit(player, [(x_player / 2), y_player]) # initial position of player
clock.tick(35) # are going to happen just movements on 23 frames per second
screen.blit(shots, (invaders_matrix[random.randint(0, 4)][random.randint(0, 10)].get_rect().centerx,
invaders_matrix[random.randint(0, 4)][random.randint(0, 10)].get_rect().centery))
x_invaders, y_invaders = 105, 125
for invader in range(len(invaders_matrix)):
for invad in range(len(invaders_matrix[invader])):
screen.blit(invaders_matrix[invader][invad], [x_invaders, y_invaders])
x_invaders += 45
x_invaders = 105
y_invaders += 30
if existe_nave and (x_mother < 700):
screen.blit(mother_ship, [x_mother, 35])
x_mother += 4.5
screen.blit(mother_ship, [x_mother, 35])
elif random.randint(0, 800) == 0:
existe_nave = True
x_mother = 0
width_for_lifes = 680
for icon_lifes in lifes_list:
width_for_lifes -= 50
screen.blit(icon_lifes, (width_for_lifes, 15))
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
start = False
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
move_x -= 10
start = False
if event.key == pygame.K_RIGHT:
move_x += 10
start = False
if event.type == pygame.KEYUP:
if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT:
move_x = 0
start = False
if x_player >= 1280:
x_player = 1280
if x_player <= 25:
x_player = 25
font = pygame.font.Font(None, 36)
text = font.render("Lifes", 1, (0, 255, 85))
screen.blit(text, (450, 15))
font = pygame.font.Font(None, 36)
text = font.render("Score", 1, (0, 255, 85))
screen.blit(text, (20, 15))
x_player += move_x
pygame.display.update()