Created
June 28, 2025 08:46
-
-
Save EncodeTheCode/dc490c4ceefa61f3592b9bc9363b86f4 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
crosshair_img = pygame.Surface((285, 128), pygame.SRCALPHA) | |
pygame.draw.line(crosshair_img, (255, 0, 0), (0, 63), (285, 63), 2) | |
pygame.draw.line(crosshair_img, (255, 0, 0), (142, 0), (142, 128), 2) | |
# 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