Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save EncodeTheCode/886faf761cae2bc73af7b5e3fa78a270 to your computer and use it in GitHub Desktop.
Save EncodeTheCode/886faf761cae2bc73af7b5e3fa78a270 to your computer and use it in GitHub Desktop.
import pygame, sys
pygame.init()
S = pygame.display.set_mode((800, 600))
C = pygame.time.Clock()
# ID→file map
F = {
"w1": "wpn//air_taser.png",
"w2": "wpn//shotgun.png",
"w3": "wpn//air_taser.png",
}
# Image object
class I:
def __init__(self, f, x, y):
s = pygame.image.load(f).convert_alpha()
self.s, self.hw, self.hh = s, s.get_width()//2, s.get_height()//2
self.x, self.y, self.v, self.d = x, y, True, False
def draw(self, S):
if self.v and not self.d:
S.blit(self.s, (self.x - self.hw, self.y - self.hh))
def hide(self): self.v = False
def show(self): self.v = True
def delete(self): self.v, self.d = False, True
# Manager
class M:
def __init__(self, F):
self.F, self.I, self.cur = F, {}, None
def switch(self, k):
# hide old
if self.cur: self.I[self.cur].hide()
# load if needed
if k not in self.I:
self.I[k] = I(self.F[k], X, Y)
o = self.I[k]
# reload if deleted
if o.d:
self.I[k] = I(self.F[k], o.x, o.y)
else:
o.show()
self.cur = k
def draw_all(self, S):
for o in self.I.values(): o.draw(S)
# setup
X, Y = 400, 300
m = M(F)
# preload & hide all
for k in F:
m.I[k] = I(F[k], X + 74, Y + 181) # X: 115 - 4 ( 111 ), Y: 81
m.I[k].hide()
# init first
m.switch("w1")
# main loop
while True:
for e in pygame.event.get():
if e.type == pygame.QUIT:
pygame.quit(); sys.exit()
if e.type == pygame.KEYDOWN:
if e.key == pygame.K_1: m.switch("w1")
elif e.key == pygame.K_2: m.switch("w2")
elif e.key == pygame.K_3: m.switch("w3")
S.fill((30,30,30))
m.draw_all(S)
pygame.display.flip()
C.tick(60)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment