예제 #1
0
 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)
예제 #2
0
파일: combo.py 프로젝트: gosha1128/NYU
    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
예제 #3
0
while not done:

    for event in pygame.event.get():
        if event.type == pygame.QUIT: sys.exit()

        if (event.type == KEYUP) or (event.type == KEYDOWN):
            print event
            if (event.key == K_ESCAPE):
                done = True
            if (event.type == KEYUP and event.key == K_g):
                print "GO!"

    if simulation_mode: # read from file
        obj = get_now_file(f,debug=True)
    else: # read from vicon_proxy
        obj = get_now(vp)

    for b in balls:
        b.set_pos(obj)

    for c,b in enumerate(balls):
        # update position for display
        pos[c,:] = b.pos[0:2] # only take x,y

    update_ballpos(pos,col)
    # print pos
    canvas.draw()
    raw_data = renderer.tostring_rgb()
    surf = pygame.image.fromstring(raw_data, size, "RGB")
    screen.blit(surf, (0,0))
    pygame.display.flip()
예제 #4
0
파일: combo.py 프로젝트: gosha1128/NYU
    def run(self):

        self.mode = REVEAL

        cam,targets = self.reset(self.mode)
        
        done = False
        while not done:

            timeChange = self.clock.tick(self.TARGET_FPS)

            self.advance()
            
            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)

            # only retrieve from chosen cam
            frame = cv.RetrieveFrame(cam.capture)

            if not frame:
                print "Frame is None"
                sys.exit(1)


            for j,b in enumerate(self.balls):
                b.set_pos(obj)
                p = b.get_pos()
                cam.object_points[j,0]=p[0]
                cam.object_points[j,1]=p[1]
                cam.object_points[j,2]=p[2]

            cam.project()


            if self.mode==GAMEON or self.mode==TARGETS:
                # determine whether any of the balls have made the target
                for i,t in enumerate(targets):
                    for j,b in enumerate(self.balls):
                        if cam.match(t,j):
                            self.hit(i,self.mode)
                            cam,targets = self.reset(self.mode)

            # update screen
            if self.mode==GAMEON:
                self.tick(timeChange)

            if self.mode==TARGETS or self.mode==GAMEON:
                self.targetdraw(frame,cam,targets)
            else:
                self.revealdraw(frame,cam)

            if self.mode==REVEAL:
                pass
                ## elements = 640*480*3*255
                ## coverage = np.sum(pygame.surfarray.array3d(self.mask))/elements
                ## print "Coverage: %f" % coverage

            if self.mode==GAMEON and self.digit==0:
                print "GAME OVER"
                self.sounds.gameover_sound.play()
                self.mode=TARGETS

            self.handle_events()

                
                        ## self.score = [0]*options.numteams
                        ## self.update_score()
                        ## self.clock = pygame.time.Clock()
                        ## self.digit = options.game_time
                        ## self.update_digit()

                        
        # Outside while loop
        pygame.quit()
예제 #5
0
    def run(self):
        """ Main game loop
        """

	global side_count, last_side_count

        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

		    #gw	
                    #self.pos = np.array(markers)[:,0:3:2] # extracts x,z 
                    #self.area = np.array(markers)[:,1] # extracts y (stores area)
                    self.pos = np.array(markers)[:,0:3:1] # extracts x,z 
		    print "pos->",self.pos
		    #transform the points
	 	    new_coords = []
		    for pos in self.pos:
		 	coord = [ pos[0]*width_factor, pos[1]*height_factor + floor_offset, pos[2] ]
			new_coords.append( coord )
		    for i in range( len(self.pos) ):
			self.pos[i][0] = new_coords[i][0]
			self.pos[i][1] = new_coords[i][1]
			self.pos[i][2] = new_coords[i][2]
                    self.area = np.array(markers)[:,2] # extracts y (stores area)
		    #gw

                    #self.area = np.random.uniform(low=self.minarea,high=self.maxarea,size=len(self.pos)) # DEBUG
                    # rescale area to be on 10,300
                    # note use of np.maximum (not np.max) to do element-wise maximum to threshold 
                    self.area=100+(np.maximum(0,self.area-self.minarea))*1.0/(self.maxarea-self.minarea)*(300-100)
                    # don't let final area go above self.maxarea
                    self.area=np.minimum(self.maxarea,self.area)
                    #print self.area # DEBUG
                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]])
                self.area = np.array([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)
            if options.swap:
                self.graphics.draw_markers(self.pos,self.area)
            else:
                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]>(FUDGE + 0):
                        self.postime+=s
                    elif m[self.options.axis]<(FUDGE + 0):
                        self.negtime+=s
                
		#gw        
                #for m in markers:
                #     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()
		#
		if ( side_count != last_side_count ):	
			self.sounds.swoosh2_sound.play()
		last_side_count = side_count

                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))

                #posscore=100-round(100*self.postime/(self.postime+self.negtime+1e-9))
                #negscore=100-round(100*self.negtime/(self.postime+self.negtime+1e-9))

                # sigmoid approach
                # p(pos wins) = sigmoid( negfrac-posfrac+bias)
                posfrac = self.postime/(self.postime+self.negtime+1e-9)
                negfrac = self.negtime/(self.postime+self.negtime+1e-9)
                posscore = 1/(1+np.exp(-(negfrac-posfrac+self.bias))) # on 0,1
                negscore = 100*(1-posscore)
                posscore = 100*posscore

                # 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()
예제 #6
0
파일: game.py 프로젝트: gosha1128/NYU
    def run(self):

        mode = WAITING

        cam,targets = self.reset()
        
        done = False
        while not done:

            timeChange = self.clock.tick(self.TARGET_FPS)

            self.advance()
            
            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)

            # only retrieve from chosen cam
            frame = cv.RetrieveFrame(cam.capture)

            if not frame:
                print "Frame is None"
                sys.exit(1)


            for j,b in enumerate(self.balls):
                b.set_pos(obj)
                p = b.get_pos()
                cam.object_points[j,0]=p[0]
                cam.object_points[j,1]=p[1]
                cam.object_points[j,2]=p[2]

            cam.project()

            # determine whether any of the balls have made the target
            for i,t in enumerate(targets):
                for j,b in enumerate(self.balls):
                    if cam.match(t,j):
                        self.hit(i,mode)
                        cam,targets = self.reset()

            # update screen
            if mode==GAMEON:
                self.tick(timeChange)
            self.draw(frame,cam,targets)


            if mode==GAMEON and self.digit==0:
                print "GAME OVER"
                self.sounds.gameover_sound.play()
                mode=WAITING

            

            for event in pygame.event.get():
                if event.type == pygame.QUIT: sys.exit()

                if (event.type == KEYUP) or (event.type == KEYDOWN):
                    print event

                    if hasattr(event, 'key') and event.type == pygame.KEYDOWN:
                        if event.key == pygame.K_f:
                            print "f"
                            if self.fullscreen:
                                self.fullscreen=0
                                window = pygame.display.set_mode(self.resolution)
                                self.update_score()
                                self.update_digit()
                            else:
                                self.fullscreen=1
                                window = pygame.display.set_mode(self.resolution, pygame.FULLSCREEN)
                                self.update_score()
                                self.update_digit()

                    if (event.type == KEYDOWN and event.key == K_ESCAPE):
                        if self.simulation_mode:
                            self.f.close()
                        else:
                            self.vp.close()
                        done = True
                    if (event.type == KEYDOWN and event.key == K_SPACE):
                        # select new camera, target
                        cam,targets = self.reset()

                    if (event.type == KEYDOWN and event.key == K_g):
                        print "GO!"
                        mode=GAMEON
                        self.score = [0]*options.numteams
                        self.update_score()
                        self.clock = pygame.time.Clock()
                        self.digit = options.game_time
                        self.update_digit()
                        
        # Outside while loop
        pygame.quit()
예제 #7
0
파일: simon.py 프로젝트: gosha1128/NYU
    def run(self):

        # could adjust game params here (sequence length, etc.)

        # set up ball
        self.b = Ball(self.f if simulation_mode else self.vp,options.object)

        L = options.length
        T = options.max_time


        while True:
            print "Sequence length: %d, Time: %fs" % (L,T)
            WON_GAME = False            

            p = self.b.get_pos()
            print "Initializing sequence"
            self.s = Sequence(L,T,self.r.partitions,\
                              self.r.get_partition(p[0],p[1]))

            print "Displaying sequence"
            self.show_sequence()


            print "GAME ON"
            self.s.start()

            while not self.s.isover():

                if simulation_mode: # read from file
                    obj = get_now_file(self.f,debug=True)
                else: # read from vicon_proxy
                    obj = get_now(self.vp)
                self.b.set_pos(obj)
                p = self.b.get_pos()
                #print "ball: %s time left: %f" % (p,self.s.timeleft())
                #print "ball in partition: %d" % self.r.get_partition(p[0],p[1])

                v = self.b.get_vel()

                self.graphics.UpdateBall(p[0],p[1])
                
                #print "velocity: %f" % v

                #only look for matches if the ball has approximately stopped
                if self.s.partitions[0].isin(p[0],p[1]):
                    print "IN REGION"
                    if v<options.min_vel:
                        p = self.s.partitions[0]
                        self.fs.noteon(0,p.note,127)
                        self.graphics.HighlightPatch(p.idx)
                        print "MATCH"
                        # play the note for 1s then note off
                        # this is a bit stupid though since everything stops
                        time.sleep(1)
                        self.fs.noteoff(0,p.note)
                        self.graphics.RestorePatch(p.idx)
                        self.s.match()
                    

                if len(self.s.partitions)<1:
                    WON_GAME = True
                    break;

                time.sleep(1.0/10)
            if WON_GAME:
                print "SUCCESS! : time elapsed %fs" % (time.time()-self.s.start_time)

                # Applause
                self.fs.program_select(0,self.sfid,0,126)
                self.fs.noteon(0,60,127)
                time.sleep(1)
                self.fs.noteon(0,72,127)
                time.sleep(1)
                self.fs.noteon(0,84,127)
                time.sleep(3)
                self.fs.noteoff(0,60)
                self.fs.noteoff(0,72)
                self.fs.noteoff(0,84)

                # Adjust level
                if not options.crowd:
                    L = L+1
            else:
                print "Try again"


                # Buzzer sound
                self.fs.program_select(0,self.sfid,0,61)
                self.fs.noteon(0,36,127)
                time.sleep(1)
                self.fs.noteoff(0,36)

                time.sleep(3)

                # reset length of sequence to base
                L = options.length


        if simulation_mode:
            self.f.close()
        else:
            self.vp.close()
        if self.pt:
            self.pt.close()
        self.fs.delete()
예제 #8
0
파일: uncover.py 프로젝트: gosha1128/NYU
    def run(self):

        mode = WAITING

        cam = self.reset()

        done = False
        while not done:

            timeChange = self.clock.tick(self.TARGET_FPS)

            self.advance()

            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)

            # only retrieve from chosen cam
            frame = cv.RetrieveFrame(cam.capture)

            if not frame:
                print "Frame is None"
                sys.exit(1)

            for j, b in enumerate(self.balls):
                b.set_pos(obj)
                p = b.get_pos()
                cam.object_points[j, 0] = p[0]
                cam.object_points[j, 1] = p[1]
                cam.object_points[j, 2] = p[2]

            cam.project()

            # update screen
            if mode == GAMEON:
                self.tick(timeChange)
            self.draw(frame, cam)

            if mode == GAMEON and self.digit == 0:
                print "GAME OVER"
                self.sounds.gameover_sound.play()
                mode = WAITING

            for event in pygame.event.get():
                if event.type == pygame.QUIT:
                    sys.exit()

                if (event.type == KEYUP) or (event.type == KEYDOWN):
                    print event
                    if event.type == KEYDOWN and event.key == K_ESCAPE:
                        if self.simulation_mode:
                            self.f.close()
                        else:
                            self.vp.close()
                        done = True
                    if event.type == KEYDOWN and event.key == K_SPACE:
                        # select new camera, target
                        cam = self.reset()

                    if event.type == KEYDOWN and event.key == K_g:
                        print "GO!"
                        mode = GAMEON
                        self.score = [0] * options.numteams
                        self.update_score()
                        self.clock = pygame.time.Clock()
                        self.digit = options.game_time