Newbie here, trying to figure out pygame (and the complexities of Python too, I guess) from a background of college-level C++. I've gotten a basic platforming mechanic down, mostly by looking into example programs - I'm currently making a little rectangle jump a round. I'm currently trying to add a second rectangle, attached to the first, whose end will follow the mouse. However, my current methods aren't working, and - bizarrely enough - the sprite resizes within the "base" rather than spin.
Here's my __init__
method. Apologies for the sloppy formatting, it just doesn't
class Player(_Physics):
def __init__(self,location,speed):
_Physics.__init__(self)
self.image = pg.image.load('playertst.png')
self.rect = self.image.get_rect(topleft=location)
self.speed = speed
self.jump_power = -9.0
self.jump_cut_magnitude = -3.0
self.on_moving = False
self.collide_below = False
self.original_hand = pg.image.load('playertst2.png')
self.fake_hand = self.original_hand.copy()
self.hand = self.original_hand.copy()
self.hand_rect = self.hand.get_rect(topleft=location)
self.angle = self.get_angle(pg.mouse.get_pos())
The get_angle
method is apparently what's going wrong, so here's that:
def get_angle(self,mouse):
offset = (mouse[1]-self.hand_rect.centery, mouse[0]-self.hand_rect.centerx)
self.angle = 135-math.degrees(math.atan2(*offset))
self.hand = pg.transform.rotate(self.original_hand, self.angle)
self.hand_rect = self.hand.get_rect(topleft=self.rect.topleft)
The two are blitted on normally, and only the self.rect
interacts with the platforms - and it's working perfectly fine. It's just the "hand" that reshapes itself rather than spin. Here's an example. It can also compress itself to a small rectangle at the top of the base rectangle. I don't have the foggiest idea what's going on; does anyone else have any ideas?