TransWikia.com

My Projectiles Stop Shooting Problem How To Fix?

Stack Overflow Asked by Habib Ismail on November 19, 2020

enter image description hereVIDEO When my player distance is a little farther than the knife they stop shooting at the player I am not sure why? How could I fix it and make sure it shoots where ever the player distance is. Like I don’t want it to stop shooting when the player is a little farther away from the projectile I am not sure if I have a range for it to shoot the player or maybe when my player scrolls
when it stops shooting at the player the shooting sound still plays but my projectiles aren’t shooting and that causes my sound to keep playing rapidly.

This is how my enemies shoot:


        for shootss in shootsright:
            shootss.x += shootss.xspeed
            shootss.y += shootss.yspeed

            if shootss.x > 700 or shootss.x < 0 or shootss.y > 500 or shootss.y < 0: 
                shootsright.pop(shootsright.index(shootss))
                shootss.lookAt((playerman.x,playerman.y))                


        if box1.health > 25:    
            if len(shootsright) < 1:
                for enemyshoot in enemyshooting:
                    BULLET_SPEED = 10
                    start_x = round(enemyshoot.x+enemyshoot.width+-35)
                    start_y = round(enemyshoot.y + enemyshoot.height+-25)
                    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)
                    knifesound.play()
                    if distance > 0:
                        shootsright.append(enemyboolss(start_x,start_y,(0,0,0),dir_x, dir_y))



This is the class for the projectile:



    class projectile(object):
       def __init__(self, x, y, dirx, diry, color):
           self.x = x
           self.y = y
           self.dirx = dirx
           self.diry = diry
           self.isJump = False
           self.slash = pygame.image.load("round.png")
           self.slash = pygame.transform.scale(self.slash,(self.slash.get_width()//6,self.slash.get_height()//6))
           self.rect  = self.slash.get_rect()
           self.rect.topleft = ( self.x, self.y )
           self.speed = 18
           self.color = color
           self.hitbox = (self.x + -18, self.y, 46,60)
       def move(self):
           self.x += self.dirx * self.speed
           self.y += self.diry * self.speed
       def draw(self, window):
           self.rect.topleft = (round(self.x), round(self.y))

           window.blit(self.slash, self.rect)
           self.hitbox = (self.x + -18, self.y, 30,30)


my full code: script

One Answer

Your screen is 800x800, but the knife code is checking 700x500. The knife projectile is created but immediately removed from the knife list at the next loop:

if shootss.x > 700 or shootss.x < 0 or shootss.y > 500 or shootss.y < 0: 
    shootsright.pop(shootsright.index(shootss))
    shootss.lookAt((playerman.x,playerman.y)) 

This causes the knife flash but no movement for knives past 700. After setting the check to 800, the knives worked correctly including sound.

Correct answer by Mike67 on November 19, 2020

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP