Created
July 3, 2019 17:04
-
-
Save rjbudzynski/71997952d47121e5456c3b2dbc8770fc to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#! /usr/bin/python3 | |
import pygame | |
from numpy.random import randint | |
WIDTH, HEIGHT = 800, 600 | |
def main(): | |
pygame.init() | |
pygame.display.set_caption("video noise") | |
screen = pygame.display.set_mode((WIDTH, HEIGHT)) | |
font = pygame.font.SysFont('Courier New, courier, monospace', 32, bold=True) | |
running = True | |
clock = pygame.time.Clock() | |
clock.tick() | |
dt = 0 | |
text = font.render('fps: ?', False, (255, 255, 0)) | |
# main loop | |
while running: | |
noise = randint(0, 256, (WIDTH, HEIGHT)) | |
pixels = pygame.surfarray.pixels3d(screen) | |
for i in range(3): | |
pixels[:, :, i] = noise | |
del pixels | |
# it makes sense to throttle the framerate (which is what the 30 in | |
# clock.tick(30) achieves) because it is pointless to have a framerate | |
# higher than the screen refresh rate (which is usually 60). To run at | |
# full speed, remove the 30 | |
dt += clock.tick(30) | |
# don't refresh the OSD more than once per sec. | |
if dt > 1000: | |
dt = 0 | |
text = font.render(f'fps: {int(clock.get_fps())}', True, (255, 255, 0)) | |
screen.blit(text, (10, 10)) | |
pygame.display.flip() | |
for event in pygame.event.get(): | |
if event.type == pygame.QUIT: | |
running = False | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment