import pygame import IPython import random import serial, sys import threading if (len(sys.argv) != 3): print "command line: rx.py serial_port port_speed" sys.exit() port = sys.argv[1] speed = int(sys.argv[2]) ser = serial.Serial(port,speed) ser.setDTR() ser.flushInput() inp = 0 pygame.init() X_DIM = 800 Y_DIM = 600 GUY_DIM = 60 screen = pygame.display.set_mode((X_DIM, Y_DIM + GUY_DIM)) done = False SCALE = 2.0 JUMP_ACCEL = 16 / SCALE WORLD_ACCEL = -4 / SCALE guy_velocity = 0. guy_position = 0. MAX_VELOCITY = 20. / SCALE X_POS = 200 #Where the guy lives in x MIN_Y = 0 MAX_Y = Y_DIM OBSTACLE_DIM = 80 obstacles = [] X_VELOCITY = 5 / SCALE prob_thresh = 1.0/30.0 GUY_COLOR = (0, 128, 255) OBSTACLE_COLOR = (255, 128, 0) def read_input_serial(): #Define globals: global inp while True: inp = ser.read() #Launch the thread thread = threading.Thread(target=read_input_serial, args=()) thread.daemon = True thread.start() def read_input(event): """ if event.type == pygame.KEYDOWN and event.key == pygame.K_SPACE: #TODO: this will eventually have to change to be pyserial return True """ return inp == 'u' def apply_physics(inp): #Globals global guy_velocity global guy_position #Tells us whether or not we should make the guy flap if inp: guy_velocity += JUMP_ACCEL #Then applies gravity. guy_velocity += WORLD_ACCEL if guy_position == 0 and guy_velocity < 0: #First, if he's on the ground, his velocity should start at 0 guy_velocity = 0 if guy_velocity > MAX_VELOCITY: guy_velocity = MAX_VELOCITY #Then update position guy_position += guy_velocity if guy_position > MAX_Y: guy_position = MAX_Y if guy_position < MIN_Y: guy_position = MIN_Y def update_obstacles(): #globals global obstacles for obstacle in obstacles: #Update the position obstacle[0] -= X_VELOCITY def manage_obstacles(): #globals global obstacles #First, should we add something? r = random.random() if r < prob_thresh: #Second, where should we add it? loc = int(random.random() * Y_DIM) #Third, add it obstacles.append([X_DIM, loc]) #Lastly, if they're off the board, let's delete it obstacles = [obstacle for obstacle in obstacles if obstacle[0] > 0.0] def collision_detect(): guy_rect = pygame.Rect(X_POS, guy_position, GUY_DIM, GUY_DIM) for obstacle in obstacles: obstacle_rect = pygame.Rect(obstacle[0], obstacle[1], OBSTACLE_DIM, OBSTACLE_DIM) if guy_rect.colliderect(obstacle_rect): print "BLAH" ser.write(chr(255)) return True #send stuff ser.write(chr(0)) return False def draw(): #First, draw the guy pygame.draw.rect(screen, GUY_COLOR, pygame.Rect(X_POS, guy_position, GUY_DIM, GUY_DIM)) for obstacle in obstacles: #Second, draw the obstacles pygame.draw.rect(screen, OBSTACLE_COLOR, pygame.Rect(obstacle[0], obstacle[1], OBSTACLE_DIM, OBSTACLE_DIM)) while not done: clock = pygame.time.Clock() for event in pygame.event.get(): if event.type == pygame.QUIT: done = True #This is our main game loop, apparently #Zeroth: cleanup screen.fill((0, 0, 0)) #First, read inputs inp = read_input(event) #Then, apply physics apply_physics(inp) #Third, move any obstacles update_obstacles() #Fourth, manage obstacles in buffer manage_obstacles() #Fifth, check for collisions collision = collision_detect() if collision: exit() #Sixth, draw! draw() pygame.display.flip() clock.tick(60)