def __init__(self,vp,objectname): self.objectname = objectname self.simulation = isinstance(vp,file) if self.simulation: obj = get_now_file(vp) else: obj = get_now(vp) self.set_pos(obj) print "Ball created at %s" % str(self.pos)
def lookfor_objects(self): objects = None if self.simulation_mode: # read from file obj = get_now_file(self.f,debug=True) else: # read from vicon_proxy obj = get_now(self.vp) try: assert(obj['mode']==1) # should be in object mode # list of objects objs=obj['objs'] except: print "ERROR while parsing objs. Is proxy in raw mode?" return objects # add every object currently broadcasting objects = [] for o in objs: objects.append(o['name']) print "Added: %s" % o['name'] return objects
def run(self): """ Main game loop """ self.mode = WAITING self.graphics.set_caption("Possession: Press 'g' to start") print "Press 'g' to start play" #mode = GAMEON # debug #reset_clock() done = False while not done: if self.simulation_mode: # read from file obj = get_now_file(self.f,debug=self.options.debug) else: # read from vicon_proxy obj = get_now(self.vp) ## for b in self.balls: ## b.set_pos(obj) markers = get_vicon_raw(obj) # if we couldn't get a proper object (i.e. tracking mode) # then markers will be none # just wait for data but still handle events if markers is None: # maybe not raw mode, keep trying print "Still waiting for data..." self.handle_events() continue # time.sleep(1.0/60) # If there are no markers, then markers will be an empty list # we cannot convert to array # So we create a single dummy neutral point far, far away if len(markers)>0: if options.swap: # use x and z self.pos = np.array(markers)[:,0:3:2] else: self.pos = np.array(markers)[:,0:2] # only take x,y else: print "NO MARKERS" if options.axis==0: self.pos = np.array([[0,1000000]]) else: self.pos = np.array([[1000000,0]]) ## c=0 ## for b in self.balls: ## pos = b.get_pos() ## # update position for display ## self.pos[c,:] = b.pos[0:2] # only take x,y ## c+=1 # regardless of whether we're playing game # update ball position on display #self.graphics.update_ballpos(self.pos) self.graphics.draw_markers(self.pos) # accumulate time on each side if self.mode==GAMEON: s = self.sincelastupdate() # time since last update for m in markers: if m[self.options.axis]>0: self.postime+=s elif m[self.options.axis]<0: self.negtime+=s ## for b in self.balls: ## pos=b.get_pos() ## changed=b.changed(self.options.axis) # did it change sides? ## if pos[self.options.axis] > 0: ## if self.options.debug: ## print "%s + " % b.objectname ## self.postime += s ## elif pos[self.options.axis] < 0: ## if self.options.debug: ## print "%s - " % b.objectname ## self.negtime += s ## # play sounds if ball changed sides ## if changed==1: ## # self.sounds.play("swoosh1") ## self.sounds.swoosh1_sound.play() ## elif changed==2: ## # self.sounds.play("swoosh2") ## self.sounds.swoosh2_sound.play() self.update() #update clock # update text if game is on if self.mode==GAMEON: timeleft = self.time_left() posscore=100-round(100*self.postime/(self.postime+self.negtime+1e-9)) negscore=100-round(100*self.negtime/(self.postime+self.negtime+1e-9)) # I don't see a set_radius command # So I just remove the patch collection, recreate wedge, collection # and add collection again self.graphics.erase_clock() # clock takes a single argument - hand position in degrees self.graphics.draw_clock(360*timeleft/options.game_time) self.graphics.update_scores(posscore,negscore) # regardless of whether we are playing or not, update plot self.graphics.redraw() if self.mode==GAMEON and g.isgameover(): self.mode=GAMEDONE self.gameover(posscore,negscore) self.mode=WAITING self.graphics.set_caption("Possession: Press 'g' to start") self.handle_events() # Outside while loop pygame.quit()
def run(self): """ Main game loop """ self.mode = WAITING self.graphics.set_caption("Possession: Press 'g' to start") print "Press 'g' to start play" #mode = GAMEON # debug #reset_clock() done = False while not done: if self.simulation_mode: # read from file obj = get_now_file(self.f,debug=self.options.debug) else: # read from vicon_proxy obj = get_now(self.vp) for b in self.balls: b.set_pos(obj) # time.sleep(1.0/60) c=0 for b in self.balls: pos = b.get_pos() # update position for display self.pos[c,:] = b.pos[0:2] # only take x,y c+=1 # regardless of whether we're playing game # update ball position on display self.graphics.update_ballpos(self.pos) # accumulate time on each side if self.mode==GAMEON: s = self.sincelastupdate() # time since last update for b in self.balls: pos=b.get_pos() changed=b.changed(self.options.axis) # did it change sides? if pos[self.options.axis] > 0: if self.options.debug: print "%s + " % b.objectname self.postime += s elif pos[self.options.axis] < 0: if self.options.debug: print "%s - " % b.objectname self.negtime += s # play sounds if ball changed sides if changed==1: # self.sounds.play("swoosh1") self.sounds.swoosh1_sound.play() elif changed==2: # self.sounds.play("swoosh2") self.sounds.swoosh2_sound.play() self.update() #update clock # update text if game is on if self.mode==GAMEON: timeleft = self.time_left() posscore=100-round(100*self.postime/(self.postime+self.negtime+1e-9)) negscore=100-round(100*self.negtime/(self.postime+self.negtime+1e-9)) # I don't see a set_radius command # So I just remove the patch collection, recreate wedge, collection # and add collection again self.graphics.erase_clock() # clock takes a single argument - hand position in degrees self.graphics.draw_clock(360*timeleft/self.game_time) self.graphics.update_scores(posscore,negscore) # regardless of whether we are playing or not, update plot self.graphics.redraw() if self.mode==GAMEON and g.isgameover(): self.mode=GAMEDONE self.gameover(posscore,negscore) # do one last write self.write_game_state(posscore,negscore,self.time_elapsed(),self.game_time,self.pos_global,self.neg_global) self.mode=WAITING self.graphics.set_caption("Possession: Press 'g' to start") if self.mode==GAMEON: self.write_game_state(posscore,negscore,self.time_elapsed(),self.game_time,self.pos_global,self.neg_global) self.handle_events() # Outside while loop pygame.quit()