Skip to content

Instantly share code, notes, and snippets.

@smitelli
Last active June 22, 2019 12:54
Show Gist options
  • Save smitelli/cc3320fd70b23379d90e0c9bd5b9387e to your computer and use it in GitHub Desktop.
Save smitelli/cc3320fd70b23379d90e0c9bd5b9387e to your computer and use it in GitHub Desktop.
Pygame is actually a lot faster than I thought it would be.
#!/usr/bin/python3
"""
Python 3.6 or above.
Cygwin: startxwin ./pygame-fps.py
"""
import numpy
import pygame
import time
START_SIZE = (1155, 866) # (width, height) for roughly one 4:3 megapixel
FPS_DATA_LENGTH = 50 # How many frames to average when measuring FPS
COLOR_MAX = 256 ** 3 # Full 24-bit RGB color spectrum
surface = pygame.display.set_mode(START_SIZE, pygame.RESIZABLE)
surfsize = surface.get_size()
fps_data = numpy.full(FPS_DATA_LENGTH, numpy.inf)
fps_index = 0
done = False
while not done:
fps_start_time = time.time()
pygame.display.set_caption(
f'{FPS_DATA_LENGTH / sum(fps_data):.1f} fps @ {surfsize[0]}x{surfsize[1]}')
pygame.surfarray.blit_array(surface, numpy.random.randint(0, COLOR_MAX, surfsize))
pygame.display.update()
for event in pygame.event.get():
if event.type in (pygame.KEYUP, pygame.QUIT):
done = True
elif event.type == pygame.VIDEORESIZE:
surface = pygame.display.set_mode(event.size, pygame.RESIZABLE)
surfsize = surface.get_size()
fps_data[fps_index] = time.time() - fps_start_time
fps_index = (fps_index + 1) % FPS_DATA_LENGTH
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment