Example #1
0
    def __init__(self):
        self.r = Room(options.xmin,options.xmax,options.ymin,options.ymax)

        self.fs = fluidsynth.Synth()
        self.fs.start(driver="coreaudio")
        self.sfid = self.fs.sfload("./FluidR3_GM.sf2")
        self.notes = [69, 81, 74, 79, 57, 62, 67, 93, 86, 91]

        self.r.partition(options.partition,self.notes)

        try:            
            self.pt = PanTilt()
            # set to max speed
            self.pt.set_max_speed()

        except:
            print "Couldn't initialize pan-tilt unit"
            self.pt = None
            pass


        if simulation_mode:
            self.f = open(options.vicon_file,'r')
            # should we read ahead?
            for i in xrange(options.line):
                self.f.readline()
        else:
            try:            
                self.vp = ViconProxy()
            except:
                print "Couldn't initialize Vicon Proxy"
                self.vp = 0
                pass


        self.m = Mapper(options.netfile)

        self.graphics = Graphics(self.r)
        self.graphics.DrawBall()
Example #2
0
class Game:

    def __init__(self):
        self.r = Room(options.xmin,options.xmax,options.ymin,options.ymax)

        self.fs = fluidsynth.Synth()
        self.fs.start(driver="coreaudio")
        self.sfid = self.fs.sfload("./FluidR3_GM.sf2")
        self.notes = [69, 81, 74, 79, 57, 62, 67, 93, 86, 91]

        self.r.partition(options.partition,self.notes)

        try:            
            self.pt = PanTilt()
            # set to max speed
            self.pt.set_max_speed()

        except:
            print "Couldn't initialize pan-tilt unit"
            self.pt = None
            pass


        if simulation_mode:
            self.f = open(options.vicon_file,'r')
            # should we read ahead?
            for i in xrange(options.line):
                self.f.readline()
        else:
            try:            
                self.vp = ViconProxy()
            except:
                print "Couldn't initialize Vicon Proxy"
                self.vp = 0
                pass


        self.m = Mapper(options.netfile)

        self.graphics = Graphics(self.r)
        self.graphics.DrawBall()

    def show_sequence(self):
        self.fs.program_select(0,self.sfid,0,103)
        for p in self.s.partitions:
            pc = p.get_center()
            print pc
            ptmap = self.m.map(pc) # map to pan-tilt
            pan,tilt = ptmap[0]
            if self.pt:
                self.pt.go_block(pan,tilt,True)
            self.fs.noteon(0,p.note,127)

            self.graphics.HighlightPatch(p.idx)
            # raw_input("Press Enter to continue...")
            time.sleep(options.show_sleep)
            self.fs.noteoff(0,p.note)
            self.graphics.RestorePatch(p.idx)

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