Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save EncodeTheCode/7effe0256853dac6512c571ece91a418 to your computer and use it in GitHub Desktop.
Save EncodeTheCode/7effe0256853dac6512c571ece91a418 to your computer and use it in GitHub Desktop.
import pygame
import sys
from pathlib import Path
pygame.init()
WIDTH, HEIGHT = 800, 600
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("Crosshair Ultra-Fast Example")
gfx = Path("gfx")
xhair = gfx / "xhair.png"
crosshair_img = pygame.image.load(xhair).convert_alpha()
crosshair_offset = pygame.math.Vector2(142, 63)
crosshair_mode = False
def enter_first_person_mode():
pygame.mouse.set_visible(False)
print("Switched to first person mode (crosshair active).")
def enter_third_person_mode():
pygame.mouse.set_visible(True)
print("Switched to third person mode (crosshair hidden).")
clock = pygame.time.Clock()
# Pre-bind functions and values for micro-optimization
blit = screen.blit
get_pos = pygame.mouse.get_pos
offset_x, offset_y = crosshair_offset.x, crosshair_offset.y
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_F3:
crosshair_mode = not crosshair_mode
if crosshair_mode:
enter_first_person_mode()
else:
enter_third_person_mode()
screen.fill((30, 30, 30))
if crosshair_mode:
mx, my = get_pos()
blit(crosshair_img, (mx - offset_x, my - offset_y))
pygame.display.flip()
clock.tick(60)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment