I want to make a player that can shoot bullets. To do this i tried defining a shoot function that gets called when space bar is pressed. The function looks like this (p is the player object btw):
class bullet:
def __init__(self):
self.x = None
self.y = None
self.radius = 10
self.shooting = False
self.speed = 5
def shoot(self):
self.shooting = True
if self.shooting == True:
self.x = (p.x + 60)
self.y = (p.y + 25)
self.x += self.speed
self.y += self.speed
pygame.draw.circle(d, (0, 0, 0), (self.x, self.y), self.radius)
shoot()
I was hoping that the function would keep calling itself and the bullet would keep moving forward. However what actually happens is when i press space, it gives me an error
shoot()
NameError: name 'shoot' is not defined
How i called the function:
while True:
d.fill((98, 98, 98))
p.draw()
for event in pygame.event.get():
pass
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_SPACE:
b.shoot()
Something i observed is that if i remove the recursion shoot() at the end of the shoot() function, it runs but the bullet stays in the same place as long as the space is being held down(as expected as recursion was removed). How can i fix this problem? Thanks
update()function. pygame.org/docs/ref/sprite.htmlthisisn’t the implicit scope for instance methods.