To print how many clicks were made, let's have a look at this amazing answer for how to display text on screen(Answer by: Bartlomiej Lewandowsk, Original answer: link)
You can create a surface with text on it. For this take a look at this
short example:
pygame.font.init() # you have to call this at the start,
# if you want to use this module.
myfont = pygame.font.SysFont('Comic Sans MS', 30)
This creates a new object on which you can call the render method.
textsurface = myfont.render('Some Text', False, (0, 0, 0))
This creates a new surface with text already drawn onto it. At the end you
can just blit the text surface onto your main screen.
screen.blit(textsurface,(0,0))
Bear in mind, that everytime the text changes, you have to recreate the surface > again, to see the new
text.
I've revised you're code, have a look, I'll explain below
import sys, pygame
from pygame.locals import *
#colors
black = (0,0,0)
red = (255,0,0)
green = (0,255,0)
blue = (0,0,255)
white = (255,255,255)
#vars
clicks = 0
run_me = True
#init
pygame.init()
pygame.font.init()
#display init
screen_size = screen_Width, sceen_height = 600, 400
screen = pygame.display.set_mode(screen_size)
pygame.display.set_caption('click speed test')
myfont = pygame.font.SysFont('Comic Sans MS', 30)
Fps = 60
clock = pygame.time.Clock()
first_click = False
frst_time = 0
nxt_time = 0
while run_me:
for event in pygame.event.get():
if event.type == QUIT:
quit()
sys.exit()
if event.type == pygame.MOUSEBUTTONDOWN:
if first_click == False:
frst_time = clock.tick_busy_loop() / 1000
first_click = True
clicks += 1
elif(first_click == True):
if nxt_time < 5:
clicks += 1
if first_click == True and nxt_time < 5:
nxt_time += clock.tick_busy_loop()/1000
textsurface = myfont.render(str(clicks), False,black)
textsurface2 = myfont.render(str(nxt_time), False, black)
screen.fill(white)
screen.blit(textsurface,(0,0))
screen.blit(textsurface2,(0,100))
pygame.display.update()
First, I made a variable named first_click
to get the clock to start when the player made the first click. Remember that the first tick() gets you the time from the pygame.init() was called.
Second, nxt_time < 5:
this statement makes the clicks count go up only when below 5(In you're case it would be 1)
Third, tick() only gets the interval time between each ticks. This doesn't add up the time. So, to get the time added up. I made the variable nxt_time. This variable holds the time value. The time from first click was made.
BTW: tick_busy_loop is more accurate than just tick. But it uses more CPU.
And last
- Never leave out
pygame.init()
- Never leave out
pygame.display.update()
- Never leave out
quit action