Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Simple CPS counter
#1
I am trying to make a CPS counter, you will press a button and the timer starts then it will record your CPS and display it at the end of the second.

My trouble is the timer, time.sleep() doesn't work as that would stop everything, so I need a simple timer.

Here is the code so far.

import turtle
cps = 0 #is the CPS goes up when seconddone = False and sets to 0 when seconddone = True
seconddone = True #tells when the timer starts 
wn = turtle.Screen()
wn.setup(width=200, height=200)
wn.bgcolor("white")

cpsb = turtle.Turtle()
cpsb.shape("square")
cpsb.color("black")
cpsb.goto(0,0)

def start():
    seconddone = False
def click():
    cps = cps + 1

wn.onscreenclick(click, 1)
wn.onscreenclick(start, 3)
wn.onkeypress()
wn.listen()
while True:
    wn.update()
while seconddone:
    cps = 0
if anyone could help with making a timer that would be great (note I dont want to optimize the code at this point)
Reply
#2
There are two ways. I will show both of them and then you can decide what to use
1. threading
import threading

def timer():
    while True:
        time.sleep(5)
        #action


def main():
    t = threading.Thread(target=timer)
    t.start()
    #rest of main

main()
2. time.time()
import time

def main():
    dialogue = 0
    dialogueDict = {0 : 'Loading terrain', 1 : 'Creating structures', 2 : 'Spawning entities', 3 : 'Spawning Player'}
    while True:
        past = time.time() #Get current time
        while time.time() - past < 5: #While 5 seconds hven't passed print dialogue
            print(dialogueDict[dialogue]) #print
        dialogue += 1 #Then when 5 seconds have passed, loop again after raising dialogue
        if dialogue == 4: #After looping 4 times, break from loop
            break
    print('Done') #After loop has broke print "Done"

main()
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  simple counter mcmxl22 13 7,710 Feb-04-2018, 04:36 PM
Last Post: snippsat

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020