Countdown timer in Pygame
On this page you will find what you are looking for http://www.pygame.org/docs/ref/time.html#pygame.time.get_ticks
You download ticks once before beginning the countdown (which can be a trigger in the game - the key event, whatever).
For example:
start_ticks=pygame.time.get_ticks() #starter tick
while mainloop: # mainloop
seconds=(pygame.time.get_ticks()-start_ticks)/1000 #calculate how many seconds
if seconds>10: # if more than 10 seconds close the game
break
print (seconds) #print how many seconds
How to make a circular countdown timer in Pygame?
Just use pygame.draw.arc
and specify the stop_angle argument depending on the counter:
percentage = counter/100
end_angle = 2 * math.pi * percentage
pygame.draw.arc(window, (255, 0, 0), arc_rect, 0, end_angle, 10)
Minimal example:
import pygame
import math
pygame.init()
window = pygame.display.set_mode((200, 200))
clock = pygame.time.Clock()
font = pygame.font.SysFont(None, 100)
counter = 100
text = font.render(str(counter), True, (0, 128, 0))
timer_event = pygame.USEREVENT+1
pygame.time.set_timer(timer_event, 1000)
def drawArc(surf, color, center, radius, width, end_angle):
arc_rect = pygame.Rect(0, 0, radius*2, radius*2)
arc_rect.center = center
pygame.draw.arc(surf, color, arc_rect, 0, end_angle, width)
run = True
while run:
clock.tick(60)
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
elif event.type == timer_event:
counter -= 1
text = font.render(str(counter), True, (0, 128, 0))
if counter == 0:
pygame.time.set_timer(timer_event, 0)
window.fill((255, 255, 255))
text_rect = text.get_rect(center = window.get_rect().center)
window.blit(text, text_rect)
drawArc(window, (255, 0, 0), (100, 100), 90, 10, 2*math.pi*counter/100)
pygame.display.flip()
pygame.quit()
exit()
Sadly the quality of pygame.draw.arc
with a width > 1 is poor. However this can be improved, using cv2 and cv2.ellipse
:
import pygame
import cv2
import numpy
pygame.init()
window = pygame.display.set_mode((200, 200))
clock = pygame.time.Clock()
font = pygame.font.SysFont(None, 100)
counter = 100
text = font.render(str(counter), True, (0, 128, 0))
timer_event = pygame.USEREVENT+1
pygame.time.set_timer(timer_event, 1000)
def drawArcCv2(surf, color, center, radius, width, end_angle):
circle_image = numpy.zeros((radius*2+4, radius*2+4, 4), dtype = numpy.uint8)
circle_image = cv2.ellipse(circle_image, (radius+2, radius+2),
(radius-width//2, radius-width//2), 0, 0, end_angle, (*color, 255), width, lineType=cv2.LINE_AA)
circle_surface = pygame.image.frombuffer(circle_image.flatten(), (radius*2+4, radius*2+4), 'RGBA')
surf.blit(circle_surface, circle_surface.get_rect(center = center))
run = True
while run:
clock.tick(60)
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
elif event.type == timer_event:
counter -= 1
text = font.render(str(counter), True, (0, 128, 0))
if counter == 0:
pygame.time.set_timer(timer_event, 0)
window.fill((255, 255, 255))
text_rect = text.get_rect(center = window.get_rect().center)
window.blit(text, text_rect)
drawArcCv2(window, (255, 0, 0), (100, 100), 90, 10, 360*counter/100)
pygame.display.flip()
pygame.quit()
exit()
Python Countdown Timer
In python, \r
in a string stands for the carriage return character. It does reset the position of the cursor to the start of the current line. The end argument in the print function is the string that is printed after the given text. By default, this is \n
, a newline character.
In the timer we do want that the new time overwrites the previous one, not that the new time is printed after the previous one. Thus, the end argument of the print function is set to \r
, such that the cursor is moved to the back of the line. This makes thet the new time is printed over the new one.
Creating a countdown timer in Pygame
Yes this is possible using the pygame.time.Clock. You just use the tick
method with the fps that you want. But keep in mind you are only going to see updates as fast as your screen refreshes.
clock = pygame.time.Clock()
while True:
clock.tick(1000) # waits until the next ms
#clear your frame here
#update and draw your text here
#flip your display window here
How to start a countdown when pygame.QUIT is initialized
Instead of exiting the application, start a countdown timer. Quit the application when the countdown is complete.
See Countdown timer in Pygame.
Minimal example:
import pygame
pygame.init()
window = pygame.display.set_mode((200, 200))
clock = pygame.time.Clock()
font = pygame.font.SysFont(None, 100)
counter = 0
text = font.render(str(10), True, (0, 128, 0))
timer_event = pygame.USEREVENT+1
run = True
while run:
clock.tick(60)
for event in pygame.event.get():
if event.type == pygame.QUIT:
counter = 10
pygame.time.set_timer(timer_event, 1000)
elif event.type == timer_event:
counter -= 1
text = font.render(str(counter), True, (0, 128, 0))
if counter == 0:
run = False
window.fill((255, 255, 255))
# draw game
# [...]
if counter > 0:
text_rect = text.get_rect(center = window.get_rect().center)
window.blit(text, text_rect)
pygame.display.flip()
pygame.quit()
exit()
Related Topics
How to Improve Performance of This Code
How Does Assignment Work With List Slices
What Are the Advantages of Numpy Over Regular Python Lists
Can a Variable Number of Arguments Be Passed to a Function
How to Install a Python Package With a .Whl File
Choosing the Correct Upper and Lower Hsv Boundaries For Color Detection With'Cv::Inrange' (Opencv)
Convert Dataframe Column Type from String to Datetime
How to Copy a String to the Clipboard
Best Way to Return Multiple Values from a Function
Installing Pip Is Not Working in Python ≪ 3.6
Count the Number of Occurrences of a Character in a String
How to Install Python Packages [Ssl: Tlsv1_Alert_Protocol_Version]
Why Is This Printing 'None' in the Output
How to Prevent Tensorflow from Allocating the Totality of a Gpu Memory
Manually Raising (Throwing) an Exception in Python
Multiple Aggregations of the Same Column Using Pandas Groupby.Agg()