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 with counting

    Pythonista
    2
    3
    1418
    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

      In this game, the player will have to click a hundred times as quickly as possible. However, I need help with calculating the time it takes them to click 100 times.
      Or even if I can't count the time, could you help me with creating a count loop once the button is first clicked.

      Thanks in advance!

      Here's the code:

      <pre><code>
      import time
      from scene import *
      from PIL import Image
      clicks = 0
      t=0
      class JitterClick(Scene):
      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
      	
      def draw(self):
      	background(0,0,0)
      	self.button.background = Color(0,0,0)
      	self.button.draw()
      	text('Clicks: %i' % clicks, x=self.size.w/2, y=self.size.h/3.8*3, font_size=57)
      	if clicks == 100:
      		run(desp())
      

      class desp(Scene):
      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('It took you --\nto click 100\ntimes. You were\nclicking at --', x=self.size.w/2, y=self.size.h/3.8*3, font_size=35)
      

      run(JitterClick())
      </code></pre>

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

        Again, I will say I know nothing about using Scene. Never used it. Maybe what I wrote is stupid. Not sure. But can't understand why you are using a global for clicks. It appears to me that the init method works correctly inside the scene. From your button action you can access you class variables. You have used the param sender as a name, I think it should be named self. I know it doesn't matter the name, but that param name through me off for a while until I realised it was a reference to the class instance. Ok, regardless, I hope this helps. If I am am way off, I am sure one of the other more exp. guys will jump in.

        I didn't try to format your output correctly.

        
        import time
        from scene import *
        from PIL import Image
        clicks = 0
        t=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('Clicks: %i' % clicks, x=self.size.w/2, y=self.size.h/3.8*3, font_size=57)
        		if clicks == 5:
        			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
        		
        	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('It took you {}\nto click {}\ntimes. You were\nclicking at --'.format(self.duration, self.xclicks), x=self.size.w/2, y=self.size.h/3.8*3, font_size=35)
        
        run(JitterClick())
        
        1 Reply Last reply Reply Quote 0
        • Pythonistapro777
          Pythonistapro777 last edited by

          YOU'RE A G!
          Thanks so much!

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