So I am pretty new to Pygame, and I am stuck on how to show the collision between the bird and the Bamboo in the flappy bird game and how to keep running it if it hasn't collided with the bamboo. I haven't written the whole code yet, so please tell me what to write for showing collision(with steps and why is it used? In pygame) and also a source code to display after the collision the screen shows GAME OVER.(optional) (Thanks in advance:)
import pygame
pygame.init()
WINDOWWIDTH = 820
WINDOWHEIGHT = 480
win = pygame.display.set_mode((WINDOWWIDTH, WINDOWHEIGHT))
pygame.display.set_caption("The Flappy Bird Game")
birdload = [pygame.image.load('C:/Users/Vinod/Desktop/Flappy Bird/flapbird.png')]
bgimg = pygame.image.load('C:/Users/Vinod/Desktop/Flappy Bird/bg.jpg')
clock=pygame.time.Clock()
UP = 'up'
DOWN ='down'
LEFT ='left'
RIGHT = 'right'
WHITE =(255, 255, 255)
BLACK = (0, 0 ,0)
RED = (255, 0, 0)
GREEN = (0, 255, 0)
DARKGREEN = (0, 155,0)
DARKGRAY = (40, 40, 40)
YELLOW = (255, 255, 0)
ORANGE = (255, 165, 0)
BGCOLOR = BLACK
BROWN = (90, 39, 41)
score = 0
def showstartscreen():
global BASICFONT, FPS
FPS = 15
BASICFONT = pygame.font.Font('freesansbold.ttf', 18)
titleFont = pygame.font.Font('freesansbold.ttf', 100)
titleSurf1 = titleFont.render('The Flappy Bird!', True, WHITE, YELLOW)
titleSurf2 = titleFont.render('The Flappy Bird!', True, ORANGE)
degrees1 = 0
degrees2 = 0
while True:
win.fill(BGCOLOR)
rotatedSurf1= pygame.transform.rotate(titleSurf1, degrees1)
rotatedRect1 = rotatedSurf1.get_rect()
rotatedRect1.center= (WINDOWWIDTH / 2, WINDOWHEIGHT / 2)
win.blit(rotatedSurf1, rotatedRect1)
rotatedSurf2 = pygame.transform.rotate(titleSurf2, degrees2)
rotatedRect2 = rotatedSurf2.get_rect()
rotatedRect2.center = (WINDOWWIDTH / 2, WINDOWHEIGHT / 2)
win.blit(rotatedSurf2,rotatedRect2)
drawPressKeyMsg()
pygame.display.update()
clock.tick(FPS)
degrees1 += 3 #rotate 3 degrees each frame
degrees2 += 7 #rotate by 7 degrees per frame
def drawPressKeyMsg():
pressKeySurf = BASICFONT.render('Press a key to play.', True, DARKGRAY)
pressKeyRect = pressKeySurf.get_rect()
pressKeyRect.topleft = (WINDOWWIDTH - 200, WINDOWHEIGHT - 30)
win.blit(pressKeySurf, pressKeyRect)
def terminate():
pygame.quit()
class bird(object):
def __init__(self, x, y, width, height, end):
self.x = x
self.y = y
self.width = width
self.height = height
self.end = end
self.path = [self.x, self.end]
self.flyCount = 0
self.vel = 3
self.hitbox = (self.x + 17, self.y + 2, 31, 57)
self.visible = True
def draw(self, win):
if self.flyCount + 1 >= 27:
self.flyCount = 0
if not(self.standing):
win.blit(birdload[self.flyCount//3], (self.x,self.y))
self.flyCount +=1
else:
win.blit(birdload[0], (self.x, self.y))
self.hitbox = (self.x + 17, self.y + 11, 29, 52)
#pygame.draw.rect(win, (255,0,0), self.hitbox,2)
def hit(self):
self.isJump = False
self.jumpCount = 10
self.x = 100
self.y = 410
self.flyCount = 0
font1 = pygame.font.SysFont('comicsans', 100)
text = font1.render('-5', 1, (255,0,0))
win.blit(text, (250 - (text.get_width()/2),200))
pygame.display.update()
i = 0
while i < 200:
pygame.time.delay(10)
i += 1
for event in pygame.event.get():
if event.type == pygame.QUIT:
i = 201
pygame.quit()
def fly(self):
if self.vel > 0:
if self.x + self.vel < self.path[1]:
self.x += self.vel
else:
self.vel = self.vel * -1
self.flyCount = 0
else:
if self.x - self.vel > self.path[0]:
self.x += self.vel
else:
self.vel = self.vel * -1
self.flyCount = 0
def bamboo():
bamb0 = pygame.draw.rect(win, BROWN,(310, 0, 28, 140))#left, top, width, height
pygame.display.flip()
bamb1 = pygame.draw.rect(win, BROWN, (310, 230 , 28, 250))
pygame.display.flip()
bamb2 = pygame.draw.rect(win, BROWN,(410, 0, 28, 100))#left, top, width, height
pygame.display.flip()
bamb3 = pygame.draw.rect(win, BROWN,(410, 160, 28,320))#left, top, width, height
pygame.display.flip()
bamb4 = pygame.draw.rect(win, BROWN,(510, 0, 28,110))#left, top, width, height
pygame.display.flip()
bamb5 = pygame.draw.rect(win, BROWN,(510, 180, 28,300))#left, top, width, height
pygame.display.flip()
bamb6 = pygame.draw.rect(win, BROWN,(600, 0, 28,240))#left, top, width, height
pygame.display.flip()
bamb7 = pygame.draw.rect(win, BROWN,(600, 320, 28,160))#left, top, width, height
pygame.display.flip()
bamb8 = pygame.draw.rect(win, BROWN,(700, 0, 28,120))#left, top, width, height
pygame.display.flip()
bamb9 = pygame.draw.rect(win, BROWN,(700, 200, 28,280))#left, top, width, height
pygame.display.flip()
bamb10 = pygame.draw.rect(win, BROWN,(770, 0, 28,190))#left, top, width, height
pygame.display.flip()
bamb11 = pygame.draw.rect(win, BROWN,(770, 250, 28,230))#left, top, width, height
pygame.display.flip()
class sticks(object):
bamboo()
def collision():
if bird.x == sticks.x and bird.y == sticks.y:
print("Game Over")
gameover()
while True:
if checkForKeyPress():
pygame.event.get() # clear event queue
return
else:
True()
Please refer to the 'def collision' in the 'class sticks' in the last, i know the code i've added is probably wrong... please answer for this part only... Regards, Nishita Thakur.
pygame.display.flip
once. – user12291970