1] > 15: missles.remove(self) try: # Start game while True: # Clear previous framebuffer led_matrix.fill(0) # Update and redraw missles for m in missles: m.update() led_matrix.point(m.pos[0], m.pos[1]) # Get angles from accelerometer data = accel.angles() # Generate smooth movement data using IIR filter, and make a 1/4 turn move # the player to the edge of the screen player_pos[0] = player_pos[0] + (clamp(data[0] * 8 * 4 / 90 + 7) - player_pos[0]) * 0.1 # Draw player led_matrix.point(int(round(player_pos[0])), int(round(player_pos[1]))) # Show framebuffer led_matrix.show() # Delay one game tick, in this case 1ms time.sleep(0.001)
#Game entity data player_pos = [7, 0] #Function to clamp data to the size of the screen def clamp(x): return max(0, min(x, 15)) try: # Start game while True: # Clear previous framebuffer led_matrix.fill(0) # Get angles from accelerometer data = accel.angles() # Generate smooth movement data using IIR filter, and make a 1/4 turn move # the player to the edge of the screen player_pos[0] = player_pos[0] + (clamp(data[0]*8*4/90 + 7) - player_pos[0])*0.1 # Draw player led_matrix.point(int(round(player_pos[0])), int(round(player_pos[1]))) # Show framebuffer led_matrix.show() # Delay one game tick, in this case 1ms time.sleep(0.001) #Stop if player hits Ctrl-C
#Initialize bricks for x in range(4): for y in range(3): bricks.append(Brick([x*4, 15 - y*3 - 1],[3, 2])) #Initialize ball ball = Ball([8,1],[1,1]) #Initialize player movement data velocity = 0.0 player_pos = 7.0 alpha = 0.1 while True: # Clear frame buffer led_matrix.fill(0) # Get angle data angles = accel.angles() # Simple lowpass filter for velocity data velocity = velocity*alpha + (angles[0]*2*8/90)*(1 - alpha) # Move player's paddle player_pos = player_pos + velocity player_pos = clamp(player_pos, 0, 15 - (p.size[0] - 1)) # If the paddle hits the edge the velocity can't be into the edge if int(player_pos) <= 0 and velocity < 0: velocity = 0 elif int(player_pos) >= 15 and velocity > 0: velcoity = 0 # Move player p.move(player_pos) # Update physics if ball_tick == 0: ball.update()
bricks.append(Brick([x*4, 15 - y*3 - 1],[3, 2])) #Initialize ball ball = Ball([8,1],[1,1]) #Declare paddle for use in ball class p = Paddle([7, 0], [4, 0]) state = State.PLAYING elif state == State.IDLE: # display breakout title scroll_text("BREAKOUT") elif state == State.PLAYING: # Clear frame buffer led_matrix.fill(0) # Get angle data angles = accel.angles() # Simple lowpass filter for velocity data velocity = velocity*alpha + (angles[0]*2*8/90)*(1 - alpha) # Move player's paddle player_pos = player_pos + velocity player_pos = clamp(player_pos, 0, 15 - (p.size[0] - 1)) # If the paddle hits the edge the velocity can't be into the edge if int(player_pos) <= 0 and velocity < 0: velocity = 0 elif int(player_pos) >= 15 and velocity > 0: velcoity = 0 # Move player p.move(player_pos) # Update physics if ball_tick == 0: ball.update()
# Import accel module from rstem import accel from time import sleep # Initialze accelerometer on i2c bus 1, on early Pi's this may be 0 instead accel.init(1) # Loop to display values while True: force = accel.read() # Returns a tuple of the form (x, y, z) acceleration angles = accel.angles() # Returns a tuple of the form (roll, pitch, elevation) force # Print values in a nicely formatted way print( "X: {0[0]: < 8.4f} Y: {0[1]: < 8.4f} Z: {0[2]: < 8.4f} Roll: {1[0]: < 8.4f} Pitch: {1[1]: < 8.4f} Elevation: {1[2]: < 8.4f}".format( force, angles ) ) sleep(0.25)
# setup start button to exit game GPIO.setup(START, GPIO.IN, pull_up_down=GPIO.PUD_UP) GPIO.setup(SELECT, GPIO.IN, pull_up_down=GPIO.PUD_UP) # setup A button to roll dice GPIO.setup(A, GPIO.IN, pull_up_down=GPIO.PUD_UP) # notify of progress print("P90") sys.stdout.flush() # 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)
def main(stdscr): # Clear screen curses.noecho() curses.cbreak() curses.curs_set(0) stdscr.addstr("Accelerometer Tester", curses.A_REVERSE) stdscr.chgat(-1, curses.A_REVERSE) #stdscr.addstr(curses.LINES-1, 0, "Press 'Q' to quit") stdscr.nodelay(1) # make getch() non-blocking # set up window to bounce ball ball_win = curses.newwin(curses.LINES-2, curses.COLS, 1, 0) ball_win.box() #ball_win.addch(curses.LINES-1,curses.COLS-1, ord('F')) # Update the internal window data structures stdscr.noutrefresh() ball_win.noutrefresh() # Redraw the screen curses.doupdate() box_LINES, box_COLS = ball_win.getmaxyx() ball_x, ball_y = (int(box_COLS/2), int(box_LINES/2)) while True: # Quit if 'Q' was pressed c = stdscr.getch() if c == ord('Q') or c == ord('q'): break # remove previous location of ball ball_win.addch(ball_y, ball_x, ord(' ')) angles = accel.angles() x_diff = angles[0] y_diff = angles[1] stdscr.addstr(curses.LINES-1, 0, "Press 'Q' to quit | Roll: {0: < 8.4f} Pitch: {1: < 8.4f}".format(x_diff, y_diff)) if x_diff > THRESHOLD and ball_x < box_COLS-2: ball_x += 1 elif x_diff < -THRESHOLD and ball_x > 1: ball_x -= 1 if y_diff > THRESHOLD and ball_y > 1: ball_y -= 1 elif y_diff < -THRESHOLD and ball_y < box_LINES-2: ball_y += 1 # update ball location ball_win.addch(ball_y, ball_x, ord('0')) # Refresh the windows from the bottom up stdscr.noutrefresh() ball_win.noutrefresh() curses.doupdate() # Restore the terminal curses.nocbreak() curses.echo() curses.curs_set(1) curses.endwin()
#Import accel module from rstem import accel from time import sleep #Initialze accelerometer on i2c bus 1, on early Pi's this may be 0 instead accel.init(1) #Loop to display values while True: force = accel.read() #Returns a tuple of the form (x, y, z) acceleration angles = accel.angles() #Returns a tuple of the form (roll, pitch, elevation) force # Print values in a nicely formatted way print("X: {0[0]: < 8.4f} Y: {0[1]: < 8.4f} Z: {0[2]: < 8.4f} Roll: {1[0]: < 8.4f} Pitch: {1[1]: < 8.4f} Elevation: {1[2]: < 8.4f}".format(force, angles)) sleep(0.25)