Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import pygame
- from pygame.sprite import Group, Sprite
- # Simple state
- class State:
- def __init__(self, engine):
- self.engine = engine
- def on_draw(self, surface):
- pass
- def on_event(self, event):
- pass
- def on_update(self, delta, ticks):
- pass
- # Simple display and state manager
- class DisplayEngine:
- def __init__(self, caption, width, height, flags=0):
- pygame.display.set_caption(caption)
- self.surface = pygame.display.set_mode((width, height), flags)
- self.rect = self.surface.get_rect()
- self.clock = pygame.time.Clock()
- self.running = False
- self.delta = 0
- self.fps = 60
- self.state = State(self)
- self.on_quit = self.quit
- @property
- def width(self):
- return self.rect.width
- @property
- def height(self):
- return self.rect.height
- @property
- def size(self):
- return self.rect.size
- def main_loop(self):
- self.running = True
- while self.running:
- for event in pygame.event.get():
- if event.type == pygame.QUIT:
- self.on_quit()
- else:
- self.state.on_event(event)
- ticks = pygame.time.get_ticks()
- self.state.on_draw(self.surface)
- self.state.on_update(self.delta, ticks)
- pygame.display.flip()
- self.delta = self.clock.tick(self.fps)
- def quit(self):
- self.running = False
- class Hitbox(Sprite):
- group = Group()
- @classmethod
- def create_image(cls, color, width, height):
- cls.image = pygame.Surface((width, height), pygame.SRCALPHA)
- cls.image.fill((0, 0, 0, 0))
- pygame.draw.rect(cls.image, color, (0, 0, width, height), 1)
- def __init__(self):
- Sprite.__init__(self)
- self.image = Hitbox.image
- self.rect = self.image.get_rect()
- Hitbox.group.add(self)
- class Enemy(Sprite):
- group = Group()
- @classmethod
- def create_image(cls, color, width, height):
- cls.image = pygame.Surface((width, height))
- cls.image.fill(color)
- def __init__(self, position ,anchor="topleft"):
- Sprite.__init__(self)
- self.image = Enemy.image
- self.rect = self.image.get_rect()
- setattr(self.rect, anchor, position)
- self.center = pygame.Vector2(self.rect.center)
- self.velocity = 0.1
- setattr(self.rect, anchor, position)
- self.hitbox = Hitbox()
- Enemy.group.add(self)
- def kill(self):
- self.hitbox.kill()
- Sprite.kill(self)
- def update(self, delta):
- self.center.x -= self.velocity * delta
- self.rect.center = self.center
- self.hitbox.rect.center = self.center
- if self.rect.right < 0:
- self.kill()
- class Label:
- def __init__(self, pen, text, position, anchor="topleft"):
- self.pen = pen
- self.image = pen.write(text)
- self.rect = self.image.get_rect()
- setattr(self.rect, anchor, position)
- self.anchor = anchor
- self.position = position
- def draw(self, surface):
- surface.blit(self.image, self.rect)
- def update(self, text):
- self.image = self.pen.write(text)
- self.rect = self.image.get_rect()
- setattr(self.rect, self.anchor, self.position)
- class Pen:
- def __init__(self, font, color):
- self.font = font
- self.color = color
- def write(self, text, alias=1):
- return self.font.render(text, alias, self.color)
- class ExampleState(State):
- def __init__(self, engine):
- State.__init__(self, engine)
- self.draw_hitboxes = True
- position = engine.rect.right - 10, 10
- pen = Pen(pygame.font.Font(None, 28), pygame.Color("lawngreen"))
- self.label = Label(pen, "Enemy Count: 0", position, "topright")
- self.enemy_count = 0
- def add_enemy(self):
- Enemy(self.engine.rect.center, "center")
- def on_draw(self, surface):
- surface.fill(pygame.Color('black'))
- Enemy.group.draw(surface)
- if self.draw_hitboxes:
- Hitbox.group.draw(surface)
- self.label.draw(surface)
- def on_event(self, event):
- if event.type == pygame.KEYDOWN:
- if event.key == pygame.K_SPACE:
- self.draw_hitboxes = not self.draw_hitboxes
- elif event.key == pygame.K_e:
- self.add_enemy()
- def on_update(self, delta, ticks):
- Enemy.group.update(delta)
- if self.enemy_count != len(Enemy.group):
- self.enemy_count = len(Enemy.group)
- self.update_label()
- def update_label(self):
- text = "Enemy Count: {}".format(len(Enemy.group))
- self.label.update(text)
- def main():
- pygame.init()
- engine = DisplayEngine("Example", 800, 600)
- Hitbox.create_image(pygame.Color("snow"), 50, 70)
- Enemy.create_image(pygame.Color("dodgerblue"), 200, 200)
- example = ExampleState(engine)
- engine.state = example
- engine.main_loop()
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement