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.


    Syntax for scene_drawing module...

    Pythonista
    2
    5
    3170
    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.
    • themusicman
      themusicman last edited by

      Hi All

      So I am trying to draw a simple line in Pythonista. I have selected a new game type template and the documentation states;

      scene_drawing — Drawing Functions for the scene module
      The functions in this module can be used within the scene.Scene.draw() method when using the classic render loop mode of the scene module.
      

      The default game/animation template is thus;

      from scene import *
      import sound
      import random
      import math
      
      A = Action
      
      class MyScene(Scene):
      	def setup(self):
      		pass
      
      	def did_change_size(self):
      		pass
      
      	def update(self):
      		pass
      
      	def touch_began(self, touch):
      		pass
      
      	def touch_moved(self, touch):
      		pass
      
      	def touch_ended(self, touch):
      		pass
      
      if __name__ == '__main__':
      	run(MyScene(), show_fps=False)
      
      

      From the documentation I think I need to set up a def draw(self): function, but I am not sure what the documentation means when it refers to the scene.Scene.draw() method when using the classic render loop mode of the scene module.

      Could someone help explain the syntax of what I need to include where please. Many thanks.

      1 Reply Last reply Reply Quote 0
      • JonB
        JonB last edited by

        there are sort of two types of Scene.

        The first, newer style, uses SpriteNodes, and has its update method called each frame.

        The second uses a draw() method, called every frame, where you can use the scene drawing methods. those methods must be used inside of draw() because that is where a drawing context is set up for you.

        As an alternative, you can use a ui.ImageContext and the Path drawing commands, then get the image from the context, and set as the texture to a sprite node. Or, for simple path drawing, you can use ShapeNodes.

        i think there are probably some old examples in the pythonista tools repo.

        1 Reply Last reply Reply Quote 0
        • themusicman
          themusicman last edited by

          Thanks @jonb much appreciated. I’ve now managed to get the webIDE up and running, too!

          So, I have done some stuff using the spritenode system, and managed to pretty much successfully (well at least for me!) to get a version of connect4 coded and working using the default game/animation template as the start. However, I couldn’t find any way of drawing a simple line... surely I am missing something here Jon, and one should easily be able to do that somehow? Any pointers?

          1 Reply Last reply Reply Quote 0
          • JonB
            JonB last edited by

            If you wanted to,say, draw a line connecting the 4, over the top, you would use a ShapeNode. Things are a little annoying because, iirc, the shape gets autoresized in annoying ways.

            But effectively something like
            P=ui.Path()
            P.line_to(dx, dy)
            L=ShapeNode(path=P)
            L.anchor_point=(dx<0,dy<0)

            You need to set the anchor point depending on the signs of dx and dy-- if you want the x,y position of L to set the location of the start of the line -- if the line is going right, the anchor point should be on the left, etc.

            You may need to set the ShapeNode line color, fill color, stroke width, etc.

            1 Reply Last reply Reply Quote 0
            • JonB
              JonB last edited by

              For what it's worth -- https://forum.omz-software.com/topic/4168/analog-clock-example-to-ui-example-help
              Had an example of a scene that used a rounded_rect ShapeNode, setting the rotation to cause it to rotate. Likewise this gives you more control of your line -- you would have to calculate the angle to rotate based on your desired start/end points (using math.atan)

              1 Reply Last reply Reply Quote 0
              • First post
                Last post
              Powered by NodeBB Forums | Contributors