def onUpdate(self): self.gameObject.aggressive = True if self.movingUp: if self.active: self.gameObject.getComponent(Physics).velocity = Vector2( 0, 50) else: self.gameObject.aggressive = False if self.gameObject.transform.position.y >= self.startPositionY + 24: self.gameObject.getComponent(Physics).velocity = Vector2( 0, 0) elif not self.movingUp: self.gameObject.getComponent(Physics).velocity = Vector2( 0, -50) if self.gameObject.transform.position.y <= self.startPositionY: self.gameObject.getComponent(Physics).velocity = Vector2( 0, 0) self.gameObject.aggressive = False if not self.active: Timer.remove(self.hideTimer)
def on_update(cls, dt: float): cls.dt = dt while not cls.callableQueue.empty(): callable = cls.callableQueue.get() callable() for gameObject in cls.gameObjects: for script in gameObject.scripts: script.onUpdate() from gameengine.managers.CameraManager import CameraManager for camera in CameraManager().collection: for script in camera.scripts: script.onUpdate() from gameengine.managers.PhysicsManager import PhysicsManager PhysicsManager().onUpdate(dt) from gameengine.managers.CollisionManager import CollisionManager CollisionManager().onUpdate(dt) Timer.tick() for gameObject in cls.gameObjects.copy(): for script in gameObject.scripts: script.onLateUpdate() from gameengine.managers.CameraManager import CameraManager for camera in CameraManager().collection: for script in camera.scripts: script.onLateUpdate() cls.frameCount += 1
def onCollisionStay(self, other): if "PowerUp" in other.tags and other.popped: # print(World.frameCount) if "SuperMushroom" in other.tags or "FireFlower" in other.tags: self.gameObject.state.upgrade() elif "SuperStar" in other.tags: self.gameObject.tags.append("Invincible") Resources.bgMusic.pause() p = Resources.star.play() p.volume = 0.15 def f(): Resources.bgMusic.play() p.set_handler("on_player_eos", f) def f(): self.gameObject.tags.remove("Invincible") state = str(self.gameObject.state) if state == "Small": self.gameObject.spriteSet = Resources.playerSprites[ self.gameObject.character]["small"] elif state == "Big": self.gameObject.spriteSet = Resources.playerSprites[ self.gameObject.character]["big"] elif state == "Fire": self.gameObject.spriteSet = Resources.playerSprites[ "fire"] Timer.add(f, (), 10000, 0, 1)
def onCollisionEnter(self, other, side): if "Player" in other.tags: Timer.remove(self.walkTimer) if side == Sides.TOP_SIDE: self.script.state = KoopaTroopaScript.Stomped(self.script) else: self.script.state = KoopaTroopaScript.Bowling(self.script, other)
def goPassive(self): self.tags.append("Passive") self.getComponent(SpriteRenderer).opacity = 127 def f(): self.tags.remove("Passive") self.getComponent(SpriteRenderer).opacity = 255 Timer.add(f, (), 2000, 0, 1)
def destroy(cls, gameObject, delay=0): def f(): if gameObject not in cls.gameObjects: return gameObject.prepareDestroy() cls.gameObjects.remove(gameObject) if delay == 0: cls.callableQueue.put(f) else: Timer.add(cls.callableQueue.put, (f, ), delay, 0, 1)
def init(self, *args): self.gameObject.getComponent(Physics).velocity.y = -120 Resources.bgMusic.pause() Resources.flagpole.play().volume = 0.05 World.findByTag("Time")[0].stop() World.findByTag("Score")[0].add(5000) def goToNextLevel(): if self.gameObject.prepareNextLevel: self.gameObject.prepareNextLevel() if self.gameObject.nextScene: SceneManager().loadScene(self.gameObject.nextScene, *self.gameObject.nextSceneArgs, *self.gameObject.nextSceneKwargs) Timer.add(goToNextLevel, (), 2000, 0, 1)
def init(self, *args, **kwargs): self.tags.append("Instructions") self.font_name = "Emulogic" self.font_size = 16 self.text = "Deplacement : fleches\nCourir/jeter des boules de feu : SHIFT" self.width = 1000 self.multiline = True self.set_style("align", "left") DrawingManager().GUI.add(self) def f(): DrawingManager().remove(self) DrawingManager().GUI.remove(self) World.destroy(self) Timer.add(f, (), 5000, 0, 1)
def active(self, val): if val is True and self._active is False: Timer.remove(self.showUpTimer) def goUp(): self.movingUp = True self.showUpTimer = Timer.add(goUp, (), 2000, 4000, -1) Timer.remove(self.hideTimer) def goDown(): self.movingUp = False self.hideTimer = Timer.add(goDown, (), 0, 4000, -1) if val is False and self._active is True: Timer.remove(self.showUpTimer) Timer.remove(self.hideTimer) self._active = val
def decrement(self): self.time -= 1 self.text = "TIME\n{:03d}".format(self.time) if self.time == 100: Resources.bgMusic.pause() p = Resources.hurryUp.play() p.volume = 0.15 def f(): setBgMusic(self.fastMusic).play() Resources.bgMusic.pitch = 1.05 p.set_handler("on_player_eos", f) if self.time <= 0: Timer.remove(self.countdown) players = World.findByTag("Player") for player in players: # TODO: Just die player.state.downgrade()
def onLoad(self, *args, **kwargs): self.mainCamera.backgroundColor = (0, 0, 0, 0) self.mainCamera.zoom = 3 # players = World.findByTag("Player") # if players: # from gameengine.components.Physics import Physics # players[0].getComponent(Physics).customGravity = (0, 0) from game.Player import Player Player.lives = 3 gameOverLabel = World.instantiate(Label, (500, 400)) gameOverLabel.text = "Game Over" gameOverLabel.font_name = "Emulogic" gameOverLabel.font_size = 22 gameOverLabel.set_style("align", "center") DrawingManager().GUI.add(gameOverLabel) scoreLabel = World.instantiate(Label, (400, 330)) scoreLabel.text = "final score: {}".format(World.findByTag("Score")[0].score) scoreLabel.font_name = "Emulogic" scoreLabel.font_size = 22 scoreLabel.set_style("align", "center") DrawingManager().GUI.add(scoreLabel) def loadNextLevel(): DrawingManager().remove(gameOverLabel) DrawingManager().GUI.remove(gameOverLabel) DrawingManager().remove(scoreLabel) DrawingManager().GUI.remove(scoreLabel) World.findByTag("Score")[0].reset() World.findByTag("Coins")[0].reset() from game.levels.Level1_1 import Level1_1 SceneManager().loadScene(Level1_1) Timer.add(loadNextLevel, (), 4000, 0, 1)
def init(self, *args, **kwargs): self.tags.append("Time") self.time = 500 self.font_name = "Emulogic" self.font_size = 18 self.text = "TIME\n{:03d}".format(self.time) self.width = 100 self.multiline = True self.set_style("align", "right") self.countdown = Timer.add(self.decrement, (), 400, 400, -1) self.fastMusic = None DrawingManager().GUI.add(self)
def __init__(self, script): self.script = script self.koopaTroopa = script.gameObject self.koopaTroopa.getComponent(Physics).velocity.x = 0 self.koopaTroopa.getComponent(SpriteRenderer).setImage(Resources.theme[self.koopaTroopa.theme][self.koopaTroopa.color]["koopaTroopaStomped"]) self.koopaTroopa.getComponent(Collider).offset = (0, 0) Resources.stomp.play().volume = 0.05 def recover(): self.script.state = KoopaTroopaScript.Recovering(self.script) self.recoverTimer = Timer.add(recover, (), 2500, 0, 1) World.findByTag("Score")[0].add(20)
def __init__(self, script): self.script = script self.koopaTroopa = script.gameObject self.koopaTroopa.getComponent(SpriteRenderer).setImage(Resources.theme[self.koopaTroopa.theme][self.koopaTroopa.color]["koopaTroopaRecovering"]) def walk(): self.koopaTroopa.transform.position += (0, 4) self.script.state = KoopaTroopaScript.Walk(self.script) players = World.findByTag("Player") if not players: return player = players[0] self.koopaTroopa.getComponent(Physics).velocity.x = \ -30 if player.transform.position.x < self.koopaTroopa.transform.position.x else 30 self.walkTimer = Timer.add(walk, (), 2000, 0, 1)
def __init__(self, script): self.script = script self.player = script.gameObject Player.lives -= 1 collider = self.player.getComponent(Collider) print("die", collider.gameObject) self.player.removeComponent(collider) self.player.getComponent(SpriteRenderer).setImage( self.player.spriteSet["die"]) Resources.bgMusic.pause() Resources.mariodie.play().volume = 0.2 World.findByTag("Time")[0].stop() self.player.getComponent(Physics).customGravity = (0, 0) self.player.getComponent(Physics).velocity.set(0, 0) def f(): self.player.getComponent(Physics).customGravity = None self.player.getComponent(Physics).velocity.set(0, 450) Timer.add(f, (), 500, 0, 1) def goToGameOver(): from game.levels.GameOverSplash import GameOverSplash SceneManager().loadScene(GameOverSplash) def resetLevel(): from game.levels.Level1_1 import Level1_1 from game.levels.LevelSplash import LevelSplash World.findByTag("Time")[0].restart() currentLevelClass = type(SceneManager().currentScene) SceneManager().loadScene(LevelSplash, currentLevelClass, currentLevelClass.major, currentLevelClass.minor) if self.player.lives <= 0: Timer.add(goToGameOver, (), 4000, 0, 1) else: Timer.add(resetLevel, (), 4000, 0, 1)
def restart(self): Timer.remove(self.countdown) self.countdown = Timer.add(self.decrement, (), 400, 400, -1)
def stop(self): Timer.remove(self.countdown)
def onDestroy(self): Timer.remove(self.showUpTimer) Timer.remove(self.hideTimer) from gameengine.core.World import World World.findByTag("Score")[0].add(100)
def onDestroy(self): Timer.remove(self.walkTimer)
def onLoad(self, nextLevel: Type[Scene], major, minor): self.mainCamera.backgroundColor = (0, 0, 0, 0) self.mainCamera.zoom = 3 if not World.findByTag("Score"): score = World.instantiate(Score, (80, self.mainCamera.size.y - 50)) score.keepBetweenScenes = True if not World.findByTag("Coins"): coins = World.instantiate(Coins, (400, self.mainCamera.size.y - 50)) coins.keepBetweenScenes = True if not World.findByTag("Level"): level = World.instantiate(Level, (800, self.mainCamera.size.y - 50)) level.keepBetweenScenes = True if not World.findByTag("Time"): time = World.instantiate(Time, (1100, self.mainCamera.size.y - 50)) time.keepBetweenScenes = True World.findByTag("Level")[0].major = major World.findByTag("Level")[0].minor = minor worldLabel = World.instantiate(Label, (500, 400)) worldLabel.text = "World {}-{}".format(major, minor) worldLabel.font_name = "Emulogic" worldLabel.font_size = 22 worldLabel.set_style("align", "center") DrawingManager().GUI.add(worldLabel) lifesLabel = World.instantiate(Label, (500, 325)) # players = World.findByTag("Player") # player = players[0] if players else None from game.Player import Player lifesLabel.text = "lives * {}".format(Player.lives) lifesLabel.font_name = "Emulogic" lifesLabel.font_size = 22 lifesLabel.set_style("align", "right") DrawingManager().GUI.add(lifesLabel) def loadNextLevel(): DrawingManager().remove(worldLabel) DrawingManager().GUI.remove(worldLabel) DrawingManager().remove(lifesLabel) DrawingManager().GUI.remove(lifesLabel) SceneManager().loadScene(nextLevel) # Prevent player falling while loading level class KeepPlayerFromFalling(Script): def init(self, *args, **kwargs): self.player = None players = World.findByTag("Player") if players: self.player = players[0] def onUpdate(self): if self.player: from gameengine.components.Physics import Physics self.player.getComponent(Physics).velocity.y = 0 from gameengine.core.GameObject import GameObject go = World.instantiate(GameObject, (0, 0)) go.addScript(KeepPlayerFromFalling) Timer.add(loadNextLevel, (), 1500, 0, 1)
def onDestroy(self): Timer.remove(self.recoverTimer)
def onCollisionEnter(self, other, side): if "Player" in other.tags: Timer.remove(self.recoverTimer) self.script.state = KoopaTroopaScript.Bowling(self.script, other)
from gameengine.core.Timer import Timer from time import sleep def fun1(text): print(text) t1 = Timer.add(fun1, ("1"), 1000, 500, 3) t2 = Timer.add(fun1, ("2"), 750, 500, -1) t3 = Timer.add(fun1, ("3"), 100, 1000, 1) t4 = Timer.add(fun1, ("2"), 750, 500, -1) Timer.remove(t4) while True: Timer.tick() sleep(0.01)