Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save EncodeTheCode/0cc62499d4fb5e10ee6d21381a84b3a3 to your computer and use it in GitHub Desktop.
Save EncodeTheCode/0cc62499d4fb5e10ee6d21381a84b3a3 to your computer and use it in GitHub Desktop.
import pygame
import sys
# Initialize pygame
pygame.init()
# Create window
WIDTH, HEIGHT = 800, 600
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("Crosshair Toggle Example")
# Load crosshair image
gfx = "gfx//"
xhair = gfx + "xhair.png"
crosshair_img = pygame.image.load(xhair).convert_alpha()
# Crosshair offset so that center aligns with mouse
crosshair_offset = pygame.math.Vector2(142, 63)
# Mode flag
crosshair_mode = False
# Dummy functions
def enter_first_person_mode():
print("Switched to first person mode (crosshair active).")
def enter_third_person_mode():
print("Switched to third person mode (crosshair hidden).")
# Main loop
clock = pygame.time.Clock()
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)) # Background
if crosshair_mode:
mouse_x, mouse_y = pygame.mouse.get_pos()
# Compute top-left corner so that center of crosshair is at mouse position
draw_pos = (mouse_x - crosshair_offset.x, mouse_y - crosshair_offset.y)
screen.blit(crosshair_img, draw_pos)
pygame.display.flip()
clock.tick(60)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment