Reputation: 47
so I have a sprite that shoots projectiles the projectiles shoot at the player I was wondering how I could make the sprite rotate to the player? VIDEO the player bullets attack the player what ever position he is at but how could I make the cannon sprite do the same?
my cannon class
shotsright = pygame.image.load("canss.png")
class enemyshoot:
def __init__(self,x,y,height,width,color):
self.x = x
self.y =y
self.height = height
self.width = width
self.color = color
self.shootsright = pygame.image.load("canss.png")
self.shootsright = pygame.transform.scale(self.shootsright,(self.shootsright.get_width()-150,self.shootsright.get_height()-150))
self.rect = pygame.Rect(x,y,height,width)
self.health = 10
self.hitbox = (self.x + -20, self.y + 30, 31, 57)
def draw(self):
self.rect.topleft = (self.x,self.y)
window.blit(self.shootsright,self.rect)
self.hits = (self.x + 20, self.y, 28,60)
pygame.draw.rect(window, (255,0,0), (self.hitbox[0], self.hitbox[1] - 60, 50, 10)) # NEW
pygame.draw.rect(window, (0,255,0), (self.hitbox[0], self.hitbox[1] - 60, 50 - (5 * (10 - self.health)), 10))
self.hitbox = (self.x + 100, self.y + 200, 81, 87)
black = (0,0,0)
enemyshoots1 = enemyshoot(1100,10,100,100,black)
enemyshooting = [enemyshoots1]
my full code: script
Upvotes: 1
Views: 138
Reputation: 14906
Basically you can rotate the image to point to some co-ordinate quite simply. You create a vector of the distance between your cannon and the player. This is then converted to an angle with the Vector.as_polar()
function. The angle is used to rotate an original copy of the bitmap to the desired angle. Image rotation can be fairly CPU-consuming.
class enemyshoot:
def __init__(self, x, y, height, width, color):
[...]
# Make a Reference Copy of the bitmap for later rotation
self.original = pygame.image.load("canss.png")
self.image = self.original
self.rect = self.image.get_rect()
self.position = pygame.math.Vector2( ( x, y ) )
def lookAt( self, coordinate ):
# Rotate image to point in the new direction
delta_vector = coordinate - self.position
radius, angle = delta_vector.as_polar()
self.image = pygame.transform.rotozoom( self.original, -angle, 1 )
# Re-set the bounding rectangle and position since
# the dimensions and centroid will have (probably) changed.
current_pos = self.rect.center
self.rect = self.image.get_rect()
self.rect.center = current_pos
So the idea is you take the original bitmap, and rotate from that each time. If you keep rotating the same bitmap the slight differences will compound and the image will loose definition.
The other thing of note is that we're rotating around the bitmap's centre-point. But the rotation changes the bitmap's dimensions, also changing the centre-point. So this needs to be re-calculated and preserved.
You may find it useful to cache the rotated images once they have been rotated to save CPU. Maybe round the angle to the nearest 10 degrees, and then see if it's already been rotated. This would also allow you to pre-rotate all images and store them in a look-up table.
Edit: Calling lookAt()
the mouse position:
Upvotes: 3