Instantly share code, notes, and snippets.
Created
June 27, 2025 12:07
-
Star
1
(1)
You must be signed in to star a gist -
Fork
0
(0)
You must be signed in to fork a gist
-
Save EncodeTheCode/fb84d95da63846e227ab262050bfea9d 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 os | |
# --- Config --- | |
SCREEN_WIDTH = 800 | |
SCREEN_HEIGHT = 600 | |
TWEEN_DISTANCE = 30 | |
TWEEN_SPEED = 2 # pixels per frame | |
WEAPON_FOLDER = "wpn" | |
SWITCH_DELAY = 300 # ms delay for weapon swap | |
# --- Init Pygame --- | |
pygame.init() | |
screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT)) | |
clock = pygame.time.Clock() | |
# --- Preload weapon images from folder --- | |
def load_weapons(folder): | |
weapons = {} | |
for fname in os.listdir(folder): | |
if fname.lower().endswith(".png"): | |
key = fname.rsplit(".",1)[0] | |
path = os.path.join(folder, fname) | |
weapons[key] = pygame.image.load(path).convert_alpha() | |
return weapons | |
weapon_images = load_weapons(WEAPON_FOLDER) | |
# --- TweenWeapon Class --- | |
class TweenWeapon: | |
def __init__(self, weapon_name="air_taser", show_flag=1): | |
self.weapon_name = weapon_name | |
self.image = weapon_images[weapon_name] | |
self.visible = False # start hidden | |
self.final_x = SCREEN_WIDTH - self.image.get_width() - TWEEN_DISTANCE | |
self.y = SCREEN_HEIGHT - self.image.get_height() | |
self.x = self.final_x | |
self.tweening = False | |
# control flag for whether tween is allowed | |
self.allow_tween = True | |
# for delayed swap | |
self.switching = False | |
self.switch_timer = 0 | |
self.next_weapon = None | |
# apply initial visibility if desired | |
self.set_visible(show_flag) | |
def set_visible(self, show_flag): | |
"""Show/hide. If showing now, trigger tween only if allow_tween.""" | |
show = bool(show_flag) | |
if show and not self.visible and self.allow_tween: | |
# play tween-in | |
self.x = SCREEN_WIDTH | |
self.tweening = True | |
self.visible = show | |
def swap_weapon(self, new_weapon): | |
"""Hide immediately, then after delay swap+show (with tween if allowed).""" | |
if new_weapon in weapon_images: | |
self.visible = False | |
self.switching = True | |
self.switch_timer = pygame.time.get_ticks() | |
self.next_weapon = new_weapon | |
else: | |
print(f"Weapon '{new_weapon}' not found!") | |
def update(self): | |
now = pygame.time.get_ticks() | |
# complete swap after delay | |
if self.switching and now - self.switch_timer >= SWITCH_DELAY: | |
self.weapon_name = self.next_weapon | |
self.image = weapon_images[self.weapon_name] | |
self.final_x = SCREEN_WIDTH - self.image.get_width() - TWEEN_DISTANCE | |
self.y = SCREEN_HEIGHT - self.image.get_height() | |
self.switching = False | |
# use set_visible to show; will tween only if allow_tween | |
self.set_visible(1) | |
# handle tween motion | |
if self.tweening and self.x > self.final_x: | |
self.x -= TWEEN_SPEED | |
if self.x <= self.final_x: | |
self.x = self.final_x | |
self.tweening = False | |
# disable further tweens | |
self.allow_tween = False | |
def draw(self, surface): | |
if self.visible: | |
surface.blit(self.image, (self.x, self.y)) | |
# --- Main Loop --- | |
weapon = TweenWeapon() # starts hidden, then tweens once when shown | |
running = True | |
while running: | |
for evt in pygame.event.get(): | |
if evt.type == pygame.QUIT: | |
running = False | |
if evt.type == pygame.KEYDOWN: | |
# swap weapons (will hide, then after delay show+tween if allowed) | |
if evt.key == pygame.K_1: | |
weapon.swap_weapon("air_taser") | |
if evt.key == pygame.K_2: | |
weapon.swap_weapon("shotgun") | |
# manual hide/show (show will tween only once) | |
if evt.key == pygame.K_h: | |
weapon.set_visible(0) | |
if evt.key == pygame.K_s: | |
weapon.set_visible(1) | |
# example: re-enable tween flag on menu close | |
if evt.key == pygame.K_m: | |
# simulate exiting menu: allow tween again, then show | |
weapon.allow_tween = True | |
weapon.set_visible(1) | |
weapon.update() | |
screen.fill((30, 30, 30)) | |
weapon.draw(screen) | |
pygame.display.flip() | |
clock.tick(60) | |
pygame.quit() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment