DETECTING COLLISIONS
So now we will check if the player has collided or not. There will be 4 types of collision.
First, if our bird has moved out of the screen. Second, if the bird hits the ground. The third and fourth will be the collision between pipes.
crashTest = isCollide(playerx, playery, upperPipes, lowerPipes) # This function will return true if the player is crashed
if crashTest:
return
This function will return true if the player is crashed and then will return to the collide function and will check which collision is happened and then will follow the further statement.
So now we are going to create our function first we will check collision with the ground and upmost screen.
def isCollide(playerx, playery, upperPipes, lowerPipes):
if playery> GROUNDY - 25 or playery<0:
GAME_SOUNDS['hit'].play()
gameOver()
It will check if the player’s Y-coordinate has become greater than GROUNDY – 25 or if the player’s Y-coordinate is smaller than 0 then it will play the hit sound effect and our game will over and after then we call our gameOver function which we’ll create after some time, so don’t be confused.
Now, we will see the collision between the pipes. So, let’s see the code for them.
def isCollide(playerx, playery, upperPipes, lowerPipes): if playery> GROUNDY - 25 or playery<0: GAME_SOUNDS['hit'].play() gameOver() #UPPER PIPE COLLISION for pipe in upperPipes: pipeHeight = GAME_SPRITES['pipe'][0].get_height() if(playery < pipeHeight + pipe['y'] and abs(playerx - pipe['x']) < GAME_SPRITES['pipe'][0].get_width()-20): GAME_SOUNDS['hit'].play() print(playerx, pipe['x'],) gameOver() #LOWER PIPE COLLISION for pipe in lowerPipes: if (playery + GAME_SPRITES['player'].get_height() > pipe['y']) and abs(playerx - pipe['x']) < GAME_SPRITES['pipe'][0].get_width()-20: GAME_SOUNDS['hit'].play() gameOver() return False
What we did is first we obtained the pipe height and then checked if playery is smaller than the sum of pipe height and pipe’s y and after then we checked if the absolute value of the difference of player x and pipe x is smaller than the upper pipe’s width – 20.
If yes, then we played the ‘hit sound effect’ and called our gameOver function. The same goes with the lower pipe collision.
So, we have successfully created the collision function for our game. Now, we’ll create the game over function that we have called after every collision.
CREATING GAME OVER FUNCTION
Now, we’ll create our game over function in this game over function, we would have some images and then we’ll give the player a choice to either retry or go home. So, let’s have a look at the whole code of this function.
def gameOver(): SCREEN = pygame.display.set_mode((SCREENWIDTH, SCREENHEIGHT)) pygame.display.set_caption('Flappy Bird With Sameer') GAME_SPRITES['OVER'] = pygame.image.load('resources/SPRITES/gameover.png').convert_alpha() GAME_SPRITES['RETRY'] = pygame.image.load('resources/SPRITES/retry.png').convert_alpha() GAME_SPRITES['HOME'] = pygame.image.load('resources/SPRITES/Home.png').convert_alpha() SCREEN.blit(GAME_SPRITES['background'],(0,0)) SCREEN.blit(GAME_SPRITES['base'],(0,GROUNDY)) SCREEN.blit(GAME_SPRITES['OVER'], (0,0)) SCREEN.blit(GAME_SPRITES['RETRY'], (30, 220)) SCREEN.blit(GAME_SPRITES['HOME'], (30, 280)) pygame.display.update() while True: for event in pygame.event.get(): if event.type == QUIT or (event.type == KEYDOWN and event.key == K_ESCAPE): pygame.quit() sys.exit() if event.type == KEYDOWN and event.key == K_SPACE: mainGame() pygame.mouse.set_cursor(pygame.SYSTEM_CURSOR_ARROW) if pygame.mouse.get_pos()[0]>30 and pygame.mouse.get_pos()[0]< 30+GAME_SPRITES['RETRY'].get_width(): if pygame.mouse.get_pos()[1]>220 and pygame.mouse.get_pos()[1]< 220+GAME_SPRITES['RETRY'].get_height(): pygame.mouse.set_cursor(pygame.SYSTEM_CURSOR_HAND) if event.type == pygame.MOUSEBUTTONDOWN and event.button == 1: mainGame() if pygame.mouse.get_pos()[0]>30 and pygame.mouse.get_pos()[0]< 30+GAME_SPRITES['HOME'].get_width(): if pygame.mouse.get_pos()[1]>280 and pygame.mouse.get_pos()[1]< 280+GAME_SPRITES['HOME'].get_height(): pygame.mouse.set_cursor(pygame.SYSTEM_CURSOR_HAND) if event.type == pygame.MOUSEBUTTONDOWN and event.button == 1: welcomeScreen()
We first made the screen and then loaded our retry and home icons then we blitted all our necessary images for our game over.
After then we made the while infinite loop and then gave the user to close the game and if the user presses the space bar then restart the game by calling the main game function.
Adding Buttons
After then, here comes the logical part which is our restart button and home button. So, look at the code.
pygame.mouse.set_cursor(pygame.SYSTEM_CURSOR_ARROW) if pygame.mouse.get_pos()[0]>30 and pygame.mouse.get_pos()[0]< 30+GAME_SPRITES['RETRY'].get_width(): if pygame.mouse.get_pos()[1]>220 and pygame.mouse.get_pos()[1]< 220+GAME_SPRITES['RETRY'].get_height(): pygame.mouse.set_cursor(pygame.SYSTEM_CURSOR_HAND) if event.type == pygame.MOUSEBUTTONDOWN and event.button == 1: mainGame() if pygame.mouse.get_pos()[0]>30 and pygame.mouse.get_pos()[0]< 30+GAME_SPRITES['HOME'].get_width(): if pygame.mouse.get_pos()[1]>280 and pygame.mouse.get_pos()[1]< 280+GAME_SPRITES['HOME'].get_height(): pygame.mouse.set_cursor(pygame.SYSTEM_CURSOR_HAND) if event.type == pygame.MOUSEBUTTONDOWN and event.button == 1: welcomeScreen() if pygame.mouse.get_pos()[0]>30 and pygame.mouse.get_pos()[0]< 30+GAME_SPRITES['HOME'].get_width(): if pygame.mouse.get_pos()[1]>280 and pygame.mouse.get_pos()[1]< 280+GAME_SPRITES['HOME'].get_height(): pygame.mouse.set_cursor(pygame.SYSTEM_CURSOR_HAND) if event.type == pygame.MOUSEBUTTONDOWN and event.button == 1: welcomeScreen()
First, we set the mouse cursor to an arrow because we’ll change it to a hand gesture when we’ll hover on retry and home buttons.
After then, we checked if the mouse cursor’s x-coordinate is greater than 30 and less than the width of the button which is home in this case and after that we checked if the mouse cursor’s y-coordinate is greater than 220 and less than the 220 + height of the button.
The same logic goes with the home button but coordinates are different which you can see in the code.
After then, we changed the mouse cursor to a hand gesture if the above statement is true. After it, we checked it the user clicked on the button if yes then gave them the desired result like if they click on the home button then return to the welcome screen by calling the welcome screen function and if they click on the retry button then start the game for them again by calling our main game function.