Pygame CODE
Pygame CODE
import random
pygame.init()
SCREEN_WIDTH = 400
SCREEN_HEIGHT = 600
FPS = 40
BLACK = (0, 0, 0)
BIRD_WIDTH = 30
BIRD_HEIGHT = 30
BIRD_X = 50
BIRD_Y = SCREEN_HEIGHT // 2
BIRD_VELOCITY = 0
GRAVITY = 0.5
FLAP_STRENGTH = -10
PIPE_WIDTH = 70
PIPE_HEIGHT = 500
PIPE_GAP = 150
PIPE_VELOCITY = -5
pygame.display.set_caption("Flappy Bird")
bird_image = pygame.Surface((BIRD_WIDTH, BIRD_HEIGHT))
bird_image.fill(WHITE)
pipe_image.fill(GREEN)
def draw_bird(y):
def main():
clock = pygame.time.Clock()
bird_y = BIRD_Y
bird_velocity = BIRD_VELOCITY
pipe_x = SCREEN_WIDTH
score = 0
running = True
while running:
if event.type == pygame.QUIT:
running = False
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_SPACE:
bird_velocity = FLAP_STRENGTH
bird_velocity += GRAVITY
bird_y += bird_velocity
pipe_x += PIPE_VELOCITY
pipe_x = SCREEN_WIDTH
score += 1
if (BIRD_X + BIRD_WIDTH > pipe_x and BIRD_X < pipe_x + PIPE_WIDTH) and \
(bird_y < pipe_y + PIPE_HEIGHT or bird_y + BIRD_HEIGHT > pipe_y + PIPE_HEIGHT + PIPE_GAP):
running = False
running = False
screen.fill(BLACK)
draw_bird(bird_y)
draw_pipe(pipe_x, pipe_y)
pygame.display.flip()
clock.tick(FPS)
pygame.quit()
if __name__ == "__main__":
main()