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.


    [SOLVED!] Help needed (restart button)

    Pythonista
    2
    3
    1365
    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.
    • Pythonistapro777
      Pythonistapro777 last edited by

      This is the last help I will be needing for this project. At the end of the code I created a restart scene with a restart button that runs JitterClick scene. However, it doesn't restart the scene it just continues where it left off. So I need help with that. Also, I new help with making the text underneath the start button to be white, it used to be white but it randomly turned black.

      (btw this isn't the FULL code, this so just the part I need help with)

      Thank's in advance!

      Here's the code:
      <pre><code>
      #coding: utf-8
      import time
      from scene import *
      from PIL import Image
      clicks = 0
      class JitterClick(Scene):
      def init(self):
      self.start_time = 0
      self.finish_time = 0
      def setup(self):
      self.button = Button(Rect(self.size.w/2-100, self.size.h/2-140, 200, 200))
      self.button.background = Color(0,0,0)
      self.button.stroke = Color(0,0,0)
      self.button.image = 'Red_Circle'
      self.button.action = self.add_clicks
      self.add_layer(self.button)
      def add_clicks( sender):
      global clicks
      clicks += 1
      if clicks == 1:
      sender.start_time = time.time()
      def draw(self):
      background(0,0,0)
      self.button.background = Color(0,0,0)
      self.button.draw()
      text('Taps: %i' % clicks, x=self.size.w/2, y=self.size.h/3.83, font_size=57)
      if clicks == 100:
      self.finish_time = time.time()
      run(desp(clicks, self.finish_time - self.start_time))
      class desp(Scene):
      def init(self, xclicks, duration):
      self.xclicks = xclicks
      self.duration = duration
      self.duration = "%.3f" % self.duration
      self.speed = duration/xclicks
      self.speed = "%.3f" % self.speed
      def setup(self):
      self.show_instructions = True
      self.p_size = 64 if self.size.w > 700 else 32
      def draw(self):
      background(0, 0, 0)
      text('\n\n\n\n\n\nIt took you \n{} seconds \nto tap {} \ntimes. You were\ntapping at \n{} taps a\nsecond.'.format(self.duration, self.xclicks, self.speed), x=self.size.w/2, y=self.size.h/3.8
      3, font_size=42)
      def touch_ended(self, touch):
      run(restart())
      class restart(Scene):
      def setup(self):
      self.button = Button(Rect(self.size.w/2-80, self.size.h/2--45, 150, 150), 'Restart')
      self.button.background = Color(0,0,0)
      self.button.stroke = Color(0,0,0)
      self.button.image = 'Blue_Circle'
      self.button.action = self.add_clicks
      self.add_layer(self.button)
      def add_clicks( sender):
      run(JitterClick())
      def draw(self):
      background(0,0,0)
      self.button.background = Color(0,0,0)
      self.button.draw()
      s = 45 if self.size.w > 100 else 7
      text('\n\n\nOr you can\nclick the ❎\nat the top\nright of the\nscreen to exit.', 'Futura', s, *self.bounds.center().as_tuple())
      run(JitterClick())
      </code></pre>

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

        The text appears white for me. But the restart problem was your global clicks. I just changed it in your code. I also changed the param to your button action to self.

        
        #coding: utf-8
        import time
        from scene import *
        from PIL import Image
        #clicks = 0
        class JitterClick(Scene):
        	def __init__(self):
        		self.start_time = 0
        		self.finish_time = 0
        		self.clicks = 0
        
        	def setup(self):
        		self.button = Button(Rect(self.size.w/2-100, self.size.h/2-140, 200, 200))
        		self.button.background = Color(0,0,0)
        		self.button.stroke = Color(0,0,0)
        		self.button.image = 'Red_Circle'
        		self.button.action = self.add_clicks
        		self.add_layer(self.button)
        	def add_clicks( self):
        		#global clicks
        		self.clicks += 1
        		if self.clicks == 1:
        			self.start_time = time.time()
        	def draw(self):
        		background(0,0,0)
        		self.button.background = Color(0,0,0)
        		self.button.draw()
        		text('Taps: %i' % self.clicks, x=self.size.w/2, y=self.size.h/3.8*3, font_size=57)
        		if self.clicks == 5:
        			self.finish_time = time.time()
        			run(desp(self.clicks, self.finish_time - self.start_time))
        class desp(Scene):
        	def __init__(self, xclicks, duration):
        		self.xclicks = xclicks
        		self.duration = duration
        		self.duration =  "%.3f" % self.duration
        		self.speed = duration/xclicks
        		self.speed =  "%.3f" % self.speed
        	def setup(self):
        		self.show_instructions = True
        		self.p_size = 64 if self.size.w > 700 else 32
        	def draw(self):
        		background(0, 0, 0)
        		text('\n\n\n\n\n\nIt took you \n{} seconds \nto tap {} \ntimes. You were\ntapping at \n{} taps a\nsecond.'.format(self.duration, self.xclicks, self.speed), x=self.size.w/2, y=self.size.h/3.8*3, font_size=42)
        	def touch_ended(self, touch):
        		run(restart())
        class restart(Scene):
        	def setup(self):
        		self.button = Button(Rect(self.size.w/2-80, self.size.h/2--45, 150, 150), 'Restart')
        		self.button.background = Color(0,0,0)
        		self.button.stroke = Color(0,0,0)
        		self.button.image = 'Blue_Circle'
        		self.button.action = self.add_clicks
        		self.add_layer(self.button)
        	def add_clicks( sender):
        		run(JitterClick())
        	def draw(self):
        		background(0,0,0)
        		self.button.background = Color(0,0,0)
        		self.button.draw()
        		s = 45 if self.size.w > 100 else 7
        		text('\n\n\nOr you can\nclick the ❎\nat the top\nright of the\nscreen to exit.', 'Futura', s, *self.bounds.center().as_tuple())
        run(JitterClick())
        
        1 Reply Last reply Reply Quote 0
        • Pythonistapro777
          Pythonistapro777 last edited by

          Thank you! @Phuket2

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