I'm currently making a plane shooting game in Pygame. I have created plane class and now started to create the bullet class. However, i'm experiencing an issue that i couldn't solve : i really need to use pygame.event.get() loops both inside the class and in the while loop (the main while loop of the game) . So when i used 2 loops like that, the game controls' became so weird and messy and after i searched for this problem, i found that normally it must be only one loop on pygame.event.get(). I don't know what to do because if i combine the two loops into the main one situated in the while loop, then i wouldn't be able to refer to the bullet object because it will be outside of the class. This is my code :
class Singlebullet(object):
def __init__(self):
self.img = pygame.image.load(r'C:\Users\me\Documents\MyGame\bullet.png')
self.x = planeX + 71
self.y = planeY
self.state = 'ready'
self.speed = 0
self.firerate = 1000
def shoot(self):
if current_bullet_type == 'single':
if self.state == 'ready':
MyScrollingScreen.blit(self.img, (self.x, self.y))
if self.state == 'fire':
self.speed = -8
self.y += bullet_speed
MyScrollingScreen.blit(self.img, (self.x, self.y))
if self.y < -20:
self.state = 'ready'
for e in pygame.event.get():
if e.type == pygame.KEYDOWN or e.type == pygame.KEYUP:
if e.key == pygame.K_SPACE:
self.state = 'fire'
pygame.time.set_timer(pygame.USEREVENT+1, self.firerate)
if e.type == pygame.USEREVENT+1:
self.state = 'ready'
s1 = Singlebullet()
while running:
#[...]
for event in pygame.event.get():
#[...]