Windspar

Pygame Test How Many Moving Images.

Jan 25th, 2026
167
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 6.23 KB | Gaming | 0 0
  1. # Just a basic test to see how many moving sprites pygame can handle.
  2. # This will very on size of surface and computer specs.
  3. import pygame
  4. import random
  5. from pygame.sprite import Group, Sprite
  6.  
  7. class Entity(Sprite):
  8.     def __init__(self, image, position, anchor='topleft'):
  9.         super().__init__()
  10.         self.image = image
  11.         self.rect = image.get_frect(**{anchor: position})
  12.  
  13. class MovingEntity(Entity):
  14.     def __init__(self, image, vector, speed, position, anchor='topleft'):
  15.         super().__init__(image, position, anchor)
  16.         self.vector = vector
  17.         self.speed = speed
  18.  
  19.     def bounds(self, bound):
  20.         if not bound.contains(self.rect):
  21.             clamp = self.rect.clamp(bound)
  22.             if clamp.x != self.rect.x:
  23.                 self.vector.x = -self.vector.x
  24.                 self.rect.x = clamp.x
  25.  
  26.             if clamp.y != self.rect.y:
  27.                 self.vector.y = -self.vector.y
  28.                 self.rect.y = clamp.y
  29.  
  30.     def update(self, delta, bound):
  31.         self.rect.center += self.vector * self.speed * delta
  32.         self.bounds(bound)
  33.  
  34. class EntityAnchor(Entity):
  35.     def __init__(self, image, position, anchor='topleft'):
  36.         super().__init__(image, position, anchor)
  37.         self.anchor = {anchor: position}
  38.  
  39.     def draw(self, surface):
  40.         surface.blit(self.image, self.rect)
  41.  
  42.     def update_image(self, image):
  43.         self.image = image
  44.         self.rect = image.get_rect(**self.anchor)
  45.  
  46. # Simple Infinite State Machine.
  47. class Scene:
  48.     def __init__(self, control):
  49.         self.control = control
  50.  
  51.     def on_draw(self, surface): pass
  52.     def on_event(self, event): pass
  53.     def on_delta(self, delta): pass
  54.  
  55. class SceneControl:
  56.     def __init__(self, caption, size, flags=0, fps=60):
  57.         pygame.display.set_caption(caption)
  58.         self.surface = pygame.display.set_mode(size, flags)
  59.         self.rect = self.surface.get_rect()
  60.         self.clock = pygame.time.Clock()
  61.         self.running = True
  62.         self.delta = 0
  63.         self.fps = fps
  64.  
  65.         self.current_scene = Scene(self)
  66.         self.next_scene = None
  67.  
  68.     def loop(self):
  69.         while self.running:
  70.             self.update_scene()
  71.             for event in pygame.event.get():
  72.                 if event.type != pygame.QUIT:
  73.                     self.current_scene.on_event(event)
  74.                 else:
  75.                     self.running = False
  76.  
  77.             self.current_scene.on_update(self.delta)
  78.             self.current_scene.on_draw(self.surface)
  79.             pygame.display.flip()
  80.             self.delta = self.clock.tick(self.fps) / 1000
  81.  
  82.     def run(self, scene=None):
  83.         if scene:
  84.             self.current_scene = scene
  85.  
  86.         self.loop()
  87.  
  88.     def update_scene(self):
  89.         if self.next_scene:
  90.             self.current_scene = self.next_scene
  91.             self.next_scene = None
  92.  
  93. class Main(Scene):
  94.     fps_timer = pygame.event.custom_type()
  95.  
  96.     def __init__(self, control):
  97.         super().__init__(control)
  98.         self.background_color = 'black'
  99.         self.sprites = Group()
  100.         self.entities = Group()
  101.         self.font = pygame.font.Font(None, 50)
  102.         self.fps_image = self.create_stats(str(0), 'topright')
  103.         self.image_count = self.create_stats(str(len(self.entities)), 'midtop')
  104.         pygame.time.set_timer(Main.fps_timer, 500)
  105.  
  106.         self.add_sprites()
  107.  
  108.     def create_stats(self, value, anchor):
  109.         fimage = self.font.render(value, 1, 'white')
  110.         rect = self.control.rect.inflate(-20, -20)
  111.         entity = EntityAnchor(fimage, getattr(rect, anchor), anchor)
  112.         #self.sprites.add(entity)
  113.         return entity
  114.  
  115.     def add_sprites(self, count=1000):
  116.         rect = self.control.rect.inflate(-100, -100)
  117.         nrange = (rect.left, rect.right), (rect.top, rect.bottom)
  118.         size = random.randint(20, 50)
  119.         ncolor = random.randint(0, 6)
  120.  
  121.         for i in range(count):
  122.             image = pygame.Surface((size, size))
  123.             color = self.get_color(ncolor)
  124.             image.fill(color)
  125.             vector = pygame.Vector2()
  126.             vector.from_polar((1, random.randint(0, 360)))
  127.             speed = random.randint(30, 100)
  128.             pos = random.randint(*nrange[0]), random.randint(*nrange[1])
  129.             entity = MovingEntity(image, vector, speed, pos, 'center')
  130.             self.sprites.add(entity)
  131.             self.entities.add(entity)
  132.  
  133.         self.update_count()
  134.  
  135.     def get_color(self, ncolor):
  136.         r = g = b = 0
  137.         if ncolor == 0:
  138.             r = random.randint(120, 255)
  139.         elif ncolor == 1:
  140.             g = random.randint(120, 255)
  141.         elif ncolor == 2:
  142.             b = random.randint(120, 255)
  143.         elif ncolor == 3:
  144.             r = random.randint(120, 255)
  145.             g = random.randint(120, 255)
  146.         elif ncolor == 4:
  147.             r = random.randint(120, 255)
  148.             b = random.randint(120, 255)
  149.         elif ncolor == 5:
  150.             g = random.randint(120, 255)
  151.             b = random.randint(120, 255)
  152.         elif ncolor == 6:
  153.             r = random.randint(120, 255)
  154.             g = random.randint(120, 255)
  155.             b = random.randint(120, 255)
  156.  
  157.         return pygame.Color(r, g, b)
  158.  
  159.     def on_draw(self, surface):
  160.         surface.fill(self.background_color)
  161.         self.sprites.draw(surface)
  162.         self.fps_image.draw(surface)
  163.         self.image_count.draw(surface)
  164.  
  165.     def on_event(self, event):
  166.         if event.type == Main.fps_timer:
  167.             self.update_fps()
  168.         elif event.type == pygame.MOUSEBUTTONDOWN:
  169.             if event.button == 1:
  170.                 self.add_sprites(100)
  171.             elif event.button == 3:
  172.                 self.add_sprites(500)
  173.  
  174.     def on_update(self, delta):
  175.         self.entities.update(delta, self.control.rect)
  176.  
  177.     def update_count(self):
  178.         image = self.font.render(str(len(self.entities)), 1, "white")
  179.         self.image_count.update_image(image)
  180.  
  181.     def update_fps(self):
  182.         fps = int(self.control.clock.get_fps())
  183.         image = self.font.render(str(fps), 1, "white")
  184.         self.fps_image.update_image(image)
  185.  
  186. if __name__ == "__main__":
  187.     pygame.init()
  188.     control = SceneControl("Moving Sprites", (800, 600))
  189.     control.run(Main(control))
  190.     pygame.quit()
  191.  
Advertisement
Add Comment
Please, Sign In to add comment