Advertisement
Arcot

flappy birb simplified without images

May 14th, 2022 (edited)
237
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.12 KB | None | 0 0
  1. import pygame
  2. from random import randint
  3.  
  4. BLACK = (0, 0, 0)
  5. WHITE = (255, 255, 255)
  6. GREEN = (0, 255, 0)
  7. RED = (255, 0, 0)
  8.  
  9. pygame.init()
  10.  
  11. size = 700, 500
  12. screen = pygame.display.set_mode(size)
  13. pygame.display.set_caption("Flappy bird")
  14.  
  15. done = False
  16. clock = pygame.time.Clock()
  17.  
  18. def ball(x, y):
  19.     #radius of 20px
  20.     pygame.draw.circle(screen, BLACK, [x, y], 20)
  21.  
  22. def gameover():
  23.     font = pygame.font.SysFont(None, 55)
  24.     text = font.render("Game Over", True, RED)
  25.     screen.blit(text, [150, 250])
  26.  
  27. def obstacles(xloc, yloc, xsize, ysize):
  28.     pygame.draw.rect(screen, GREEN, [xloc, yloc, xsize, ysize])
  29.     pygame.draw.rect(screen, GREEN, [xloc, int(yloc+ysize+space), xsize, ysize+500])
  30.  
  31. #when ball between 2 points on screen, increase score
  32. def Score(score):
  33.     font = pygame.font.SysFont(None, 55)
  34.     text = font.render("Score:" + str(score), True, BLACK)
  35.     screen.blit(text, [0,0])
  36.  
  37.  
  38. x = 350
  39. y = 250
  40. x_speed = 0
  41. y_speed = 0
  42. ground = 477
  43. xloc = 700
  44. yloc = 0
  45. xsize = 70
  46. ysize = randint(0, 350)
  47. #size of space between 2 blocks
  48. space = 150
  49. obspeed = 2
  50. score= 0
  51.  
  52. while not done:
  53.     for event in pygame.event.get():
  54.         if event.type == pygame.QUIT:
  55.             done = True
  56.        
  57.         if event.type == pygame.KEYDOWN:
  58.             if event.key == pygame.K_UP:
  59.                 y_speed = -10
  60.            
  61.         if event.type == pygame.KEYUP:
  62.             if event.key == pygame.K_UP:
  63.                 y_speed = 5
  64.  
  65.     screen.fill(WHITE)
  66.     obstacles(xloc, yloc, xsize, ysize)
  67.     ball(x, y)
  68.     Score(score)
  69.  
  70.     y += y_speed
  71.     xloc -= obspeed
  72.  
  73.     if y > ground:
  74.         gameover()
  75.         y_speed = 0
  76.         obspeed = 0
  77.  
  78.     if x+20 > xloc and y-20 < ysize and x-15 < xsize+xloc:
  79.         gameover()
  80.         y_speed = 0
  81.         obspeed = 0
  82.  
  83.     if x+20 > xloc and y+20 < ysize and x-15 < xsize+xloc:
  84.         gameover()
  85.         y_speed = 0
  86.         obspeed = 0
  87.    
  88.     if xloc < -80:
  89.         xloc = 700
  90.         ysize = randint(0, 350)
  91.  
  92.     if x > xloc and  x < xloc+3:
  93.         score = score + 1
  94.  
  95.     pygame.display.flip()
  96.     clock.tick(60)
  97. pygame.quit()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement