Skip to content

Instantly share code, notes, and snippets.

@whatisor
Created April 3, 2023 12:59
Show Gist options
  • Save whatisor/5808ddbb8293a6cf3e626da59bc18551 to your computer and use it in GitHub Desktop.
Save whatisor/5808ddbb8293a6cf3e626da59bc18551 to your computer and use it in GitHub Desktop.
python-opengl-pygame-high-accuracy-clock
import pygame
import time
# Initialize Pygame
pygame.init()
# Set the window size
size = (640, 480)
# Create the window
screen = pygame.display.set_mode(size)
# Set the font
font = pygame.font.SysFont("arial", 36)
# Render the text
text = font.render("Hello, world!", True, (255, 255, 255))
# Get the text rectangle
text_rect = text.get_rect()
# Center the text
text_rect.center = (320, 240)
counter = 0
fps = 0
starttime = 0
# Main game loop
while True:
# Handle events
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
# Clear the screen
screen.fill((0, 0, 0))
# Get the current time
current_time = time.time()
# Split the time into hours, minutes, and seconds
current_time_tuple = time.localtime(current_time)
hours = current_time_tuple.tm_hour % 12
minutes = current_time_tuple.tm_min
seconds = current_time_tuple.tm_sec
milliseconds = int((current_time - int(current_time)) * 1000)
# Render the text
text = font.render(str(milliseconds) + " mil/10f " + str(fps), True, (255, 255, 255))
# Blit the text to the screen
screen.blit(text, text_rect)
# Update the display
pygame.display.update()
if counter == 0:
starttime = seconds * 1000 + milliseconds
counter += 1
if counter == 10:
fps = (seconds * 1000 + milliseconds - starttime)
counter = 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment