import time
import random
import pygame
import Adafruit_SSD1306
import RPi.GPIO as GPIO
# GPIO setup for joystick and buttons
JOY_X_PIN = 18
JOY_Y_PIN = 23
JOY_BTN_PIN = 24
BUTTON_SELECT_PIN = 27
GPIO.setmode(GPIO.BCM)
GPIO.setup(JOY_X_PIN, GPIO.IN)
GPIO.setup(JOY_Y_PIN, GPIO.IN)
GPIO.setup(JOY_BTN_PIN, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(BUTTON_SELECT_PIN, GPIO.IN, pull_up_down=GPIO.PUD_UP)
# OLED display setup
disp = Adafruit_SSD1306.SSD1306_128_64(rst=None)
disp.begin()
disp.clear()
disp.display()
width, height = disp.width, disp.height
# Pygame setup for music
pygame.mixer.init()
pygame.mixer.music.load("your_music_file.mp3")
# Define directions for falling objects
DIRECTIONS = ["Up", "Down", "Right", "Left"]
class FallingObjectsGame:
def __init__(self):
self.level = "hard" # Default level
self.lives = 3 # Default lives
self.points = 0
self.line_position = height - 10
self.falling_object = None # Only one falling object at a time
def display_objects(self):
disp.clear()
# Display falling object
if self.falling_object:
disp.setTextSize(1)
disp.setTextColor(1)
disp.setCursor(0, self.falling_object['y'])
disp.print(self.falling_object['direction'])
# Display line
disp.drawLine(0, self.line_position, width - 1, self.line_position, 1)
# Display game info
disp.setTextSize(1)
disp.setTextColor(1)
disp.setCursor(0, 0)
disp.print("Level: {}".format(self.level))
disp.setCursor(0, 10)
disp.print("Points: {}".format(self.points))
disp.setCursor(0, 20)
disp.print("Lives: {}".format(self.lives))
disp.display()
def start_game(self):
pygame.mixer.music.play(-1) # -1 for looping the music
while True:
# Add new falling object when the previous one reaches the bottom
if not self.falling_object:
self.falling_object = {'direction': random.choice(DIRECTIONS), 'y': 0}
# Update falling object's position
self.falling_object['y'] += 1
# Check if the object is on the line
if self.falling_object['y'] >= self.line_position:
# Check if the correct button or joystick direction is pressed
if self.check_input():
print("Correct direction!")
self.points += 1
else:
print("Wrong direction!")
self.lives -= 1
# Reset falling object
self.falling_object = None
# Check for game over
if self.lives <= 0:
self.game_over()
break
self.display_objects()
time.sleep(0.05)
def check_input(self):
# Check if the correct button or joystick direction is pressed
if self.falling_object['direction'] == "Up" and GPIO.input(JOY_Y_PIN) == GPIO.LOW:
return True
elif self.falling_object['direction'] == "Down" and GPIO.input(JOY_Y_PIN) == GPIO.HIGH:
return True
elif self.falling_object['direction'] == "Right" and GPIO.input(JOY_X_PIN) == GPIO.LOW:
return True
elif self.falling_object['direction'] == "Left" and GPIO.input(JOY_X_PIN) == GPIO.HIGH:
return True
else:
return False
def game_over(self):
disp.clear()
disp.setCursor(0, height // 2 - 10)
disp.setTextSize(2)
disp.setTextColor(1)
disp.print("Game Over")
disp.display()
time.sleep(10) # Display "Game Over" for 10 seconds
def select_level(self):
levels = ["hard", "mid-hard", "extra-hard"]
current_level_index = 0
while True:
disp.clear()
disp.setCursor(0, height // 2 - 10)
disp.setTextSize(1)
disp.setTextColor(1)
disp.print("Select Level:")
disp.setCursor(0, height // 2 + 10)
disp.print(levels[current_level_index])
disp.display()
# Check for joystick press to change the level
if GPIO.input(BUTTON_SELECT_PIN) == GPIO.LOW:
self.level = levels[current_level_index]
break
# Check for joystick press to navigate between levels
if GPIO.input(JOY_X_PIN) == GPIO.LOW:
current_level_index = (current_level_index + 1) % len(levels)
time.sleep(0.2)
def select_lives(self):
lives_options = list(range(1, 11)) + ["No Limit"]
current_lives_index = 0
while True:
disp.clear()
disp.setCursor(0, height // 2 - 10)
disp.setTextSize(1)
disp.setTextColor(1)
disp.print("Select Lives:")
disp.setCursor(0, height // 2 + 10)
disp.print(str(lives_options[current_lives_index]))
disp.display()
# Check for joystick press to set lives
if GPIO.input(BUTTON_SELECT_PIN) == GPIO.LOW:
if current_lives_index < 10:
self.lives = lives_options[current_lives_index]
break
# Check for joystick press to navigate between lives options
if GPIO.input(JOY_X_PIN) == GPIO.LOW:
current_lives_index = (current_lives_index + 1) % len(lives_options)
time.sleep(0.2)
if __name__ == "__main__":
game = FallingObjectsGame()
while True:
game.select_level()
game.select_lives()
game.start_game()