I want to make a shooter in pygame, currently the bullet only goes to the right but that's a placeholder, the real problem is that when i try to spawn my bullets inside my player they just spawn in the inital coordinates of the player, instead of the current ones
Sorry if the code is kinda hard to read, I'm not used to pygame
import pygame
import math
from pygame.locals import *
from sys import exit
pygame.init()
screen_width = 640
screen_height = 576
screen = pygame.display.set_mode((screen_width, screen_height))
pygame.display.set_caption('Hollow Shooter')
clock = pygame.time.Clock()
class Player(pygame.sprite.Sprite):
def __init__(self):
super().__init__()
self.image = pygame.image.load('player.png').convert_alpha()
self.rect = self.image.get_rect(center = (100, 100))
def playerInput(self):
if pygame.key.get_pressed()[pygame.K_w]:
self.rect.y -= 20
if pygame.key.get_pressed()[pygame.K_a]:
self.rect.x -= 20
if pygame.key.get_pressed()[pygame.K_s]:
self.rect.y += 20
if pygame.key.get_pressed()[pygame.K_d]:
self.rect.x += 20
def update(self):
self.playerInput()
def createBullet(self):
return Bullet(self.rect.x, self.rect.y)
player = Player()
playerGroup = pygame.sprite.GroupSingle()
playerGroup.add(Player())
class Bullet(pygame.sprite.Sprite):
def __init__(self, bulletx, bullety):
super().__init__()
self.image = pygame.Surface((10, 10))
self.image.fill((255, 0, 0))
self.rect = self.image.get_rect(center = (bulletx, bullety))
def update(self):
self.rect.x += 5
bulletGroup = pygame.sprite.Group()
while True:
clock.tick(30)
screen.fill((0,0,0))
playerGroup.update()
bulletGroup.update()
playerGroup.draw(screen)
bulletGroup.draw(screen)
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
exit()
if event.type == MOUSEBUTTONDOWN:
bulletGroup.add(player.createBullet())
pygame.display.update()