Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import pygame
- import serial
- ser = serial.Serial('/dev/ttyACM0', 9600)
- # Define some colors
- BLACK = (0, 0, 0)
- WHITE = (255, 255, 255)
- GREEN = (0, 255, 0)
- RED = (255, 0, 0)
- def draw_stick_figure(screen, x, y):
- # Head
- pygame.draw.ellipse(screen, BLACK, [1 + x, y, 10, 10], 0)
- # Setup
- pygame.init()
- # Set the width and height of the screen [width,height]
- size = [320, 240]
- screen = pygame.display.set_mode(size)
- pygame.display.set_caption("LunkVehikel Control Pannel")
- # Loop until the user clicks the close button.
- done = False
- # Used to manage how fast the screen updates
- clock = pygame.time.Clock()
- # Hide the mouse cursor
- pygame.mouse.set_visible(0)
- # Speed in pixels per frame
- x_speed = 0
- y_speed = 0
- # Current position
- x_coord = 160
- y_coord = 120
- # -------- Main Program Loop -----------
- while not done:
- # --- Event Processing
- for event in pygame.event.get():
- if event.type == pygame.QUIT:
- done = True
- # User pressed down on a key
- elif event.type == pygame.KEYDOWN:
- # Figure out if it was an arrow key. If so
- # adjust speed.
- if event.key == pygame.K_KP4:
- x_speed = -3
- ser.write('4')
- elif event.key == pygame.K_KP6:
- x_speed = 3
- ser.write('6')
- elif event.key == pygame.K_KP8:
- y_speed = -3
- ser.write('8')
- elif event.key == pygame.K_KP2:
- y_speed = 3
- ser.write('2')
- # User let up on a key
- elif event.type == pygame.KEYUP:
- # If it is an arrow key, reset vector back to zero
- if event.key == pygame.K_KP4 or event.key == pygame.K_KP6:
- x_speed = 0
- ser.write('5')
- elif event.key == pygame.K_KP8 or event.key == pygame.K_KP2:
- y_speed = 0
- ser.write('7')
- # --- Game Logic
- # Move the object according to the speed vector.
- x_coord = x_coord + x_speed
- if x_coord > 320:
- x_coord = 1
- if x_coord < 0:
- x_coord = 319
- y_coord = y_coord + y_speed
- if y_coord > 240:
- y_coord = 1
- if y_coord < 0:
- y_coord = 239
- # --- Drawing Code
- # First, clear the screen to WHITE. Don't put other drawing commands
- # above this, or they will be erased with this command.
- screen.fill(WHITE)
- draw_stick_figure(screen, x_coord, y_coord)
- # draw_up_arrow(screen, 160, 120)
- # Go ahead and update the screen with what we've drawn.
- pygame.display.flip()
- # Limit frames per second
- clock.tick(60)
- # Close the window and quit.
- pygame.quit()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement