Pygame- make bullet shoot toward cursor direction

 ̄綄美尐妖づ 提交于 2020-01-30 05:45:05

问题


I'm trying to make the bullets in my game shoot toward the direction of my cursor until it hits the window borders. My bullets currently go toward my cursor but stops once it reaches where the cursor was.

I noticed that the bullet could reach the border if my cursor were offscreen and so I tried to modify the cursor position used to calculate the movement of my bullet to always be outside of the window via multiplying and adding but I couldn't quite get it working the way I want it to.

win = pygame.display.set_mode((1000, 800))
pos = pygame.mouse.get_pos()
keys = pygame.key.get_pressed()

def shoot(bullets):
    for bullet in bullets:
        if bullet.x > 0 and bullet.x <1000 and bullet.y > 0 and bullet.y < 800:
            pygame.draw.circle(win, (255, 255, 255), (round(bullet.x) ,round(bullet.y)),5)
            diffX = bullet.targetX - bullet.x
            diffY = bullet.targetY - bullet.y
            ang = math.atan2(diffY,diffX)
            bullet.x += math.cos(ang)*bullet.vel
            bullet.y += math.sin(ang)*bullet.vel

if keys[pygame.K_SPACE] and RegBullet.canShoot:
        RegBullet.canShoot = False
        regBullets.append(RegBullet(win,x=p1.getX(),y=p1.getY(),targetX=pos[0],targetY=pos[1]))
        pause = threading.Thread(target=cool_down,args=(1,))
        pause.start()

回答1:


The issue is that you calculate the the direction of the bullet by vector from the current position of the bullet to the target point (bullet.targetX, bullet.targetY).
Once the bullet reaches the target, then this direction is (0, 0) and the bullet doesn't move any more.
Don't store the target position in bullet. Store the initial direction vector instead. e.g.:

bullet.diffX = targetX - bullet.x
bullet.diffY = targetY - bullet.y
ang = math.atan2(bullet.diffY, bullet.diffX)
bullet.x += math.cos(ang)*bullet.vel
bullet.y += math.sin(ang)*bullet.vel

Use pygame.Rect and .collidepoint() to verify if the bullet is inside the window:

for bullet in bullets:
   if pygame.Rect(0, 0, 1000, 800).collidepoint(bullet.x, bullet.y):
       # [...]

Or even .colliderect:

for bullet in bullets:
   radius = 5
   bullet_rect = pygame.Rect(-radius, -radius, radius, radius);
   bullet_rect.center = (bullet.x, bullet.y)
   if pygame.Rect(0, 0, 1000, 800).colliderect(bullet_rect):
       # [...]

I recommend to use pygame.math.Vector2 for calculation of the movement of the bullet e.g.:

bullet.pos = pg.math.Vector2(bullet.x, bullet.y)
bullet.dir = pg.math.Vector2(targetX, targetY) - bullet.pos
bullet.dir = bullet.dir.normalize()
for bullet in bullets:

    if #[...]

        bullet.pos += bullet.dir * bullet.vel
        bullet.x, bullet.y = (round(bullet.pos.x), round(bullet.pos.y))


来源:https://stackoverflow.com/questions/57283687/pygame-make-bullet-shoot-toward-cursor-direction

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!