Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I have here my script that targets the player what ever position he is at but the projectiles aren't showing on my screen VIDEO. He isn't attacking at all, I don't know why. I am in my main loop I draw the bullets to.

my enemy bullet class

    # enemys bullets
    ksud = pygame.image.load("heart.png")
    class Boolss(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)
        

this goes on my main loop, it appends bullets and targets the player

            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(Boolss(start_x,start_y,(0,0,0),dir_x, dir_y))

I draw the bullets that are appending on my screen but they don't show

            for shootss in shootsright:
                shootss.draw(window)

my full code script

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
149 views
Welcome To Ask or Share your Answers For Others

1 Answer

I didn't run code but I think you have wrong indentations and this make problem.

You append to shootsright` inside

  for shootss in shootsright:

      shootsright.append(...)

but at start shootsright is empty so it never runs for shootss in shootsright: and it never runs shootsright.append() - so shootsright is always empty.

Probably you have to move it outside this loop (you have to change indentations)

  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))


  # outside `for`-loop

  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(Boolss(start_x,start_y,(0,0,0),dir_x, dir_y))

BTW: next time you can use print() in many places to see values in this list in different moments and to see which part of code is executed. It is called "print debuging".


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...