Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import os
- import lzma, base64
- from random import randint
- from pygame import Rect
- from pygame import Vector2
- import pygame
- # graphics engine
- class graphics:
- def __init__(s, resolution, windowScale):
- s.windowScale = windowScale
- s.scale = windowScale
- s.res = Vector2(resolution)
- s.winRes = s.res * s.scale
- s.setResVars()
- s.sprites = {}
- s.updates = []
- s.unblits = []
- s.fullscreenFlags = pygame.FULLSCREEN | pygame.SCALED | pygame.HWACCEL | pygame.HWSURFACE | pygame.DOUBLEBUF
- pygame.init()
- pygame.mouse.set_visible(False)
- s.primary = pygame.display.set_mode((int(s.winRes[0]), int(s.winRes[1])))
- s.primary.set_alpha(None)
- def setResVars(s):
- s.center = (s.res[0] // 2, s.res[1] // 2)
- s.w = s.res[0]
- s.h = s.res[1]
- s.midTop = (s.center[0], 0)
- s.midBottom = (s.center[0], s.res[1])
- s.midLeft = (0, s.center[1])
- s.midRight = (s.res[0], s.center[1])
- def makeUnblit(s):
- surface = pygame.Surface(s.updates[-1][1])
- surface.blit(s.primary, (0, 0), s.updates[-1])
- s.unblits += [s.behind(surface, s.updates[-1])]
- def unblit(s):
- while s.unblits:
- unblit = s.unblits.pop()
- s.updates += [unblit.r]
- s.primary.blit(unblit.surface, unblit.r[0])
- def update(s):
- pygame.display.update(s.updates)
- s.updates = []
- s.unblit()
- def flush(s):
- s.updates = []
- pygame.display.update()
- def mouse(s):
- mp = pygame.mouse.get_pos()
- return (mp[0] // s.scale, mp[1] // s.scale)
- def loadSprite(s, id, filename, dimensions=(1, 1), colorkey=None):
- surface = pygame.image.load(filename).convert()
- sw, sh = Vector2(surface.get_rect().size) * s.scale
- w, h = (int(sw / dimensions[0]), int(sh / dimensions[1]))
- newSurface = pygame.transform.scale(surface, (int(sw), int(sh)))
- newSurface.set_colorkey(colorkey)
- s.sprites[id] = [newSurface.subsurface(((index % dimensions[0] * w, index // dimensions[0] * h), (w, h))) for index in range(dimensions[0] * dimensions[1])]
- def sw(s, id):
- return s.sprites[id][0].get_rect().w
- def sh(s, id):
- return s.sprites[id][0].get_rect().h
- def cx(s, id):
- return s.sprites[id][0].get_rect().center[0]
- def cy(s, id):
- return s.sprites[id][0].get_rect().center[1]
- def blit(s, id, xy, index=0, center=False, unblit=False):
- a = Vector2(xy) * s.scale
- b = s.sprites[id][0].get_rect()
- a -= b.center if center else (0, 0)
- s.updates += [(a, b.size)]
- if unblit:
- s.makeUnblit()
- s.primary.blit(s.sprites[id][index], a)
- def rect(s, color, xy, wh, lineThickness=0, unblit=False):
- r = (Vector2(xy) * s.scale, Vector2(wh) * s.scale)
- pygame.draw.rect(s.primary, color, r, lineThickness)
- s.updates += [r]
- class behind:
- def __init__(s, surface, r):
- s.surface = surface
- s.r = r
- # if more than 100 updates, update the whole screen
- BLACK = (0, 0, 0)
- WHITE = (255, 255, 255)
- RED = (128, 0, 0)
- IMAGE_CROSSHAIR = b'/Td6WFoAAATm1rRGAgAhARYAAAB0L+Wj4ACXAJJdAESUBcR6J/b37omOUJCIs6rVUCaEMRn335INOFFYHloaVPVi8u5\
- fWYmItWESIqUBzMmpnnr19bxep/TDJOFgavLr4xVaRkaNk1ElcJQYsossarUihOK/muXes2ISsz9VnoA0A+BIQ17APXpBlHeawXuKT7OAaemEpc\
- PWcIXUVjZ1PuesTr2gLw7N9ycZK/OQveAAAAAASgLlBJyi5PgAAa4BmAEAAI8uFaSxxGf7AgAAAAAEWVo='
- # DON'T PANIC!!! this saves the crosshair image to disk
- if not os.path.isfile("crosshair.png"):
- print("Creating crosshair image...", end="")
- data = lzma.decompress(base64.b64decode(IMAGE_CROSSHAIR))
- fh = open("crosshair.png", "wb")
- fh.write(data)
- fh.close()
- print("Done!")
- GRAPH = graphics((512, 360), 2)
- GRAPH.loadSprite("crosshair", "crosshair.png", colorkey=BLACK)
- # draw a load of red circles to the primary display
- for i in range(200):
- pygame.draw.circle(GRAPH.primary, RED, (randint(0, 1024), randint(0, 720)), 25)
- pygame.display.update()
- exit = False
- while not exit:
- events = pygame.event.get()
- for event in events:
- if event.type == pygame.QUIT or (event.type == pygame.KEYUP and event.key == pygame.K_ESCAPE):
- exit = True
- elif event.type == pygame.KEYUP and event.key == pygame.K_F5:
- pass
- GRAPH.blit("crosshair", GRAPH.mouse(), center=True, unblit=True)
- GRAPH.update()
- pygame.quit()
Add Comment
Please, Sign In to add comment