2

I am trying to make a click test "game" for my friends and I. However, I am a bit puzzled as for what to do next. Here's my code so far:

import os, sys, math, pygame, pygame.mixer, time
from pygame.locals import *

black = (0,0,0)
red = (255,0,0)
green = (0,255,0)
blue = (0,0,255)
white = (255,255,255)
click = 0
time = 0
timer = pygame.time.get_ticks()
run_me = True

screen_size = screen_Width, sceen_height = 600, 400
screen = pygame.display.set_mode(screen_size)
pygame.display.set_caption('click speed test')


Fps = 60
fps_clock = pygame.time.Clock()

while run_me:

    for event in pygame.event.get():
        if event.type == pygame.MOUSEBUTTONDOWN:
            time = timer + 1*1000
        if time = 1
            for event in pygame.event.get():
                if event.type == pygame.MOUSEBUTTONDOWN:
                    clicks + 1

I want to show the user how many clicks he managed to press in a second. How do I do that?

Thank you >.<

yuvi
  • 18,155
  • 8
  • 56
  • 93
Lumeriaux Yt
  • 45
  • 1
  • 1
  • 2

2 Answers2

2

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

  1. Never leave out pygame.init()
  2. Never leave out pygame.display.update()
  3. Never leave out quit action
Inyoung Kim 김인영
  • 1,434
  • 1
  • 17
  • 38
  • And If anyone have any suggestions for my answer code. Please do tell me. Thanks. – Inyoung Kim 김인영 Feb 06 '18 at 04:13
  • Could `elif(first_click == True):` be replaced with just `else:`? Other than that it looks good. I don't have pygame installed so I can't test it. – MisterMystery Feb 06 '18 at 04:25
  • This works correctly but the CPU usage is very high (probably won't matter for such a simple program, though). – skrx Feb 06 '18 at 04:56
  • @MisterMystery : Yes that should work. I just did `elif` because I wanted to make sure what was happening there. – Inyoung Kim 김인영 Feb 06 '18 at 06:19
  • @skrx : Mabye because of tick_busy_loop? I read that it uses more cpu than just tick. – Inyoung Kim 김인영 Feb 06 '18 at 06:20
  • Yes, and you'd also have to call `tick` every frame instead of just in the `if first_click == True and nxt_time < 5:` clause. However, the docs mention that `tick` is not accurate on every platform, but I don't know what exactly that means (which platforms and how inaccurate?). – skrx Feb 06 '18 at 07:09
  • @skrx: [link](https://www.libsdl.org/release/SDL-1.2.15/docs/html/sdldelay.html) On pygame doc. Its says that tick uses dsl_delay function. This is the description of SDL_function. Wait a specified number of milliseconds before returning. SDL_Delay will wait at least the specified time, but possible longer due to OS scheduling.(This may be the reason for not accurate on every platform.) Note: Count on a delay granularity of at least 10 ms. Some platforms have shorter clock ticks but this is the most common. (This too) – Inyoung Kim 김인영 Feb 06 '18 at 07:23
1

I'd use pygame.time.get_ticks to calculate the passed time since the first click (check out the comments).

import pygame as pg


pg.init()
screen = pg.display.set_mode((600, 400))
fps_clock = pg.time.Clock()
FPS = 60
GRAY = pg.Color('gray12')
WHITE = pg.Color('white')
FONT = pg.font.Font(None, 42)

clicks = 0
start_time = 0
passed_time = 0
run_me = True

while run_me:
    for event in pg.event.get():
        if event.type == pg.QUIT:
            run_me = False
        elif event.type == pg.MOUSEBUTTONDOWN:
            if event.button == 1:  # Left mouse button.
                # Start the timer if it's stopped.
                if start_time == 0:
                    start_time = pg.time.get_ticks()
                if passed_time < 1:  # Count the clicks.
                    clicks += 1
            # Press the right mouse button to reset the timer and clicks.
            elif event.button == 3:
                start_time = 0
                passed_time = 0
                clicks = 0

    if passed_time < 1 and start_time != 0:
        # Calculate the passed time. / 1000 to convert it to seconds.
        passed_time = (pg.time.get_ticks() - start_time) / 1000

    time_surface = FONT.render('Time: {}'.format(passed_time), True, WHITE)
    clicks_surface = FONT.render('Clicks: {}'.format(clicks), True, WHITE)

    screen.fill(GRAY)
    screen.blit(time_surface, (30, 30))
    screen.blit(clicks_surface, (30, 70))
    pg.display.flip()
    fps_clock.tick(FPS)

pg.quit()
skrx
  • 19,980
  • 5
  • 34
  • 48
  • 1
    thank you this was very helpful and it made my life easier. I added a few lines like CPS and PROPS to help the user so I put you into the props list! – Lumeriaux Yt Feb 06 '18 at 22:54