def win(): string = "You won! Score: %i" % (score) msg = led_matrix.LEDText(string, font_name='large') for i in range(len(string) * 6 + 15): led_matrix.fill(0) led_matrix.text(msg, (15 - i, 7)) led_matrix.show() sys.exit(0)
def lose(): string = "You lost! Score: %i" % (score) msg = led_matrix.LEDText(string, font_name='large') for i in range(len(string) * 6 + 15): led_matrix.fill(0) led_matrix.text(msg, (15 - i, 7)) led_matrix.show() # time.sleep(0.1) sys.exit(0)
def win(): GPIO.cleanup() text = "You Won in %is" % (int(time.time() - start_time)) msg = led_matrix.LEDText(text, font_name="large") for i in range(len(text) * 6 + 15): led_matrix.fill(0) led_matrix.text(msg, (15 - i, 7)) led_matrix.show() time.sleep(0.1) sys.exit()
def lose(): GPIO.cleanup() text = "Game Over!" msg = led_matrix.LEDText(text, font_name="large") for i in range(len(text) * 6 + 15): led_matrix.fill(0) led_matrix.text(msg, (15 - i, 7)) led_matrix.show() time.sleep(0.1) sys.exit(0)
def scroll_text(string): """Scrolls the given text""" msg = led_matrix.LEDText(string, font_name='large') prev_state = state for i in range(len(string)*6 + 15): if state != prev_state: return # leav if state has changed in the middle of scrolling led_matrix.erase() led_matrix.text(msg, (15 - i, 7)) led_matrix.show() time.sleep(0.1)
def __init__(self, menu_items, show_loading=False): items = [] # convert titles for i, item in enumerate(menu_items): if show_loading: led_matrix.erase() led_matrix.text(str(len(menu_items) - i)) led_matrix.show() f = os.path.join(os.path.dirname(os.path.abspath(__file__)), item[1]) if not os.path.isfile(f): raise IOError("File '" + f + "' could not be found.") items.append({ "title": item[0], "file": f, "text": led_matrix.LEDText(item[0], font_name="large") }) self.items = items self.scrolling_text_pos = 0 self.scrolling_text_clock = Menu.HOLD_CLOCK_TIME # clock used to slow down scrolling text self.scrolling_text_cycle = 5 # number of cycles between scrolling tick
from rstem import led_matrix import time led_matrix.init_grid() # This sets up the led matrix. It must be run before displaying anything. led_matrix.erase() # This clears the led matrix display incase anything is currently being displayed. # Scrolling the led matrix. =============================================== # Create a variable that holds information about the sprite my_text_sprite = led_matrix.LEDText("Hello World!") # Make a while loop that keeps looping forever while True: # by making the conditional True, the while loop will never break because True is always True! # Get the width of the text in pixels. text_width = my_text_sprite.width # this is the number of LEDs wide our my_text_sprite is. # Set the original x value to be the width of the led matrix display. x = led_matrix.width() # this is the number of LEDs wide the display is. # Make another while loop that keeps moving the text to the left. # - When x is less then -text_width, our text will not be on the display anymore. # - When this happens we want to stop this while loop and run through the outside while loop. while x > -text_width: # when x is less then -text_width, our text will not be on the display anymore # Erase the previous drawn text led_matrix.erase() # Draw the text at this current x position led_matrix.sprite(text, (x, 0))
# set button handler to physical buttons GPIO.setmode(GPIO.BCM) for button in [UP, DOWN, LEFT, RIGHT, SELECT, START, A, B]: GPIO.setup(button, GPIO.IN, pull_up_down=GPIO.PUD_UP) GPIO.add_event_detect(button, GPIO.FALLING, callback=button_handler, bouncetime=100) # notify of progress print("P90") sys.stdout.flush() # create intro title (a vertical display of "TETRIS") title = led_matrix.LEDText("S").rotate(90) for character in reversed("TETRI"): # append a 1 pixel wide (high) spacing title.append(led_matrix.LEDSprite(width=1, height=title.height)) # append next character title.append(led_matrix.LEDText(character).rotate(90)) # rotate title up title.rotate(-90) # notify menu we are ready for the led matrix print("READY") sys.stdout.flush() while True: # state when a piece is slowly moving down the display if curr_state == State.MOVINGDOWN:
curr_state = State.PLAYING GPIO.setmode(GPIO.BCM) for but in [A, START, SELECT]: GPIO.setup(but, GPIO.IN, pull_up_down=GPIO.PUD_UP) GPIO.add_event_detect(but, GPIO.FALLING, callback=button_handler, bouncetime=200) # notify of progress print("P70") sys.stdout.flush() title = led_matrix.LEDText("STACK-EM", font_name="large") # notify of progress print("P80") sys.stdout.flush() # create intro title (a vertical display of "STACKEM") title = led_matrix.LEDText("M").rotate(90) for character in reversed("STACKE"): # append a 1 pixel wide (high) spacing title.append(led_matrix.LEDSprite(width=1, height=title.height)) # append next character title.append(led_matrix.LEDText(character).rotate(90)) # rotate title up title.rotate(-90)
DOWN = 24 LEFT = 23 RIGHT = 18 START = 27 SELECT = 22 # states IN_MENU = 1 IN_GAME = 2 KONAMI = 3 curr_state = IN_MENU konami_number = 0 konami_code = [UP, UP, DOWN, DOWN, LEFT, RIGHT, LEFT, RIGHT, B, A] loading_text = led_matrix.LEDText("LOADING...") class Menu(object): HOLD_CLOCK_TIME = -15 # number of cycles to hold scrolling text def __init__(self, menu_items, show_loading=False): items = [] # convert titles for i, item in enumerate(menu_items): if show_loading: led_matrix.erase() led_matrix.text(str(len(menu_items) - i)) led_matrix.show() f = os.path.join(os.path.dirname(os.path.abspath(__file__)),
if len(snake.body) >= field.size: curr_state = State.WIN continue # if snake has got the apple, set up a new apple and start growing if snake.head() == field.apple: field.new_apple(snake) # create new apple score += 1 snake.growing = True # snake starts growing snake.grow_clock = GROW_CYCLES # reset grow clock time.sleep(.20) elif curr_state == State.IDLE: # display horizontal scrolling title title = led_matrix.LEDText("SNAKE", font_name="large") x_pos = led_matrix.width() - 1 y_pos = led_matrix.height() / 2 - title.height / 2 while x_pos > -title.width - 1: # break if state has changed, so we don't have to wait for it to finish if curr_state != State.IDLE: break led_matrix.erase() led_matrix.sprite(title, (x_pos, 1)) led_matrix.show() time.sleep(.1) x_pos -= 1 elif curr_state == State.RESET: snake = Snake() field = Field(led_matrix.width(), led_matrix.height())
if ship.collided(): state.skipto(GAME_OVER) if state.current() == WALL_SCENE: state.next_if_after(10) elif state.current() == WALL_2_ENEMY_SCENE: state.next_if_after(3) elif state.current() == ENEMY_SCENE: if len(enemies) == 0: # if all enemies killed state.next() elif state.current() == BIG_BOSS: state.next() elif state.current() == GAME_OVER: # Scroll "GAME OVER" message text = led_matrix.LEDText("GAME OVER!!!") pos_x, pos_y = (led_matrix.width(), 0) while -pos_x < text.width: if exit: break # break if user pressed the start button time.sleep(.1) led_matrix.erase() led_matrix.text(text, (pos_x, pos_y)) led_matrix.show() pos_x -= 1 state.next() t = next_tick - time.time() if t > 0: time.sleep(t)
print("P70") sys.stdout.flush() GPIO.setmode(GPIO.BCM) GPIO.setup(START, GPIO.IN, pull_up_down=GPIO.PUD_UP) GPIO.setup(SELECT, GPIO.IN, pull_up_down=GPIO.PUD_UP) # notify of progress print("P100") sys.stdout.flush() # notify menu we are ready for the led matrix print("READY") sys.stdout.flush() while True: if GPIO.input(START) == 0 or GPIO.input(SELECT) == 0: button.cleanup() led_matrix.cleanup() sys.exit(0) hour = time.strftime("%I", time.localtime()) hour = led_matrix.LEDText(hour) minute = time.strftime("%M", time.localtime()) minute = led_matrix.LEDText(minute) led_matrix.erase() led_matrix.text(hour) led_matrix.text(minute, (hour.width + 2, 0)) led_matrix.show()
RIGHT = 18 START = 27 SELECT = 22 # accelometer threshold THRESHOLD = 3 class State(object): PLAYING, IDLE, SCORE, EXIT = range(4) # starting variables state = State.IDLE field = None title = led_matrix.LEDText( "ASPIRIN - Press A to use accelometer or B to use buttons") # notify of progress print("P90") sys.stdout.flush() class Direction(object): LEFT, RIGHT, UP, DOWN = range(4) class Apple(object): def __init__(self, position): self.position = position def draw(self):