Forum Archive

[SOLVED!] Help with counting

Pythonistapro777

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:


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())
Phuket2

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())
Pythonistapro777

YOU'RE A G!
Thanks so much!