Skip to main content

Flappy Bird Game using Python ๐Ÿ”ฅ๐Ÿ”ฅ Python in Pydroid3 | Amazing Python Project | Vast Coding

Flappy Bird Game using Python ๐Ÿ”ฅ๐Ÿ”ฅ Python in Pydroid3 | Amazing Python Project | Vast Coding
                                  
Here is the Source Code:-

main.py

import pygame
import random

from objects import Grumpy, Pipe, Base, Score

# Setup *******************************************

pygame.init()
SCREEN = WIDTH, HEIGHT = 288, 512
display_height = 0.80 * HEIGHT
info = pygame.display.Info()

width = info.current_w
height = info.current_h

if width >= height:
	win = pygame.display.set_mode(SCREEN, pygame.NOFRAME)
else:
	win = pygame.display.set_mode(SCREEN, pygame.NOFRAME | pygame.SCALED | pygame.FULLSCREEN)

# win = pygame.display.set_mode(SCREEN, pygame.SCALED | pygame.FULLSCREEN)
clock = pygame.time.Clock()
FPS = 60

# COLORS

RED = (255, 0, 0)
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)

# Backgrounds

bg1 = pygame.image.load('Assets/background-day.png')
bg2 = pygame.image.load('Assets/background-night.png')

bg = random.choice([bg1, bg2])

im_list = [pygame.image.load('Assets/pipe-green.png'), pygame.image.load('Assets/pipe-red.png')]
pipe_img = random.choice(im_list)

gameover_img =  pygame.image.load('Assets/gameover.png')
flappybird_img =  pygame.image.load('Assets/flappybird.png')
flappybird_img = pygame.transform.scale(flappybird_img, (200,80))

# Sounds & fx


die_fx = pygame.mixer.Sound('Sounds/die.wav')
hit_fx = pygame.mixer.Sound('Sounds/hit.wav')
point_fx = pygame.mixer.Sound('Sounds/point.wav')
swoosh_fx = pygame.mixer.Sound('Sounds/swoosh.wav')
wing_fx = pygame.mixer.Sound('Sounds/wing.wav')

# Objects

pipe_group = pygame.sprite.Group()
base = Base(win)
score_img = Score(WIDTH // 2, 50, win)
grumpy = Grumpy(win)

# Variables

base_height = 0.80 * HEIGHT
speed = 0
game_started = False
game_over = False
restart = False
score = 0
start_screen = True
pipe_pass = False
pipe_frequency = 1600

running =  True
while running:
	win.blit(bg, (0,0))
	
	if start_screen:
		speed = 0
		grumpy.draw_flap()
		base.update(speed)
		
		win.blit(flappybird_img, (40, 50))
	else:
		
		if game_started and not game_over:
			
			next_pipe = pygame.time.get_ticks()
			if next_pipe - last_pipe >= pipe_frequency:
				y = display_height // 2
				pipe_pos = random.choice(range(-100,100,4))
				height = y + pipe_pos
				
				top = Pipe(win, pipe_img, height, 1)
				bottom = Pipe(win, pipe_img, height, -1)
				pipe_group.add(top)
				pipe_group.add(bottom)
				last_pipe = next_pipe
		
		pipe_group.update(speed)
		base.update(speed)	
		grumpy.update()
		score_img.update(score)
		
		if pygame.sprite.spritecollide(grumpy, pipe_group, False) or grumpy.rect.top <= 0:
			game_started = False
			if grumpy.alive:
				hit_fx.play()
				die_fx.play()
			grumpy.alive = False
			grumpy.theta = grumpy.vel * -2
	
		if grumpy.rect.bottom >= display_height:
			speed = 0
			game_over = True
	
		if len(pipe_group) > 0:
			p = pipe_group.sprites()[0]
			if grumpy.rect.left > p.rect.left and grumpy.rect.right < p.rect.right and not pipe_pass and grumpy.alive:
				pipe_pass = True
	
			if pipe_pass:
				if grumpy.rect.left > p.rect.right:
					pipe_pass = False
					score += 1
					point_fx.play()
					
	if not grumpy.alive:
		win.blit(gameover_img, (50,200))
		
	for event in pygame.event.get():
		if event.type == pygame.QUIT:
			running = False
		if event.type == pygame.KEYDOWN:
			if event.key == pygame.K_ESCAPE or \
				event.key == pygame.K_q:
				running = False
		if event.type == pygame.MOUSEBUTTONDOWN:
			if start_screen:
				game_started = True
				speed = 2
				start_screen = False

				game_over = False
			#	grumpy.reset()
				last_pipe = pygame.time.get_ticks() - pipe_frequency
				next_pipe = 0
				pipe_group.empty()
				
				speed = 2
				score = 0
				
			if game_over:
				start_screen = True
				grumpy = Grumpy(win)
				pipe_img = random.choice(im_list)
				bg = random.choice([bg1, bg2])
				
			

	clock.tick(FPS)
	pygame.display.update()
pygame.quit()

objects.py

import pygame
import random

SCREEN = WIDTH, HEIGHT = 288, 512
display_height = 0.80 * HEIGHT

pygame.mixer.init()
wing_fx = pygame.mixer.Sound('Sounds/wing.wav')

class Grumpy:
	def __init__(self, win):
		self.win = win

		self.im_list = []
		bird_color = random.choice(['red', 'blue', 'yellow'])
		for i in range(1,4):
			img =  pygame.image.load(f'Assets/Grumpy/{bird_color}{i}.png')
			self.im_list.append(img)
		
		self.reset()
		
	def update(self):
		# gravity
		self.vel += 0.3
		if self.vel >= 8:
			self.vel = 8
		if self.rect.bottom <= display_height:
			self.rect.y += int(self.vel)
		
		if self.alive:
			
			# jump
			if pygame.mouse.get_pressed()[0] == 1 and not self.jumped:
				wing_fx.play()
				self.jumped = True
				self.vel = -6
			if pygame.mouse.get_pressed()[0] == 0:
				self.jumped = False
			
			self.flap_counter()
			
			self.image = pygame.transform.rotate(self.im_list[self.index], self.vel * -2)
		else:
			if self.rect.bottom <= display_height:
				self.theta -= 2
			self.image = pygame.transform.rotate(self.im_list[self.index], self.theta)
			
	#	if not alive:
	#		self.image = self.im_list[1]
		
		self.win.blit(self.image, self.rect)
		
	def flap_counter(self):
		#animation
		self.counter += 1
		if self.counter > 5:
			self.counter = 0
			self.index += 1
		if self.index >= 3:
			self.index = 0
			
	def draw_flap(self):
		self.flap_counter()
		if self.flap_pos <= -10 or self.flap_pos > 10:
			self.flap_inc *= -1
		self.flap_pos += self.flap_inc
		self.rect.y += self.flap_inc
		self.rect.x = WIDTH // 2 - 20
		self.image = self.im_list[self.index]
		self.win.blit(self.image, self.rect)
		
	def reset(self):
		self.index = 0
		self.image = self.im_list[self.index]
		self.rect = self.image.get_rect()
		self.rect.x = 60
		self.rect.y = int(display_height) // 2
		self.counter = 0
		self.vel = 0
		self.jumped = False
		self.alive = True
		self.theta = 0
		self.mid_pos = display_height // 2
		self.flap_pos = 0
		self.flap_inc = 1

class Base:
	def __init__(self, win):
		self.win = win

		self.image1 = pygame.image.load('Assets/base.png')
		self.image2 = self.image1
		self.rect1 = self.image1.get_rect()
		self.rect1.x = 0
		self.rect1.y = int(display_height)
		self.rect2 = self.image2.get_rect()
		self.rect2.x = WIDTH
		self.rect2.y = int(display_height)
		
	def update(self, speed):
		self.rect1.x -= speed
		self.rect2.x -= speed
		
		if self.rect1.right <= 0:
			self.rect1.x = WIDTH - 5
		if self.rect2.right <= 0:
			self.rect2.x = WIDTH - 5

		self.win.blit(self.image1, self.rect1)
		self.win.blit(self.image2, self.rect2)


class Pipe(pygame.sprite.Sprite):
	def __init__(self, win, image, y, position):
		super(Pipe, self).__init__()
		
		self.win = win
		self.image = image
		self.rect = self.image.get_rect()
		pipe_gap = 100 // 2
		x = WIDTH

		if position == 1:
			self.image = pygame.transform.flip(self.image, False, True)
			self.rect.bottomleft = (x, y - pipe_gap)
		elif position == -1:
			self.rect.topleft = (x, y + pipe_gap)
		
	def update(self, speed):
		self.rect.x -= speed
		if self.rect.right < 0:
			self.kill()
		self.win.blit(self.image,  self.rect)
		
class Score:
	def __init__(self, x, y, win):
		self.score_list = []
		for score in range(10):
			img = pygame.image.load(f'Assets/Score/{score}.png')
			self.score_list.append(img)
			self.x = x
			self.y = y

		self.win = win
		
	def update(self, score):
		score = str(score)
		for index, num in enumerate(score):
			self.image = self.score_list[int(num)]
			self.rect = self.image.get_rect()
			self.rect.topleft = self.x - 15 * len(score) + 30 * index, self.y
			self.win.blit(self.image, self.rect)import pygame
import random

SCREEN = WIDTH, HEIGHT = 288, 512
display_height = 0.80 * HEIGHT

pygame.mixer.init()
wing_fx = pygame.mixer.Sound('Sounds/wing.wav')

class Grumpy:
	def __init__(self, win):
		self.win = win

		self.im_list = []
		bird_color = random.choice(['red', 'blue', 'yellow'])
		for i in range(1,4):
			img =  pygame.image.load(f'Assets/Grumpy/{bird_color}{i}.png')
			self.im_list.append(img)
		
		self.reset()
		
	def update(self):
		# gravity
		self.vel += 0.3
		if self.vel >= 8:
			self.vel = 8
		if self.rect.bottom <= display_height:
			self.rect.y += int(self.vel)
		
		if self.alive:
			
			# jump
			if pygame.mouse.get_pressed()[0] == 1 and not self.jumped:
				wing_fx.play()
				self.jumped = True
				self.vel = -6
			if pygame.mouse.get_pressed()[0] == 0:
				self.jumped = False
			
			self.flap_counter()
			
			self.image = pygame.transform.rotate(self.im_list[self.index], self.vel * -2)
		else:
			if self.rect.bottom <= display_height:
				self.theta -= 2
			self.image = pygame.transform.rotate(self.im_list[self.index], self.theta)
			
	#	if not alive:
	#		self.image = self.im_list[1]
		
		self.win.blit(self.image, self.rect)
		
	def flap_counter(self):
		#animation
		self.counter += 1
		if self.counter > 5:
			self.counter = 0
			self.index += 1
		if self.index >= 3:
			self.index = 0
			
	def draw_flap(self):
		self.flap_counter()
		if self.flap_pos <= -10 or self.flap_pos > 10:
			self.flap_inc *= -1
		self.flap_pos += self.flap_inc
		self.rect.y += self.flap_inc
		self.rect.x = WIDTH // 2 - 20
		self.image = self.im_list[self.index]
		self.win.blit(self.image, self.rect)
		
	def reset(self):
		self.index = 0
		self.image = self.im_list[self.index]
		self.rect = self.image.get_rect()
		self.rect.x = 60
		self.rect.y = int(display_height) // 2
		self.counter = 0
		self.vel = 0
		self.jumped = False
		self.alive = True
		self.theta = 0
		self.mid_pos = display_height // 2
		self.flap_pos = 0
		self.flap_inc = 1

class Base:
	def __init__(self, win):
		self.win = win

		self.image1 = pygame.image.load('Assets/base.png')
		self.image2 = self.image1
		self.rect1 = self.image1.get_rect()
		self.rect1.x = 0
		self.rect1.y = int(display_height)
		self.rect2 = self.image2.get_rect()
		self.rect2.x = WIDTH
		self.rect2.y = int(display_height)
		
	def update(self, speed):
		self.rect1.x -= speed
		self.rect2.x -= speed
		
		if self.rect1.right <= 0:
			self.rect1.x = WIDTH - 5
		if self.rect2.right <= 0:
			self.rect2.x = WIDTH - 5

		self.win.blit(self.image1, self.rect1)
		self.win.blit(self.image2, self.rect2)


class Pipe(pygame.sprite.Sprite):
	def __init__(self, win, image, y, position):
		super(Pipe, self).__init__()
		
		self.win = win
		self.image = image
		self.rect = self.image.get_rect()
		pipe_gap = 100 // 2
		x = WIDTH

		if position == 1:
			self.image = pygame.transform.flip(self.image, False, True)
			self.rect.bottomleft = (x, y - pipe_gap)
		elif position == -1:
			self.rect.topleft = (x, y + pipe_gap)
		
	def update(self, speed):
		self.rect.x -= speed
		if self.rect.right < 0:
			self.kill()
		self.win.blit(self.image,  self.rect)
		
class Score:
	def __init__(self, x, y, win):
		self.score_list = []
		for score in range(10):
			img = pygame.image.load(f'Assets/Score/{score}.png')
			self.score_list.append(img)
			self.x = x
			self.y = y

		self.win = win
		
	def update(self, score):
		score = str(score)
		for index, num in enumerate(score):
			self.image = self.score_list[int(num)]
			self.rect = self.image.get_rect()
			self.rect.topleft = self.x - 15 * len(score) + 30 * index, self.y
			self.win.blit(self.image, self.rect)
Output:- 

Output

Download Source Code and All Files from here๐Ÿ‘‡




Steps to run code:-

1) Download the zip file from the download button.
2)Make a folder in your storage.
3)Extract zip file in the folder.
4)Open Pydroid3 and then open.py files.
5)Run the code and Enjoy your Game๐Ÿ˜ƒ

Note:- Please help me to grow my Youtube Channel called "Vast Coding"



Comments

Popular posts from this blog

Draw anyone's Sketch using Python Turtle Graphics in Python Programming

Draw anyone's Sketch using Python Turtle Graphics in Python Programming ๐Ÿ‘จ‍๐Ÿ’ป๐Ÿ”ฅ In this post ,I will tell you about How to make Anyone's Sketch in Python using svg file and using these libraries: Turtle,cv2(opencv-python),svgpathtools,svg.path and tqdm. You can install these libraries using pip command. Turtle is pre-installed in mostly all programming softwares. And here are the pip commands to install other libraries : pip install opencv-python pip install svgpathtools pip install svg.path pip install tqdm ๐—ฆ๐˜๐—ฒ๐—ฝ๐˜€ ๐˜๐—ผ ๐—บ๐—ฎ๐—ธ๐—ฒ ๐˜€๐—ธ๐—ฒ๐˜๐—ฐ๐—ต :  1)Go to https://svgconvert.com/#/  2)Upload the image and change the threshold value according to the image and download it as a svg file.  3)Save the code and svg file in the same folder.  Source Code : import turtle as tu import cv2 from svgpathtools import svg2paths2 from svg.path import parse_path from tqdm import tqdm class sketch_from_svg: def __init__(self,path,scale=30,x_offset=400,y_offset=400): self.path =

Robert Downey Jr - Tony Stark - Ironman Sketch using Python Turtle Graphics

Robert Downey Jr Sketch using Python Turtle Graphics ๐Ÿ‘จ‍๐Ÿ’ป About Python Turtle Graphics : Turtle graphics is a popular way for introducing programming to kids. It was part of the original Logo programming language developed by Wally Feurzeig, Seymour Papert and Cynthia Solomon in 1967. Imagine a robotic turtle starting at (0, 0) in the x-y plane. After an import turtle , give it the command turtle. Note: If you are facing any issue to copy the code then copy the full page clicking 'select all',paste it to your notepad and copy the code from there. Here is the source code : import turtle as tu class rdj: def __init__(self): self.mouth = [(374, 382),(368, 382),(359, 382),(347, 379),(339, 381),(334, 384),(323, 380),(315, 380),(293, 385),(321, 387),(339, 387),(345, 386),(357, 385),(374, 383),(-1, -1),(389, 389),(393, 387),(395, 381),(395, 373),(377, 362),(366, 357),(358, 360),(351, 357),(345, 354),(344, 357),(337, 355),(333, 357),(327, 355),(323, 357),(303, 362),(

Sketchpy Python Library | How to use and How to Install

How to use sketchpy library in python | How to install sketchpy library in python programming Hello visitors, In this blogpost I will tell you about new python library called ' Sketchpy ' It's a new library in python programming. You can consider sketchpy as beginning level python project. What is sketchpy? Sketchpy is a library which allows you to draw any sketch using turtle graphics. In sketchpy library there are several classes and you can import these classes and then draw Rdj,Thalapathy Vijay,Apj Abdul kalam ,etc. This library also contains very useful codes like image coordinates tracer ,etc Sketchpy is a beginning level python project to draw amazing and awesome animations,drawings,designs using python turtle graphics. Usage ⬤  Install the package using    pip install sketchpy ⬤  Import it as import sketchpy in your project How to Install ⬤   Install using Terminal ⬤    Install using CMD {Command Prompt} Install pip install sketchpy It will definetely work, bu