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