Advertisement
MrLunk

Pygame USB-serial keyboard control - Raspberry Pi to arduino

Jan 27th, 2018
194
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.79 KB | None | 0 0
  1. import pygame
  2. import serial
  3.  
  4. ser = serial.Serial('/dev/ttyACM0', 9600)
  5.  
  6. # Define some colors
  7. BLACK = (0, 0, 0)
  8. WHITE = (255, 255, 255)
  9. GREEN = (0, 255, 0)
  10. RED = (255, 0, 0)
  11.  
  12.  
  13. def draw_stick_figure(screen, x, y):
  14.     # Head
  15.     pygame.draw.ellipse(screen, BLACK, [1 + x, y, 10, 10], 0)
  16.  
  17.  
  18.  
  19. # Setup
  20. pygame.init()
  21.  
  22. # Set the width and height of the screen [width,height]
  23. size = [320, 240]
  24. screen = pygame.display.set_mode(size)
  25.  
  26. pygame.display.set_caption("LunkVehikel Control Pannel")
  27.  
  28. # Loop until the user clicks the close button.
  29. done = False
  30.  
  31. # Used to manage how fast the screen updates
  32. clock = pygame.time.Clock()
  33.  
  34. # Hide the mouse cursor
  35. pygame.mouse.set_visible(0)
  36.  
  37. # Speed in pixels per frame
  38. x_speed = 0
  39. y_speed = 0
  40.  
  41. # Current position
  42. x_coord = 160
  43. y_coord = 120
  44.  
  45. # -------- Main Program Loop -----------
  46. while not done:
  47.     # --- Event Processing
  48.     for event in pygame.event.get():
  49.         if event.type == pygame.QUIT:
  50.             done = True
  51.             # User pressed down on a key
  52.  
  53.         elif event.type == pygame.KEYDOWN:
  54.             # Figure out if it was an arrow key. If so
  55.             # adjust speed.
  56.            
  57.             if event.key == pygame.K_KP4:
  58.                 x_speed = -3
  59.                 ser.write('4')
  60.  
  61.             elif event.key == pygame.K_KP6:
  62.                 x_speed = 3
  63.                 ser.write('6')
  64.  
  65.             elif event.key == pygame.K_KP8:
  66.                 y_speed = -3
  67.                 ser.write('8')
  68.  
  69.             elif event.key == pygame.K_KP2:
  70.                 y_speed = 3
  71.                 ser.write('2')
  72.  
  73.  
  74.         # User let up on a key
  75.         elif event.type == pygame.KEYUP:
  76.             # If it is an arrow key, reset vector back to zero
  77.             if event.key == pygame.K_KP4 or event.key == pygame.K_KP6:
  78.                 x_speed = 0
  79.                 ser.write('5')
  80.             elif event.key == pygame.K_KP8 or event.key == pygame.K_KP2:
  81.                 y_speed = 0
  82.                 ser.write('7')
  83.  
  84.     # --- Game Logic
  85.  
  86.     # Move the object according to the speed vector.
  87.     x_coord = x_coord + x_speed
  88.  
  89.     if x_coord > 320:
  90.         x_coord = 1
  91.  
  92.     if x_coord < 0:
  93.         x_coord = 319
  94.    
  95.     y_coord = y_coord + y_speed
  96.  
  97.     if y_coord > 240:
  98.         y_coord = 1
  99.  
  100.     if y_coord < 0:
  101.         y_coord = 239
  102.  
  103.  
  104.     # --- Drawing Code
  105.  
  106.     # First, clear the screen to WHITE. Don't put other drawing commands
  107.     # above this, or they will be erased with this command.
  108.     screen.fill(WHITE)
  109.  
  110.     draw_stick_figure(screen, x_coord, y_coord)
  111.     # draw_up_arrow(screen, 160, 120)
  112.  
  113.  
  114.     # Go ahead and update the screen with what we've drawn.
  115.     pygame.display.flip()
  116.  
  117.     # Limit frames per second
  118.     clock.tick(60)
  119.  
  120. # Close the window and quit.
  121. pygame.quit()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement