SHARE
TWEET

Untitled

a guest May 24th, 2019 51 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import sense_hat
  2. from time import sleep, time
  3. from random import randint
  4.  
  5. sense = sense_hat.SenseHat()
  6. sense.clear()
  7.  
  8. """
  9.  
  10.   Make your Sense HAT into a Flappy HAT!
  11.  
  12.   Help the bird fly though the columns.  Each column is
  13.   one point.  How many points can you get?
  14.  
  15.   Read through the code to learn how the game is made and
  16.   try changing some of the variables at the top. Turn on
  17.   debug_mode to print extra information about what the program
  18.   is doing.
  19.  
  20.   Inspired by the Astro Pi team's flappy astronaut!
  21.   https://www.raspberrypi.org/learning/flappy-astronaut
  22.  
  23.   Note: Requires sense_hat version 2.2.0 or later
  24. """
  25.  
  26. # Edit these variables to change key game parameters
  27. col_color = (0,255,0)      # RGB color of column pixels
  28. bg_color = (0,0,0)         # RGB color of background pixels
  29. bird_color = (200,200,0)   # RGB color of the bird pixel
  30. bird_y = 2                 # Where the player starts
  31. bird_lives = 3             # How many lives does the bird get?
  32. columns = [ (7, 3, 3)]     # Starting column x index, gap y start, gap size
  33. column_speed_base = 1      # Base speed for column movement, from 1 to 5
  34. debug_mode = False         # Set to true to print extra information
  35.  
  36. # Variables for convenience and readability
  37. up_key = sense_hat.DIRECTION_UP
  38. pressed = sense_hat.ACTION_PRESSED
  39.  
  40. # Initial settings - should be no need to alter these
  41. speed = 0
  42. game_over = False
  43. setup = True
  44. moved = False
  45. column_interval = 10
  46.  
  47. # Debug functions: pause or print messages if debug_mode is set to True
  48. def debug_message(message):
  49.   if debug_mode:
  50.     print(message)
  51.  
  52. def debug_pause(message):
  53.   # Currently not used
  54.   # To examine a specific portion of the program, add a debug_pause with a message
  55.   # Or, change an existing debug_message call to debug_pause to pause there
  56.   if debug_mode:
  57.     input(str(message) + "  Press enter to continue...")
  58.    
  59. # Get true color values for comparisons: get_pixel() sometimes returns different values
  60. sense.set_pixel(0,0, col_color)
  61. col_color = sense.get_pixel(0,0)
  62. sense.set_pixel(0,0, bg_color)
  63. bg_color = sense.get_pixel(0,0)
  64. sense.set_pixel(0,0, bird_color)
  65. bird_color = sense.get_pixel(0,0)
  66. sense.clear()
  67.  
  68. # Save setup in a tuple to restart game
  69. reset_state = (col_color, bg_color, bird_color, bird_y, bird_lives, columns, column_speed_base, speed, game_over)
  70.  
  71. ####
  72. # Game functions
  73. ####
  74.  
  75. def move_columns():
  76.   debug_message("Moving columns")
  77.   global columns
  78.   starting_cols = len(columns)
  79.  
  80.   # Shift x coordinate of colums to left
  81.   columns = [(c[0] -1, c[1], c[2]) for c in columns]
  82.  
  83.   # Add a new column if needed
  84.   if max([c[0] for c in columns]) == 4:
  85.     gap_size = randint(2,4)
  86.     row_start = randint(1 + gap_size, 6)
  87.     columns.append((7,row_start,gap_size))
  88.  
  89. def draw_column(col, custom_color = None):
  90.   debug_message("Drawing column")
  91.  
  92.   if custom_color:
  93.     back_color = custom_color
  94.   else:
  95.     back_color = bg_color
  96.  
  97.   # Construct a list of column color and background color tuples, then set those pixels
  98.   x, gap_start, gap_size = col
  99.   c = [col_color] * 8
  100.   c[gap_start - gap_size: gap_start] = [back_color] * gap_size
  101.   for y, color in enumerate(c):  
  102.     sense.set_pixel(x,y,color)
  103.  
  104. def draw_screen(custom_color = None):
  105.   debug_message("Drawing screen")
  106.  
  107.   if custom_color:
  108.     back_color = custom_color
  109.   else:
  110.     back_color = bg_color
  111.  
  112.   sense.clear(back_color)
  113.   # Filter out offscreen columns then draw visible columns
  114.   visible_cols = [c for c in columns if c[0] >= 0]
  115.   for c in visible_cols:
  116.     draw_column(c, back_color)  
  117.  
  118. def draw_bird(falling = True):
  119.   debug_message("drawing bird")
  120.   global bird_y, bird_lives, game_over
  121.   # Replace bird with upcoming background or column at x=4
  122.   sense.set_pixel(3,bird_y,sense.get_pixel(3, bird_y))
  123.  
  124.   if falling:
  125.     bird_y += speed
  126.  
  127.   # Stay onscreen
  128.   if bird_y > 7:
  129.     bird_y = 7
  130.   if bird_y < 0:
  131.     bird_y = 0
  132.  
  133.   # Collisions are when the bird moves onto a column
  134.   hit = sense.get_pixel(3, bird_y) == col_color
  135.   if hit:
  136.     flash_screen()
  137.     bird_lives -= 1
  138.     # ignore any keypresses here
  139.     sense.stick.get_events()
  140.  
  141.   # Draw bird lives
  142.   if bird_lives > 8:
  143.     # Can only draw 8 at a time
  144.     draw_lives = 8
  145.   else:
  146.     draw_lives = bird_lives
  147.    
  148.   for i in range(draw_lives):
  149.     sense.set_pixel(0,i, (200,200,200))
  150.  
  151.   game_over = bird_lives < 0
  152.  
  153.   # Draw bird in new position
  154.   sense.set_pixel(3,bird_y,bird_color)
  155.   debug_message("Bird drawn")
  156.  
  157. def flash_screen():
  158.   for i in range(3):
  159.     custom_color = ([randint(50, x) for x in [255,255,255]])
  160.     draw_screen(custom_color)
  161.     # Make sure bird is still visible
  162.     sense.set_pixel(3,bird_y,bird_color)
  163.     sleep(.1)
  164.   draw_screen()
  165.  
  166. ####
  167. # Main Game Loop
  168. ####
  169.  
  170. while True:
  171.  
  172.   if setup:
  173.     col_color, bg_color, bird_color, bird_y, bird_lives, columns, column_speed_base, speed, game_over = reset_state
  174.     # Initial screen setup
  175.     last_redraw = round(time(), 1) * column_interval
  176.     draw_screen()
  177.     draw_bird()
  178.    
  179.     # Clear joystick events
  180.     sense.stick.get_events()
  181.     column_speed = int(column_speed_base)
  182.    
  183.     setup = False
  184.  
  185.   column_tick = round(time(), 1) * column_interval
  186.  
  187.   if (column_tick % (column_interval - column_speed) == 0) and (column_tick > last_redraw):
  188.     # Enough time has passed: columns move
  189.     debug_message("Tick!")
  190.     speed = 1
  191.     # make columns faster if possible as game goes on
  192.     if column_interval > (column_speed + 2):
  193.       column_speed = column_speed_base + len(columns) // (column_interval * 3)
  194.  
  195.     move_columns()
  196.     draw_screen()
  197.     draw_bird()
  198.     debug_message("Tick length: " + str(column_tick - last_redraw) \
  199.                    + " Speed: " + str(column_speed)\
  200.                    + "Columns: " + str(len(columns)))
  201.     last_redraw = column_tick
  202.  
  203.   events = sense.stick.get_events()
  204.   if events:
  205.     for e in events:
  206.       debug_message("Processing joystick events")
  207.       if e.direction ==  up_key and e.action == pressed:
  208.         # User pressed up: move bird up and columns over
  209.         debug_message("Joystick up press detected")
  210.         move_columns()
  211.         draw_screen()
  212.         speed = -1
  213.         draw_bird()
  214.         moved = True
  215.         # Prevent double falls
  216.         last_redraw -= column_interval // 2
  217.   else:
  218.     moved = False
  219.    
  220.   if game_over:
  221.     flash_screen()
  222.      
  223.     # Score is number of columns survived
  224.     score = len(columns) - 2
  225.     sense.show_message(str(score) + "pts!", text_colour=(255,255,255))
  226.    
  227.     # Start over
  228.     setup = True
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top