the code below is the bullet class for my shooter game in pygame. as you can see if you run the full game (https://github.com/hailfire006/economy_game/blob/master/shooter_game.py) the code works great to fire bullets at the cursor as long as the player isn't moving. However, I recently added scrolling, where I change a global offsetx and offsety every time the player gets close to an edge. These offsets are then used to draw each object in their respective draw functions.
unfortunately, my bullet physics in the bullet's init function no longer work as soon as the player scrolls and the offsets are added. Why are the offsets messing up my math and how can I change the code to get the bullets to fire in the right direction?
class Bullet: def __init__(self,mouse,player): self.exists = True centerx = (player.x + player.width/2) centery = (player.y + player.height/2) self.x = centerx self.y = centery self.launch_point = (self.x,self.y) self.width = 20 self.height = 20 self.name = "bullet" self.speed = 5 self.rect = None self.mouse = mouse self.dx,self.dy = self.mouse distance = [self.dx - self.x, self.dy - self.y] norm = math.sqrt(distance ** 2 + distance ** 2) direction = [distance / norm, distance / norm] self.bullet_vector = [direction * self.speed, direction * self.speed] def move(self): self.x += self.bullet_vector self.y += self.bullet_vector def draw(self): make_bullet_trail(self,self.launch_point) self.rect = pygame.Rect((self.x + offsetx,self.y + offsety),(self.width,self.height)) pygame.draw.rect(screen,(255,0,40),self.rect)