Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save EncodeTheCode/41b85c0725a62d1f7ee18b37d3957455 to your computer and use it in GitHub Desktop.
Save EncodeTheCode/41b85c0725a62d1f7ee18b37d3957455 to your computer and use it in GitHub Desktop.
# Images should be 500x250 PNG format with transparency. Take into account (X:115, Y:81) reference M-16 position on screen, does not match up with PyGame measurements, adjust accordingly.
import pygame
import sys
# --- Configuration ---
SCREEN_SIZE = (800, 600)
BG_COLOR = (30, 30, 30)
FPS = 60
# Map each weapon ID to its filename
WEAPON_FILES = {
"weapon001": "wpn//air_taser.png",
"weapon002": "wpn//shotgun.png",
"weapon003": "wpn//air_taser.png",
}
# --- Image Manager ---
class ImageManager:
def __init__(self, file_map):
self.file_map = file_map
self.images = {} # id → ImageObject
self.current_weapon = None # currently active weapon ID
def create(self, img_id, x, y):
"""Load (or reload) image by ID at anchor (x,y)."""
filepath = self.file_map[img_id]
surf = pygame.image.load(filepath).convert_alpha()
self.images[img_id] = ImageObject(surf, x, y)
return self.images[img_id]
def show(self, img_id):
self.images[img_id].show()
def hide(self, img_id):
self.images[img_id].hide()
def delete(self, img_id):
self.images[img_id].delete()
def restore(self, img_id):
"""If deleted, reload it at its stored x,y."""
obj = self.images[img_id]
if obj.deleted:
self.create(img_id, obj.x, obj.y)
def draw_all(self, screen):
for img in self.images.values():
img.draw(screen)
def switch_to(self, new_id):
"""Hide old weapon, show or (re)load new weapon, mark current."""
# hide old
if self.current_weapon is not None:
self.images[self.current_weapon].hide()
# if never created or deleted, create at its last or default pos
if new_id not in self.images:
# default position if first time: center screen
cx, cy = SCREEN_SIZE[0]//2, SCREEN_SIZE[1]//2
self.create(new_id, cx, cy)
else:
obj = self.images[new_id]
if obj.deleted:
self.restore(new_id)
else:
self.show(new_id)
# set as current
self.current_weapon = new_id
class ImageObject:
def __init__(self, surface, x, y):
self.surface = surface
self.x = x
self.y = y
self.visible = True
self.deleted = False
self.half_w = surface.get_width() // 2
self.half_h = surface.get_height() // 2
def draw(self, screen):
if self.visible and not self.deleted:
dx = self.x - self.half_w
dy = self.y - self.half_h
screen.blit(self.surface, (dx, dy))
def show(self):
self.visible = True
def hide(self):
self.visible = False
def delete(self):
self.visible = False
self.deleted = True
# --- Pygame Setup ---
pygame.init()
screen = pygame.display.set_mode(SCREEN_SIZE)
pygame.display.set_caption("Weapon Image Manager with Switching")
clock = pygame.time.Clock()
# --- Instantiate Manager and Pre-create All Weapons ---
mgr = ImageManager(WEAPON_FILES)
center_x, center_y = SCREEN_SIZE[0]//2, SCREEN_SIZE[1]//2
for wid in WEAPON_FILES:
mgr.create(wid, center_x + 74, center_y + 181) # X: 115 - 4 ( 111 ), Y: 81
mgr.hide(wid) # start hidden
# Optionally, initialize with a default weapon
mgr.switch_to("weapon001")
# --- Main Loop ---
while True:
for ev in pygame.event.get():
if ev.type == pygame.QUIT:
pygame.quit()
sys.exit()
if ev.type == pygame.KEYDOWN:
# Number keys to switch
if ev.key == pygame.K_1:
mgr.switch_to("weapon001")
if ev.key == pygame.K_2:
mgr.switch_to("weapon002")
if ev.key == pygame.K_3:
mgr.switch_to("weapon003")
# Draw
screen.fill(BG_COLOR)
mgr.draw_all(screen)
pygame.display.flip()
clock.tick(FPS)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment