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
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()
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()
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
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()
I making a game with Python and Pygame, and I'm using time.time to time the users going through levels. However, I also have a pause menu. How can I make so when the pause menu is open, time.time won't continue?
I think I'd do something like this: Use the time that clock.tick() returns to increase a timer each frame if the game is not paused, and when the user pauses and unpauses the game call it without an argument to discard the time that passed while the game was paused.
import sys
import pygame as pg
pg.init()
screen = pg.display.set_mode((640, 480))
clock = pg.time.Clock()
font = pg.font.Font(None, 30)
timer = 0
dt = 0
paused = False
running = True
while running:
for event in pg.event.get():
if event.type == pg.QUIT:
running = False
elif event.type == pg.KEYDOWN:
paused = not paused
# This is needed to discard the time that
# passed while the game was paused.
clock.tick()
if not paused:
timer += dt # Add delta time to increase the timer.
screen.fill((30, 30, 30))
txt = font.render(str(round(timer, 2)), True, (90, 120, 40))
screen.blit(txt, (20, 20))
pg.display.flip()
dt = clock.tick(30) / 1000 # dt = time in seconds since last tick.
pg.quit()
sys.exit()