Timer between keyboard actions - python

I want to make a timer between two actions, for example showing an image and then pressing a key on the keyboard. I have tried to start this by trying to measure the time between pressing space. For example pressing space starts the timer and pressing it again stops the timer. Then print the time.
I am using pygame as a key pressed tracker.
Here is my code, why is it not working?
import time
import pygame
pygame.init()
pygame.event.get()
for event in pygame.event.get():
if event.type == pygame.KEYDOWN:
if key == pygame.K_SPACE:
start = time.time()
if key == pygame.K_SPACE:
end = time.time()
print(end - start)
I thought it may be the way I try to track the space key; and utilized the helpful comment but still can't get it to work:
import time
import pygame
pygame.init()
key=pygame.key.get_pressed()
start_time = None
if key[pygame.K_SPACE]:
if start_time == None:
start_time = time.time()
else:
print(time.time() - start_time)
start_time = None

You need to save the state (or start time) of the first event somewhere.
Currently both your if-clauses will evaluate to True and the time will always be 0.
start_time = None
for event in pygame.event.get():
if event.type == pygame.KEYDOWN:
if key == pygame.K_SPACE:
if start_time == None:
start_time = time.time()
else:
print(time.time() - start_time)
start_time = None

First, I'm pretty sure you need a window for pygame to produce any events. Next, you need to loop until you have 2 space presses. Here's an example:
import time
import pygame
pygame.init()
# Open a window
(width, height) = (300, 200)
screen = pygame.display.set_mode((width, height))
pygame.display.flip()
start = None
end = None
# Loop until both keypresses received
while None == end:
for event in pygame.event.get():
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_SPACE: # Note you had typo here
if None == start:
start = time.time()
else:
end = time.time()
print(end - start)
# Close window
pygame.display.quit()
pygame.quit()

Related

Detecting sequence in joystick input through pygame [duplicate]

I want to start my program, measure the time when the program starts and then wait some seconds, push a button (K_RIGHT) and messure the time when I push the button. I am using Pygame to registrate the Keydown. But in my code below it does not registrate my Keydown. What I am doing wrong here?
start = time.time()
for e in pygame.event.get():
if e.type == pygame.KEYDOWN:
if e.key == pygame.K_RIGHT:
end= time.time()
diff = end-start
Here's a minimal, complete example that prints the correct time difference. The passed time is just the difference between time.time() (now) and the start time.
You could use pygame.time.get_ticks instead of time.time as well (it returns the time in milliseconds instead of seconds).
import time
import pygame as pg
pg.init()
screen = pg.display.set_mode((640, 480))
clock = pg.time.Clock()
BG_COLOR = pg.Color('gray12')
start = time.time()
done = False
while not done:
for event in pg.event.get():
if event.type == pg.QUIT:
done = True
elif event.type == pg.KEYDOWN:
if event.key == pg.K_RIGHT:
diff = time.time() - start
print(diff)
screen.fill(BG_COLOR)
pg.display.flip()
clock.tick(60)
pg.quit()

How to recieve the user inpout from joystick of PS4 and use it to recognise the pattern in the input and also identify time using pygame [duplicate]

I want to start my program, measure the time when the program starts and then wait some seconds, push a button (K_RIGHT) and messure the time when I push the button. I am using Pygame to registrate the Keydown. But in my code below it does not registrate my Keydown. What I am doing wrong here?
start = time.time()
for e in pygame.event.get():
if e.type == pygame.KEYDOWN:
if e.key == pygame.K_RIGHT:
end= time.time()
diff = end-start
Here's a minimal, complete example that prints the correct time difference. The passed time is just the difference between time.time() (now) and the start time.
You could use pygame.time.get_ticks instead of time.time as well (it returns the time in milliseconds instead of seconds).
import time
import pygame as pg
pg.init()
screen = pg.display.set_mode((640, 480))
clock = pg.time.Clock()
BG_COLOR = pg.Color('gray12')
start = time.time()
done = False
while not done:
for event in pg.event.get():
if event.type == pg.QUIT:
done = True
elif event.type == pg.KEYDOWN:
if event.key == pg.K_RIGHT:
diff = time.time() - start
print(diff)
screen.fill(BG_COLOR)
pg.display.flip()
clock.tick(60)
pg.quit()

How can i calculate the mouse speed with Pygame?

import pygame
import datetime
with open('textdatei.txt', 'a') as file:
pygame.init()
print("Start: " + str(datetime.datetime.now()), file=file)
screen = pygame.display.set_mode((640, 480))
clock = pygame.time.Clock()
BG_COLOR = pygame.Color('gray12')
done = False
while not done:
# This event loop empties the event queue each frame.
for event in pygame.event.get():
# Quit by pressing the X button of the window.
if event.type == pygame.QUIT:
done = True
elif event.type == pygame.MOUSEBUTTONDOWN:
# MOUSEBUTTONDOWN events have a pos and a button attribute
# which you can use as well. This will be printed once per
# event / mouse click.
print('In the event loop:', event.pos, event.button)
print("Maus wurde geklickt: " + str(datetime.datetime.now()), file=file)
# Instead of the event loop above you could also call pygame.event.pump
# each frame to prevent the window from freezing. Comment it out to check it.
# pygame.event.pump()
click = pygame.mouse.get_pressed()
mousex, mousey = pygame.mouse.get_pos()
print(click, mousex, mousey, file=file)
screen.fill(BG_COLOR)
pygame.display.flip()
clock.tick(60) # Limit the frame rate to 60 FPS.
print("Ende: " + str(datetime.datetime.now()), file=file)
Hello, I am new in Pygame and for now, my Programm can track the mouse coordinates and it creates the time of the click. But i want to calculate the Speed of the Mouse from one click to the next click (in Pixels per Second).
Thanks in advance.
Use pygame.time.get_ticks() to return the number of milliseconds since pygame.init() was called.
Compute the Euclidean distance between 2 clicks and divide it by the time difference:
import math
prev_time = 0
prev_pos = (0, 0)
click_count = 0
done = False
while not done:
# This event loop empties the event queue each frame.
for event in pygame.event.get():
# Quit by pressing the X button of the window.
if event.type == pygame.QUIT:
done = True
elif event.type == pygame.MOUSEBUTTONDOWN:
act_time = pygame.time.get_ticks() # milliseconds
act_pos = event.pos
if click_count > 0:
dt = act_time - prev_time
dist = math.hypot(act_pos[0] - prev_pos[0], act_pos[1] - prev_pos[1])
speed = 1000 * dist / dt # pixle / seconds
print(speed, "pixel/second")
prev_time = act_time
prev_pos = act_pos
click_count += 1
# [...]

Count time and push values into CSV file using python and pandas [duplicate]

I am trying to test the time between mouse down and mouse up events by using a simple stopwatch in a while loop. The mouse down event works fine, but when i release the mouse for mouse up, the seconds continue to go up and do not stop.
from pygame import *
import time
screen = display.set_mode((160, 90))
sec = 0
while True:
new_event = event.poll()
if new_event.type == MOUSEBUTTONDOWN:
while True: # Basic stopwatch started
time.sleep(1)
sec += 1
print(sec)
# In loop, when mouse button released,
# supposed to end stopwatch
if new_event.type == MOUSEBUTTONUP:
break
display.update()
I want the stopwatch to end after the mouse is released. eg. If the mouse is just clicked, the seconds should be 1. If the mouse is held for 5 seconds, it should not continue past 5.
Use pygame.time.get_ticks to get the number of milliseconds since pygame.init() was called.
Store the milliseconds when MOUSEBUTTONDOWN and calculate the time difference in the main loop:
from pygame import *
screen = display.set_mode((160, 90))
clock = time.Clock()
run = True
started = False
while run:
for new_event in event.get():
if new_event.type == QUIT:
run = False
if new_event.type == MOUSEBUTTONDOWN:
start_time = time.get_ticks()
started = True
if new_event.type == MOUSEBUTTONUP:
started = False
if started:
current_time = time.get_ticks()
sec = (current_time - start_time) / 1000.0
print(sec)
display.update()

How to measure time in Python?

I want to start my program, measure the time when the program starts and then wait some seconds, push a button (K_RIGHT) and messure the time when I push the button. I am using Pygame to registrate the Keydown. But in my code below it does not registrate my Keydown. What I am doing wrong here?
start = time.time()
for e in pygame.event.get():
if e.type == pygame.KEYDOWN:
if e.key == pygame.K_RIGHT:
end= time.time()
diff = end-start
Here's a minimal, complete example that prints the correct time difference. The passed time is just the difference between time.time() (now) and the start time.
You could use pygame.time.get_ticks instead of time.time as well (it returns the time in milliseconds instead of seconds).
import time
import pygame as pg
pg.init()
screen = pg.display.set_mode((640, 480))
clock = pg.time.Clock()
BG_COLOR = pg.Color('gray12')
start = time.time()
done = False
while not done:
for event in pg.event.get():
if event.type == pg.QUIT:
done = True
elif event.type == pg.KEYDOWN:
if event.key == pg.K_RIGHT:
diff = time.time() - start
print(diff)
screen.fill(BG_COLOR)
pg.display.flip()
clock.tick(60)
pg.quit()

Categories

Resources