def notify(world, font, message, from_scene, to_scene): entity = world.create_entity() world.add_component(entity, components.Position(640, 500)) world.add_component(entity, components.Image("speech.png")) util.drawText(world.component_for_entity(entity, components.Image).image, message, (255, 255, 255), pygame.Rect(30, 20, 246, 134), font) world.add_component(entity, components.Size(307, 173)) world.add_component(entity, components.Delay(2, fade_up, world, entity, from_scene, to_scene)) return entity
def fade_up(world, entity, from_scene, to_scene): world.add_component( entity, components.ChangePosition((0, -100), 4, interpolation.Smooth(), True, fade_out, world)) world.add_component(entity, components.ChangeAlpha(0, 4, interpolation.Smooth())) world.add_component(entity, components.Delay(5, next_scene, from_scene, to_scene))
def start_game(): for ent, c in self.world.get_component(components.CircleAnimation): c.loop = False c.chain = move_to_bottom for ent in [start, quitbutton, title]: pos = self.world.component_for_entity(ent, components.Position) self.world.add_component( ent, components.ChangePosition((pos.x, pos.y + 100), 1)) self.world.add_component(ent, components.ChangeAlpha(1, 0, 1)) def change_scene(): print("TODO: game screen") next_scene = self.world.create_entity() self.world.add_component(next_scene, components.Delay(2, change_scene))
def fade_scene(): for ent in [text, continue_text]: if self.world.has_component(ent, components.ChangeAlpha): self.world.remove_component(ent, components.ChangeAlpha) self.world.add_component(ent, components.ChangeAlpha(0, .5)) self.world.add_component( ent, components.ChangePosition( (640, self.world.component_for_entity( ent, components.Position).y + 50), .5)) self.world.add_component(text, components.Delay(1, next_scene)) pygame.mixer.Sound( os.path.join(components.get_base_path(), 'audio', 'click')).play()
def start_game(): for ent, (p, i) in self.world.get_components(components.Position, components.Image): self.world.add_component( ent, components.ChangePosition((p.x, p.y + 100), .25)) self.world.add_component(ent, components.ChangeAlpha(0, .25)) def change_scene(): self.switch_to_scene( text.TextScene( "This is the story of an adventurer named NaN, known across the land for his unwavering enthusiasm for helping anyone with anything. His abilities were known far and wide, and indeed our story even begins with him defeating a dragon with ease...", game.SceneOne())) next_scene = self.world.create_entity() self.world.add_component(next_scene, components.Delay(.5, change_scene))
def process(self, filtered_events, pressed_keys, dt, screen): for ent, (p, s, v) in self.world.get_components(components.Position, components.Size, components.Velocity): if v.y == 0 and self.world.has_component( ent, components.RotationalVelocity): r = self.world.component_for_entity( ent, components.RotationalVelocity) i = self.world.component_for_entity(ent, components.Image) i.image = r.image s.width = r.width s.height = r.height self.world.remove_component(ent, components.RotationalVelocity) v.y = min(v.y + 9.81 * 100 * dt, 53 * 100) # terminal velocity if not self.world.has_component(ent, components.Player): v.x *= .98 p.x = max(min(p.x + v.x * dt, 1280), 0) if self.ground != -1: p.y = min(p.y + v.y * dt, self.ground - s.height * s.scale / 2) if p.y >= self.ground - s.height * s.scale / 2 and v.y > 0: v.y = 0 else: p.y -= v.y * dt #Touch Physics for ent, (t, p, s) in self.world.get_components(components.Touch, components.Position, components.Size): rect = pygame.Rect(p.x + t.rect.x, p.y + s.height / 2 + t.rect.y, s.width + t.rect.width, s.height + t.rect.height) tp = self.world.component_for_entity(t.target, components.Position) ts = self.world.component_for_entity(t.target, components.Size) if rect.colliderect(pygame.Rect(tp.x, tp.y, ts.width, ts.height)): if not t.active: t.touch(*t.args) if t.multi: t.active = True else: self.world.remove_component(ent, components.Touch) else: t.active = False #Hanging Object Physics for hangEnt, (h, p, s) in self.world.get_components(components.Hang, components.Position, components.Size): rect = pygame.Rect(p.x, p.y, s.width, s.height) for ent, (v, tp, ts) in self.world.get_components(components.Velocity, components.Position, components.Size): if not self.world.has_component(ent, components.Player): if rect.colliderect( pygame.Rect(tp.x, tp.y, ts.width, ts.height)): if self.world.has_component(hangEnt, components.Hang): self.world.remove_component( hangEnt, components.Hang) self.world.add_component(hangEnt, components.Velocity(0, 0)) break #Platform physics for platEnt, (tl, box, pf) in self.world.get_components(components.Position, components.Size, components.Platform): for ent, (p, s, v) in self.world.get_components(components.Position, components.Size, components.Velocity): if (tl.x - box.width / 2) < p.x < (tl.x + box.width / 2) and ( tl.y - box.height) < (p.y + s.height / 2) - 20 < tl.y: if v.y > 0: p.y = min(((tl.y - box.height) - s.height / 2) + 20, p.y) v.y = min(0, v.y) #Flamable stuff for flamEnt, (f, p, s) in self.world.get_components(components.Flammable, components.Position, components.Size): if f.lit: if not self.world.has_component(flamEnt, components.Delay): flame = self.world.create_entity() self.world.add_component( flame, components.Position( p.x - s.width / 2 + random.random() * s.width, p.y - s.height / 2 + random.random() * s.height)) self.world.add_component(flame, components.Size(60, 60)) size = int(random.random() * 20) self.world.add_component( flame, components.Rect((255, random.random() * 255, 0), pygame.Rect(0, 0, size, size))) self.world.add_component( flame, components.ChangePosition( (-25 + random.random() * 50, -50 - random.random() * 100), 1, interpolation.PowIn(2), True, self.remove_entity, flame)) self.world.add_component(flamEnt, components.Delay(.03)) for ent, (f2, tp, ts) in self.world.get_components( components.Flammable, components.Position, components.Size): if not f2.lit: rect = pygame.Rect(p.x, p.y, s.width, s.height) if rect.colliderect( pygame.Rect(tp.x, tp.y, ts.width, ts.height)): f2.lit = True
def init(self): self.font = pygame.font.Font("RobotoMono-Regular.ttf", 42) self.titlefont = pygame.font.Font("RobotoMono-Regular.ttf", 72) def start_game(): for ent, c in self.world.get_component(components.CircleAnimation): c.loop = False c.chain = move_to_bottom for ent in [start, quitbutton, title]: pos = self.world.component_for_entity(ent, components.Position) self.world.add_component( ent, components.ChangePosition((pos.x, pos.y + 100), 1)) self.world.add_component(ent, components.ChangeAlpha(1, 0, 1)) def change_scene(): print("TODO: game screen") next_scene = self.world.create_entity() self.world.add_component(next_scene, components.Delay(2, change_scene)) def quit_game(): self.Terminate() def highlight(entity): t = self.world.component_for_entity(entity, components.Text) image = t.font.render("> " + t.text + " <", False, (128, 0, 128)) self.world.component_for_entity(entity, components.Image).image = image size = self.world.component_for_entity(entity, components.Size) size.width = image.get_width() size.height = image.get_height() self.world.add_component( entity, components.ChangeSize(1, .25, interpolation.Smooth())) def lowlight(entity): t = self.world.component_for_entity(entity, components.Text) image = t.font.render(t.text, False, t.color) self.world.component_for_entity(entity, components.Image).image = image size = self.world.component_for_entity(entity, components.Size) size.width = image.get_width() size.height = image.get_height() self.world.add_component( entity, components.ChangeSize(.5, .25, interpolation.Smooth())) def start_circle(circle, idx): self.world.add_component( circle, components.CircleAnimation(400, 1, math.pi * (1.25 + .15 * idx), True, None, circle, idx)) def move_to_bottom(circle, idx): self.world.add_component( circle, components.ChangePosition((16 + 250 * idx, 16), .5)) start = self.world.create_entity() image = self.font.render("start", False, (0, 128, 0)) self.world.add_component(start, components.Position(640, 480)) self.world.add_component(start, components.Text("start", self.font)) self.world.add_component(start, components.Image(image=image)) self.world.add_component( start, components.Size(image.get_width(), image.get_height(), .5)) self.world.add_component(start, components.Click(start_game)) self.world.add_component(start, components.Over(highlight, lowlight)) quitbutton = self.world.create_entity() image = self.font.render("quit", False, (0, 128, 0)) self.world.add_component(quitbutton, components.Position(640, 540)) self.world.add_component(quitbutton, components.Text("quit", self.font)) self.world.add_component(quitbutton, components.Image(image=image)) self.world.add_component( quitbutton, components.Size(image.get_width(), image.get_height(), .5)) self.world.add_component(quitbutton, components.Click(quit_game)) self.world.add_component(quitbutton, components.Over(highlight, lowlight)) title = self.world.create_entity() image = self.titlefont.render("Puzzle Painter", False, (0, 128, 0)) self.world.add_component(title, components.Position(640, 240)) self.world.add_component( title, components.Text("Puzzle Painter", self.titlefont)) self.world.add_component(title, components.Image(image=image)) self.world.add_component( title, components.Size(image.get_width(), image.get_height())) for idx, val in enumerate(["blue", "green", "orange", "purple", "red"]): circle = self.world.create_entity() self.world.add_component(circle, components.Position(640, 300)) self.world.add_component(circle, components.Image("pastel" + val + ".png")) self.world.add_component(circle, components.Size(16, 16)) self.world.add_component(circle, components.Velocity()) self.world.add_component( circle, components.Delay(idx / 5, start_circle, circle, idx)) self.world.add_processor(processors.ClickProcessor(), priority=10) self.world.add_processor(processors.RenderProcessor()) self.world.add_processor(processors.OverProcessor(), priority=10) self.world.add_processor(processors.VelocityProcessor(), priority=5) self.world.add_processor(processors.AnimationProcessor(), priority=5)