def game(surface): # Main game part # Initialization pygame.init() # Creating objects clock = pygame.time.Clock() pygame.display.set_caption('Flappy Bird') # Setting score as global variable global score # Declaring pipe objects firstPipe = Pipe(surface, surfaceWidth + 30) secondPipe = Pipe(surface, surfaceWidth + 180) # Grouping pipes pipeGroup = pygame.sprite.Group() pipeGroup.add(firstPipe.upperBlock) pipeGroup.add(secondPipe.upperBlock) pipeGroup.add(firstPipe.lowerBlock) pipeGroup.add(secondPipe.lowerBlock) # setting some variabe to make the game mechanism work isAlive = 10 moved = False pause = 0 genrationCount = 1 pipe1Pos = [0, 0, 0] pipe2Pos = [0, 0, 0] global framesPerSecond # Whole game mechanism while True: # draw over the background surface.blit(background, (0, 0)) # Check for event in pygame.event.get(): # if close button is pressed if event.type == QUIT: pygame.quit() sys.exit() if event.type == KEYDOWN and ( event.key == K_i): # if '+' is pressed, if yes increase speed framesPerSecond *= 2 if event.type == KEYDOWN and (event.key == K_d): framesPerSecond /= 2 ''' if event.type == KEYDOWN and (event.key == K_SPACE or event.key == K_w or event.key == K_UP): flappyBird.move("FLAP") moved = True if moved == False: flappyBird.move(None) else: moved = False ''' ''' Uncomment this for debug output birdCount = 1 print("=====================================================") ''' # Check if birds and pipes are colliding # This part is unoptimized right now. This could be improved more. for bird in birds.birds: # Loop through all birds colide = (pygame.sprite.spritecollideany(bird, pipeGroup) ) # Check if collide ''' Uncomment this for debug output print("Generation: ", genrationCount) print("Nodes:", bird.network.nodes) print("Edges:", bird.network.edges) print("Bird No.:", birdCount) print() birdCount += 1 if birdCount == 10: birdCount = 1 ''' if colide != None or (bird.y == 509 - bird.height) or ( bird.y == 0 ): # if collide or touch the top or bottom of the screen bird.isAlive = False # set isAlive attribute to False isAlive -= 1 # decrement isAlive count score = 0 # set score back to 0. bird.distance = 0 # set distance covered by bird to zero if isAlive == 0: # if no bird is alive # surface.blit(background,(0,0)) # time.sleep(5) # game(surface) isAlive = 10 # reset counter firstPipe.setPos(surfaceWidth + 30) secondPipe.setPos(surfaceWidth + 180) _, genrationCount = birds.nextGen() # make new genration birdCord = [bird.x, bird.y] # getting coordinates of bird closestPipe = pipe1Pos if (pipe1Pos[0] < pipe2Pos[0] ) else pipe2Pos # getting closest pipe # (closest because 2 pipe are draw at the same time on screen) gapCord = [ closestPipe[0], ((closestPipe[1] + closestPipe[2]) / 2) ] # getting mid point of the gap bird.fitness = calcFitness(score, bird.distance, birdCord, gapCord) # calculating fitness bird.move( gapCord[0] - birdCord[0], gapCord[1] - birdCord[1] ) # move the bird. NOTE: Here the two parameter are send as input of neural network birds.sortBird( ) # Sort bird. This is done to just take the fittest one and based on that increment the score pipe1Pos = firstPipe.move() # Move the first pipe if pipe1Pos[0] <= int(surfaceWidth * 0.2) - int( birds.birds[0].rect.width / 2): # Compairing if the bird crossed the pipe if firstPipe.behindBird == 0: firstPipe.behindBird = 1 score += 1 # incrementing score print("Current Score:", score) # print score to console # Doin g the exact same thing with 2nd pipe pipe2Pos = secondPipe.move() if pipe2Pos[0] <= int(surfaceWidth * 0.2) - int( birds.birds[0].rect.width / 2): if secondPipe.behindBird == 0: secondPipe.behindBird = 1 score += 1 print("Current Score:", score) # Display details topDisplay(surface, score, birds.birds[0].distance, birdCord, gapCord, genrationCount) # This part is still not implemented if pause == 0: pygame.display.update() else: pygame.time.wait(1000) # setting FPS clock.tick(framesPerSecond)
def game(): # main game part # Initialization pygame.init() # Creating objects clock = pygame.time.Clock() surface = pygame.display.set_mode((surfaceWidth, surfaceHeight)) pygame.display.set_caption('Flappy Bird') # Setting score as global variable global score # Declaring bird flappyBird = Bird(surface) firstPipe = Pipe(surface, surfaceWidth+100) secondPipe = Pipe(surface, surfaceWidth+250) # Grouping pipes pipeGroup = pygame.sprite.Group() pipeGroup.add(firstPipe.upperBlock) pipeGroup.add(secondPipe.upperBlock) pipeGroup.add(firstPipe.lowerBlock) pipeGroup.add(secondPipe.lowerBlock) moved = False pause = 0 # Whole game machenism while True: # draw over the background surface.blit(background,(0,0)) # check if bird and pipes are colliding t = pygame.sprite.spritecollideany(flappyBird,pipeGroup) # check if the bird is toching the screen top or bottom if t!=None or (flappyBird.y == 509 - flappyBird.height) or (flappyBird.y == 0): # if yes the it's game over print("GAME OVER") print("FINAL SCORE IS %d"%score) gameOver(surface, clock) # else check for any event (eg. button pressed) for event in pygame.event.get(): if event.type == QUIT: pygame.quit() sys.exit() if event.type == KEYDOWN and (event.key == K_SPACE or event.key == K_w or event.key == K_UP): flappyBird.move("FLAP") moved = True if moved == False: flappyBird.move(None) else: moved = False pipe1Pos = firstPipe.move() if pipe1Pos[0] <= int(surfaceWidth * 0.2) - int(flappyBird.rect.width/2): if firstPipe.behindBird == 0: firstPipe.behindBird = 1 score += 1 print("SCORE IS %d"%score) pipe2Pos = secondPipe.move() if pipe2Pos[0] <= int(surfaceWidth * 0.2) - int(flappyBird.rect.width/2): if secondPipe.behindBird == 0: secondPipe.behindBird = 1 score += 1 print("SCORE IS %d"%score) birdCord = [flappyBird.x, flappyBird.y] closestPipe = pipe1Pos if (pipe1Pos[0]<pipe2Pos[0]) else pipe2Pos gapCord = [closestPipe[0], ((closestPipe[1] + closestPipe[2])/2)] topDisplay(surface, score, flappyBird.distance, birdCord, gapCord) if pause==0: pygame.display.update() else: pygame.time.wait(1000) clock.tick(framesPerSecond)