Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import pygame
- import random
- from pygame.sprite import Group, Sprite
- # How many moving sprites does it take to break steady fps. Surface Vs Sprite Vs Vector2.
- class Scene:
- scenes = {}
- def __init__(self, engine, scene_name=None):
- self.engine = engine
- self._store_scene(scene_name)
- def _store_scene(self, scene_name):
- if scene_name == "":
- scene_name = self.__class__.__name__
- if scene_name:
- Scene.scenes[scene_name] = self
- def set_scene(self, scene, *args):
- if isinstance(scene, str):
- next_scene = Scene.scenes[scene]
- else:
- next_scene = scene
- next_scene.on_focus(*args)
- self.engine.next = next_scene
- def on_draw(self, surface):
- pass
- def on_event(self, event):
- pass
- def on_focus(self, *args):
- pass
- def on_lost_focus(self):
- pass
- def on_update(self, delta, ticks):
- pass
- def on_quit(self):
- self.engine.running = False
- 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.gamespeed = 1
- self.delta = 0
- self.fps = 60
- self.scene = Scene(self)
- self.next = None
- @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:
- self.update_scene()
- for event in pygame.event.get():
- if event.type == pygame.QUIT:
- self.scene.on_quit()
- else:
- self.scene.on_event(event)
- ticks = pygame.time.get_ticks()
- self.scene.on_draw(self.surface)
- self.scene.on_update(self.delta, ticks)
- pygame.display.flip()
- self.delta = self.clock.tick(self.fps) * 0.001 * self.gamespeed
- def update_scene(self):
- if self.next:
- self.scene.on_lost_focus()
- self.scene = self.next
- self.next = None
- class Box:
- def __init__(self, image, position, direction):
- self.image = image
- self.rect = self.image.get_rect(center=position)
- self.position = self.rect.center
- self.vector = direction
- def move(self, x, y):
- self.position = self.position[0] + x, self.position[1] + y
- self.rect.center = self.position
- def draw(self, surface):
- surface.blit(self.image, self.rect)
- def update(self, delta, area):
- self.position = (self.position[0] + self.vector[0] * delta,
- self.position[1] + self.vector[1] * delta)
- self.rect.center = self.position
- boolean = False
- clamp = self.rect.clamp(area)
- if self.rect.x != clamp.x:
- self.vector[0] = -self.vector[0]
- boolean = True
- if self.rect.y != clamp.y:
- self.vector[1] = -self.vector[1]
- boolean = True
- if boolean:
- self.rect = clamp
- self.position = self.rect.center
- class SBox(Sprite):
- def __init__(self, image, position, direction):
- super().__init__()
- self.image = image
- self.rect = self.image.get_rect(center=position)
- self.position = self.rect.center
- self.vector = direction
- def move(self, x, y):
- self.position = self.position[0] + x, self.position[1] + y
- self.rect.center = self.position
- def update(self, delta, area):
- self.position = (self.position[0] + self.vector[0] * delta,
- self.position[1] + self.vector[1] * delta)
- self.rect.center = self.position
- boolean = False
- clamp = self.rect.clamp(area)
- if self.rect.x != clamp.x:
- self.vector[0] = -self.vector[0]
- boolean = True
- if self.rect.y != clamp.y:
- self.vector[1] = -self.vector[1]
- boolean = True
- if boolean:
- self.rect = clamp
- self.position = self.rect.center
- class VBox(Sprite):
- def __init__(self, image, position, direction):
- super().__init__()
- self.image = image
- self.rect = self.image.get_rect(center=position)
- self.position = pygame.Vector2(self.rect.center)
- self.vector = pygame.Vector2(direction)
- def move(self, x, y):
- self.position += pygame.Vector2(x, y)
- self.rect.center = self.position
- def update(self, delta, area):
- self.position += self.vector * delta
- self.rect.center = self.position
- boolean = False
- clamp = self.rect.clamp(area)
- if self.rect.x != clamp.x:
- self.vector.x = -self.vector.x
- boolean = True
- if self.rect.y != clamp.y:
- self.vector.y = -self.vector.y
- boolean = True
- if boolean:
- self.rect = clamp
- self.position = self.rect.center
- class BoxState:
- BASIC = 0
- SPRITE = 1
- VECTOR = 2
- MAX = 3
- class BoxScene(Scene):
- def __init__(self, engine):
- Scene.__init__(self, engine)
- self.create_images()
- self.state = BoxState.BASIC
- self.load_boxes()
- self.fps_trip = 0
- self.fps_interval = 1000
- self.fps_tick = pygame.time.get_ticks() + self.fps_interval
- self.data = {0:[], 1:[], 2:[]}
- def add_box(self):
- if self.state == BoxState.BASIC:
- box_type = Box
- elif self.state == BoxState.SPRITE:
- box_type = SBox
- else:
- box_type = VBox
- directions = []
- for i in range(6, 40, 2):
- directions.append(i)
- directions.append(-i)
- image = random.choice(self.images)
- w, h = image.get_size()
- sw, sh = self.engine.size
- position = random.randint(0, sw - w), random.randint(0, sh - h)
- direction = [random.choice(directions), random.choice(directions)]
- if self.state == BoxState.BASIC:
- self.boxes.append(box_type(image, position, direction))
- else:
- self.boxes.add(box_type(image, position, direction))
- def create_images(self):
- self.images = []
- color_names = "dodgerblue", "lawngreen", "firebrick", "orange", "purple"
- for color_name in color_names:
- for size in range(8, 16, 2):
- surface = pygame.Surface((size, size))
- surface.fill(pygame.Color(color_name))
- self.images.append(surface)
- def load_boxes(self):
- self.count = 1000
- if self.state == BoxState.BASIC:
- self.boxes = []
- else:
- self.boxes = Group()
- for i in range(0, self.count):
- self.add_box()
- def on_draw(self, surface):
- surface.fill(pygame.Color('black'))
- if self.state == BoxState.BASIC:
- for box in self.boxes:
- box.draw(surface)
- else:
- self.boxes.draw(surface)
- def on_update(self, delta, ticks):
- if self.state == BoxState.BASIC:
- for box in self.boxes:
- box.update(delta, self.engine.rect)
- else:
- self.boxes.update(delta, self.engine.rect)
- if ticks > self.fps_tick:
- self.fps_tick += self.fps_interval
- avg = int(self.engine.clock.get_fps())
- if self.fps_trip < 4:
- print(avg)
- if avg >= self.engine.fps:
- self.fps_trip = 0
- print(self.count)
- add = 500
- self.count += add
- for i in range(add):
- self.add_box()
- else:
- self.fps_trip += 1
- else:
- self.data[self.state].append(self.count)
- if self.state == BoxState.VECTOR and len(self.data[0]) > 3:
- self.on_quit()
- self.fps_trip = 0
- self.state += 1
- self.state %= BoxState.MAX
- self.load_boxes()
- def on_quit(self):
- for i in range(BoxState.MAX):
- print(self.data[i])
- Scene.on_quit(self)
- if __name__ == "__main__":
- pygame.init()
- engine = DisplayEngine("Box Testing", 800, 600)
- engine.scene = BoxScene(engine)
- engine.main_loop()
- pygame.quit()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement