Posts: 5
Threads: 1
Joined: Jun 2020
Jun-13-2020, 01:48 PM
(This post was last modified: Jun-13-2020, 02:24 PM by Yoriz.)
Hiya guys. It's been a while and this place has changed a bit. Well, I'm working on a clicker-style game and I need an accurate way to calculate clicks per second (cps). I've sort of hard-coded a simple way of doing it, but I find it isn't as accurate.
Code:
///===CREATE EVENT===///
taps=0;// number of taps/clicks player makes
frames=0;// initial time keeping variable
sec=0;// conversion from frames to seconds
taps_per_sec=0;// how many taps/clicks per second
///===STEP EVENT===///
//...increase time
frames += 1;
//...convert time (frames) to sec (seconds)
sec = frames / room_speed;
//...find tps
taps_per_sec = ceil(taps / sec);
///===ALARM 0===///
// Each time the player clicks the alarm is reset to room_speed and taps is
// increased by 1
///...reset
taps=0;
frames=0;
taps_per_sec=0; This works a okay, but I'm in need of a more accurate method. This is where algebra comes in and I stink at creating algebraic equations. Maybe if there is a time stamp of each click and then calculating the time in between clicks, like the pneumatic road tubes calculating car speed. I don't know. I'm kind of stuck here though so any help would be appreciated.
Posts: 419
Threads: 34
Joined: May 2019
I think if you put a counter in your events loop it will do what you want: if event.type == pygame.MOUSEBUTTONDOWN and event.button == LEFT:
self.click_counter += 1 No one is going to click faster that 60 times per second, so it should be pretty accurate.
Posts: 544
Threads: 15
Joined: Oct 2016
1. What gui is this for ?
Pygame example. Any less 200 millisecond. Probably won't be detected.
import os
import pygame
class Screen:
@staticmethod
def center():
os.environ['SDL_VIDEO_CENTERED'] = '1'
def __init__(self, caption, width, height, flags=0):
pygame.display.set_caption(caption)
self.surface = pygame.display.set_mode((800, 600), flags)
self.rect = self.surface.get_rect()
self.clock = pygame.time.Clock()
self.delta = 0
self.fps = 60
def idle(self):
self.delta = self.clock.tick(self.fps)
class Label:
def __init__(self, text, font, color, position, anchor):
self._text = text
self._font = font
self._color = color
self._position = position
self._anchor = anchor
self.render()
def draw(self, surface):
surface.blit(self.image, self.rect)
def render(self):
self.image = self._font.render(self._text, 1, self._color)
self.rect = self.image.get_rect(**{self._anchor: self._position})
def set_text(self, text):
self._text = text
self.render()
def set_color(self, color):
self._color = color
self.render()
def main():
pygame.init()
Screen.center()
screen = Screen("Mouse Double Click", 800, 600)
background = pygame.Color("black")
font = pygame.font.Font(None, 32)
position = screen.rect.centerx, 20
label_click = Label("Single Click", font, pygame.Color("lawngreen"), position, "midtop")
# Double click variables.
click_start = 0
click_within = 200
single_click = False
running = True
while running:
ticks = pygame.time.get_ticks()
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
elif event.type == pygame.MOUSEBUTTONDOWN:
if event.button == 1:
if click_start < ticks < click_start + click_within:
# Double click action
label_click.set_text("Double Click - {}".format(ticks - click_start))
single_click = False
click_start = 0
else:
# Set double click variable
click_start = ticks
single_click = True
if single_click and ticks > click_start + click_within:
# Single click action.
label_click.set_text("Single Click")
single_click = False
click_start = 0
screen.surface.fill(background)
label_click.draw(screen.surface)
pygame.display.update()
screen.idle()
if __name__ == "__main__":
main()
99 percent of computer problems exists between chair and keyboard.
Posts: 5
Threads: 1
Joined: Jun 2020
Jun-14-2020, 05:04 AM
(This post was last modified: Jun-14-2020, 05:46 AM by buran.)
Thank you for your quick responses!
CLICK_BITE LINK REMOVED with this refer some user created this site, please check out I got surprised by seeing website like this very impressive design and function.
Posts: 8,160
Threads: 160
Joined: Sep 2016
The code in your original post is not python. This looks like very much like advertisment thread, just to promote the link in your latest post which I now removed.
If you have python related question - post your python code, in python tags. ask specific questions.
Posts: 2
Threads: 0
Joined: Feb 2021
Feb-09-2021, 07:33 PM
(This post was last modified: Mar-20-2022, 06:10 PM by Larz60+.)
Hello. This seems related to my issue. I need to submit a project in python (which I am not really good at). I am looking to replicate something like ... So far, I took help from a few forums and have reached up to this -
import tkinter as tk
from tkinter import ttk
count = 0
def clicked(): # without event because I use `command=` instead of `bind`
global count
count = count + 1
label1.configure(text=f'Button was clicked {count} times!!!')
windows = tk.Tk()
windows.title("My Application")
label = tk.Label(windows, text="Hello World")
label.grid(column=0, row=0)
label1 = tk.Label(windows)
label1.grid(column=0, row=1)
custom_button = ttk.Button(windows, text="Click on me", command=clicked)
custom_button.grid(column=1, row=0)
windows.mainloop() Now, I am able to count the clicks succesfully... but don't know how to add a timer to it. I need a simple 10 sec countdown timer after which the click counting should stop.
Can someone please help add the code to this? it'll really help me pass in this project. TIA.
Larz60+ write Mar-20-2022, 06:10 PM:Link removed for same reason shown in post 5
Posts: 6,800
Threads: 20
Joined: Feb 2020
I don't think this question ever got an adequate answer. To know how many clicks per second you have to keep track of when there was a click. The code below keeps click times in a queue. Each time the button is pressed it adds the current time to the queue. To calculate click rate over the last second it removes all times older than 1 second and uses the first and last time and the tick count to compute the rate.
import tkinter as tk
from time import time
clicks = []
def click():
now = time()
clicks.append(time())
start_time = now - 1
while clicks and clicks[0] < start_time:
clicks.pop(0)
if len(clicks) > 1:
print(len(clicks)/(clicks[-1] - clicks[0]))
else:
print(1)
root = tk.Tk()
tk.Button(root, text='Click', command=click).pack(padx=5, pady=5)
tk.mainloop()
Posts: 2
Threads: 0
Joined: Feb 2021
(Feb-09-2021, 09:03 PM)deanhystad Wrote: I don't think this question ever got an adequate answer. To know how many clicks per second you have to keep track of when there was a click. The code below keeps click times in a queue. Each time the button is pressed it adds the current time to the queue. To calculate click rate over the last second it removes all times older than 1 second and uses the first and last time and the tick count to compute the rate.
import tkinter as tk
from time import time
clicks = []
def click():
now = time()
clicks.append(time())
start_time = now - 1
while clicks and clicks[0] < start_time:
clicks.pop(0)
if len(clicks) > 1:
print(len(clicks)/(clicks[-1] - clicks[0]))
else:
print(1)
root = tk.Tk()
tk.Button(root, text='Click', command=click).pack(padx=5, pady=5)
tk.mainloop()
Not sure about the code but I think this is a better approach logically to get the click rate. how can we add a time countdown to this?
Posts: 6,800
Threads: 20
Joined: Feb 2020
If you have a fixed duration you could do something like read the time on the first click and set an event to call a function after some time has passed. Between the two you just count clicks. In tkinter I would use .after(). In Qt I would use QTimer. I have no experience with PyGame, but they must have a way of specifying when you want to do something.
|