I have class People, which have a class Weapon in self.weapon. When i`m call the self.weapon in def attack(), which calling by required in class by pygame def update(), class Weapon calling the class Bullet and create a sprite of bullet.
But i have a problem. Bullets or didnt spawning, or spawning for random (if i delete the cycle "for event" and check for MOUSEBUTTONDOWN, then that going to shooting like a minigun, without simply clicking)
For call class Bullet under class Weapon i using that default code:
for event in pygame.event.get(): #checking some event by keyboard\mouse
if event.type == pygame.MOUSEBUTTONDOWN: #checks the mouse clicking
mousestat = pygame.mouse.get_pressed()
if mousestat[0]: #if clicked mouse button is LMB
modules.mymodules.bulletSpawn([*args]) #then i calling from outside function for summon the my Bullet class and create object of Bullet. (if i summon this class directly then nothing will not change)
My code details, if it required:
class Weapon():
def __init__(self, shooter, selfenemygroup):
self.damage = 40
self.range = 600
self.shooter = shooter
self.targets = selfenemygroup
def attack(self, mouse, entitygroup):
for event in pygame.event.get():
if event.type == pygame.MOUSEBUTTONDOWN:
mousestat = pygame.mouse.get_pressed()
if mousestat[0]:
modules.mymodules.bulletSpawn(mouse=mouse, shooter=self.shooter, damage=self.damage, range=self.range, entitygroup=entitygroup, selfenemygroup=self.targets)
class Player(Entity):
_instances = set()
def __init__(self, color, resolution, keys, name='Player', entitygroup=None, selfentitygroup=None, selfenemygroup=None, textentitygroup=None):
Player._instances.add(self)
Entity._instances.add(self)
super().__init__()
self.resolution = resolution
self.image = pygame.Surface((40, 40))
self.image.fill(color)
self.rect = self.image.get_rect()
self.rect.center = (self.resolution[0] / 2 + random.randint(1, 256), self.resolution[1] / 2 + random.randint(1, 256))
if entitygroup != None:
self.add(entitygroup)
if selfentitygroup != None:
self.add(selfentitygroup)
self.weapon = Weapon(self, selfenemygroup)
self.name = modules.mysprites.Text(name, 24, entity=self, textentitygroup=textentitygroup)
self.health = 100
self.maxhealth = 100
def attack(self, mouse, entitygroup):
self.weapon.attack(mouse, entitygroup)
def update(self, screen, mouse, entitygroup, dtime, TARGET_FPS):
self.attack(mouse, entitygroup)
I believe u understand correctly my post. Srry for my not ideal English, and thank you if you pay attention =)
- I tried to delete the cycle
for event in pygame.event.get():
and checking for type of clicking
if event.type == pygame.MOUSEBUTTONDOWN:
--> if i tried to click by mouse, the shooting became the shooting like from the minigun 2) I tried delete all checks:
mousestat = pygame.mouse.get_pressed()
if mousestat[0]:
--> same result 3) I didnt tried to relocate the shooting function in the main loop, because it doesn`t fit my idea with weapon classes. And it won't be grammatical --> if i relocate this function all working "good", but this code will not be able to improve in future