0

I'm dipping my toes into neuralnetworks/neuroevolution, but I keep running into this issue in pygame where the pipes in flappy bird are spawning twice in rapid succesion at the start of a new generation. How can I stop this?

Relevant code:

SPAWNPIPE = pg.USEREVENT
pg.time.set_timer(SPAWNPIPE, 1000)

while True:
clock.tick(60)

for event in pg.event.get():
    if event.type == pg.QUIT:
        pg.quit()

    if event.type == SPAWNPIPE and canspawn:
        pipelist.append(Pipe())
        
        
if game_active:
    #draw bg/fground        
    
    screen.blit(bgsurface, (0,0))
    ground_x -= 1
    if ground_x <= -512:
        ground_x = -256
    screen.blit(ground, (ground_x, height/1.4))
    
    #draw pipes
    
    for pipe in pipelist:
        pipe.move()
        pipe.draw()
        if pipe.delete():
            pipelist.remove(pipe)
    #birds stuff
    fitness += 1
    displayscore(generation, fitness)
            
    if birds.update(pipelist) == 0:
        fitness = 0
        pipelist.clear()
        canspawn = False
        generation += 1
        
        birds.newgen(pop, mrate)
        canspawn = True
pg.display.update()
Josh Lee
  • 11
  • 1
  • 2
  • The code is `remove()`ing from `pipelist` as it iterates over the list. I can't see how that would make pipes spawn sooner, but it's not a good practice. – Kingsley Jul 23 '20 at 06:06
  • sorry if this is dumb but why isn't it good practice? – Josh Lee Jul 27 '20 at 04:17
  • When you remove an item from the list being iterated over, it can skip the "next" object (depends on how you do it). It's especially an issue with numerically indexed lists. Generally if you *need* to do it this way, you can start from the last element in the list working backwards, so anything removed is loop-safe. – Kingsley Jul 27 '20 at 05:22
  • I see, Thank you. – Josh Lee Jul 27 '20 at 17:33

0 Answers0