Detecting sequence in joystick input through pygame [duplicate] - 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()

Related

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()

Why does my pygame window freeze and crash?

Whenever I try to run my program it freezes up and crashes. I'm not a master pygame coder, but I'm almost sure its something to do with my main loop. It's supposed to allow the user to move left and right using arrow keys. I've tried adding a clock and changing the size of the screen but that didn't work. Here is the code:
import pygame
import sys
import random
import time
pygame.init()
screen = pygame.display.set_mode((500,500))
events = pygame.event.get()
clock = pygame.time.Clock()
x = 50
y = 50
game_over = False
while not game_over:
for event in events:
if event.type != pygame.QUIT:
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_RIGHT:
x += 5
elif event.key == pygame.K_LEFT:
x -= 5
else:
sys.exit()
screen.fill((0,0,0))
pygame.draw.rect(screen, (255,255,255), (x,y,50,50))
clock.tick(30)
pygame.display.update()
The code needs to process the event queue continuously, otherwise your operating environment will consider your window to be non-responsive (locked up). Your code is almost there, except it only fetches the new events once, but this needs to be done every iteration of the main loop.
import pygame
import sys
pygame.init()
screen = pygame.display.set_mode((500,500))
clock = pygame.time.Clock()
x = 50
y = 50
game_over = False
while not game_over:
events = pygame.event.get() # <<-- HERE handle every time
for event in events:
if event.type != pygame.QUIT:
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_RIGHT:
x += 5
elif event.key == pygame.K_LEFT:
x -= 5
else:
sys.exit()
screen.fill((0,0,0))
pygame.draw.rect(screen, (255,255,255), (x,y,50,50))
clock.tick(30)
pygame.display.update()
You cant do foe event in events, as when you call pygame.events.get(), you are updating them, you are updating them once and looping over them every frame, you need to use for event in pygame.event.get(): so you are calling the function every frame and updating the events every frame
unless you were trying to do this?
events = pygame.event.get
...
for event in events():
which does the same as above

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()

macro program not working

I have been looking for a way to make an autoclicker, since I haven't had any experience with clicking/typing in Python via a macro. I wanted the program to be able to detect when I press a button (F1) and start clicking constantly until I press the stop button (F2); unfortunately, my code won't output the cps variable and the x and y variable. I just need to be able to detect that it's working there to move on to my actual clicking.
Basically, I'm asking how to fix the key detection.
Python version: 3.6.5
EDIT: i know it checks for 1 and 2, the f1 was opening a python help screen when pressed- so for now im just doing 1 and 2
import random, pygame, pyautogui, time
loop = 1
on = 0
pygame.init()
while(loop == 1):
key = pygame.key.get_pressed()
if(key[pygame.K_1]):
on = 1
elif(key [pygame.K_2]):
on = 0
if(on == 1):
x,y = pygame.mouse.get_pos()
cps = random.randint(10,20)
print(cps, x,y)
Define a user event and call pygame.time.set_timer with this event as the first argument and pygame will start adding the event to the queue after the specified time interval.
import random
import pygame as pg
pg.init()
screen = pg.display.set_mode((640, 480))
clock = pg.time.Clock()
BG_COLOR = pg.Color('gray12')
CLICK_EVENT = pg.USEREVENT + 1
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_1:
pg.time.set_timer(CLICK_EVENT, 1000) # Start the timer.
elif event.key == pg.K_2:
pg.time.set_timer(CLICK_EVENT, 0) # Stop the timer.
elif event.type == CLICK_EVENT:
print(random.randint(10, 20), pg.mouse.get_pos())
screen.fill(BG_COLOR)
pg.display.flip()
clock.tick(30)
pg.quit()
Your code currently checks for the 1 and 2 number keys.
You want K_F1 and K_F2, not K_1 and K_2, for the function keys.

Timer between keyboard actions

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()

Categories

Resources