A small flappy bird clone I made in an evening. Pygame is pretty cool!
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

flappy.py 2.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. import random
  2. class Flappy:
  3. # Static values, in the class so they're contained
  4. gravity = 250
  5. max_falling_speed = 200
  6. scrolling_speed = 100
  7. max_y = 480
  8. jump_cooldown = 0.3
  9. jump_force = 250
  10. player_width = 32
  11. player_height = 32
  12. pipe_interval = 250
  13. pipe_width = 50
  14. pipe_gap_height = 150
  15. # Player state
  16. score = 0
  17. player_x = 0
  18. player_y = 240
  19. player_falling_velocity = 0
  20. current_jump_cooldown = 0
  21. game_over = False
  22. # Pipe state
  23. pipe_x = 400
  24. pipe_y = 240
  25. pipe_scored = False
  26. # Returns true if the game has not ended, false on game-over
  27. def step(self, delta_time, jump):
  28. # Return false right away if game has ended to avoid nasty post-death-continuation bugs
  29. if self.game_over:
  30. return False
  31. # Jumping
  32. if self.current_jump_cooldown > 0:
  33. self.current_jump_cooldown -= delta_time
  34. if jump and self.current_jump_cooldown <= 0:
  35. self.player_falling_velocity = -self.jump_force
  36. self.current_jump_cooldown = self.jump_cooldown
  37. else:
  38. self.player_falling_velocity = min(self.max_falling_speed, self.player_falling_velocity + self.gravity * delta_time)
  39. # Moving the flapper
  40. self.player_y += self.player_falling_velocity * delta_time
  41. self.player_x += self.scrolling_speed * delta_time
  42. # Checking for losing
  43. if (self.player_x + self.player_width / 2 > self.pipe_x - self.pipe_width / 2 and \
  44. (self.player_y - self.player_height / 2 < self.pipe_y - self.pipe_gap_height / 2 or \
  45. self.player_y + self.player_height / 2 > self.pipe_y + self.pipe_gap_height / 2)) or \
  46. self.player_y - self.player_height / 2 > self.max_y:
  47. self.game_over = True
  48. return False
  49. # Scoring
  50. if self.player_x - self.player_width / 2 > self.pipe_x + self.pipe_width / 2:
  51. self.pipe_scored = False
  52. self.pipe_x += self.pipe_interval
  53. self.pipe_y = random.randrange(self.max_y / 2 - self.max_y / 4, self.max_y / 2 + self.max_y / 4)
  54. elif self.player_x > self.pipe_x and not self.pipe_scored:
  55. self.pipe_scored = True
  56. self.score += 1
  57. return True
  58. # For terminal videogaming fun. Do not recommend, it is not actually fun.
  59. def render_to_ascii(self):
  60. size = 48
  61. result = ""
  62. for y in range(int(self.max_y / size)):
  63. for x in range(20):
  64. if x == 0 and y == int(self.player_y / size):
  65. result += "@"
  66. elif x == int((self.pipe_x - self.player_x) / size) and \
  67. (y < int((self.pipe_y - self.pipe_gap_height / 2) / size) or \
  68. y > int((self.pipe_y + self.pipe_gap_height / 2) / size)):
  69. result += "#"
  70. else:
  71. result += "."
  72. result += "\n"
  73. return result