Esempio n. 1
0
    o = Point.origin()

    random_force = Force.random()
    # print(random_force)
    random_force += gravity
    # print(random_force)

    result = Force.sum([gravity, propulsion, random_force])
    # print("Force.sum:",result)

    x, y = result
    # print(x,y) #Unpacking is compatible for vectors

    f = gravity
    print(result)

    from mycontext import Context

    context = Context()
    position = (0, 0)
    while context.open:
        context.check()
        context.control()
        context.clear()
        context.show()
        position = context.point()
        f.components = list(position)
        f.show(context, o)
        context.flip()
        # context.wait(0.1)
Esempio n. 2
0
class Manager:
    def __init__(self,title="Unnamed"):
        """Create a manager using a context, this methods it to be overloaded."""
        self.context=Context(name=title)

    def __call__(self):
        """Call the main loop, this method is to be overloaded."""
        self.main()

    def main(self):
        """Main loop of the simple manager."""
        self.setup() #Name choices inspired from processing
        while self.context.open:
            self.loop()

    def setup(self):
        """Code executed before the loop."""
        pass

    def loop(self):
        """Code executed during the loop."""
        self.eventsLoop()
        self.updateLoop()
        self.showLoop()

    def eventsLoop(self):
        """Deal with the events in the loop."""
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                self.reactQuit()
            if event.type == KEYDOWN:
                self.reactKeyDown(event.key)
            if event.type == pygame.MOUSEBUTTONDOWN:
                self.reactMouseButtonDown(event.button)


    def reactQuit(self,event):
        """React to a quit event."""
        self.context.open=False

    def reactKeyDown(self,key):
        """React to a keydown event."""
        if key == K_ESCAPE:
            self.context.open=False

    def reactMouseButtonDown(self,button):
        """React to a mouse button down event."""
        if button == 4: self.context.draw.plane.zoom([1.1,1.1])
        if button == 5: self.context.draw.plane.zoom([0.9,0.9])



    def updateLoop(self):
        """Update the manager while in the loop."""
        self.update()

    def update(self):
        """Update the components of the manager of the loop. This method is to be
        overloaded."""
        pass

    def showLoop(self):
        """Show the graphical components and deal with the context in the loop."""
        self.context.control()
        self.context.clear()
        self.context.show()
        self.show()
        self.context.showConsole()
        self.context.flip()

    def show(self):
        """Show the graphical components on the context. This method is to be
        overloaded."""
        pass
Esempio n. 3
0
    def showBar(self, context, t, l):
        """Show the bar."""
        v = self.getVector()
        v *= l
        p = self(t)
        v.rotate(math.pi / 2)
        p1 = v(p)
        v.rotate(math.pi)
        p2 = v(p)
        s = Segment(p1, p2)
        s.color = self.color
        s.show(context)


if __name__ == "__main__":
    from mycontext import Context

    surface = Context()
    o = Point.origin()
    s = Step.random()
    print(s)
    while surface.open:
        surface.check()
        surface.control()
        surface.clear()
        surface.show()
        s.rotate(0.001, o)
        o.show(surface)
        s.show(surface)
        surface.flip()
Esempio n. 4
0
class Manager:
    def __init__(self, name="Manager", dt=10e-3, **kwargs):
        """Create a manager using a context, this methods it to be overloaded."""
        self.context = Context(name=name, **kwargs)
        self.count = self.context.count
        self.pause = False
        self.dt = dt
        # Typing stuff
        self.typing = False
        self.alphabet = "abcdefghijklmnopqrstuvwxyz"
        self.caps_numbers = ")!@#$%^&*("
        self.numbers = "0123456789"
        self.typing = False
        self.shiftlock = False
        self.capslock = False
        self.altlock = False

    def __str__(self):
        """Return the string representation of the manager."""
        return type(self).__name__ + "(\n{}\n)".format("\n".join(
            map(lambda x: ":".join(map(str, x)), self.__dict__.items())))

    def __call__(self):
        """Call the main loop, this method is to be overloaded."""
        self.main()

    def main(self):
        """Main loop of the simple manager."""
        self.setup()  # Name choices inspired from processing
        while self.context.open:
            self.loop()

    def setup(self):
        """Code executed before the loop."""
        pass

    def loop(self):
        """Code executed during the loop."""
        self.eventsLoop()
        self.updateLoop()
        self.showLoop()

    def eventsLoop(self):
        """Deal with the events in the loop."""
        for event in pygame.event.get():
            self.react(event)

    def react(self, event):
        """React to the pygame events."""
        if event.type == QUIT:
            self.switchQuit()
        elif event.type == KEYDOWN:
            self.reactKeyDown(event.key)
        elif event.type == KEYUP:
            self.reactKeyUp(event.key)
        elif event.type == MOUSEBUTTONDOWN:
            self.reactMouseButtonDown(event.button, event.pos)
        elif event.type == MOUSEBUTTONUP:
            self.reactMouseButtonUp(event.button, event.pos)
        elif event.type == MOUSEMOTION:
            self.reactMouseMotion(event.pos)

    def switchQuit(self):
        """React to a quit event."""
        self.context.open = not (self.context.open)

    def reactKeyDown(self, key):
        """React to a keydown event."""
        self.reactAlways(key)
        if self.typing:
            self.reactTyping(key)
        else:
            self.reactMain(key)

    def reactKeyUp(self, key):
        """React to a keyup event."""
        pass

    def reactAlways(self, key):
        """React to a key whether or not the typing mode is on."""
        # print(key) for debugging the keys
        if key == K_ESCAPE:
            self.switchQuit()
        if key == K_SLASH or key == K_BACKSLASH:
            if not self.typing:
                self.context.console("Typing activated.")
            self.typing = True
        if key == K_BACKQUOTE:
            self.switchTyping()

    def reactLock(self, key):
        """React to a locking key."""
        if key == K_CAPSLOCK:
            self.capslock = not (self.capslock)
        elif key == K_LSHIFT or key == K_RSHIFT:
            self.shiftlock = True
        elif key == K_LALT or key == K_RALT:
            self.altlock = True

    def reactTyping(self, key):
        """React to a typing event."""
        self.reactLock(key)
        if self.altlock:
            self.reactAltCase(key)
        elif self.capslock or self.shiftlock:
            self.reactUpperCase(key)
        else:
            self.reactLowerCase(key)

        if key == K_SPACE:
            self.write(" ")
        elif key == 8:
            self.delete()
        if key == K_LCTRL:
            self.context.console.nextArg()
        elif key == K_UP:
            self.context.console.back()
        elif key == K_DOWN:
            self.context.console.forward()
        elif key == K_RETURN:
            self.eval()
            self.context.console.nextLine()

    def eval(self):
        """Execute a line."""
        content = self.context.console.line.content
        if content[0] == "/":
            for command in content[1:]:
                try:
                    self.context.console(str(eval(command)))
                except:
                    self.context.console("Invalid command.")
        if content[0] == "\\":
            for command in content[1:]:
                try:
                    exec(command)
                    self.context.console("Command " + command + " executed.")
                except Exception as e:
                    self.context.console(str(e))
        self.context.console.eval()

    def reactAltCase(self, key):
        """React when typing with alt key pressed."""
        if key == K_e:
            self.write("`")  # Stupid
        elif key == 167:
            self.write("´")

    def reactLowerCase(self, key):
        """React when typing in lower case."""
        d = {
            K_COMMA: ",",
            K_PERIOD: ".",
            K_SEMICOLON: ";",
            K_LEFTBRACKET: "[",
            K_RIGHTBRACKET: "]",
            39: "'",
            45: "-",
            K_EQUALS: "="
        }
        if 48 <= key <= 57:
            self.write(self.numbers[key - 48])
        elif 97 <= key <= 122:
            self.write(self.alphabet[key - 97])
        elif key in d:
            self.write(d[key])
        elif key == K_SLASH:
            if not self.context.console.line.empty:
                self.context.console.nextLine()
            self.write("/")
            self.context.console.nextArg()
        elif key == K_BACKSLASH:
            if not self.context.console.line.empty:
                self.context.console.nextLine()
            self.write("\\")
            self.context.console.nextArg()

    def reactUpperCase(self, key):
        """React to a key when typing in uppercase."""
        d = {59: ":''", 44: "<", 46: ">", 47: "?", 45: "_", 39: "\"", 61: "+"}
        if 48 <= key <= 57:
            self.write(self.caps_numbers[key - 48])
        elif 97 <= key <= 122:
            self.write(self.alphabet[key - 97].upper())
        elif key in d:
            self.write(d[key])

    def write(self, c):
        """Write some content."""
        self.context.console.lines[-1].content[-1] += c
        self.context.console.lines[-1].refresh()
        self.shiftlock = False
        self.altlock = False

    def delete(self, n=1):
        """Delete some content."""
        self.context.console.lines[-1].content[
            -1] = self.context.console.lines[-1].content[-1][:-n]
        self.context.console.lines[-1].refresh()

    def reactMain(self, key):
        """React as usual when not typing."""
        if key == K_f:
            self.switchFullscreen()
        if key == K_1:
            self.switchCapture()
        if key == K_2:
            self.switchCaptureWriting()
        if key == K_3:
            self.switchScreenWriting()
        if key == K_LALT:
            self.switchPause()

    def switchTyping(self):
        """Switch the typing mode."""
        self.typing = not (self.typing)
        if self.typing:
            self.context.console("Typing activated.")
            self.context.console.nextLine()
        else:
            self.context.console("Typing deactivated.")

    def switchScreenWriting(self):
        """Switch the screen writing mode."""
        if self.context.camera.screen_writing:
            self.context.camera.screen_writer.release()
        self.context.camera.switchScreenWriting()
        if self.context.camera.screen_writing:
            self.context.console('The screen is being written.')
        else:
            self.context.console('The screen video has been released')
            self.context.console('and is not being written anymore.')

    def switchCaptureWriting(self):
        """Switch the capture writing mode."""
        if self.context.camera.capture_writing:
            self.context.camera.capture_writer.release()
        self.context.camera.switchCaptureWriting()
        if self.context.camera.capture_writing:
            self.context.console('The capture is being written.')
        else:
            self.context.console('The capture video has been released')
            self.context.console('and is not being written anymore.')

    def switchPause(self):
        """React to a pause event."""
        self.pause = not self.pause
        if self.pause:
            self.context.console('The system is paused.')
        else:
            self.context.console('The system is unpaused.')

    def switchCapture(self):
        """React to a capture event."""
        self.context.camera.switchCapture()
        if self.context.camera.capturing:
            self.context.console('The camera capture is turned on.')
        else:
            self.context.console('The camera capture is turned off.')

    def switchFullscreen(self):
        """React to a fullscreen event."""
        self.context.switch()
        if self.context.fullscreen:
            self.context.console("The fullscreen mode is set.")
        else:
            self.context.console("The fullscreen mode is unset.")

    def reactMouseButtonDown(self, button, position):
        """React to a mouse button down event."""
        if button == 4:
            self.context.draw.plane.zoom([1.1, 1.1])
        if button == 5:
            self.context.draw.plane.zoom([0.9, 0.9])

    def reactMouseButtonUp(self, button, position):
        """React to a mouse button up event."""
        pass

    def reactMouseMotion(self, position):
        """React to a mouse motion event."""
        pass

    def updateLoop(self):
        """Update the manager while in the loop."""
        if not self.pause:
            self.update()
            self.count()
        self.context.camera.write(
        )  # Write on the camera writers if they are on

    def update(self):
        """Update the components of the manager of the loop. This method is to be
        overloaded."""
        pass

    def showLoop(self):
        """Show the graphical components and deal with the context in the loop."""
        if not self.typing:  # Ugly fix for easier pratical use
            self.context.control()
        self.context.clear()
        self.context.show()
        self.show()
        self.showCamera()
        self.context.console.show()
        self.context.flip()

    def show(self):
        """Show the graphical components on the context. This method is to be
        overloaded."""
        pass

    def showCamera(self):
        """Show the camera if active."""
        if self.context.camera.capturing:
            self.context.camera.show()

    def counter():
        doc = "The Counter property."

        def fget(self):
            """Bind the counter of the manager to the one of the context."""
            return self.context.counter

        def fset(self, counter):
            """Set the counter of the context."""
            self.context.counter = counter

        return locals()

    counter = property(**counter())