omz:forum

    • Register
    • Login
    • Search
    • Recent
    • Popular

    Welcome!

    This is the community forum for my apps Pythonista and Editorial.

    For individual support questions, you can also send an email. If you have a very short question or just want to say hello — I'm @olemoritz on Twitter.


    Control Ui in Scene?

    Pythonista
    2
    3
    1617
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • DoinStuffMobile
      DoinStuffMobile last edited by

      Hello everyone!

      I was wondering if it is possible to put a joystick-like control on a game in scene (think like on a mobile rpg game)?

      I have tried different combinations of touch_began and moved but the three things i am looking for are:

      1. Character moves in direction the touch is dragged from initial touch, but not to the location of touch

      2. For this to only happen on one side of the screen (example : only on the left side)

      3. Is possible to have an overlay that includes a joystick on the left and buttons on the right, for example. I can provide pictures if i haven't described this well enough.

      Thank you for any guidance or links to where to look. I've only looked at the Scene and Turtle documentation and tinkered around.

      If this is not possible I also was wondering if Pythonista's Turtle module would be getting keyboard inputs back since the popularity of bluetooth, smart, and magic keyboards? I think it currently doesnt include them, right? 😅

      Thanks again! I haven't ever made a thread before, but I've learned a lot from reading others.
      DoinStuff

      mikael 2 Replies Last reply Reply Quote 0
      • mikael
        mikael @DoinStuffMobile last edited by

        @DoinStuffMobile, all of your goals are feasible and nothing too exotic.

        The trick to handling touches on different sides of the screen in Scene is to use touch_id property of a touch to differentiate between them, and of course the location to see which side of the screen the touch starts in.

        Connecting external keyboard keypresses to turtle logic seems easy as well, but I have done nothing with physical keyboards and Pythonista.

        1 Reply Last reply Reply Quote 1
        • mikael
          mikael @DoinStuffMobile last edited by

          @DoinStuffMobile, here’s a very simple example of touches on different sides of the screen controlling different things:

          from scene import *
          import math
          
          class MyScene (Scene):
          
              def setup(self):
                  l = self.left_ship = SpriteNode('spc:PlayerShip1Orange')
                  l.position = (self.size.width / 4, self.size.height / 2)
                  l.rotation = -math.pi / 2
                  self.add_child(l)
                  
                  r = self.right_ship = SpriteNode('spc:PlayerShip3Blue')
                  r.position = (self.size.width / 4*3, self.size.height / 2)
                  r.rotation = math.pi / 2
                  self.add_child(r)
                  
                  self.left_touch = self.right_touch = None
                  
              def touch_began(self, t):
                  on_left = t.location.x < self.size.width / 2
                  if on_left and self.left_touch is None:
                      self.left_touch = t.touch_id
                  elif not on_left and self.right_touch is None:
                      self.right_touch = t.touch_id
                      
              def touch_moved(self, t):
                  touch = t.touch_id
                  if not touch in (self.left_touch, self.right_touch):
                      return
                      
                  delta_y = t.location.y - t.prev_location.y
                  
                  if touch == self.left_touch:
                      ship = self.left_ship
                  elif touch == self.right_touch:
                      ship = self.right_ship
                      
                  x, y = ship.position
                  ship.position = x, y + delta_y
                      
              def touch_ended(self, t):
                  touch = t.touch_id
                  
                  if touch == self.left_touch:
                      self.left_touch = None
                  elif touch == self.right_touch:
                      self.right_touch = None
                   
          run(MyScene())
          
          1 Reply Last reply Reply Quote 1
          • First post
            Last post
          Powered by NodeBB Forums | Contributors