cookertron

Strange rendering Python Pygame

Oct 4th, 2021 (edited)
232
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.54 KB | None | 0 0
  1. import os
  2. import lzma, base64
  3. from random import randint
  4. from pygame import Rect
  5. from pygame import Vector2
  6. import pygame
  7.  
  8. # graphics engine
  9. class graphics:
  10.     def __init__(s, resolution, windowScale):
  11.         s.windowScale = windowScale
  12.         s.scale = windowScale
  13.         s.res = Vector2(resolution)
  14.         s.winRes = s.res * s.scale
  15.         s.setResVars()
  16.  
  17.         s.sprites = {}
  18.         s.updates = []
  19.         s.unblits = []
  20.  
  21.         s.fullscreenFlags = pygame.FULLSCREEN | pygame.SCALED | pygame.HWACCEL | pygame.HWSURFACE | pygame.DOUBLEBUF
  22.         pygame.init()
  23.         pygame.mouse.set_visible(False)
  24.         s.primary = pygame.display.set_mode((int(s.winRes[0]), int(s.winRes[1])))
  25.         s.primary.set_alpha(None)
  26.  
  27.     def setResVars(s):
  28.         s.center = (s.res[0] // 2, s.res[1] // 2)
  29.         s.w = s.res[0]
  30.         s.h = s.res[1]
  31.         s.midTop = (s.center[0], 0)
  32.         s.midBottom = (s.center[0], s.res[1])
  33.         s.midLeft = (0, s.center[1])
  34.         s.midRight = (s.res[0], s.center[1])
  35.  
  36.     def makeUnblit(s):
  37.         surface = pygame.Surface(s.updates[-1][1])
  38.         surface.blit(s.primary, (0, 0), s.updates[-1])
  39.         s.unblits += [s.behind(surface, s.updates[-1])]
  40.  
  41.     def unblit(s):
  42.         while s.unblits:
  43.             unblit = s.unblits.pop()
  44.             s.updates += [unblit.r]
  45.             s.primary.blit(unblit.surface, unblit.r[0])
  46.  
  47.     def update(s):
  48.         pygame.display.update(s.updates)
  49.         s.updates = []
  50.         s.unblit()
  51.  
  52.     def flush(s):
  53.         s.updates = []
  54.         pygame.display.update()
  55.  
  56.     def mouse(s):
  57.         mp = pygame.mouse.get_pos()
  58.         return (mp[0] // s.scale, mp[1] // s.scale)
  59.  
  60.     def loadSprite(s, id, filename, dimensions=(1, 1), colorkey=None):
  61.         surface = pygame.image.load(filename).convert()
  62.         sw, sh = Vector2(surface.get_rect().size) * s.scale
  63.         w, h = (int(sw / dimensions[0]), int(sh / dimensions[1]))
  64.         newSurface = pygame.transform.scale(surface, (int(sw), int(sh)))
  65.         newSurface.set_colorkey(colorkey)
  66.         s.sprites[id] = [newSurface.subsurface(((index % dimensions[0] * w, index // dimensions[0] * h), (w, h))) for index in range(dimensions[0] * dimensions[1])]
  67.  
  68.     def sw(s, id):
  69.         return s.sprites[id][0].get_rect().w
  70.  
  71.     def sh(s, id):
  72.         return s.sprites[id][0].get_rect().h
  73.  
  74.     def cx(s, id):
  75.         return s.sprites[id][0].get_rect().center[0]
  76.  
  77.     def cy(s, id):
  78.         return s.sprites[id][0].get_rect().center[1]
  79.  
  80.     def blit(s, id, xy, index=0, center=False, unblit=False):
  81.         a = Vector2(xy) * s.scale
  82.         b = s.sprites[id][0].get_rect()
  83.         a -= b.center if center else (0, 0)
  84.         s.updates += [(a, b.size)]
  85.         if unblit:
  86.             s.makeUnblit()
  87.         s.primary.blit(s.sprites[id][index], a)
  88.  
  89.     def rect(s, color, xy, wh, lineThickness=0, unblit=False):
  90.         r = (Vector2(xy) * s.scale, Vector2(wh) * s.scale)
  91.         pygame.draw.rect(s.primary, color, r, lineThickness)
  92.         s.updates += [r]
  93.    
  94.     class behind:
  95.         def __init__(s, surface, r):
  96.             s.surface = surface
  97.             s.r = r
  98.  
  99. # if more than 100 updates, update the whole screen
  100. BLACK = (0, 0, 0)
  101. WHITE = (255, 255, 255)
  102. RED = (128, 0, 0)
  103.  
  104. IMAGE_CROSSHAIR = b'/Td6WFoAAATm1rRGAgAhARYAAAB0L+Wj4ACXAJJdAESUBcR6J/b37omOUJCIs6rVUCaEMRn335INOFFYHloaVPVi8u5\
  105. fWYmItWESIqUBzMmpnnr19bxep/TDJOFgavLr4xVaRkaNk1ElcJQYsossarUihOK/muXes2ISsz9VnoA0A+BIQ17APXpBlHeawXuKT7OAaemEpc\
  106. PWcIXUVjZ1PuesTr2gLw7N9ycZK/OQveAAAAAASgLlBJyi5PgAAa4BmAEAAI8uFaSxxGf7AgAAAAAEWVo='
  107.  
  108. # DON'T PANIC!!! this saves the crosshair image to disk
  109. if not os.path.isfile("crosshair.png"):
  110.     print("Creating crosshair image...", end="")
  111.     data = lzma.decompress(base64.b64decode(IMAGE_CROSSHAIR))
  112.     fh = open("crosshair.png", "wb")
  113.     fh.write(data)
  114.     fh.close()
  115.     print("Done!")
  116.  
  117. GRAPH = graphics((512, 360), 2)
  118. GRAPH.loadSprite("crosshair", "crosshair.png", colorkey=BLACK)
  119.  
  120. # draw a load of red circles to the primary display
  121. for i in range(200):
  122.     pygame.draw.circle(GRAPH.primary, RED, (randint(0, 1024), randint(0, 720)), 25)
  123. pygame.display.update()
  124.  
  125. exit = False
  126. while not exit:
  127.     events = pygame.event.get()
  128.     for event in events:
  129.         if event.type == pygame.QUIT or (event.type == pygame.KEYUP and event.key == pygame.K_ESCAPE):
  130.             exit = True
  131.         elif event.type == pygame.KEYUP and event.key == pygame.K_F5:
  132.             pass
  133.  
  134.         GRAPH.blit("crosshair", GRAPH.mouse(), center=True, unblit=True)
  135.         GRAPH.update()
  136.  
  137. pygame.quit()
  138.  
Add Comment
Please, Sign In to add comment