Skip to main content
    for shootss in shootsright:
            shootss.x += shootss.xspeed
            shootss.y += shootss.yspeed
            if shootss.x > 500 or shootss.x < 0 or shootss.y > 500 or shootss.y < 0:
                shootsright.pop(shootsright.index(shootss))



        if len(shootsright) < 2:
            start_x = round(enemyshoots1.x+enemyshoots1.width-107
            start_y = round(enemyshoots1.y + enemyshoots1.height-50)
            target_x = playerman.x+playerman.width//2
            target_y = playerman.y+playerman.width//2
                    dir_x, dir_y = target_x - start_x, target_y - start_y
                    distance = math.sqrt(dir_x**2 + dir_y**2)
                    if distance > 0:
                        shootsright.append(Bools(start_x,start_y,(0,0,0),dir_x, dir_y))```

EDIT:

The last part of code makes the bullets fly to fast. A solution for this problem was found here

    for shootss in shootsright:
            shootss.x += shootss.xspeed
            shootss.y += shootss.yspeed
            if shootss.x > 500 or shootss.x < 0 or shootss.y > 500 or shootss.y < 0:
                shootsright.pop(shootsright.index(shootss))



        if len(shootsright) < 2:
            start_x = round(enemyshoots1.x+enemyshoots1.width-107
            start_y = round(enemyshoots1.y + enemyshoots1.height-50)
            target_x = playerman.x+playerman.width//2
            target_y = playerman.y+playerman.width//2
                    dir_x, dir_y = target_x - start_x, target_y - start_y
                    distance = math.sqrt(dir_x**2 + dir_y**2)
                    if distance > 0:
                        shootsright.append(Bools(start_x,start_y,(0,0,0),dir_x, dir_y))
    for shootss in shootsright:
            shootss.x += shootss.xspeed
            shootss.y += shootss.yspeed
            if shootss.x > 500 or shootss.x < 0 or shootss.y > 500 or shootss.y < 0:
                shootsright.pop(shootsright.index(shootss))



        if len(shootsright) < 2:
            start_x = round(enemyshoots1.x+enemyshoots1.width-107
            start_y = round(enemyshoots1.y + enemyshoots1.height-50)
            target_x = playerman.x+playerman.width//2
            target_y = playerman.y+playerman.width//2
                    dir_x, dir_y = target_x - start_x, target_y - start_y
                    distance = math.sqrt(dir_x**2 + dir_y**2)
                    if distance > 0:
                        shootsright.append(Bools(start_x,start_y,(0,0,0),dir_x, dir_y))```

EDIT:

The last part of code makes the bullets fly to fast. A solution for this problem was found here

added 1885 characters in body
Source Link
sirreldar
  • 254
  • 1
  • 5

EDIT:

Try something like this.

# enemys bullets
ksud = pygame.image.load("heart.png")
class Bools(object):
   def __init__(self, x, y,color, xspeed, yspeed):
       self.x = x
       self.y = y
       self.xspeed = xspeed
       self.yspeed = yspeed
       self.ksud = pygame.image.load("heart.png")
       self.hitbox  = self.ksud.get_rect()
       self.rect  = self.ksud.get_rect()
       self.rect.topleft = (self.x,self.y)
       self.speed = 10
       self.color = color
       self.hitbox = (self.x + 57, self.y + 33, 29, 52) # NEW
   def draw(self, window):
        self.rect.topleft = (self.x,self.y)
        player_rect = self.ksud.get_rect(center = self.rect.center) 
        player_rect.centerx += 0 # 10 is just an example
        player_rect.centery += 0 # 15 is just an example
        window.blit(self.ksud, player_rect)
        self.hitbox = (self.x + 97, self.y + 33, 10, 10) # NEW
        window.blit(self.ksud,self.rect)

and

    for shootss in shootsright:
            shootss.x += shootss.xspeed
            shootss.y += shootss.yspeed
            if shootss.x > 500 or shootss.x < 0 or shootss.y > 500 or shootss.y < 0:
                shootsright.pop(shootsright.index(shootss))



        if len(shootsright) < 2:
            start_x = round(enemyshoots1.x+enemyshoots1.width-107
            start_y = round(enemyshoots1.y + enemyshoots1.height-50)
            target_x = playerman.x+playerman.width//2
            target_y = playerman.y+playerman.width//2
                    dir_x, dir_y = target_x - start_x, target_y - start_y
                    distance = math.sqrt(dir_x**2 + dir_y**2)
                    if distance > 0:
                        shootsright.append(Bools(start_x,start_y,(0,0,0),dir_x, dir_y))

EDIT:

Try something like this.

# enemys bullets
ksud = pygame.image.load("heart.png")
class Bools(object):
   def __init__(self, x, y,color, xspeed, yspeed):
       self.x = x
       self.y = y
       self.xspeed = xspeed
       self.yspeed = yspeed
       self.ksud = pygame.image.load("heart.png")
       self.hitbox  = self.ksud.get_rect()
       self.rect  = self.ksud.get_rect()
       self.rect.topleft = (self.x,self.y)
       self.speed = 10
       self.color = color
       self.hitbox = (self.x + 57, self.y + 33, 29, 52) # NEW
   def draw(self, window):
        self.rect.topleft = (self.x,self.y)
        player_rect = self.ksud.get_rect(center = self.rect.center) 
        player_rect.centerx += 0 # 10 is just an example
        player_rect.centery += 0 # 15 is just an example
        window.blit(self.ksud, player_rect)
        self.hitbox = (self.x + 97, self.y + 33, 10, 10) # NEW
        window.blit(self.ksud,self.rect)

and

    for shootss in shootsright:
            shootss.x += shootss.xspeed
            shootss.y += shootss.yspeed
            if shootss.x > 500 or shootss.x < 0 or shootss.y > 500 or shootss.y < 0:
                shootsright.pop(shootsright.index(shootss))



        if len(shootsright) < 2:
            start_x = round(enemyshoots1.x+enemyshoots1.width-107
            start_y = round(enemyshoots1.y + enemyshoots1.height-50)
            target_x = playerman.x+playerman.width//2
            target_y = playerman.y+playerman.width//2
                    dir_x, dir_y = target_x - start_x, target_y - start_y
                    distance = math.sqrt(dir_x**2 + dir_y**2)
                    if distance > 0:
                        shootsright.append(Bools(start_x,start_y,(0,0,0),dir_x, dir_y))
Source Link
sirreldar
  • 254
  • 1
  • 5

Your problem is here:

            if enemyshoots1.x < playerman.x:
                shootss.x +=  5
            else:
                shootss.x -= 5

            if enemyshoots1.y < playerman.y:
                shootss.y += 5
            else:
                shootss.y -= 5

If you want the bullet to move straight towards the player, you need to employ some trigonometry. Unfortunately, I have never used PyGame, and its been quite some time since I used python, so the best I can give you is some C#-looking psuedocode. You will want something like this:

//define bullet speed
float bulletSpeed = 5.0f;

//the number of degrees or radians between your bullet spawn and player
//this is possible using the screen or world coordinates of your 
//sprites and some built in functions to calculate the angle
float angle = angle from player.x / player.y and bulletSpawn.x/bulletSpawn.y;

//again, use built in functions to calculate these values:
float xMagnitude = cosine of angle;
float yMagnitude = sine of angle;

//update the bullet
shootss.x += xMagnitude * bulletSpeed;
shootss.y += yMagnitude * bulletSpeed;

Of course, it would be much better to cache the x and y magnitudes as a member variable in the bullet instance so that you aren't calculating these values for every bullet every frame.