rects
This commit is contained in:
parent
6efae3e29e
commit
b9d2468194
@ -1,5 +1,12 @@
|
||||
from doodles import Rectangle
|
||||
from doodles import Rectangle, Color, world
|
||||
import random
|
||||
|
||||
def create():
|
||||
for _ in range(100):
|
||||
Rectangle().random(250)
|
||||
for _ in range(25):
|
||||
Rectangle().random(200).color(Color.BLACK).z_index(10)
|
||||
Rectangle().random(150).color(Color.DARK_BLUE).z_index(15)
|
||||
Rectangle().random(100).color(Color.DARK_GREY).z_index(20)
|
||||
Rectangle().random(50).color(Color.LIGHT_GREY).z_index(30)
|
||||
# Rectangle().random(250).color(
|
||||
# random.choice((Color.BLACK, Color.LIGHT_GREY, Color.DARK_GREY, Color.WHITE))
|
||||
# )
|
||||
|
@ -5,9 +5,6 @@ import importlib
|
||||
import typer
|
||||
from .world import world
|
||||
|
||||
FPS = 60
|
||||
MS_PER_FRAME = 1000 / 60
|
||||
|
||||
|
||||
def get_examples():
|
||||
module_path = Path(__file__).parent / "examples"
|
||||
@ -34,7 +31,6 @@ def load_module(modname):
|
||||
|
||||
|
||||
def main(modname: str = None):
|
||||
pygame.init()
|
||||
world.init()
|
||||
|
||||
examples = get_examples()
|
||||
@ -44,9 +40,6 @@ def main(modname: str = None):
|
||||
else:
|
||||
load_module(examples[ex_index])
|
||||
|
||||
elapsed = 0
|
||||
clock = pygame.time.Clock()
|
||||
|
||||
while True:
|
||||
for event in pygame.event.get():
|
||||
if event.type == pygame.QUIT:
|
||||
@ -59,14 +52,7 @@ def main(modname: str = None):
|
||||
elif event.key == pygame.K_LEFT:
|
||||
ex_index = (ex_index - 1) % len(examples)
|
||||
load_module(examples[ex_index])
|
||||
elapsed += clock.tick(FPS)
|
||||
while elapsed > MS_PER_FRAME:
|
||||
elapsed -= MS_PER_FRAME
|
||||
world.tick()
|
||||
world.render()
|
||||
#print(clock.get_fps())
|
||||
pygame.display.flip()
|
||||
|
||||
world.update()
|
||||
|
||||
if __name__ == "__main__":
|
||||
typer.run(main)
|
||||
|
@ -34,6 +34,9 @@ class World:
|
||||
|
||||
WIDTH = 800
|
||||
HEIGHT = 600
|
||||
FPS = 60
|
||||
MS_PER_FRAME = 1000 / FPS
|
||||
|
||||
_instance = None
|
||||
|
||||
def __init__(self):
|
||||
@ -52,7 +55,10 @@ class World:
|
||||
"""
|
||||
if self.screen:
|
||||
raise ValueError("Can't initialize world twice!")
|
||||
pygame.init()
|
||||
self.screen = pygame.display.set_mode((world.WIDTH, world.HEIGHT))
|
||||
self.clock = pygame.time.Clock()
|
||||
self._elapsed = 0
|
||||
|
||||
def clear(self):
|
||||
self._drawables = []
|
||||
@ -64,13 +70,21 @@ class World:
|
||||
for d in self._drawables:
|
||||
d.update()
|
||||
|
||||
def render(self):
|
||||
def update(self):
|
||||
"""
|
||||
Draw world to screen
|
||||
Update & draw world to screen.
|
||||
"""
|
||||
# update
|
||||
self._elapsed += self.clock.tick(self.FPS)
|
||||
while self._elapsed > self.MS_PER_FRAME:
|
||||
self._elapsed -= self.MS_PER_FRAME
|
||||
self.tick()
|
||||
|
||||
# rendering
|
||||
self.screen.fill(self.background_color)
|
||||
for d in sorted(self._drawables, key=lambda d: d._z_index):
|
||||
d.draw(self.screen)
|
||||
pygame.display.flip()
|
||||
|
||||
|
||||
# our singleton instance
|
||||
|
Loading…
Reference in New Issue
Block a user