0

so I made this short script when my enemy projectiles collid with the player rect it should remove 1 out 80 of my health and pop the bullet but for some reason VIDEO its colliding way to much and removing my player health at rapid speed if I keep moving forward for some reason I set the speed of the projectiles appending at 5 but I dont know why it keeps doing iot


            for shootss in shootsright:
                if shootss.rect.colliderect(playerman.rect):
                    if playerman.health > -5:
                        playerman.health -= 1
                    shootsright.pop(shootsright.index(shootss))

I dont know if it has anything to do with my projectiles appending but here

            if len(shootsright) < 1:
                BULLET_SPEED = 5
                start_x = round(enemyshoots1.x+enemyshoots1.width+30)
                start_y = round(enemyshoots1.y + enemyshoots1.height+120)
                target_x = playerman.x+playerman.width//2
                target_y = playerman.y+playerman.width//2
                delta_x, delta_y = target_x - start_x, target_y - start_y
                distance = math.sqrt(delta_x ** 2 + delta_y ** 2)
                dir_x = BULLET_SPEED * delta_x / distance
                dir_y = BULLET_SPEED * delta_y / distance
                distance = math.sqrt(dir_x**2 + dir_y**2)
                if distance > 0:
                    shootsright.append(Boolss(start_x,start_y,(0,0,0),dir_x, dir_y))
Habib Ismail
  • 69
  • 5
  • 16
  • 1
    Removing from a list white iterating over it is *tricky*. Consider iterating from the far-end of the list, then when you remove an item it doesn't change all the order and indexing of the remaining elements. As you test for collisions, print out some debug with the bullet index, and whether it hits or not. – Kingsley Jun 18 '20 at 00:53
  • I printed hit and it shows that its hitting it but I dont know why its hitting it so fast when I set my bullet speed to 5 basically its printing("hit") alot [video](https://gyazo.com/1b77617d4072f8ffcfc75892c9a2f6f4) – Habib Ismail Jun 18 '20 at 00:55
  • 1
    Print more than just "hit" ... print the index of the bullet, maybe it's co-ordinates. I think you'll find it's being hit by a bullet you didn't iterate over properly, so it's never removed. Better fix that iteration too, it's not working the way you think. What is the next element processed when you remove element[0]?? (it's #2 (not #1, that's skipped because it shuffles down to position #0)) – Kingsley Jun 18 '20 at 01:16
  • 2
    use `print()` to display values in different places and messages which show which part of code is executed. You can also create `clock = pygame.time.Clock()` and use ie. `clock.tick(5)` after `pygame.display.update()` to slow down all code to 5FPS (Frames Per Second) - and this way you can easier observe objects on screen and message printed in console. It helps to compare displayed values with situation on screen. – furas Jun 18 '20 at 01:44
  • 1
    BTW: instead of `enemyshoots1.x + enemyshoots1.width` you can use `enemyshoots1.right`. And instead of `enemyshoots1.y + enemyshoots1.height` you can use `enemyshoots1.bottom` – furas Jun 18 '20 at 01:46
  • 1
    BTW: Instead of `playerman.x+playerman.width//2` you can use `playerman.centerx` and instead of `playerman.y+playerman.width//2` you can use `playerman.centery`. Or you can also use `target_x, target_y = playerman.center` – furas Jun 18 '20 at 01:48
  • 1
    BTW: you can do `distance = pygame.math.Vector2(start_x, star_y).distance_to(playerman.center)` – furas Jun 18 '20 at 01:51
  • 1
    I think I found the problem its because the projectile rect its huge I used `pygame.draw.rect(window, (0,0,0), self.rect)` to see the rect size its big how could I shrink it down my [Projectile class](https://pastebin.com/2TYyMFi3) [VIDEO](https://gyazo.com/a06395f0d75532d6b4369146812e4bfe) – Habib Ismail Jun 18 '20 at 01:51
  • 1
    you should set it when you create `projectile` - if you use image then use common `self.rect = self.image.get_rect()`. If you don't use image then `self.rect.width = ... `, `self.rect.height = ... ` – furas Jun 18 '20 at 01:53

0 Answers0