HTML5 - JS: Shoot Bullet From Facing Direction
By : Richard D.
Date : March 29 2020, 07:55 AM
I wish this help you Actually, I would recommend not using trig functions, simply for efficiency. Math.atan2, Math.cos, and Math.sin can get expensive. You already have (slightly renamed) code :
var deltaX = input.mouseX - (this.localX + this.width/2);
var deltaY = input.mouseY - (this.localY + this.height/2);
var magnitude = Math.sqrt(deltaX * deltaX + deltaY * deltaY);
var velocityScale = bulletSpeed / magnitude;
velocityInstance.x = deltaX * velocityScale;
velocityInstance.y = deltaY * velocityScale;
|
pygame: I am trying to shoot a bullet from my Tank but I can't find the start point of the bullet
By : kimig82
Date : March 29 2020, 07:55 AM
Any of those help part of the Tanks class: , You need something like this code :
a = RealTank.Trect.centerx + math.cos(math.radians(RealTank.turret_angle+90))*55
b = RealTank.Trect.centery + math.sin(math.radians(RealTank.turret_angle-90))*55
pygame.draw.circle(screen,red,[int(a), int(b)],5,0)
|
How to shoot a bullet at an angle in pygame?
By : user1745632
Date : March 29 2020, 07:55 AM
this one helps. You should normalize the distance to the mouse if you want the speed not to depend on the mouse distance. In other words, regardless of where the mouse is, use the coordinates of the point which is in that same direction, but at a fixed distance from the starting position, e.g. when you calculate your b_m_xand b_m_y, do this: code :
b_m_x, b_m_y = m_x - self.pos.x, m_y - self.pos.y
distance = (b_m_x ** 2 + b_m_y ** 2) ** .5
if distance != 0:
# if distance is 0, nothing can be done
NORMALIZED_DISTANCE = 100
mutiplier = NORMALIZED_DISTANCE / distance
b_m_x *= multipler
b_m_y *= multipler
|
How to shoot bullets from a character facing in direction of cursor in pygame?
By : Div1000
Date : March 29 2020, 07:55 AM
this will help Pass the mouse position to rotator.shoot(), when the mouse button is pressed: code :
if event.type == pg.MOUSEBUTTONDOWN:
rotator.shoot(event.pos)
def shoot(self, mousepos):
dx = mousepos[0] - self.rect.centerx
dy = mousepos[1] - self.rect.centery
if abs(dx) > 0 or abs(dy) > 0:
bullet = Bullet(self.rect.centerx, self.rect.centery, dx, dy)
all_sprites.add(bullet)
bullets.add(bullet)
class Bullet(pg.sprite.Sprite):
def __init__(self, x, y, dx, dy):
pg.sprite.Sprite.__init__(self)
self.image = pg.transform.smoothscale(pg.image.load('bullet.png').convert_alpha(), (10,10))
self.rect = self.image.get_rect()
self.rect.center = (x, y)
self.speed = 8
self.pos = pg.math.Vector2(x, y)
self.dir = pg.math.Vector2(dx, dy).normalize()
class Bullet(pg.sprite.Sprite):
# [...]
def update(self):
self.pos += self.dir * self.speed
self.rect.center = (round(self.pos.x), round(self.pos.y))
if not self.rect.colliderect(0, 0, width, height):
self.kill()
|
Python Pygame press two direction key and another key to shoot there's no bullet
By : user3428213
Date : March 29 2020, 07:55 AM
I hope this helps you . If you press UP + LEFT + SPACE then the SPACE key doesn't appear to be pressed immediately. You've to release the UP or LEFT key to get the pygame.KEYDOWN event for SPACE. This is a known bug in pygame and doesn't seem to be solved yet: Incorrect handling of pressed keys #235
|