0

I'm working on a small shmup in pygame and when enemies are defeated they have a small chance to spawn a "buff" object that is created in the dead enemy's location and then slowly floats off in a random direction. The problem I'm having is that when the object's coordinates on either the x or y axis = 0, the object will stop moving on that plane. In other words, it rides the left and top edges of the screen. However, if the x or y coordinate skips 0 and is -1 for example, it continues on the proper trajectory.

Here's the class in question:

class Buff(pygame.sprite.Sprite):
    def __init__(self,name,origin): ###origin is simply the dead enemy's rect coords
        pygame.sprite.Sprite.__init__(self)
        self.name = name
        if name == "shield":
            self.images = [pygame.image.load("p_1.png").convert_alpha(),pygame.image.load("p_2.png").convert_alpha()]
        elif name == "health":
            self.images = [pygame.image.load("m_1.png").convert_alpha(),pygame.image.load("m_2.png").convert_alpha()]
            self.sound = pygame.mixer.Sound("healed.wav")
    self.image = self.images[0]
    self.rect = self.image.get_rect()
    self.rect.x = origin[0]
    self.rect.y = origin[1]
    self.index = 0
    self.anim = 0
    self.angle = random.randrange(0,360) #### direction is set to a random degree
    self.rads = math.radians(self.angle) #### converted to radians for funtionality
    def Barrier(self,): #### Cleanup
        if self.rect.x > DISPLAY[0]: #### DISPLAY is a tuple holding the screen size value (x,y)
            return True
        if self.rect.x < 0-self.rect[2]:
            return True
        if self.rect.y > DISPLAY[1]:
            return True
        if self.rect.y < 0-self.rect[3]:
            return True
        return False
    def update(self,): ####This update function is run every gameloop iteration
        if self.Barrier():
            buffs.remove(self)
        self.anim+=1
        if self.anim > 30:
            self.anim = 0
            self.index+=1
            if self.index > 1:
                self.index = 0
            self.image = self.images[self.index]
###### BELOW IS RELEVANT MOVEMENT CODE #######
        self.rect.x+=math.cos(self.rads)*1.2 
        self.rect.y+=math.sin(self.rads)*1.2

There's no outside code manipulating the object's coordinates, any alterations are handled by the included update function. I'm just not understanding why a specific coordinate (0 in this case) can negate addition.

oxrock
  • 643
  • 5
  • 12
  • 1
    In my opinion the problem is that `x` and `y` are integers. If the `cos()` or `sin()` of an angle is a negative decimal number the sum for instance of `self.rect.x + math.cos(self.rads) * 1.2` will **always** be only `self.rect.x`, because `int(a negativ number between 0 and 1)` (i.e. `int(math.cos(self.rads) * 1.2`) equals `0`. – elegent Mar 19 '15 at 12:55
  • You might try storing a pair of float x,y coordinates, and convert them to int each time through the update loop. – aghast Feb 27 '16 at 08:11
  • And yes, they're integers: [pygame.Rect](http://www.pygame.org/docs/ref/rect.html) – aghast Feb 27 '16 at 08:14

0 Answers0