Windspar

Pygame Example Overriding Sprite kill method.

Feb 22nd, 2021
1,084
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import pygame
  2. from pygame.sprite import Group, Sprite
  3.  
  4. # Simple state
  5. class State:
  6.     def __init__(self, engine):
  7.         self.engine = engine
  8.  
  9.     def on_draw(self, surface):
  10.         pass
  11.  
  12.     def on_event(self, event):
  13.         pass
  14.  
  15.     def on_update(self, delta, ticks):
  16.         pass
  17.  
  18. # Simple display and state manager
  19. class DisplayEngine:
  20.     def __init__(self, caption, width, height, flags=0):
  21.         pygame.display.set_caption(caption)
  22.         self.surface = pygame.display.set_mode((width, height), flags)
  23.         self.rect = self.surface.get_rect()
  24.         self.clock = pygame.time.Clock()
  25.         self.running = False
  26.         self.delta = 0
  27.         self.fps = 60
  28.  
  29.         self.state = State(self)
  30.         self.on_quit = self.quit
  31.  
  32.     @property
  33.     def width(self):
  34.         return self.rect.width
  35.  
  36.     @property
  37.     def height(self):
  38.         return self.rect.height
  39.  
  40.     @property
  41.     def size(self):
  42.         return self.rect.size
  43.  
  44.     def main_loop(self):
  45.         self.running = True
  46.         while self.running:
  47.             for event in pygame.event.get():
  48.                 if event.type == pygame.QUIT:
  49.                     self.on_quit()
  50.                 else:
  51.                     self.state.on_event(event)
  52.  
  53.             ticks = pygame.time.get_ticks()
  54.             self.state.on_draw(self.surface)
  55.             self.state.on_update(self.delta, ticks)
  56.             pygame.display.flip()
  57.             self.delta = self.clock.tick(self.fps)
  58.  
  59.     def quit(self):
  60.         self.running = False
  61.  
  62. class Hitbox(Sprite):
  63.     group = Group()
  64.  
  65.     @classmethod
  66.     def create_image(cls, color, width, height):
  67.         cls.image = pygame.Surface((width, height), pygame.SRCALPHA)
  68.         cls.image.fill((0, 0, 0, 0))
  69.         pygame.draw.rect(cls.image, color, (0, 0, width, height), 1)
  70.  
  71.     def __init__(self):
  72.         Sprite.__init__(self)
  73.         self.image = Hitbox.image
  74.         self.rect = self.image.get_rect()
  75.  
  76.         Hitbox.group.add(self)
  77.  
  78. class Enemy(Sprite):
  79.     group = Group()
  80.  
  81.     @classmethod
  82.     def create_image(cls, color, width, height):
  83.         cls.image = pygame.Surface((width, height))
  84.         cls.image.fill(color)
  85.  
  86.     def __init__(self, position ,anchor="topleft"):
  87.         Sprite.__init__(self)
  88.         self.image = Enemy.image
  89.         self.rect = self.image.get_rect()
  90.         setattr(self.rect, anchor, position)
  91.         self.center = pygame.Vector2(self.rect.center)
  92.         self.velocity = 0.1
  93.         setattr(self.rect, anchor, position)
  94.         self.hitbox = Hitbox()
  95.  
  96.         Enemy.group.add(self)
  97.  
  98.     def kill(self):
  99.         self.hitbox.kill()
  100.         Sprite.kill(self)
  101.  
  102.     def update(self, delta):
  103.         self.center.x -= self.velocity * delta
  104.         self.rect.center = self.center
  105.         self.hitbox.rect.center = self.center
  106.  
  107.         if self.rect.right < 0:
  108.             self.kill()
  109.  
  110. class Label:
  111.     def __init__(self, pen, text, position, anchor="topleft"):
  112.         self.pen = pen
  113.         self.image = pen.write(text)
  114.         self.rect = self.image.get_rect()
  115.         setattr(self.rect, anchor, position)
  116.         self.anchor = anchor
  117.         self.position = position
  118.  
  119.     def draw(self, surface):
  120.         surface.blit(self.image, self.rect)
  121.  
  122.     def update(self, text):
  123.         self.image = self.pen.write(text)
  124.         self.rect = self.image.get_rect()
  125.         setattr(self.rect, self.anchor, self.position)
  126.  
  127. class Pen:
  128.     def __init__(self, font, color):
  129.         self.font = font
  130.         self.color = color
  131.  
  132.     def write(self, text, alias=1):
  133.         return self.font.render(text, alias, self.color)
  134.  
  135. class ExampleState(State):
  136.     def __init__(self, engine):
  137.         State.__init__(self, engine)
  138.         self.draw_hitboxes = True
  139.         position = engine.rect.right - 10, 10
  140.         pen = Pen(pygame.font.Font(None, 28), pygame.Color("lawngreen"))
  141.         self.label = Label(pen, "Enemy Count: 0", position, "topright")
  142.         self.enemy_count = 0
  143.  
  144.     def add_enemy(self):
  145.         Enemy(self.engine.rect.center, "center")
  146.  
  147.     def on_draw(self, surface):
  148.         surface.fill(pygame.Color('black'))
  149.         Enemy.group.draw(surface)
  150.         if self.draw_hitboxes:
  151.             Hitbox.group.draw(surface)
  152.  
  153.         self.label.draw(surface)
  154.  
  155.     def on_event(self, event):
  156.         if event.type == pygame.KEYDOWN:
  157.             if event.key == pygame.K_SPACE:
  158.                 self.draw_hitboxes = not self.draw_hitboxes
  159.             elif event.key == pygame.K_e:
  160.                 self.add_enemy()
  161.  
  162.     def on_update(self, delta, ticks):
  163.         Enemy.group.update(delta)
  164.         if self.enemy_count != len(Enemy.group):
  165.             self.enemy_count = len(Enemy.group)
  166.             self.update_label()
  167.  
  168.     def update_label(self):
  169.         text = "Enemy Count: {}".format(len(Enemy.group))
  170.         self.label.update(text)
  171.  
  172. def main():
  173.     pygame.init()
  174.     engine = DisplayEngine("Example", 800, 600)
  175.     Hitbox.create_image(pygame.Color("snow"), 50, 70)
  176.     Enemy.create_image(pygame.Color("dodgerblue"), 200, 200)
  177.     example = ExampleState(engine)
  178.     engine.state = example
  179.     engine.main_loop()
  180.  
  181. main()
  182.  
RAW Paste Data