Playing Flappy Astronaut Game on Raspberry Pi Sense HAT

Let's play Flappy Astronaut on Sense HAT.

Introduction

Have you enjoy playing snake games on Raspberry Pi Sense HAT? This time we try to make another game called Flappy Astronaut. This game is inspired by Flappy Bird.

Video

This tutorial shows how to create and play Flappy Astronaut game on Sense HAT using Raspberry Pi.

Hardware Preparation

This is the list of items used in the video.

Sample Program

This is python3 sample program. You can use it with Thonny Python IDE.

# References:
# https://projects.raspberrypi.org/en/projects/flappy-astronaut
from sense_hat import SenseHat
from random import randint
from time import sleep
sense = SenseHat()
RED = (255, 0, 0)
BLUE = (0, 0, 255)
YELLOW = (255, 255, 0)
DELAY_DECREASE = 0.02
def flatten(matrix):
flattened = [pixel for row in matrix for pixel in row]
return flattened
def gen_pipes(matrix):
for row in matrix:
row[1] = RED
gap = randint(1, 6)
matrix[gap][1] = BLUE
matrix[gap 1][1] = BLUE
matrix[gap + 1][1] = BLUE
return matrix
def move_pipes(matrix):
for row in matrix:
for i in range(7):
row[i] = row[i + 1]
row[1] = BLUE
return matrix
def draw_astronaut(event):
global y
global x
global score
global gameOver
sense.set_pixel(x, y, BLUE)
if event.action == "pressed":
if event.direction == "up" and y > 0 and gameOver == False:
y -= 1
elif event.direction == "down" and y < 7 and gameOver == False:
y += 1
elif event.direction == "right" and x < 7 and gameOver == False:
x += 1
elif event.direction == "left" and x > 0 and gameOver == False:
x -= 1
elif event.direction == "middle" and score > 0:
score = 0
sense.set_pixel(x, y, YELLOW)
def check_collision(matrix):
global gameOver
if matrix[y][x] == RED:
gameOver = True
return True
else:
return False
x = 0
y = 0
score = 0
gameDelay = 1
gameOver = False
sense.stick.direction_any = draw_astronaut
while True:
matrix = [[BLUE for column in range(8)] for row in range(8)]
gameDelay = 1
gameOver = False
while True:
matrix = gen_pipes(matrix)
if check_collision(matrix):
sleep(1)
break
for i in range(3):
matrix = move_pipes(matrix)
sense.set_pixels(flatten(matrix))
sense.set_pixel(x, y, YELLOW)
if check_collision(matrix):
sleep(1)
break
if i == 2:
score = score + 1
gameDelay = gameDelay DELAY_DECREASE
if gameDelay < 0.2:
gameDelay = 0.2
sleep(gameDelay)
while score:
sense.show_message("Score: {}".format(score2), text_colour=YELLOW)
# Press joystick middle button to play again

Thank You

References:

Thanks for reading this tutorial. If you have any technical inquiries, please post at Cytron Technical Forum.

Please be reminded, this tutorial is prepared for you to try and learn.
You are encouraged to improve the code for better application.

Leave a Comment

Your email address will not be published.

Share this Tutorial

Share on facebook
Share on whatsapp
Share on email
Share on print
Share on twitter
Share on pinterest
Share on facebook
Share on whatsapp
Share on email
Share on print
Share on twitter
Share on pinterest

Latest Tutorial

Automatic Dustbin Using REKA:BIT
Ultrasonic HC-SR04P Using Raspberry Pi Pico
Object Sense With Servo Using Maker Pi RP2040
Build Otto DIY Robot Using Maker Pi RP2040
Raspberry Pi RP2040 vs STM32F1 vs SAMD21G18 vs ESP32-S2
Tutorials of Cytron Technologies Scroll to Top