0

I was wondering if anyone could help me find a way to find the X and Y values of class PlayerBullet once it has been spawned. I have it so the bullet goes toward the cursor but so that could be a way to calculate where it is going to go but I am unsure how to do so.

import pygame
import sys
import math
pygame.init()

display = pygame.display.set_mode((1600, 1000))
clock = pygame.time.Clock()
pygame.display.set_caption("Among Us")

x = 50
y = 50
width = 40
height = 40
vel = 10




class PlayerBullet:
    def __init__(self, x, y, mouse_x, mouse_y):
        self.x = x
        self.y = y
        self.mouse_x = mouse_x
        self.mouse_y = mouse_y
        self.speed = 15
        self.angle = math.atan2(y-mouse_y, x-mouse_x)
        self.x_vel = math.cos(self.angle) * self.speed
        self.y_vel = math.sin(self.angle) * self.speed
    def main(self, display):
        self.x -= int(self.x_vel)
        self.y -= int(self.y_vel)

        pygame.draw.circle(display, (0,0,0), (self.x+16, self.y+16), 5)
    

refresh = 2
egg = 0
player_bullets = []

while True:

    display.fill((50,50,50))

    mouse_x, mouse_y = pygame.mouse.get_pos()

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            sys.exit()
            pygame.quit()
        if event.type == pygame.MOUSEBUTTONDOWN:
            if event.button == 1:
                player_bullets.append(PlayerBullet(playerX, playerY, mouse_x, mouse_y))

            
    playerX = x
    playerY = y

    for bullet in player_bullets:
        bullet.main(display)


    pygame.draw.rect(display, (255,0,0), (x, y, width, height))   
    clock.tick(60)
    pygame.display.update()
  • What do you mean by *"where it is going to go"*. The bullet moves on. Are you looking for [Collision detection](https://stackoverflow.com/questions/29640685/how-do-i-detect-collision-in-pygame/65064907#65064907)? Do you want to know when the bullet goes off screen? – Rabbid76 Dec 06 '21 at 21:06

1 Answers1

0

I feel like I must be missing something here as it seems too simple but isn’t this just the x and y attributes of the PlayerBullet objects? So for example if you wanted to move the first PlayerBullet that was spawned to the point (50, 50) it would be:

player_bullets[0].x = 50
player_bullets[0].y = 50
ljdyer
  • 1,946
  • 1
  • 3
  • 11