1

I've recentley started using pygame to see what I could come up with and come across a problem which I can't find an answer to without this pygame.sprite.Sprite and rect things, my question is how do I find a position of an image since I need the position of it to calculate a rotation angle. If it helps, here's the code that i am using:

import sys, pygame, math, time;
from pygame.locals import *;
spaceship = ('spaceship.png')
mouse_c = ('crosshair.png')
backg = ('background.jpg')
fire_beam = ('beam.png')
pygame.init()
screen = pygame.display.set_mode((800, 600))
bk = pygame.image.load(backg).convert_alpha()
mousec = pygame.image.load(mouse_c).convert_alpha()
space_ship = pygame.image.load(spaceship).convert_alpha()
f_beam = pygame.image.load(fire_beam).convert_alpha()
clock = pygame.time.Clock()
pygame.mouse.set_visible(False)
space_ship_rect = space_ship.get_rect() #added
while True:
    screen.blit(bk, (0, 0))
    for event in pygame.event.get():
        if event.type == QUIT:
            pygame.quit()
            sys.exit()
        elif event.type == MOUSEBUTTONDOWN and event.button == 1:
            print("Left Button Pressed")
        elif event.type == MOUSEBUTTONDOWN and event.button == 3:
            print("Right Button Pressed")
        if event.type == MOUSEMOTION:
            x1, y1 = pygame.mouse.get_pos()
            x2, y2 = space_ship_rect.x, space_ship_rect.y #added
            dx, dy = x2 - x1, y2 - y1
            rads = math.atan2(dx, dy)
            degs = math.degrees(rads)
            print degs
            pygame.transform.rotate(space_ship, degs)
            pygame.display.update()
    pos = pygame.mouse.get_pos()
    screen.blit(mousec, (pos))
    #screen.blit(space_ship, (375, 300)) #space_ship_rect did not work here.
    pygame.display.update()
user2993584
  • 139
  • 2
  • 5
  • 13

1 Answers1

10

You can use .get_rect() for image to get image rectangle (pygame.Rect())

space_ship = pygame.image.load(spaceship).convert_alpha()

space_ship_rect = space_ship.get_rect()

and than you can get x, y, width, height and even centerx, centery, center etc.

print space_ship_rect.x, space_ship_rect.y, 
print space_ship_rect.centerx, space_ship_rect.centery, 
print space_ship_rect.center
print space_ship_rect.left, space_ship_rect.right
print space_ship_rect.top, space_ship_rect.bottom
print space_ship_rect.topleft, space_ship_rect.bottomright
print space_ship_rect.width, space_ship_rect.height

by the way: .get_rect() works also with your screen and other pygame.Surface().

But you can't assign new values to image so you have to keep space_ship_rect and change values in it (so use get_rect() only once to get image size)

space_ship_rect.x = 100
space_ship_rect.y = 200
 # or
space_ship_rect.centerx = 100
space_ship_rect.centery = 200

If you change x, y rectangle recalculate centerx, centery using width and height. If you change centerx, centery rectangle recalculate x and y

You could create class with self.image for bitmap and self.rect for image size and position.

ps. you can use screen.blit(space_ship, space_ship_rect)

furas
  • 134,197
  • 12
  • 106
  • 148
  • I want to point out that if you know attributes like `top` and `left` that you _want_ the new `pygame.Rect` to have, you can do something like `space_ship_rect = spaceship_pic.get_rect(top=spaceship_top, left=spaceship_left)`. This is something that will probably save a considerable amount of time and boilerplate code if you go the `pygame.Sprite`-less route. – SimonT Nov 23 '13 at 22:32
  • @furas thanks for the reply, seems to be running with no errors but the space_ship image is not rotating instead just flickering whenever I move the cursor around the screen, heres the new updated code (not sure if I coded it the way you thought I should) (edited it) – user2993584 Nov 23 '13 at 22:44
  • @SimonT OP has to use `get_rect()` only once - after image load - and than OP should use only `space_ship_rect` to save time. But `get_rect()` with argument is nice thing - I like `object.get_rect(center=screen.center)` to center object on screen. – furas Nov 23 '13 at 22:48
  • @user2993584 you asked only how to get image position :) – furas Nov 23 '13 at 22:49
  • @user2993584 now you blit spaceship only when mouse is moving (inside `if event.type == MOUSEMOTION:`) but you have to blit spaceship also when mouse isn't moving - or something like this. – furas Nov 23 '13 at 22:52
  • @furas I managed to get it up and running, it's now working perfectly and no longer flickering, many thanks to you furas. ;) – user2993584 Nov 23 '13 at 23:01