Skip to content

Instantly share code, notes, and snippets.

@quonic
Created July 2, 2019 02:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save quonic/2dc717e578d00a850ea02a6410473600 to your computer and use it in GitHub Desktop.
Save quonic/2dc717e578d00a850ea02a6410473600 to your computer and use it in GitHub Desktop.
Example of static screen at about 4 fps on a ryzen 7 1800X.
import sys
import random
# import math
from itertools import *
import numpy as np
import pygame
# from past.builtins import xrange
from profilehooks import profile
def get_pos_at_index(index, width, height):
if index > width*height:
return False
return divmod(index, height)
def get_index_at_pos(x, y, width, height):
if x*height+y > width*height:
return False
return x*height+y
@profile
def main():
WIDTH = 400
HEIGHT = 300
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
clock = pygame.time.Clock()
area_range = range(WIDTH * HEIGHT)
pygame.init()
pygame.mixer.init()
size = WIDTH, HEIGHT
screen = pygame.display.set_mode(size)
surface = pygame.display.get_surface()
font = pygame.font.SysFont('Arial', 16, bold=True)
fps_count = 0
tpf_count = 0
fps_last = 0
one_second = 1000
while True:
screen.fill(BLACK)
for event in pygame.event.get():
if event.type == pygame.QUIT:
# sound.stop()
sys.exit()
# region Static Generator
sample_list = np.random.randint(0, 255 + 1, size=(WIDTH * HEIGHT, 3))
for i in area_range:
# region Color Static
surface.set_at(get_pos_at_index(i, WIDTH, HEIGHT), sample_list[i])
# endregion
# region Black and White variant
# color = (0, 0, 0)
# if random.getrandbits(1):
# color = (255, 255, 255)
# surface.set_at(divmod(i, HEIGHT), color)
# endregion
# endregion
# region FPS Counter
msElapsed = clock.tick()
tpf_count += msElapsed
if tpf_count >= one_second:
fps_last = fps_count
fps_count = 0
tpf_count = tpf_count - one_second
else:
fps_count += 1
screen.blit(font.render("fps: " + str(fps_last) + ", ms: " + str(msElapsed), True, WHITE), (10, 10))
# endregion
pygame.display.flip()
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment