Esempio n. 1
0
def MainLoop():
    global x, oldX, y, oldY, direction, oldDirection
    timer = utils.Timer(
    )  # Prevents the user from walking faster than the sound can play. You're not the Flash.
    WALK_SPEED = 300  # milliseconds
    timer.elapsed = WALK_SPEED  # So the user can walk straight away
    xAxis, yAxis = (0, 0)

    while True:
        time.sleep(0.005)  # Be kind to processors
        lucia.process_events()

        # Set the movement coordinate changes
        if lucia.key_down(lucia.K_UP): yAxis = 1
        if lucia.key_down(lucia.K_DOWN): yAxis = -1
        if lucia.key_up(lucia.K_UP) and lucia.key_up(lucia.K_DOWN): yAxis = 0
        if lucia.key_down(lucia.K_RIGHT): xAxis = 1
        if lucia.key_down(lucia.K_LEFT): xAxis = -1
        if lucia.key_up(lucia.K_RIGHT) and lucia.key_up(lucia.K_LEFT):
            xAxis = 0

        if (not xAxis == 0 or not yAxis == 0) and timer.elapsed >= WALK_SPEED:
            timer.restart()

            # Set the direction
            if xAxis == 0 and yAxis == 1: direction = 90
            elif xAxis == 1 and yAxis == 1: direction = 45
            elif xAxis == 1 and yAxis == 0: direction = 0
            elif xAxis == 1 and yAxis == -1: direction = 315
            elif xAxis == 0 and yAxis == -1: direction = 270
            elif xAxis == -1 and yAxis == -1: direction = 225
            elif xAxis == -1 and yAxis == 0: direction = 180
            elif xAxis == -1 and yAxis == 1: direction = 135

            # set new coordinates
            x, y = (x + xAxis, y + yAxis)

            # Update the listener
            pool.update_listener_3d(x, y, 0, direction)

            if not direction == oldDirection:
                squeak.play()
                oldDirection = direction

            if (not oldX == x) or (not oldY == y):
                step.play()
                oldX, oldY = (x, y)

        if lucia.key_pressed(lucia.K_q):
            break
        elif lucia.key_pressed(lucia.K_c):
            lucia.output.speak(
                f"you are at x: {x}, y: {y}, facing {textDirections[direction]}"
            )
        elif lucia.key_pressed(lucia.K_z):
            x = 0
            y = 0
            direction = 90
            pool.update_listener_3d(x, y, 0, direction)
Esempio n. 2
0
def main():
    #Show window
    lucia.show_window("Keyboard Example.")
    while 1:
        #Allow lucia to update it's internal queues and events
        lucia.process_events()
        #Check for key presses
        #Typically this should be done in a separate function, but we will keep this as simple as possible
        if lucia.key_pressed(lucia.K_SPACE):
            print("You pressed space!")
            sys.exit()
        #Check for key holds
        if lucia.key_down(lucia.K_RETURN):
            print("You held enter!")
            lucia.quit()
            sys.exit()
        #Sleep for 2 milliseconds
        lucia.pygame.time.wait(2)
Esempio n. 3
0
def MainLoop():
    global x, y, width, height
    timer = utils.Timer(
    )  # Prevents the user from walking faster than the sound can play. You're not the Flash.
    WALK_SPEED = 300  # milliseconds
    timer.elapsed = WALK_SPEED  # So the user can walk straight away
    xAxis, yAxis = (0, 0)

    # Define the shapes
    # Each vertice followed by a fill value (True fills the shape in, False plots only the outline)
    sky = utils.Rectangle(0, 29, 29, 19,
                          True)  # Top left corner to bottom left corner
    ground = utils.Rectangle(0, 18, 29, 0, True)
    sun = utils.Circle(4, 25, 3,
                       True)  # The center point followed by the radius
    roof = utils.Triangle(11, 22, 25, 22, 18, 27,
                          True)  # The three corner vertices of the triangle
    house = utils.Rectangle(13, 21, 23, 13, True)

    # Draw each shape one by one
    for v in sky:
        field[v[0]][v[1]] = "blue"
    for v in ground:
        field[v[0]][v[1]] = "green"
    for v in sun:
        field[v[0]][v[1]] = "yellow"
    for v in roof:
        field[v[0]][v[1]] = "red"
    for v in house:
        field[v[0]][v[1]] = "brown"

    # Main action loop
    while True:
        time.sleep(0.005)  # Be kind to processors
        lucia.process_events()

        # Set the movement coordinate changes
        if lucia.key_down(lucia.K_UP): yAxis = 1
        if lucia.key_down(lucia.K_DOWN): yAxis = -1
        if lucia.key_up(lucia.K_UP) and lucia.key_up(lucia.K_DOWN): yAxis = 0
        if lucia.key_down(lucia.K_RIGHT): xAxis = 1
        if lucia.key_down(lucia.K_LEFT): xAxis = -1
        if lucia.key_up(lucia.K_RIGHT) and lucia.key_up(lucia.K_LEFT):
            xAxis = 0

        # Only do something if the keys are pressed and slow down any change based on the speed of movement
        if (not xAxis == 0 or not yAxis == 0) and timer.elapsed >= WALK_SPEED:
            timer.restart()

            # set new coordinates
            if x + xAxis >= 0 and y + yAxis >= 0 and x + xAxis < width and y + yAxis < height:  # Check to be sure the coordinates are within the field
                x, y = (x + xAxis, y + yAxis)
                lucia.output.speak(str(field[x][y]))
            else:
                lucia.output.speak("bump")

        # Some other function keys (These are not restricted by the walk speed)
        if lucia.key_pressed(lucia.K_q):  # Quit the program
            break
        elif lucia.key_pressed(lucia.K_z):  # Speak the coordinates of the user
            lucia.output.speak(f"you are at x: {x}, y: {y}")
Esempio n. 4
0
    def run(self, intro="", isfile=False):
        """ Displayes the menu and waits for a selection

		This function blocks until the menu is closed, either by selecting an item or pressing escape.
		Available controls are up/down arrows, tab and shift + tab, ctrl + up and down, space and enter, and escape.

		args:
		        intro (str, optional): Some brief instructions spoken after the menu title.
		    isfile (bool, optional): Set to True if the intro is the name of an audio file that contains the intro

		returns:
		        The name of a file or directory
		"""

        try:
            self._running = True

            self.__play__("open")
            if self._title: lucia.output.speak(self._title)
            if intro:
                if not isfile:
                    lucia.output.speak(intro, False)
                else:
                    intro = self.__load__(intro)
                    intro.play()

            while self._running:
                time.sleep(0.005)  # Be kind to processors
                lucia.process_events()

                if callable(self._callback):
                    self._callback(self)

                if lucia.key_pressed(lucia.K_ESCAPE) and self.exit_on_escape:
                    self._running = False
                    self.__play__("cancel")
                    return "-1"
                elif lucia.key_pressed(lucia.K_DOWN):
                    if lucia.key_down(lucia.K_LALT) or lucia.key_down(
                            lucia.K_RALT):
                        self.__forward__()
                    else:
                        self.__change_position__(1)
                elif lucia.key_pressed(lucia.K_UP):
                    if lucia.key_down(lucia.K_LALT) or lucia.key_down(
                            lucia.K_RALT):
                        self.__back__()
                    else:
                        self.__change_position__(-1)
                if lucia.key_pressed(lucia.K_TAB):
                    if lucia.key_down(lucia.K_RSHIFT) or lucia.key_down(
                            lucia.K_LSHIFT):
                        self.__change_tab__(-1)
                    else:
                        self.__change_tab__(1)
                elif lucia.key_pressed(lucia.K_RETURN) or lucia.key_pressed(
                        lucia.K_SPACE):
                    result = self.__return_selection__()
                    if result:
                        self._running = False
                        return result
        except Exception as e:
            lucia.logger.exception(e)
        finally:
            self._running = False
Esempio n. 5
0
 def loop(self):
     while 1:
         time.sleep(0.005)
         try:
             lucia.process_events()
             if callable(self.callback):
                 self.callback()
             if lucia.key_pressed(lucia.K_RETURN):
                 if self.items[self.itempos].can_return:
                     if self.entersound != "":
                         source = self.pool.play_stationary(self.entersound)
                     list_values = []
                     if self.items[self.itempos].event == CANCELEVENT:
                         if callable(
                                 self.items[self.itempos].item_function):
                             self.items[self.itempos].item_function()
                         return list_values
                     #returns results as a list of dictionaries. The currently focused item returns as the first item in the list
                     for x in self.items:
                         if x.name != self.items[self.itempos].name:
                             list_values.append({
                                 "name":
                                 x.name,
                                 "value":
                                 x.value,
                                 "toggle_value":
                                 x.toggle_value
                             })
                     list_values.insert(
                         0,
                         {
                             "name":
                             self.items[self.itempos].name,
                             "value":
                             self.items[self.itempos].value,
                             "toggle_value":
                             self.items[self.itempos].toggle_value,
                         },
                     )
                     if callable(self.items[self.itempos].item_function):
                         self.items[self.itempos].item_function()
                     else:
                         return list_values
             elif lucia.key_pressed(lucia.K_UP):
                 if self.itempos > 0:
                     self.itempos -= 1
                     if self.clicksound != "":
                         clicksound = self.pool.play_stationary(
                             self.clicksound)
                     if callable(self.on_index_change):
                         self.on_index_change()
                     if callable(self.items[self.itempos].on_focus):
                         self.items[self.itempos].on_focus(self)
                     if (self.items[self.itempos].has_value == False
                             and self.items[self.itempos].can_be_toggled
                             == False):
                         lucia.output.speak(self.items[self.itempos].name)
                     elif (self.items[self.itempos].has_value
                           and self.items[self.itempos].can_be_toggled
                           == False):
                         lucia.output.speak(
                             self.items[self.itempos].name + ": " +
                             str(self.items[self.itempos].value) +
                             ". Press left shift or right shift to change this item's value"
                         )
                     elif (self.items[self.itempos].has_value == False
                           and self.items[self.itempos].can_be_toggled):
                         if self.items[self.itempos].toggle_value == True:
                             lucia.output.speak(
                                 self.items[self.itempos].name + ": " +
                                 self.items[self.itempos].on_value + " " +
                                 self.items[
                                     self.itempos].toggle_item_message +
                                 " " + self.items[self.itempos].off_value)
                         else:
                             lucia.output.speak(
                                 self.items[self.itempos].name + ": " +
                                 self.items[self.itempos].off_value + " " +
                                 self.items[
                                     self.itempos].toggle_item_message +
                                 " " + self.items[self.itempos].on_value)
                     elif (self.items[self.itempos].has_value
                           and self.items[self.itempos].can_be_toggled):
                         speakstr = (self.items[self.itempos].name +
                                     ". Value: " +
                                     str(self.items[self.itempos].value))
                         if self.items[self.itempos].toggle_value == True:
                             speakstr += ". Switch: " + self.items[
                                 self.itempos].on_value
                         else:
                             speakstr += ". Switch: " + self.items[
                                 self.itempos].off_value
                         lucia.output.speak(
                             speakstr +
                             ". Press left shift or right shift to change this item's value. "
                             + self.items[self.itempos].toggle_item_message)
             elif lucia.key_pressed(lucia.K_DOWN):
                 if self.itempos < len(self.items) - 1:
                     self.itempos += 1
                     if self.clicksound != "":
                         clicksound = self.pool.play_stationary(
                             self.clicksound)
                     if self.items[self.itempos].on_focus != None:
                         self.items[self.itempos].on_focus()
                     if self.on_index_change != None:
                         self.on_index_change()
                     if (self.items[self.itempos].has_value == False
                             and self.items[self.itempos].can_be_toggled
                             == False):
                         lucia.output.speak(self.items[self.itempos].name)
                     elif (self.items[self.itempos].has_value
                           and self.items[self.itempos].can_be_toggled
                           == False):
                         lucia.output.speak(
                             self.items[self.itempos].name + ": " +
                             str(self.items[self.itempos].value) +
                             ". Press left shift or right shift to change this item's value"
                         )
                     elif (self.items[self.itempos].has_value == False
                           and self.items[self.itempos].can_be_toggled):
                         if self.items[self.itempos].toggle_value == True:
                             lucia.output.speak(
                                 self.items[self.itempos].name + ": " +
                                 self.items[self.itempos].on_value + " " +
                                 self.items[
                                     self.itempos].toggle_item_message +
                                 " " + self.items[self.itempos].off_value)
                         else:
                             lucia.output.speak(
                                 self.items[self.itempos].name + ": " +
                                 self.items[self.itempos].off_value + " " +
                                 self.items[
                                     self.itempos].toggle_item_message +
                                 " " + self.items[self.itempos].on_value)
                     elif (self.items[self.itempos].has_value
                           and self.items[self.itempos].can_be_toggled):
                         speakstr = (self.items[self.itempos].name +
                                     ". Value: " +
                                     str(self.items[self.itempos].value))
                         if self.items[self.itempos].toggle_value == True:
                             speakstr += ". Switch: " + self.items[
                                 self.itempos].on_value
                         else:
                             speakstr += ". Switch: " + self.items[
                                 self.itempos].off_value
                         lucia.output.speak(
                             speakstr +
                             ". Press left shift or right shift to change this item's value. "
                             + self.items[self.itempos].toggle_item_message)
             elif lucia.key_pressed(lucia.K_SPACE):
                 if self.itempos > -1 and self.itempos < len(self.items):
                     if self.items[self.itempos].can_be_toggled:
                         if self.entersound != "":
                             entersound = self.pool.play_stationary(
                                 self.entersound)
                         if self.items[self.itempos].toggle_value == True:
                             lucia.output.speak(
                                 self.items[self.itempos].off_value)
                             self.items[self.itempos].toggle_value = False
                         else:
                             lucia.output.speak(
                                 self.items[self.itempos].on_value)
                             self.items[self.itempos].toggle_value = True
             elif lucia.key_down(lucia.K_LSHIFT) or lucia.key_down(
                     lucia.K_RSHIFT):
                 if self.items[self.itempos].has_value == True:
                     self.items[self.itempos].value = getinput(
                         "change value",
                         self.items[self.itempos].name + ". ",
                         value=self.items[self.itempos].value,
                         mode=self.items[self.itempos].value_mode,
                     )
                     lucia.output.speak("value set to " +
                                        str(self.items[self.itempos].value))
                     if self.entersound != "":
                         entersound = self.pool.play_stationary(
                             self.entersound)
         except Exception as e:
             lucia.output.speak(str(e))
Esempio n. 6
0
# You should have received a copy of the GNU Lesser General Public License
# along with this program.  If not, see https://github.com/LuciaSoftware/lucia/blob/master/LICENSE.

# this is a simple testing game, used to show the features of lucia.
# Add this repository's lucia package to the PYTHON PATH, so this example can find the lucia module.
import sys

sys.path.append("../..")
import time

print("Importing lucia")
import lucia
import pygame

print("Initializing lucia")
lucia.initialize(audiobackend=lucia.AudioBackend.BASS)

print("Showing the window")
test = lucia.show_window()

while 1:
    time.sleep(0.005)
    lucia.process_events()
    if lucia.key_pressed(pygame.K_a):
        lucia.output.speak("a is pressed")
    if lucia.key_down(pygame.K_s):
        lucia.output.speak("s held down")
    if lucia.key_pressed(pygame.K_ESCAPE):
        lucia.quit()
        sys.exit()