问题
I have an enemy that shoots projectiles but only shoots to the right I want it to shoot at the player with any position I don't know how to do that heres a vid what I have done so fare if you could walk me throw the steps on how to do it that would be great Thank You
this is what I have done so fare it only shoots to the right I want it to shoot at the player within any position the player is at
for shootss in shootsright:
if shootss.x < 500 and shootss.x > 0:
shootss.x += 7
else:
shootsright.pop(shootsright.index(shootss))
if len(shootsright) < 1:
shootsright.append(Bools(round(enemyshoots1.x+enemyshoots1.width-107),round(enemyshoots1.y + enemyshoots1.height-50),(0,0,0)))
and here is my bullet class
# enemys bullets
ksud = pygame.image.load("heart.png")
class Bools(object):
def __init__(self, x, y,color):
self.x = x
self.y = y
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)
my enemys class
shotsright = pygame.image.load("shooting2.png")
shotsleft = pygame.image.load("shooting1.png")
class enemyshoot:
def __init__(self,x,y,height,width,color):
self.x = x
self.y =y
self.height = height
self.width = width
self.color = color
self.shootsright = pygame.image.load("shooting2.png")
self.shotsleft = pygame.image.load("shooting1.png")
self.shootsright = pygame.transform.scale(self.shootsright,(self.shootsright.get_width()//3,self.shootsright.get_height()//3))
self.shotsleft = pygame.transform.scale(self.shotsleft,(self.shotsleft.get_width()//3,self.shotsleft.get_height()//3))
self.rect = pygame.Rect(x,y,height,width)
self.health = 10
self.hitbox = (self.x + -20, self.y + 30, 31, 57)
def draw(self):
self.rect.topleft = (self.x,self.y)
window.blit(self.shootsright,self.rect)
self.hits = (self.x + 20, self.y, 28,60)
pygame.draw.rect(window, (255,0,0), (self.hitbox[0], self.hitbox[1] - 60, 50, 10)) # NEW
pygame.draw.rect(window, (0,255,0), (self.hitbox[0], self.hitbox[1] - 60, 50 - (5 * (10 - self.health)), 10))
self.hitbox = (self.x + 60, self.y + 80, 81, 87)
black = (0,0,0)
enemyshoots1 = enemyshoot(1100,240,100,100,black)
enemyshooting = [enemyshoots1]
here is my full code
script
回答1:
Looking at your script didn't help me a lot as you are maintaining many different classes.
for shootss in shootsright:
if shootss.x < 500 and shootss.x > 0:
shootss.x += 7
Here it appears that when you are updating your bullets x coordinate you are incrementing it with a positive constant value.
And then you update it like this in your bullet class.
window.blit(self.ksud,self.rect)
What you can do instead is have a bullet speed that changes its direction.You can use a code like this
if object_you_want_to_follow.x > object_shooting_the_bullet.x:
bulletSpeed = +7
elif object_you_want_to_follow.x < object_shooting_the_bullet.x:
bulletSpeed = -7
Same applies if you want to follow it vertically along the y direction.
OPTIMIZATION TIP
From what i understand having this in your bullet class makes no difference as you are updating it soon after.
window.blit(self.ksud, player_rect)
EDIT
To make things easier you can simply do this
for shootss in shootsright:
if shootss.x < 500 and shootss.x > 0:
if enemy.x < playerman.x:
shootss.x += 7
else:
shootss.x -= 7
else:
shootsright.pop(shootsright.index(shootss))
if len(shootsright) < 1:
shootsright.append(Bools(round(enemyshoots1.x+enemyshoots1.width-107),round(enemyshoots1.y + enemyshoots1.height-50),(0,0,0)))
来源:https://stackoverflow.com/questions/62274195/how-can-i-make-my-enemys-projectile-attack-the-player-where-ever-the-player-move