GithubHelp home page GithubHelp logo

cloned-flappy-bird's Introduction

Cloned-Flappy-Bird

Flappy bird is a mobile game developed by Vietnamese video game artist and programmer, Dong Nguyenm, under his game development company, Gears. The cloned flappy bird game starts with a bird floating in the air, and the user can control the bird by clicking spacebar to elevate and releasing the space bar to descent. The target of the game is to score as high as possible without colliding with the upcoming green bar.

  • built using Python with Pygame.

Initialize library

from time import sleep
import pygame
import random

Pygame contains the core library to build this cloned flappy bird game, together with Time to set some time delay and Random to randomly generate the heights for the upcoming green bars.

Set display size, background image and game title

# set game display size
WIDTH = 500
HEIGHT = 650
screen = pygame.display.set_mode((WIDTH, HEIGHT))

# set background image
background = pygame.image.load('background.jpg')

# set game title
pygame.display.set_caption("Flappy Bird")

Load bird image and set initial position

bird = pygame.image.load('bird1.png')
bird_X = 50
bird_Y = 400
bird_Y_change = 0

bird_X and bird_Y are initialized to the initial floating x-value and y-value of the bird. โœจ Note that y-value starts from top to bottom.

Set the width, random height and colour of obstacles

obstacles_width = 70
obstacles_height = random.randint(100,120)
obstacles_colour = (211,253,117)

obstacles_X_change = -2 # obstacles move from right to left on X-axis
obstacles_X = 500
obstacles_gap = 150

Display the score of the game

def display_score(score):
	font = pygame.font.Font('Flappy-Bird.ttf', 40)
	score_text = "Total: " + str(score)
	text = font.render(score_text, False, black)
	screen.blit(text, (190, 200))

Flappy-Bird.ttf font file is used for the score text.

Display the obstacles

def display_obstacles(height):
	# create top obstacle
	pygame.draw.rect(screen, obstacles_colour, (obstacles_X, 0, obstacles_width, height))

	# find remaining height and create bottom obstacle
	bottom_obstacle_height = height + obstacles_gap
	pygame.draw.rect(screen, obstacles_colour, (obstacles_X, bottom_obstacle_height, obstacles_width, 550 - bottom_obstacle_height))

To calculate the height of the bottom obstacle, we can compute height + obstacles_gap where height = top obstacle's height.

Detect collision function

def detect_collision(x, height, bird_y, bottom_obstacle_height):
	# check whether the bird collides with the obstacle
	# bird starts at x-coord = 50 with a width of 85
	if x <= (50 + 85):
		# bird current height fall within the gap
		if bird_y <= height or bird_y >= (bottom_obstacle_height - 64):
				return True
	return False

A nested if-else statement is used, with the 1st if statement checking on obstacle's x-value <= the bird's rigthmost tip, and the 2nd if statement checking on bird_y <= height (top obstacle's height) or >= bottom_obstacle_height - 64 (bottom obstacle's height + obstacle's gap size). If both statement are true, collision is detected and the function will return True.

Rendering of the gameplay

A while-loop is used to initiate the rendering of the gameplay. Some key aspects inside the while-loop are:

  1. detect keyboard event
# detect for keyboard event
for event in pygame.event.get():
	if event.type == pygame.KEYDOWN:
		if event.key == pygame.K_SPACE:
			bird_Y_change = -3

	if event.type == pygame.KEYUP:
		if event.key == pygame.K_SPACE:
			bird_Y_change = 3

pygame.KEYDOWN and pygame.KEYUP are used to detect the click down and release of the spacebar event, changing the bird_Y_change value respectively.

  1. calculate location of obstacles
# calculate new obstacle x-value
obstacles_X += obstacles_X_change
if obstacles_X <= -10:
	get_score = True
	obstacles_X = 500
	obstacles_height = random.randint(150, 350)

If obstacles_X <= -10, meaning it exceeds the limit of the leftmost of the screen, a new obstacle is generated with a random height.

  1. detect collision
collision = detect_collision(obstacles_X, obstacles_height, bird_Y, obstacles_height + obstacles_gap)

if collision:
	pygame.mixer.Sound.play(game_over_sound)
	sleep(1)
	pygame.quit()
	break
if obstacles_X <= bird_X and get_score:
	score += 1
	pygame.mixer.Sound.play(score_sound)
	get_score = False

If the collision is true, the game will play the loaded game over sound and quit the game. For each obstacles_X <= bird_X and get_score is true (indicating the obstacle reaches the leftmost of the screen, score is increment and a score sound is played.

cloned-flappy-bird's People

Contributors

elwinc2799 avatar

Watchers

 avatar

cloned-flappy-bird's Issues

Preview of Game

Would be nice to include some screenshot of the game in README.md

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.