Exemplo n.º 1
0
    def tick(self, framerate=0):
        """Update the Clock.

        This method should be called once per frame. It will compute how many
        milliseconds have passed since the previous call.

        If you pass the optional framerate argument the function will delay
        to keep the game running slower than the given ticks per second. This
        can be used to help limit the runtime speed of a game. By calling
        clock.tick(40) once per frame, the program will never run at more
        than 40 frames per second.

        Usage:
            tick(framerate=0)

        Returns:
            float (milliseconds)
        """
        now = SDL_GetTicks()
        self.raw_frametime = now - self.last
        while len(self.last_frames) > 9:
            self.last_frames.pop(0)
        if framerate == 0:
            self.last = now
            self.last_frames.append(self.raw_frametime)
            return self.raw_frametime
        frame_duration = 1.0 / framerate * 1000
        if self.raw_frametime < frame_duration:
            wait(frame_duration - self.raw_frametime)
        now = SDL_GetTicks()
        self.frametime = now - self.last
        self.last = now
        self.last_frames.append(self.frametime)
        return self.frametime
Exemplo n.º 2
0
    def run(self):
        menu_input = Input()

        last_update_time = SDL_GetTicks()  # units.MS

        while self.running:
            start_time = SDL_GetTicks()  # units.MS

            menu_input.begin_new_frame()
            menu_events = get_events()

            for event in menu_events:
                if event.type == SDL_KEYDOWN:
                    menu_input.key_down_event(event)

                elif event.type == SDL_KEYUP:
                    menu_input.key_up_event(event)

                elif event.type == SDL_QUIT:
                    self.running = False
                    break

            # Exit
            if menu_input.was_key_pressed(SDLK_ESCAPE):
                self.running = False

            # Move the cursor
            elif menu_input.was_key_pressed(SDLK_UP):
                if self.cursor_position != 0:
                    self.cursor_position -= 1
            elif menu_input.was_key_pressed(SDLK_DOWN):
                if self.cursor_position != 4:
                    self.cursor_position += 1

            # Select option
            elif menu_input.was_key_pressed(SDLK_RETURN):
                self.running = False
                if self.cursor_position == 0:
                    self.launch_game()

            current_time = SDL_GetTicks()  # units.MS
            elapsed_time = current_time - last_update_time  # units.MS

            self.update(min(elapsed_time, MAX_FRAME_TIME))

            last_update_time = current_time

            self.renderer.process(self.world, self.sprites)

            # This loop lasts 1/60th of a second, or 1000/60th ms
            ms_per_frame = 1000 // FPS  # units.MS
            elapsed_time = SDL_GetTicks() - start_time  # units.MS
            if elapsed_time < ms_per_frame:
                SDL_Delay(ms_per_frame - elapsed_time)
Exemplo n.º 3
0
 def update(self):
     if self.activated:
         self.current_ticks = SDL_GetTicks()
         self.current_ticks -= self.start_ticks
         if self.current_ticks - self.previous_ticks >= self.interval:
             self.previous_ticks = self.current_ticks
             self.enabled = True
Exemplo n.º 4
0
    def handle_new_input(e: SDL_Event, axis_norm_value: int = 0) -> bool:
        """
        Event handling for button press/hat movement/axis movement
        Only needed when an new input is present
        Returns True when 'event_queue' is modified with a new event
        """
        dev_index = active_devices[event.jdevice.which][0]
        if e.type == SDL_JOYBUTTONDOWN:
            mapped_events = configs[dev_index].get_btn_event(
                event.jbutton.button)
            event_index = f'{dev_index}_btn{event.jbutton.button}'
        elif e.type == SDL_JOYHATMOTION:
            mapped_events = configs[dev_index].get_hat_event(
                event.jhat.hat, event.jhat.value)
            event_index = f'{dev_index}_hat{event.jhat.hat}'
        elif e.type == SDL_JOYAXISMOTION and axis_norm_value != 0:
            mapped_events = configs[dev_index].get_axis_event(
                event.jaxis.axis, axis_norm_value)
            event_index = f'{dev_index}_axis{event.jaxis.axis}'

        if mapped_events is not None:
            event_queue[event_index] = [mapped_events, 0, SDL_GetTicks()]
            return True

        return False
Exemplo n.º 5
0
def wait(milliseconds):
    """Pause the program for an amount of time.

    Will pause for a given number of milliseconds. This function sleeps the
    process to share the processor with other programs. A program that waits
    for even a few milliseconds will consume very little processor time.

    Usage:
        wait(milliseconds)

    Returns:
        int (the actual number of milliseconds used)
    """
    start = SDL_GetTicks()
    SDL_Delay(int(milliseconds))
    return SDL_GetTicks() - start
Exemplo n.º 6
0
 def tick(self, framerate=0):
     """..."""
     now = SDL_GetTicks()
     self.raw_frametime = now - self.last
     while len(self.last_frames) > 9:
         self.last_frames.pop(0)
     if framerate == 0:
         self.last = now
         self.last_frames.append(self.raw_frametime)
         return self.raw_frametime
     frame_duration = 1.0 / framerate * 1000
     if self.raw_frametime < frame_duration:
         delay(frame_duration - self.raw_frametime)
     now = SDL_GetTicks()
     self.frametime = now - self.last
     self.last = now
     self.last_frames.append(self.frametime)
     return self.frametime
Exemplo n.º 7
0
	def now(self, unit=TK_MS):
		"""Fetches the current time according to the tracker's internal clock. In TryLink
		simulation mode, this returns the time since the experiment was first launched.

		Args:
			unit (int, optional): The units in which the time should be returned. Can be either
				``TK_S`` (seconds) or ``TK_MS`` (milliseconds). Defaults to milliseconds.

		Returns:
			float: The current tracker time in the specified time unit.

		"""
		time = float(SDL_GetTicks())
		return time * 0.001 if unit == TK_S else time
Exemplo n.º 8
0
def filter_active_events(event_queue: dict) -> list:
    """
    Method to filter out the event if the event:
     * fired once within the JS_POLL_DELAY_DEBOUNCE
     * fired multiple times, last fire within JS_POLL_POLL_DELAY_DEFAULT
    """
    current_time = SDL_GetTicks()
    filtered_events = []
    for e in event_queue:
        if event_queue[e][0] is None:
            continue

        last_fire_time = event_queue[e][2]
        repeat_count = event_queue[e][1]

        if repeat_count == 0 or \
             (repeat_count == 1 and current_time > (last_fire_time + JS_INIT_DELAY)) or \
             (repeat_count > 1 and current_time > (last_fire_time + JS_REPEAT_DELAY)):
            filtered_events.extend(event_queue[e][0])
            event_queue[e][2] = current_time
            event_queue[e][1] += 1

    # remove any duplicate events from the list
    return list(set(filtered_events))
Exemplo n.º 9
0
    def run(self):

        game_input = Input()

        speed_x, speed_y = 2, 1
        player_pos = [-100, -100]

        # motion_type = self.player_motion_type
        # facing = self.player_facing

        self.running = True
        last_update_time = SDL_GetTicks()  # units.MS

        while self.running:
            start_time = SDL_GetTicks()  # units.MS

            game_input.begin_new_frame()
            game_events = get_events()

            for event in game_events:
                if event.type == SDL_KEYDOWN:
                    game_input.key_down_event(event)

                elif event.type == SDL_KEYUP:
                    game_input.key_up_event(event)

                elif event.type == SDL_QUIT:
                    self.running = False
                    break

            if not self.running:
                self.clear()
                break

            # Exit
            if game_input.was_key_pressed(SDLK_ESCAPE):
                self.clear()
                self.running = False
                break

            # Player movement
            if game_input.is_key_held(SDLK_RIGHT) and game_input.is_key_held(
                    SDLK_UP):
                player_pos[0] -= speed_x
                player_pos[1] += speed_y
                self.player.velocity.vx = speed_x
                self.player.velocity.vy = -speed_y
                self.player.facing.set("right_up")
                self.player.motiontype.set("walking")
            elif game_input.is_key_held(SDLK_RIGHT) and game_input.is_key_held(
                    SDLK_DOWN):
                player_pos[0] -= speed_x
                player_pos[1] -= speed_y
                self.player.velocity.vx = speed_x
                self.player.velocity.vy = speed_y
                self.player.facing.set("right_down")
                self.player.motiontype.set("walking")
            elif game_input.is_key_held(SDLK_LEFT) and game_input.is_key_held(
                    SDLK_UP):
                player_pos[0] += speed_x
                player_pos[1] += speed_y
                self.player.velocity.vx = -speed_x
                self.player.velocity.vy = -speed_y
                self.player.facing.set("left_up")
                self.player.motiontype.set("walking")
            elif game_input.is_key_held(SDLK_LEFT) and game_input.is_key_held(
                    SDLK_DOWN):
                player_pos[0] += speed_x
                player_pos[1] -= speed_y
                self.player.velocity.vx = -speed_x
                self.player.velocity.vy = speed_y
                self.player.facing.set("left_down")
                self.player.motiontype.set("walking")
            elif game_input.is_key_held(SDLK_LEFT):
                player_pos[0] += speed_x
                self.player.velocity.vx = -speed_x
                self.player.facing.set("left")
                self.player.motiontype.set("walking")
            elif game_input.is_key_held(SDLK_RIGHT):
                player_pos[0] -= speed_x
                self.player.velocity.vx = speed_x
                self.player.facing.set("right")
                self.player.motiontype.set("walking")
            elif game_input.is_key_held(SDLK_UP):
                player_pos[1] += speed_y
                self.player.velocity.vy = -speed_y
                self.player.facing.set("up")
                self.player.motiontype.set("walking")
            elif game_input.is_key_held(SDLK_DOWN):
                player_pos[1] -= speed_y
                self.player.velocity.vy = speed_y
                self.player.facing.set("down")
                self.player.motiontype.set("walking")

            # elif game_input.was_key_pressed(SDLK_i):
            #    self.player.toggle_inventory()

            # Player Attack
            elif game_input.is_key_held(SDLK_SPACE):
                pass
                # motion_type = MotionType.CASTING

            # Nothing
            else:
                self.player.velocity.vx = 0
                self.player.velocity.vy = 0
                self.player.motiontype.set("standing")

            current_time = SDL_GetTicks()  # units.MS
            elapsed_time = current_time - last_update_time  # units.MS

            last_update_time = current_time

            self.world.process()

            # This loop lasts 1/60th of a second, or 1000/60th ms
            ms_per_frame = 1000 // FPS  # units.MS
            elapsed_time = SDL_GetTicks() - start_time  # units.MS
            if elapsed_time < ms_per_frame:
                SDL_Delay(ms_per_frame - elapsed_time)
Exemplo n.º 10
0
 def __init__(self):
     """..."""
     self.last = SDL_GetTicks()
     self.last_frames = []
     self.frametime = 0
     self.raw_frametime = 0
Exemplo n.º 11
0
def get_delta(t0):
    """..."""
    return SDL_GetTicks() - t0
Exemplo n.º 12
0
def get_time():
    """..."""
    return SDL_GetTicks()
Exemplo n.º 13
0
def wait(milliseconds):
    """..."""
    start = SDL_GetTicks()
    SDL_Delay(int(milliseconds))
    return SDL_GetTicks() - start
Exemplo n.º 14
0
 def activate(self):
     self.start_ticks = SDL_GetTicks()
     self.activated = True
Exemplo n.º 15
0
 def activate(self):
     """ Activates the timer """
     self.start_ticks = SDL_GetTicks()
     self.activated = True
Exemplo n.º 16
0
 def __init__(self):
     """Initialization."""
     self.last = SDL_GetTicks()
     self.last_frames = []
     self.frametime = 0
     self.raw_frametime = 0
Exemplo n.º 17
0
def get_time():
    """Get the current time from SDL clock."""
    return SDL_GetTicks()
Exemplo n.º 18
0
    def run(self):

        game_input = Input()

        speed_x, speed_y = 2, 1
        player_pos = [0, 0]

        motion_type = self.player.motion_type
        facing = self.player.facing

        running = True
        last_update_time = SDL_GetTicks()  # units.MS

        while running:
            start_time = SDL_GetTicks()  # units.MS

            game_input.begin_new_frame()
            game_events = get_events()

            for event in game_events:
                if event.type == SDL_KEYDOWN:
                    game_input.key_down_event(event)

                elif event.type == SDL_KEYUP:
                    game_input.key_up_event(event)

                elif event.type == SDL_QUIT:
                    running = False
                    break

            # Exit
            if game_input.was_key_pressed(SDLK_ESCAPE):
                running = False

            # Player movement
            if game_input.is_key_held(SDLK_RIGHT) and game_input.is_key_held(
                    SDLK_UP):
                player_pos[0] -= speed_x
                player_pos[1] += speed_y
                motion_type = MotionType.WALKING
                facing = Facing.RIGHT_UP
            elif game_input.is_key_held(SDLK_RIGHT) and game_input.is_key_held(
                    SDLK_DOWN):
                player_pos[0] -= speed_x
                player_pos[1] -= speed_y
                motion_type = MotionType.WALKING
                facing = Facing.RIGHT_DOWN
            elif game_input.is_key_held(SDLK_LEFT) and game_input.is_key_held(
                    SDLK_UP):
                player_pos[0] += speed_x
                player_pos[1] += speed_y
                motion_type = MotionType.WALKING
                facing = Facing.LEFT_UP
            elif game_input.is_key_held(SDLK_LEFT) and game_input.is_key_held(
                    SDLK_DOWN):
                player_pos[0] += speed_x
                player_pos[1] -= speed_y
                motion_type = MotionType.WALKING
                facing = Facing.LEFT_DOWN
            elif game_input.is_key_held(SDLK_LEFT):
                player_pos[0] += speed_x
                motion_type = MotionType.WALKING
                facing = Facing.LEFT
            elif game_input.is_key_held(SDLK_RIGHT):
                player_pos[0] -= speed_x
                motion_type = MotionType.WALKING
                facing = Facing.RIGHT
            elif game_input.is_key_held(SDLK_UP):
                player_pos[1] += speed_y
                motion_type = MotionType.WALKING
                facing = Facing.UP
            elif game_input.is_key_held(SDLK_DOWN):
                player_pos[1] -= speed_y
                motion_type = MotionType.WALKING
                facing = Facing.DOWN

            elif game_input.was_key_pressed(SDLK_i):
                self.player.toggle_inventory()

            # Player Attack
            elif game_input.is_key_held(SDLK_SPACE):
                motion_type = MotionType.CASTING

            # Nothing
            else:
                motion_type = MotionType.STANDING

            current_time = SDL_GetTicks()  # units.MS
            elapsed_time = current_time - last_update_time  # units.MS

            self.update(player_pos, motion_type, facing,
                        min(elapsed_time, MAX_FRAME_TIME))

            last_update_time = current_time

            self.get_sprites()

            self.renderer.process(self.world, self.sprites)

            self.sprites.clear()

            # This loop lasts 1/60th of a second, or 1000/60th ms
            ms_per_frame = 1000 // FPS  # units.MS
            elapsed_time = SDL_GetTicks() - start_time  # units.MS
            if elapsed_time < ms_per_frame:
                SDL_Delay(ms_per_frame - elapsed_time)