def draw(self): led_matrix.line((self.x_pos, 0), (self.x_pos, led_matrix.height() - 1)) # draw the next piece next_piece.draw(pos=(self.x_pos + 3, led_matrix.height() - 5)) # draw the score (aligned nicely) if score < 10: led_matrix.text(str(score), (self.x_pos + 5, 1)) else: led_matrix.text(str(score), (self.x_pos + 1, 1))
def draw(self): led_matrix.line((self.x_pos, 0), (self.x_pos, led_matrix.height()-1)) # draw the next piece next_piece.draw(pos=(self.x_pos + 3, led_matrix.height() - 5)) # draw the score (aligned nicely) if score < 10: led_matrix.text(str(score), (self.x_pos + 5, 1)) else: led_matrix.text(str(score), (self.x_pos + 1, 1))
def run_selected_item(self): # start child process selected = self.selected_item() GPIO_cleanup() proc = subprocess.Popen([sys.executable, selected["file"]], stdout=subprocess.PIPE, close_fds=False) # make proc.stdout a non-blocking file fd = proc.stdout.fileno() fl = fcntl.fcntl(fd, fcntl.F_GETFL) fcntl.fcntl(fd, fcntl.F_SETFL, fl | os.O_NONBLOCK) finished = False percentage = 0 # percentage of loading # display loading screen until child process wants the led matrix while not proc.poll() and not finished: x_pos = led_matrix.width() y_pos = int(led_matrix.height() / 2) - int(loading_text.height) while x_pos >= -loading_text.width: led_matrix.erase() # print "LOADING..." led_matrix.sprite(loading_text, (x_pos, y_pos)) # print progress bar led_matrix.rect((0, y_pos + int(loading_text.height) + 2), (int(percentage * led_matrix.width()), 3), fill=True) led_matrix.show() x_pos -= 1 # read stdout of the game process try: data = proc.stdout.readline() except: data = False # check if child process is ready to take control of matrix if data: game_printout = data.decode("utf-8") # update progress bar if "P**" is given if game_printout[0] == "P" and game_printout[1:-1].isdigit( ): new_percentage = int(game_printout[1:-1]) if 0 <= new_percentage <= 100: percentage = int( new_percentage) / 100 # update percentage # break out of while loop to let game take over led matrix elif game_printout == "READY\n": finished = True break time.sleep(0.05) led_matrix.erase() # clear the display led_matrix.show() # TODO: find out if we need to clean up led matrix too # wait till child process finishes proc.wait() GPIO_setup() # resetup GPIO
def __init__(self, shape, pos=None): if not valid_shape(shape): raise ValueError("Not a valid shape") if pos is None: self.pos = (int(stack.width/2 - 1), int(led_matrix.height())) else: self.pos = pos self.sprite = shape_sprites[shape].copy() # get a copy of sprite
def __init__(self, position=None, accel=False): # set position to be center of screen if position is not given if position is None: self.position = (int(led_matrix.width()/2), int(led_matrix.height()/2)) else: self.position = position self.accel = accel # True if controls are the accelometer, False if controls are buttons
def __init__(self, shape, pos=None): if not valid_shape(shape): raise ValueError("Not a valid shape") if pos is None: self.pos = (int(stack.width / 2 - 1), int(led_matrix.height())) else: self.pos = pos self.sprite = shape_sprites[shape].copy() # get a copy of sprite
def new_apple(self): # set up list of x and y choices x_pos = list(range(led_matrix.width())) y_pos = list(range(led_matrix.height())) # remove the position that player is currently in del x_pos[self.player.position[0]] del y_pos[self.player.position[1]] self.apple = Apple((random.choice(x_pos), random.choice(y_pos)))
def __init__(self, position=None, accel=False): # set position to be center of screen if position is not given if position is None: self.position = (int(led_matrix.width() / 2), int(led_matrix.height() / 2)) else: self.position = position self.accel = accel # True if controls are the accelometer, False if controls are buttons
def get_num_neighbors(curr_gen, x, y): """Returns the number of (alive) neighbors of given pixel""" count = 0 for j in range(y-1, y+2): for i in range(x-1, x+2): if not(i == x and j == y): # don't count itself if i >= 0 and i < led_matrix.width() and j >= 0 and j < led_matrix.height(): if curr_gen[j][i] == 0xF: count += 1 return count
def __init__(self, player): self.player = player empty_strikers = set() # initialize empty strikers for x_pos in range(led_matrix.width()): empty_strikers.add(Striker((x_pos, 0), Direction.UP)) for y_pos in range(led_matrix.height()): empty_strikers.add(Striker((0, y_pos), Direction.RIGHT)) self.empty_strikers = empty_strikers # strikers not used yet self.strikers = set() # active strikers self.apple = None
def get_num_neighbors(curr_gen, x, y): """Returns the number of (alive) neighbors of given pixel""" count = 0 for j in range(y - 1, y + 2): for i in range(x - 1, x + 2): if not (i == x and j == y): # don't count itself if i >= 0 and i < led_matrix.width( ) and j >= 0 and j < led_matrix.height(): if curr_gen[j][i] == 0xF: count += 1 return count
def run_selected_item(self): # start child process selected = self.selected_item() GPIO_cleanup() proc = subprocess.Popen([sys.executable, selected["file"]], stdout=subprocess.PIPE, close_fds=False) # make proc.stdout a non-blocking file fd = proc.stdout.fileno() fl = fcntl.fcntl(fd, fcntl.F_GETFL) fcntl.fcntl(fd, fcntl.F_SETFL, fl | os.O_NONBLOCK) finished = False percentage = 0 # percentage of loading # display loading screen until child process wants the led matrix while not proc.poll() and not finished: x_pos = led_matrix.width() y_pos = int(led_matrix.height()/2) - int(loading_text.height) while x_pos >= -loading_text.width: led_matrix.erase() # print "LOADING..." led_matrix.sprite(loading_text, (x_pos, y_pos)) # print progress bar led_matrix.rect((0,y_pos + int(loading_text.height) + 2), (int(percentage*led_matrix.width()), 3), fill=True) led_matrix.show() x_pos -= 1 # read stdout of the game process try: data = proc.stdout.readline() except: data = False # check if child process is ready to take control of matrix if data: game_printout = data.decode("utf-8") # update progress bar if "P**" is given if game_printout[0] == "P" and game_printout[1:-1].isdigit(): new_percentage = int(game_printout[1:-1]) if 0 <= new_percentage <= 100: percentage = int(new_percentage)/100 # update percentage # break out of while loop to let game take over led matrix elif game_printout == "READY\n": finished = True break time.sleep(0.05) led_matrix.erase() # clear the display led_matrix.show() # TODO: find out if we need to clean up led matrix too # wait till child process finishes proc.wait() GPIO_setup() # resetup GPIO
def move(self, direction): if direction == Direction.UP: if self.position[1] < led_matrix.height() - 1: self.position = (self.position[0], self.position[1] + 1) elif direction == Direction.DOWN: if self.position[1] > 0: self.position = (self.position[0], self.position[1] - 1) elif direction == Direction.LEFT: if self.position[0] > 0: self.position = (self.position[0] - 1, self.position[1]) elif direction == Direction.RIGHT: if self.position[0] < led_matrix.width() - 1: self.position = (self.position[0] + 1, self.position[1]) else: raise ValueError("Invalid direction given.")
def move(self, direction): if direction == Direction.UP: if self.position[1] < led_matrix.height()-1: self.position = (self.position[0], self.position[1]+1) elif direction == Direction.DOWN: if self.position[1] > 0: self.position = (self.position[0], self.position[1]-1) elif direction == Direction.LEFT: if self.position[0] > 0: self.position = (self.position[0]-1, self.position[1]) elif direction == Direction.RIGHT: if self.position[0] < led_matrix.width()-1: self.position = (self.position[0]+1, self.position[1]) else: raise ValueError("Invalid direction given.")
def move(self): # check if the striker hit the wall and needs to bounce back if self.direction == Direction.LEFT and self.position[0] == 0: self.direction = Direction.RIGHT elif self.direction == Direction.RIGHT and self.position[0] == led_matrix.width()-1: self.direction = Direction.LEFT elif self.direction == Direction.DOWN and self.position[1] == 0: self.direction = Direction.UP elif self.direction == Direction.UP and self.position[1] == led_matrix.height()-1: self.direction = Direction.DOWN if self.direction == Direction.LEFT: self.position = (self.position[0]-1, self.position[1]) elif self.direction == Direction.RIGHT: self.position = (self.position[0]+1, self.position[1]) elif self.direction == Direction.DOWN: self.position = (self.position[0], self.position[1]-1) elif self.direction == Direction.UP: self.position = (self.position[0], self.position[1]+1)
def move(self): # check if the striker hit the wall and needs to bounce back if self.direction == Direction.LEFT and self.position[0] == 0: self.direction = Direction.RIGHT elif self.direction == Direction.RIGHT and self.position[ 0] == led_matrix.width() - 1: self.direction = Direction.LEFT elif self.direction == Direction.DOWN and self.position[1] == 0: self.direction = Direction.UP elif self.direction == Direction.UP and self.position[ 1] == led_matrix.height() - 1: self.direction = Direction.DOWN if self.direction == Direction.LEFT: self.position = (self.position[0] - 1, self.position[1]) elif self.direction == Direction.RIGHT: self.position = (self.position[0] + 1, self.position[1]) elif self.direction == Direction.DOWN: self.position = (self.position[0], self.position[1] - 1) elif self.direction == Direction.UP: self.position = (self.position[0], self.position[1] + 1)
def draw(self): # display menu items pos_y = 0 selected_item = self.selected_item() # display all other items regularly for item in self.items: if pos_y >= led_matrix.height(): # don't diplay items outside of display break if item["title"] == selected_item["title"]: # display selected text scrolling x = self.scrolling_text_pos led_matrix.sprite(selected_item["text"], (x, pos_y)) if self.scrolling_text_clock == self.scrolling_text_cycle: self.scrolling_text_clock = 0 if self.scrolling_text_pos < -selected_item["text"].width: self.scrolling_text_pos = led_matrix.width() - 1 else: self.scrolling_text_pos -= 1 self.scrolling_text_clock += 1 else: led_matrix.sprite(item["text"], (0, pos_y)) pos_y += item["text"].height + 1
def move(self): """Moves the snake in the give direction (if it can). Snake will grow as it moves. Need to use remove_tail() afterwards if no growth wanted. Returns if it was able to succesfully move.""" head_x, head_y = self.head() direction = snake.direction if direction == Direction.LEFT: if head_x == 0: return False new_point = (head_x - 1, head_y) elif direction == Direction.RIGHT: if head_x == led_matrix.width() - 1: return False new_point = (head_x + 1, head_y) elif direction == Direction.UP: if head_y == led_matrix.height() - 1: return False new_point = (head_x, head_y + 1) elif direction == Direction.DOWN: if head_y == 0: return False new_point = (head_x, head_y - 1) self.body = [new_point] + self.body # add as new head return True
def draw(self): # display menu items pos_y = 0 selected_item = self.selected_item() # display all other items regularly for item in self.items: if pos_y >= led_matrix.height( ): # don't diplay items outside of display break if item["title"] == selected_item["title"]: # display selected text scrolling x = self.scrolling_text_pos led_matrix.sprite(selected_item["text"], (x, pos_y)) if self.scrolling_text_clock == self.scrolling_text_cycle: self.scrolling_text_clock = 0 if self.scrolling_text_pos < -selected_item["text"].width: self.scrolling_text_pos = led_matrix.width() - 1 else: self.scrolling_text_pos -= 1 self.scrolling_text_clock += 1 else: led_matrix.sprite(item["text"], (0, pos_y)) pos_y += item["text"].height + 1
if curr_state == IN_MENU: led_matrix.erase() menu.draw() led_matrix.show() time.sleep(0.01) elif curr_state == IN_GAME: menu.run_selected_item() # run game and wait for it to die curr_state = IN_MENU elif curr_state == KONAMI: from random import shuffle, randint words = ["Brian", "Jason", "Jon", "Joe", "Steph", "Jed", "Tess"] shuffle(words) raspberrySTEM = "RaspberrySTEM" for name in words: sprite = led_matrix.LEDText(name, font_name="large") y_pos = randint(0,led_matrix.height()-sprite.height) x_pos = led_matrix.width() while x_pos >= -sprite.width: led_matrix.erase() led_matrix.sprite(sprite, (x_pos, y_pos)) led_matrix.show() x_pos -= 1 time.sleep(.05) logo = led_matrix.LEDText(raspberrySTEM, font_name="large") y_pos = int(led_matrix.height()/2) - int(logo.height/2) x_pos = led_matrix.width() while x_pos >= -logo.width: led_matrix.erase() led_matrix.sprite(logo, (x_pos, y_pos)) led_matrix.show()
# 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: # up speed if score is a multiple of 5 if score != 0 and score % 5 == 0: new_speed = init_speed - (score/5*0.1) if new_speed > 0: speed = new_speed # check if stack hit the top of display if stack.height() >= led_matrix.height() - 1: curr_state = State.DONE continue # check if piece can't move down, and if so, add piece to stack and start blinking any full lines if not curr_piece.can_movedown(): stack.add(curr_piece) # add piece to stack curr_piece = None # piece is no longer curr_piece blinking_clock = BLINKING_TIME # set up blinking clock curr_state = State.BLINKING # goto blinking state continue # otherwise move piece down curr_piece.movedown() # show screen
# create flag to indicate to display some dice initially on start up just_started = True # get base_elevation base_elevation = accel.angles()[2] # set change in angle/acceleration needed to roll dice THRESHOLD = 20 # notify menu we are ready for the led matrix print("READY") sys.stdout.flush() while True: # exit if start button is pressed if GPIO.input(START) == 0 or GPIO.input(SELECT) == 0: led_matrix.cleanup() GPIO.cleanup() sys.exit(0) # roll dice if A button is pressed or accelerometer detects steep enough angle if just_started or GPIO.input(A) == 0 or abs(accel.angles()[2] - base_elevation) > THRESHOLD: led_matrix.erase() # clear old dice values # set a new random die at each matrix for y in range(0, led_matrix.height(), 8): for x in range(0, led_matrix.width(), 8): led_matrix.sprite(random.choice(dice), (x+1,y+1)) just_started = False led_matrix.show()
self.origin = [self.origin[0], self.origin[1] - 1] else: raise RuntimeError("Invalid Direction") # SETUP ========================== # setup led matrix #led_matrix.init_grid(angle=270) led_matrix.init_matrices([(0, 8), (8, 8), (8, 0), (0, 0)]) # notify of progress print("P60") sys.stdout.flush() HEIGHT = led_matrix.height() WIDTH = led_matrix.width() if WIDTH > 8: LEFT_EDGE = 3 RIGHT_EDGE = 12 else: LEFT_EDGE = -1 RIGHT_EDGE = WIDTH # initialize variables curr_state = State.IDLE # current state used for state machine blocks = [] # current block elements on screen start_width = 3 # pixel width of block on start start_speed = 5 # current speed on start (in pixel/sec) change_level = int(HEIGHT *
row.append(0xF) # add an alive pixel else: row.append(0x0) # add a dead pixel grid.append(row) return grid def draw_grid(): """Draws the current generation to led_matrix.""" for y in range(num_rows): for x in range(num_cols): led_matrix.point(x, y, curr_gen[y][x]) # whole game loop while True: # variables num_rows = led_matrix.height() num_cols = led_matrix.width() # notify of progress print("P80") sys.stdout.flush() curr_gen = random_grid(num_cols, num_rows) # notify of progress print("P90") sys.stdout.flush() next_gen = [[0 for i in range(num_cols)] for j in range(num_rows)] # TODO allow sprite input instead of random grid? # notify menu we are ready for the led matrix print("READY")
# display on matrix led_matrix.show() time.sleep(1./speed) # move all pipes to the left one at certain interval # the certain interval allows the pipes to be slower than the bird if pipe_clock % pipe_interval == 0: for pipe in pipes: pipe.move_left() # add a new pipe if pipe_clock % pipe_spacing == 0: # set hole opening to be random, but not too far away from the previous one THRESHOLD = 4 if len(pipes) != 0: max_opening = min(pipes[-1].opening_location + THRESHOLD, led_matrix.height() - 4) min_opening = max(pipes[-1].opening_location - THRESHOLD, 1) else: max_opening = led_matrix.height() - 4 min_opening = 1 opening_location = randint(min_opening, max_opening) pipes.append(Pipe(opening_location=opening_location)) # increment pipe_clock indefinitly, (we will never hit the max) pipe_clock += 1 # check for collision for pipe in reversed(pipes): # stop checking when we run off the screen if pipe.x_position < 0: break
row.append(0x0) # add a dead pixel grid.append(row) return grid def draw_grid(): """Draws the current generation to led_matrix.""" for y in range(num_rows): for x in range(num_cols): led_matrix.point(x, y, curr_gen[y][x]) # whole game loop while True: # variables num_rows = led_matrix.height() num_cols = led_matrix.width() # notify of progress print("P80") sys.stdout.flush() curr_gen = random_grid(num_cols, num_rows) # notify of progress print("P90") sys.stdout.flush() next_gen = [[0 for i in range(num_cols)] for j in range(num_rows)] # TODO allow sprite input instead of random grid? # notify menu we are ready for the led matrix print("READY")
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()) score = 0 led_matrix.erase()
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()) score = 0 led_matrix.erase()
# set up gpio inputs GPIO.setmode(GPIO.BCM) for g in [A, LEFT, DOWN, UP, RIGHT, START, SELECT]: GPIO.setup(g, GPIO.IN, pull_up_down = GPIO.PUD_UP) GPIO.add_event_detect(g, GPIO.FALLING, callback=button_handler, bouncetime=100) # notify of progress print("P70") sys.stdout.flush() # set up led matrix #led_matrix.init_grid(2,2) led_matrix.init_matrices([(0,8),(8,8),(8,0),(0,0)]) WIDTH = led_matrix.width() HEIGHT = led_matrix.height() # define walls wall_params = [ { # WALL_SCENE 0 "more_cols_min":5, "more_cols_max":8, "tunnel_min":5, "tunnel_max":HEIGHT - 3, "start_rate":2, # slow "period":5, }, { # WALL_SCENE 1 "more_cols_min":3, "more_cols_max":6,
def draw(self): for offset in range(self.width): x = self.x_position + offset led_matrix.line((x, 0), (x, self.opening_location - 1)) led_matrix.line((x, self.opening_location + self.opening_height), (x, led_matrix.height()))
field.new_apple() ret = field.add_striker() if ret == False: state = State.SCORE time.sleep(.1) elif state == State.IDLE: x = led_matrix.width() while x > -title.width: # break if state has changed, (don't wait for scroll to finish) if state != State.IDLE: break led_matrix.erase() led_matrix.sprite(title, (x, led_matrix.height()/2 - (title.height/2))) led_matrix.show() x -= 1 time.sleep(.05) elif state == State.SCORE: led_matrix.erase() led_matrix.text(str(len(field.strikers))) # led_matrix.text(str(len(field.horizontal_strikers) + len(field.vertical_strikers))) led_matrix.show() elif state == State.EXIT: GPIO.cleanup() led_matrix.cleanup() sys.exit(0) else:
GPIO.setup(g, GPIO.IN, pull_up_down=GPIO.PUD_UP) GPIO.add_event_detect(g, GPIO.FALLING, callback=button_handler, bouncetime=100) # notify of progress print("P70") sys.stdout.flush() # set up led matrix #led_matrix.init_grid(2,2) led_matrix.init_matrices([(0, 8), (8, 8), (8, 0), (0, 0)]) WIDTH = led_matrix.width() HEIGHT = led_matrix.height() # define walls wall_params = [ { # WALL_SCENE 0 "more_cols_min": 5, "more_cols_max": 8, "tunnel_min": 5, "tunnel_max": HEIGHT - 3, "start_rate": 2, # slow "period": 5, }, { # WALL_SCENE 1 "more_cols_min": 3,
led_matrix.show() time.sleep(1. / speed) # move all pipes to the left one at certain interval # the certain interval allows the pipes to be slower than the bird if pipe_clock % pipe_interval == 0: for pipe in pipes: pipe.move_left() # add a new pipe if pipe_clock % pipe_spacing == 0: # set hole opening to be random, but not too far away from the previous one THRESHOLD = 4 if len(pipes) != 0: max_opening = min(pipes[-1].opening_location + THRESHOLD, led_matrix.height() - 4) min_opening = max(pipes[-1].opening_location - THRESHOLD, 1) else: max_opening = led_matrix.height() - 4 min_opening = 1 opening_location = randint(min_opening, max_opening) pipes.append(Pipe(opening_location=opening_location)) # increment pipe_clock indefinitly, (we will never hit the max) pipe_clock += 1 # check for collision for pipe in reversed(pipes): # stop checking when we run off the screen if pipe.x_position < 0:
while True: if curr_state == IN_MENU: led_matrix.erase() menu.draw() led_matrix.show() time.sleep(0.01) elif curr_state == IN_GAME: menu.run_selected_item() # run game and wait for it to die curr_state = IN_MENU elif curr_state == KONAMI: from random import shuffle, randint words = ["Brian", "Jason", "Jon", "Joe", "Steph", "Jed", "Tess"] shuffle(words) for name in words: sprite = led_matrix.LEDText(name, font_name="large") y_pos = randint(0, led_matrix.height() - sprite.height) x_pos = led_matrix.width() while x_pos >= -sprite.width: led_matrix.erase() led_matrix.sprite(sprite, (x_pos, y_pos)) led_matrix.show() x_pos -= 1 time.sleep(.05) logo = led_matrix.LEDText("Ready Set STEM", font_name="large") y_pos = int(led_matrix.height() / 2) - int(logo.height / 2) x_pos = led_matrix.width() while x_pos >= -logo.width: led_matrix.erase() led_matrix.sprite(logo, (x_pos, y_pos)) led_matrix.show()
# 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: # up speed if score is a multiple of 5 if score != 0 and score % 5 == 0: new_speed = init_speed - (score / 5 * 0.1) if new_speed > 0: speed = new_speed # check if stack hit the top of display if stack.height() >= led_matrix.height() - 1: curr_state = State.DONE continue # check if piece can't move down, and if so, add piece to stack and start blinking any full lines if not curr_piece.can_movedown(): stack.add(curr_piece) # add piece to stack curr_piece = None # piece is no longer curr_piece blinking_clock = BLINKING_TIME # set up blinking clock curr_state = State.BLINKING # goto blinking state continue # otherwise move piece down curr_piece.movedown() # show screen
def draw(self): for offset in range(self.width): x = self.x_position + offset led_matrix.line((x, 0), (x, self.opening_location-1)) led_matrix.line((x, self.opening_location + self.opening_height), (x, led_matrix.height()))
elif field.player_collided_with_apple(): field.new_apple() ret = field.add_striker() if ret == False: state = State.SCORE time.sleep(.1) elif state == State.IDLE: x = led_matrix.width() while x > -title.width: # break if state has changed, (don't wait for scroll to finish) if state != State.IDLE: break led_matrix.erase() led_matrix.sprite(title, (x, led_matrix.height() / 2 - (title.height / 2))) led_matrix.show() x -= 1 time.sleep(.05) elif state == State.SCORE: led_matrix.erase() led_matrix.text(str(len(field.strikers))) # led_matrix.text(str(len(field.horizontal_strikers) + len(field.vertical_strikers))) led_matrix.show() elif state == State.EXIT: GPIO.cleanup() led_matrix.cleanup() sys.exit(0)
elif direction == Direction.DOWN: self.origin = [self.origin[0], self.origin[1]-1] else: raise RuntimeError("Invalid Direction") # SETUP ========================== # setup led matrix #led_matrix.init_grid(angle=270) led_matrix.init_matrices([(0,8),(8,8),(8,0),(0,0)]) # notify of progress print("P60") sys.stdout.flush() HEIGHT = led_matrix.height() WIDTH = led_matrix.width() if WIDTH > 8: LEFT_EDGE = 3 RIGHT_EDGE = 12 else: LEFT_EDGE = -1 RIGHT_EDGE = WIDTH # initialize variables curr_state = State.IDLE # current state used for state machine blocks = [] # current block elements on screen start_width = 3 # pixel width of block on start start_speed = 5 # current speed on start (in pixel/sec) change_level = int(HEIGHT*(1/3.)) # number of blocks before upping difficulty