KEYDOWN,
    QUIT,
)

#==============================================================================
# Set up the drawing window
#==============================================================================
pygame.display.init()
screen_dim = [SCREEN_WIDTH, SCREEN_HEIGHT]
screen = pygame.display.set_mode(screen_dim, pygame.FULLSCREEN)
#DISPLAYSURF = pygame.display.set_mode((0, 0), pygame.FULLSCREEN)
screen.fill((0, 0, 0))

# Create player
player = Player("stimulus.jpg", screen_dim, GAIN)
position = player.stim_center()
stim_threshold = position[0] + THRESHOLD
# create inital stimulus
screen.blit(player.surf, position)

#-----------------------------------------------------------------------------
# on soft code of state 4
#-----------------------------------------------------------------------------
# present initial stimulus
pygame.display.flip()

#==============================================================================
# py game loop
#==============================================================================
running = True
Ejemplo n.º 2
0
    def run_game(self):
        # pygame config
        pygame.init()
        fpsClock = pygame.time.Clock()
        fpsClock.tick(self.FPS)

        # define keys and user interaction
        from pygame.locals import (
            K_ESCAPE,
            KEYDOWN,
            QUIT,
        )

        #===========================
        # Set up the drawing window
        pygame.display.init()
        screen_dim = [self.SCREEN_WIDTH, self.SCREEN_HEIGHT]
        screen = pygame.display.set_mode(screen_dim, pygame.FULLSCREEN)
        #DISPLAYSURF = pygame.display.set_mode((0, 0), pygame.FULLSCREEN)
        screen.fill((0, 0, 0))

        # Create player
        player = Player(self.STIMULUS, screen_dim, self.GAIN)
        position = player.stim_center()
        stim_threshold = position[0] + self.THRESHOLD
        # create inital stimulus
        screen.blit(player.surf, position)

        #-----------------------------------------------------------------------------
        # on soft code of state 1
        #-----------------------------------------------------------------------------
        # present initial stimulus
        self.display_stim_event.wait()
        pygame.display.flip()

        #=========================
        # py game loop
        # loop is running while open loop active
        data_pref = [[0, 0, 0]]
        while self.run_open_loop:
            # Fill the background with white
            screen.fill((0, 0, 0))
            screen.blit(player.surf, position)

            #-------------------------------------------------------------------------
            # on soft code of state 2
            #-------------------------------------------------------------------------
            # read rotary encoder stream
            self.move_stim_event.wait()
            data = rotary_encoder.read_stream()
            running = True
            if len(data) == 0:
                continue
            else:
                pos_change = abs(data[0][2]) - abs(data_pref[0][2])
                #print(f"data-pref: {data_pref[0][2]}  data-now: {data[0][2]} n")
                data_pref = data

                #repositin based on changes
                if data[0][2] < 0:
                    #player.move_left(pos_change)
                    position[0] -= (pos_change * self.GAIN)

                if data[0][2] > 0:
                    #player.move_right(pos_change)
                    position[0] += (pos_change * self.GAIN)
                # # keep on screen
                # if abs(position[0]) >= stim_threshold:
                #     running = False

            pygame.display.update()

        #show stimulus after closed loop period is over until reward gieven
        self.still_show_event.wait()
        screen.fill((0, 0, 0))
        pygame.display.flip()

        fpsClock.tick(self.FPS)
        pygame.quit()
        print("end of game loop")