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():
|
def create():
|
||||||
for _ in range(100):
|
for _ in range(25):
|
||||||
Rectangle().random(250)
|
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
|
import typer
|
||||||
from .world import world
|
from .world import world
|
||||||
|
|
||||||
FPS = 60
|
|
||||||
MS_PER_FRAME = 1000 / 60
|
|
||||||
|
|
||||||
|
|
||||||
def get_examples():
|
def get_examples():
|
||||||
module_path = Path(__file__).parent / "examples"
|
module_path = Path(__file__).parent / "examples"
|
||||||
@ -34,7 +31,6 @@ def load_module(modname):
|
|||||||
|
|
||||||
|
|
||||||
def main(modname: str = None):
|
def main(modname: str = None):
|
||||||
pygame.init()
|
|
||||||
world.init()
|
world.init()
|
||||||
|
|
||||||
examples = get_examples()
|
examples = get_examples()
|
||||||
@ -44,9 +40,6 @@ def main(modname: str = None):
|
|||||||
else:
|
else:
|
||||||
load_module(examples[ex_index])
|
load_module(examples[ex_index])
|
||||||
|
|
||||||
elapsed = 0
|
|
||||||
clock = pygame.time.Clock()
|
|
||||||
|
|
||||||
while True:
|
while True:
|
||||||
for event in pygame.event.get():
|
for event in pygame.event.get():
|
||||||
if event.type == pygame.QUIT:
|
if event.type == pygame.QUIT:
|
||||||
@ -59,14 +52,7 @@ def main(modname: str = None):
|
|||||||
elif event.key == pygame.K_LEFT:
|
elif event.key == pygame.K_LEFT:
|
||||||
ex_index = (ex_index - 1) % len(examples)
|
ex_index = (ex_index - 1) % len(examples)
|
||||||
load_module(examples[ex_index])
|
load_module(examples[ex_index])
|
||||||
elapsed += clock.tick(FPS)
|
world.update()
|
||||||
while elapsed > MS_PER_FRAME:
|
|
||||||
elapsed -= MS_PER_FRAME
|
|
||||||
world.tick()
|
|
||||||
world.render()
|
|
||||||
#print(clock.get_fps())
|
|
||||||
pygame.display.flip()
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
typer.run(main)
|
typer.run(main)
|
||||||
|
@ -34,6 +34,9 @@ class World:
|
|||||||
|
|
||||||
WIDTH = 800
|
WIDTH = 800
|
||||||
HEIGHT = 600
|
HEIGHT = 600
|
||||||
|
FPS = 60
|
||||||
|
MS_PER_FRAME = 1000 / FPS
|
||||||
|
|
||||||
_instance = None
|
_instance = None
|
||||||
|
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
@ -52,7 +55,10 @@ class World:
|
|||||||
"""
|
"""
|
||||||
if self.screen:
|
if self.screen:
|
||||||
raise ValueError("Can't initialize world twice!")
|
raise ValueError("Can't initialize world twice!")
|
||||||
|
pygame.init()
|
||||||
self.screen = pygame.display.set_mode((world.WIDTH, world.HEIGHT))
|
self.screen = pygame.display.set_mode((world.WIDTH, world.HEIGHT))
|
||||||
|
self.clock = pygame.time.Clock()
|
||||||
|
self._elapsed = 0
|
||||||
|
|
||||||
def clear(self):
|
def clear(self):
|
||||||
self._drawables = []
|
self._drawables = []
|
||||||
@ -64,13 +70,21 @@ class World:
|
|||||||
for d in self._drawables:
|
for d in self._drawables:
|
||||||
d.update()
|
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)
|
self.screen.fill(self.background_color)
|
||||||
for d in sorted(self._drawables, key=lambda d: d._z_index):
|
for d in sorted(self._drawables, key=lambda d: d._z_index):
|
||||||
d.draw(self.screen)
|
d.draw(self.screen)
|
||||||
|
pygame.display.flip()
|
||||||
|
|
||||||
|
|
||||||
# our singleton instance
|
# our singleton instance
|
||||||
|
Loading…
Reference in New Issue
Block a user