Ejemplo n.º 1
0
    def __init__(self,
                 mic,
                 position,
                 color_bag,
                 size=(20, 40),
                 energy_frame_update=4):

        # init parents
        super().__init__()

        # mic
        self.mic = mic
        self.position = position
        self.color_bag = color_bag
        self.size = size
        self.energy_frame_update = energy_frame_update

        # sprites group
        self.sprites = pygame.sprite.Group()

        # bar sprite
        self.bar_sprite = BarSprite(self.mic, position, size=self.size)

        # add to sprites
        self.sprites.add(self.bar_sprite)

        # input handler
        self.input_handler = InputKeyHandler(self)
Ejemplo n.º 2
0
    def __init__(self,
                 surf,
                 mic,
                 position,
                 bar_size=(20, 40),
                 scale_margin=(10, 5),
                 energy_frame_update=4):

        # mic
        self.surf = surf
        self.mic = mic
        self.position = position
        self.bar_size = bar_size
        self.scale_margin = scale_margin
        self.energy_frame_update = energy_frame_update

        # sprites group
        self.sprites = pygame.sprite.Group()

        # bar sprite
        self.bar_sprite = BarSprite(self.mic,
                                    position,
                                    bar_size=self.bar_size,
                                    scale_margin=self.scale_margin)

        # add to sprites
        self.sprites.add(self.bar_sprite)

        # input handler
        self.input_handler = InputKeyHandler(self)
Ejemplo n.º 3
0
	def __init__(self, position, scale=(3, 3), is_gravity=False):

		# MRO check
		super().__init__()

		# arguments
		self.position = position
		self.scale = scale
		self.is_gravity = is_gravity

		# character sprite
		self.character_sprite = CharacterSprite(self.position, self.scale)

		# save init pos
		self.init_pos = position

		# speed and move dir
		self.move_speed = [3, 3]
		self.move_dir = [0, 0]

		# gravity stuff
		self.gravity_change = 0
		self.is_grounded = False
		self.max_fall_speed = 6
		self.init_fall_speed = 3
		self.jump_force = 6

		# input handler
		self.input_handler = InputKeyHandler(self)

		# interactions
		self.obstacle_sprites = pygame.sprite.Group()
		self.thing_sprites = pygame.sprite.Group()
		self.things_collected = 0
		self.is_active = True
Ejemplo n.º 4
0
    def __init__(self):

        # variables
        self.end_game = False
        self.run_loop = True

        # add input handler
        self.input_handler = InputKeyHandler(self)
Ejemplo n.º 5
0
class GameLogic(Interactable):
    """
  Game Logic class interface
  """
    def __init__(self):

        # variables
        self.end_game = False
        self.run_loop = True

        # add input handler
        self.input_handler = InputKeyHandler(self)

    def check_win_condition(self):
        """
    move character to position
    """
        pass

    def check_loose_condition(self):
        """
    check loose condition
    """
        pass

    def restart_game(self):
        """
    restart game
    """
        pass

    def esc_key(self):
        """
    if esc is pressed
    """

        # end loop
        self.run_loop = False

    def event_update(self, event):
        """
    update game logic
    """

        # quit game
        if event.type == pygame.QUIT:
            self.run_loop = False

        # input handling
        self.input_handler.handle(event)
Ejemplo n.º 6
0
  def __init__(self, grid_pos, color=(10, 200, 200), size=(20, 20), grid_move=False, mic_control=False, mic=None):

    # vars
    self.grid_pos = grid_pos
    self.grid_move = grid_move
    self.mic_control = mic_control
    self.mic = mic

    # MRO check
    super().__init__(np.array(grid_pos)*size, color, size)

    # input handler
    if self.mic_control:
      self.input_handler = InputMicHandler(self, mic=self.mic, grid_move=self.grid_move)

    else:
      self.input_handler = InputKeyHandler(self, grid_move=self.grid_move)

    # moveable init
    Moveable.__init__(self, move_sprite=self, move_rect=self.rect, move_speed=[3, 3], has_gravity=False, grid_move=self.grid_move)

    # interactions
    self.obstacle_sprites = pygame.sprite.Group()
    self.is_active = True
    
    # save init pos
    self.init_pos = self.position
    self.init_grid_pos = self.grid_pos.copy()

    # the grid
    self.move_wall_grid = None
Ejemplo n.º 7
0
  def __init__(self, position, scale=(3, 3), has_gravity=True, grid_move=False):

    # arguments
    self.position = position
    self.scale = scale

    # character sprite
    self.character_sprite = CharacterSprite(self.position, self.scale)

    # moveable init
    Moveable.__init__(self, move_sprite=self.character_sprite, move_rect=self.character_sprite.rect, move_speed=[3, 3], has_gravity=has_gravity, grid_move=grid_move)

    # save init pos
    self.init_pos = position

    # input handler
    self.input_handler = InputKeyHandler(self, grid_move=grid_move)

    # interactions
    self.obstacle_sprites = pygame.sprite.Group()
    self.thing_sprites = pygame.sprite.Group()
    self.things_collected = 0
    self.is_active = True
Ejemplo n.º 8
0
    def __init__(self,
                 grid_pos,
                 color=(10, 200, 200),
                 size=(20, 20),
                 grid_move=False,
                 mic_control=False,
                 mic=None):

        # vars
        self.grid_pos = grid_pos
        self.grid_move = grid_move

        # MRO check
        super().__init__(np.array(grid_pos) * size, color, size)

        # input handler
        if mic_control:
            self.input_handler = InputMicHandler(self,
                                                 mic=mic,
                                                 grid_move=grid_move)

        else:
            self.input_handler = InputKeyHandler(self, grid_move=grid_move)

        # speed and move dir
        self.move_speed = 2
        self.move_dir = [0, 0]

        # interactions
        self.obstacle_sprites = pygame.sprite.Group()
        self.is_active = True

        # save init pos
        self.init_pos = self.position
        self.init_grid_pos = self.grid_pos.copy()

        # the grid
        self.move_wall_grid = None
Ejemplo n.º 9
0
class MicBar(Interactable):
    """
  graphical bar for microphone energy measure
  """
    def __init__(self,
                 mic,
                 position,
                 color_bag,
                 size=(20, 40),
                 energy_frame_update=4):

        # init parents
        super().__init__()

        # mic
        self.mic = mic
        self.position = position
        self.color_bag = color_bag
        self.size = size
        self.energy_frame_update = energy_frame_update

        # sprites group
        self.sprites = pygame.sprite.Group()

        # bar sprite
        self.bar_sprite = BarSprite(self.mic, position, size=self.size)

        # add to sprites
        self.sprites.add(self.bar_sprite)

        # input handler
        self.input_handler = InputKeyHandler(self)

    def action_key(self):
        """
    if action key is pressed
    """

        print("action")

        if self.bar_sprite.act_length > 5:
            self.bar_sprite.act_length -= 5

    def enter_key(self):
        """
    if enter key is pressed
    """

        print("enter")

        if self.bar_sprite.act_length < self.size[1] - 5:
            self.bar_sprite.act_length += 5

    def reset(self):
        """
    reset stuff
    """
        pass

    def event_update(self, event):
        """
    event for mic bar
    """

        # event handling
        self.input_handler.handle(event)

    def update(self):
        """
    update
    """

        # read mic
        self.mic.read_mic_data()

        # get energy of mic
        #print("mic energy: ", len(self.mic.collector.e_all))

        # view mean energy over frames
        if len(self.mic.collector.e_all) > self.energy_frame_update:

            # energy of frames
            e_frames = self.mic.collector.e_all

            # reset collection
            self.mic.collector.reset_collection_all()

            # mean
            e_mu = np.mean(e_frames)

            # db
            e_mu_db = 10 * np.log10(e_mu)

            print("e_mu: {}, db: {}".format(e_mu, e_mu_db))

            # set bar accordingly
            self.bar_sprite.act_length = (e_mu_db /
                                          (-1 * self.bar_sprite.min_db) +
                                          1) * self.bar_sprite.total_length

            print("act_length: ", self.bar_sprite.act_length)
Ejemplo n.º 10
0
class MicBar(Interactable):
    """
  graphical bar for microphone energy measure
  """
    def __init__(self,
                 surf,
                 mic,
                 position,
                 bar_size=(20, 40),
                 scale_margin=(10, 5),
                 energy_frame_update=4):

        # mic
        self.surf = surf
        self.mic = mic
        self.position = position
        self.bar_size = bar_size
        self.scale_margin = scale_margin
        self.energy_frame_update = energy_frame_update

        # sprites group
        self.sprites = pygame.sprite.Group()

        # bar sprite
        self.bar_sprite = BarSprite(self.mic,
                                    position,
                                    bar_size=self.bar_size,
                                    scale_margin=self.scale_margin)

        # add to sprites
        self.sprites.add(self.bar_sprite)

        # input handler
        self.input_handler = InputKeyHandler(self)

    def action_key(self):
        """
    if action key is pressed
    """

        print("action")

        if self.bar_sprite.act_length > 5: self.bar_sprite.act_length -= 5

    def enter_key(self):
        """
    if enter key is pressed
    """

        print("enter")

        if self.bar_sprite.act_length < self.bar_size[1] - 5:
            self.bar_sprite.act_length += 5

    def reset(self):
        """
    reset stuff
    """
        pass

    def event_update(self, event):
        """
    event for mic bar
    """

        # event handling
        self.input_handler.handle(event)

    def update(self):
        """
    update
    """

        # debug
        #self.bar_sprite.update()
        #return

        # read mic
        self.mic.read_mic_data()

        # get energy of mic
        #print("mic energy: ", len(self.mic.collector.e_all))

        # view mean energy over frames
        if len(self.mic.collector.e_all) > self.energy_frame_update:

            # energy of frames
            e_frames = self.mic.collector.e_all

            # reset collection
            self.mic.collector.reset_collection_all()

            # mean
            e_mu = np.mean(e_frames)

            # db
            e_mu_db = 10 * np.log10(e_mu)

            #print("e_mu: {}, db: {}".format(e_mu, e_mu_db))

            # set bar accordingly
            self.bar_sprite.act_length = (e_mu_db /
                                          (-1 * self.bar_sprite.min_db) +
                                          1) * self.bar_sprite.total_length

            #print("act_length: ", self.bar_sprite.act_length)

            # update bar
            self.bar_sprite.update()

    def change_energy_thresh(self, e):
        """
    energy threshold change
    """

        # energy thresh
        self.bar_sprite.energy_thresh_position = int(
            10 * np.log10(e) * (bar_size[1] / self.min_db))

    def draw(self):
        """
    draw
    """
        self.surf.blit(self.bar_sprite.image, self.bar_sprite.position)
Ejemplo n.º 11
0
class Character(Interactable):
	"""
	character class
	"""

	def __init__(self, position, scale=(3, 3), is_gravity=False):

		# MRO check
		super().__init__()

		# arguments
		self.position = position
		self.scale = scale
		self.is_gravity = is_gravity

		# character sprite
		self.character_sprite = CharacterSprite(self.position, self.scale)

		# save init pos
		self.init_pos = position

		# speed and move dir
		self.move_speed = [3, 3]
		self.move_dir = [0, 0]

		# gravity stuff
		self.gravity_change = 0
		self.is_grounded = False
		self.max_fall_speed = 6
		self.init_fall_speed = 3
		self.jump_force = 6

		# input handler
		self.input_handler = InputKeyHandler(self)

		# interactions
		self.obstacle_sprites = pygame.sprite.Group()
		self.thing_sprites = pygame.sprite.Group()
		self.things_collected = 0
		self.is_active = True


	def set_position(self, position, is_init_pos=False):
		"""
		set position absolute
		"""

		# set internal pos
		self.position = position

		# also set initial position
		if is_init_pos:
			self.init_pos = position

		# set rect
		self.character_sprite.rect.x = self.position[0]
		self.character_sprite.rect.y = self.position[1]


	def calc_gravity(self):
		"""
		gravity
		"""

		# grounded condition
		if self.is_grounded:
			self.move_speed[1] = self.init_fall_speed

		# change speed according to gravity
		if self.move_speed[1] < self.max_fall_speed:
			self.move_speed[1] += 0.3

		# determine direction determined by move speed
		self.move_dir[1] = 1


	def jump(self):
		"""
		character jump
		"""

		# only if grounded
		if self.is_grounded:

			# change vertical speed
			self.move_speed[1] = -self.jump_force

			# not grounded anymore
			self.is_grounded = False


	def direction_change(self, direction):
		"""
		move character to position
		"""

		# apply x direction
		self.move_dir[0] += direction[0]

		# update sprite view
		if self.move_dir[0] < 0:
			self.character_sprite.change_view_sprites("side-l")

		elif self.move_dir[0] > 0:
			self.character_sprite.change_view_sprites("side-r")

		else:
			self.character_sprite.change_view_sprites("front")

		# gravity moves
		if self.is_gravity:
			return

		# apply y direction
		self.move_dir[1] += direction[1]


	def action_key(self):
		"""
		if action key is pressed
		"""

		# do a jump
		self.jump()


	def reset(self):
		"""
		reset stuff
		"""

		self.is_active = True
		self.is_grounded = False
		self.things = None
		self.things_collected = 0

		# set init position
		self.set_position(self.init_pos)


	def event_update(self, event):
		"""
		event update for character
		"""

		# event handling
		self.input_handler.handle(event)


	def update(self):
		"""
		update character
		"""

		# not active
		if not self.is_active:
			return

		# change of x
		move_change_x = self.move_dir[0] * self.move_speed[0]

		# x movement
		self.character_sprite.rect.x += move_change_x

		# collide issue
		for obst in pygame.sprite.spritecollide(self.character_sprite, self.obstacle_sprites, False):

			# stand at wall
			if move_change_x > 0:
				self.character_sprite.rect.right = obst.rect.left

			else:
				self.character_sprite.rect.left = obst.rect.right


		# y gravity
		if self.is_gravity:

			# calculate gravity
			self.calc_gravity()


		# change of y
		move_change_y = self.move_dir[1] * self.move_speed[1]

		# y movement
		self.character_sprite.rect.y += move_change_y

		# grounded false
		self.is_grounded = False

		# collide issue
		for obst in pygame.sprite.spritecollide(self.character_sprite, self.obstacle_sprites, False):
			
			# stand at wall
			if move_change_y > 0:

				# stop atop
				self.character_sprite.rect.bottom = obst.rect.top

				# grounded condition
				self.is_grounded = True

			else:

				# stop with head hit
				self.character_sprite.rect.top = obst.rect.bottom

				# no upward movement anymore
				self.move_speed[1] = 0

		# interaction with things
		for thing in pygame.sprite.spritecollide(self.character_sprite, self.thing_sprites, True):
			self.things_collected += 1
Ejemplo n.º 12
0
class Character(Interactable, Moveable):
  """
  character class
  """

  def __init__(self, position, scale=(3, 3), has_gravity=True, grid_move=False):

    # arguments
    self.position = position
    self.scale = scale

    # character sprite
    self.character_sprite = CharacterSprite(self.position, self.scale)

    # moveable init
    Moveable.__init__(self, move_sprite=self.character_sprite, move_rect=self.character_sprite.rect, move_speed=[3, 3], has_gravity=has_gravity, grid_move=grid_move)

    # save init pos
    self.init_pos = position

    # input handler
    self.input_handler = InputKeyHandler(self, grid_move=grid_move)

    # interactions
    self.obstacle_sprites = pygame.sprite.Group()
    self.thing_sprites = pygame.sprite.Group()
    self.things_collected = 0
    self.is_active = True


  def set_position(self, position, is_init_pos=False):
    """
    set position absolute
    """

    # set internal pos
    self.position = position

    # also set initial position
    if is_init_pos:
      self.init_pos = position

    # set rect
    self.character_sprite.rect.x = self.position[0]
    self.character_sprite.rect.y = self.position[1]


  def direction_change(self, direction):
    """
    move character to position
    """

    # update move direction in Movable class
    self.update_move_direction(direction)


  def view_update(self):
    """
    update view upon direction
    """

    # update sprite view
    if self.move_dir[0] < 0:
      self.character_sprite.change_view_sprites("side-l")

    elif self.move_dir[0] > 0:
      self.character_sprite.change_view_sprites("side-r")

    else:
      self.character_sprite.change_view_sprites("front")


  def action_key(self):
    """
    if action key is pressed
    """

    # do a jump
    self.jump()


  def reset(self):
    """
    reset stuff
    """

    self.is_active = True
    self.is_grounded = False
    self.things = None
    self.things_collected = 0

    # set init position
    self.set_position(self.init_pos)


  def event_update(self, event):
    """
    event update for character
    """

    # event handling
    self.input_handler.handle(event)


  def update(self):
    """
    update character
    """

    # not active
    if not self.is_active:
      return

    # move player with movement class
    self.move_update()

    # update of character view
    self.view_update()

    # interaction with things
    for thing in pygame.sprite.spritecollide(self.character_sprite, self.thing_sprites, True):
      self.things_collected += 1