Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Just a basic test to see how many moving sprites pygame can handle.
- # This will very on size of surface and computer specs.
- import pygame
- import random
- from pygame.sprite import Group, Sprite
- class Entity(Sprite):
- def __init__(self, image, position, anchor='topleft'):
- super().__init__()
- self.image = image
- self.rect = image.get_frect(**{anchor: position})
- class MovingEntity(Entity):
- def __init__(self, image, vector, speed, position, anchor='topleft'):
- super().__init__(image, position, anchor)
- self.vector = vector
- self.speed = speed
- def bounds(self, bound):
- if not bound.contains(self.rect):
- clamp = self.rect.clamp(bound)
- if clamp.x != self.rect.x:
- self.vector.x = -self.vector.x
- self.rect.x = clamp.x
- if clamp.y != self.rect.y:
- self.vector.y = -self.vector.y
- self.rect.y = clamp.y
- def update(self, delta, bound):
- self.rect.center += self.vector * self.speed * delta
- self.bounds(bound)
- class EntityAnchor(Entity):
- def __init__(self, image, position, anchor='topleft'):
- super().__init__(image, position, anchor)
- self.anchor = {anchor: position}
- def draw(self, surface):
- surface.blit(self.image, self.rect)
- def update_image(self, image):
- self.image = image
- self.rect = image.get_rect(**self.anchor)
- # Simple Infinite State Machine.
- class Scene:
- def __init__(self, control):
- self.control = control
- def on_draw(self, surface): pass
- def on_event(self, event): pass
- def on_delta(self, delta): pass
- class SceneControl:
- def __init__(self, caption, size, flags=0, fps=60):
- pygame.display.set_caption(caption)
- self.surface = pygame.display.set_mode(size, flags)
- self.rect = self.surface.get_rect()
- self.clock = pygame.time.Clock()
- self.running = True
- self.delta = 0
- self.fps = fps
- self.current_scene = Scene(self)
- self.next_scene = None
- def loop(self):
- while self.running:
- self.update_scene()
- for event in pygame.event.get():
- if event.type != pygame.QUIT:
- self.current_scene.on_event(event)
- else:
- self.running = False
- self.current_scene.on_update(self.delta)
- self.current_scene.on_draw(self.surface)
- pygame.display.flip()
- self.delta = self.clock.tick(self.fps) / 1000
- def run(self, scene=None):
- if scene:
- self.current_scene = scene
- self.loop()
- def update_scene(self):
- if self.next_scene:
- self.current_scene = self.next_scene
- self.next_scene = None
- class Main(Scene):
- fps_timer = pygame.event.custom_type()
- def __init__(self, control):
- super().__init__(control)
- self.background_color = 'black'
- self.sprites = Group()
- self.entities = Group()
- self.font = pygame.font.Font(None, 50)
- self.fps_image = self.create_stats(str(0), 'topright')
- self.image_count = self.create_stats(str(len(self.entities)), 'midtop')
- pygame.time.set_timer(Main.fps_timer, 500)
- self.add_sprites()
- def create_stats(self, value, anchor):
- fimage = self.font.render(value, 1, 'white')
- rect = self.control.rect.inflate(-20, -20)
- entity = EntityAnchor(fimage, getattr(rect, anchor), anchor)
- #self.sprites.add(entity)
- return entity
- def add_sprites(self, count=1000):
- rect = self.control.rect.inflate(-100, -100)
- nrange = (rect.left, rect.right), (rect.top, rect.bottom)
- size = random.randint(20, 50)
- ncolor = random.randint(0, 6)
- for i in range(count):
- image = pygame.Surface((size, size))
- color = self.get_color(ncolor)
- image.fill(color)
- vector = pygame.Vector2()
- vector.from_polar((1, random.randint(0, 360)))
- speed = random.randint(30, 100)
- pos = random.randint(*nrange[0]), random.randint(*nrange[1])
- entity = MovingEntity(image, vector, speed, pos, 'center')
- self.sprites.add(entity)
- self.entities.add(entity)
- self.update_count()
- def get_color(self, ncolor):
- r = g = b = 0
- if ncolor == 0:
- r = random.randint(120, 255)
- elif ncolor == 1:
- g = random.randint(120, 255)
- elif ncolor == 2:
- b = random.randint(120, 255)
- elif ncolor == 3:
- r = random.randint(120, 255)
- g = random.randint(120, 255)
- elif ncolor == 4:
- r = random.randint(120, 255)
- b = random.randint(120, 255)
- elif ncolor == 5:
- g = random.randint(120, 255)
- b = random.randint(120, 255)
- elif ncolor == 6:
- r = random.randint(120, 255)
- g = random.randint(120, 255)
- b = random.randint(120, 255)
- return pygame.Color(r, g, b)
- def on_draw(self, surface):
- surface.fill(self.background_color)
- self.sprites.draw(surface)
- self.fps_image.draw(surface)
- self.image_count.draw(surface)
- def on_event(self, event):
- if event.type == Main.fps_timer:
- self.update_fps()
- elif event.type == pygame.MOUSEBUTTONDOWN:
- if event.button == 1:
- self.add_sprites(100)
- elif event.button == 3:
- self.add_sprites(500)
- def on_update(self, delta):
- self.entities.update(delta, self.control.rect)
- def update_count(self):
- image = self.font.render(str(len(self.entities)), 1, "white")
- self.image_count.update_image(image)
- def update_fps(self):
- fps = int(self.control.clock.get_fps())
- image = self.font.render(str(fps), 1, "white")
- self.fps_image.update_image(image)
- if __name__ == "__main__":
- pygame.init()
- control = SceneControl("Moving Sprites", (800, 600))
- control.run(Main(control))
- pygame.quit()
Advertisement
Add Comment
Please, Sign In to add comment