def slashState(self): self.stop() self.anim = 'slash' r = slashRange[self.direction] backslash = False backthrust = False # when we hit an entity, we append it here so that # we know not to hurt it again. hitList = [] sound.slash1.Play() while not self._animator.kill: rect = list(r[self._animator.index]) + [self.layer] rect[0] += self.x rect[1] += self.y ents = ika.EntitiesAt(*rect) for e in ents: x = system.engine.entFromEnt[e] if isinstance(x, Enemy) and not x.invincible and x not in hitList: hitList.append(x) x.hurt( int(self.stats.att + ika.Random(0, 3)), 120, self.direction) self.giveMPforHit() if self.stats.level >= BACK_LEVEL: if (controls.up() or controls.joy_up()) and self.direction == dir.DOWN: backthrust = True elif (controls.down() or controls.joy_down()) and self.direction == dir.UP: backthrust = True elif (controls.left() or controls.joy_left()) and self.direction in [dir.RIGHT, dir.UPRIGHT, dir.DOWNRIGHT]: backthrust = True elif (controls.right() or controls.joy_right()) and self.direction in [dir.LEFT, dir.UPLEFT, dir.DOWNLEFT]: backthrust = True if (controls.attack() or controls.joy_attack()) and self.stats.level >= SLASH_LEVEL: backslash = True yield None if backthrust: self.state = self.backThrustState() yield None elif backslash: self.state = self.backSlashState() yield None else: # Stall: count = 10 while count > 0: count -= 1 if self.stats.level >= THRUST_LEVEL and (controls.attack() or controls.joy_attack()): self.state = self.thrustState() yield None
def animate(ent, frames, delay, thing=None, loop=True, text=None): class AnimException(Exception): pass # frames should be a list of (frame, delay) pairs. if thing is not None: crap.append(thing) if text is not None: text = textBox(ent, text) crap.append(text) try: while True: for frame in frames: ent.specframe = frame d = delay while d > 0: d -= 1 draw() ika.Video.ShowPage() ika.Delay(1) ika.Input.Update() if controls.attack() or controls.ui_accept( ) or controls.joy_attack(): loop = False raise AnimException if not loop: raise AnimException except: #except what? if thing: crap.remove(thing) if text: crap.remove(text) ent.specframe = 0
def gameOver(self): c = Caption('G A M E O V E R', duration=1000000, y=(ika.Video.yres - self.font.height) / 2) t = 80 i = 0 self.fields = [] while True: i = min(i + 1, t) c.update() self.tick() self.draw() # darken the screen, draw the game over message: o = i * 255 / t ika.Video.DrawRect(0, 0, ika.Video.xres, ika.Video.yres, ika.RGB(0, 0, 0, o), True) c.draw() ika.Video.ShowPage() ika.Delay(4) if i == t and (controls.attack() or controls.joy_attack() or controls.ui_accept() or controls.ui_cancel() or controls.cancel() or controls.joy_cancel()): break
def backSlashState(self): self.stop() self.anim = 'backslash' r = backSlashRange[self.direction] # when we hit an entity, we append it here so that # we know not to hurt it again. hitList = [] sound.slash2.Play() while not self._animator.kill: rect = list(r[self._animator.index]) + [self.layer] rect[0] += self.x rect[1] += self.y ents = ika.EntitiesAt(*rect) for e in ents: x = system.engine.entFromEnt[e] if isinstance(x, Enemy) and not x.invincible and x not in hitList: hitList.append(x) x.hurt(int(self.stats.att + ika.Random(0, 3) * 1.25 ), 130, self.direction) self.giveMPforHit() yield None # Stall: count = 10 while count > 0: count -= 1 if controls.rend() or controls.joy_rend(): self.state = self.hearthRendState() elif self.stats.level >= THRUST_LEVEL and (controls.attack() or controls.joy_attack()): self.state = self.thrustState() yield None
def text(where, txt): """Displays a text frame. Where can be either a point or an ika entity. """ frame = textBox(where, txt) while not (controls.attack() or controls.joy_attack() or controls.ui_accept()): draw() frame.draw() ika.Video.ShowPage() ika.Input.Update()
def delay(draw, count, snow): while count > 0: draw() snow.update() snow.draw() ika.Delay(1) count -= 1 ika.Video.ShowPage() ika.Input.Update() if controls.attack() or controls.ui_accept( ) or controls.cancel() or controls.ui_cancel() or controls.joy_cancel( ) or controls.joy_attack(): raise _DoneException()
def walkState(self): oldDir = self.direction self.anim = 'walk' while True: self.regenMP() if controls.attack() or controls.joy_attack(): self.state = self.slashState() elif controls.rend() or controls.joy_rend(): self.state = self.hearthRendState() yield None elif controls.gale() or controls.joy_gale(): self.state = self.crushingGaleState() yield None elif controls.heal() or controls.joy_heal(): self.state = self.healingRainState() yield None elif controls.bolt() or controls.joy_bolt(): self.state = self.boltState() yield None elif controls.left() or controls.joy_left(): if controls.up() or controls.joy_up(): d = dir.UPLEFT elif controls.down() or controls.joy_down(): d = dir.DOWNLEFT else: d = dir.LEFT elif controls.right() or controls.joy_right(): if controls.up() or controls.joy_up(): d = dir.UPRIGHT elif controls.down() or controls.joy_down(): d = dir.DOWNRIGHT else: d = dir.RIGHT elif controls.up() or controls.joy_up(): d = dir.UP elif controls.down() or controls.joy_down(): d = dir.DOWN else: self.state = self.standState() yield None self.move(d) # handle animation and junk if d != oldDir: self.anim = 'walk' self.direction = d oldDir = d yield None
def backThrustState(self): if self.direction == dir.UPLEFT or self.direction == dir.DOWNLEFT: self.direction = dir.LEFT elif self.direction == dir.UPRIGHT or self.direction == dir.DOWNRIGHT: self.direction = dir.RIGHT class SpeedSaver(object): def __init__(_self): _self.s = self.speed def __del__(_self): self.speed = _self.s ss = SpeedSaver() self.anim = 'backthrust' self.speed += 400 self.move(dir.invert[self.direction], 1000) sound.dodge.Play() i = 8 while i > 0: i -= 1 self.speed -= 40 yield None i = 30 thrust = False gale = False while i > 0: i -= 1 self.speed = max(0, self.speed - 10) if controls.attack() or controls.joy_attack(): thrust = True elif controls.gale() or controls.joy_gale(): gale = True yield None self.direction = dir.invert[self.direction] if thrust: self.state = self.thrustState() yield None elif gale: self.state = self.crushingGaleState() yield None self.stop()
def standState(self): self.stop() self.anim = 'stand' while True: self.regenMP() if controls.attack() or controls.joy_attack(): self.state = self.slashState() elif controls.rend() or controls.joy_rend(): self.state = self.hearthRendState() yield None elif controls.gale() or controls.joy_gale(): self.state = self.crushingGaleState() yield None elif controls.heal() or controls.joy_heal(): self.state = self.healingRainState() yield None elif controls.bolt() or controls.joy_bolt(): self.state = self.boltState() yield None elif controls.left() or controls.right() or controls.up() or controls.down() or controls.joy_left() or controls.joy_right() or controls.joy_up() or controls.joy_down(): self.state = self.walkState() self._state() # get the walk state started right now. yield None
def update(self): assert len(self.layout.children), 'There should be at least one frame in here. (either indicating no saves, or to create a new save.' ika.Input.Update() if self.curY < self.oldY: self.oldY -= 2 elif self.curY > self.oldY: self.oldY += 2 else: if (controls.up() or controls.ui_up() or controls.joy_up()) and self.cursorPos > 0: self.cursorPos -= 1 self.curY = self.cursorPos * self.wndHeight elif (controls.down() or controls.ui_down() or controls.joy_down()) and self.cursorPos < len(self.layout.children) - 1: self.cursorPos += 1 self.curY = self.cursorPos * self.wndHeight elif controls.attack() or controls.ui_accept() or controls.joy_attack(): if self.saving and self.cursorPos == len(self.layout.children) -1: #create new return New else: return self.cursorPos elif controls.cancel() or controls.ui_cancel() or controls.joy_cancel(): return Cancel return None
def menu(): bg = ika.Image('gfx/title.png') cursor = ika.Image('gfx/ui/pointer.png') snow = Snow(velocity=(0, 0.5)) snow.update() result = None cursorPos = 0 FADE_TIME = 60 font = ika.Font('system.fnt') def draw(): ika.Video.Blit(bg, 0, 0, ika.Opaque) ika.Video.Blit(cursor, 68, 128 + cursorPos * 26) txt = '(c) 2003, 2021' length = font.StringWidth(txt) font.Print(ika.Video.xres - length - 10, ika.Video.yres - 10, txt) txt = 'Version 1.02' length = font.StringWidth(txt) font.Print(ika.Video.xres - length - 10, ika.Video.yres - 20, txt) if controls.useGamePad: font.Print(10, ika.Video.yres - 10, 'Gamepad controls enabled.') for i in range(FADE_TIME - 1, -1, -1): draw() ika.Video.DrawRect(0, 0, ika.Video.xres, ika.Video.yres, ika.RGB(0, 0, 0, i * 255 / FADE_TIME), True) snow.update() snow.draw() ika.Video.ShowPage() ika.Input.Update() ika.Delay(1) u = 0 # gay unpress hack while result == None: draw() snow.update() snow.draw() ika.Video.ShowPage() ika.Input.Update() ika.Delay(1) if (controls.up() or controls.ui_up() or controls.joy_up()) and cursorPos > 0: if not u: cursorPos -= 1 u = 1 elif (controls.down() or controls.ui_down() or controls.joy_down()) and cursorPos < 2: if not u: cursorPos += 1 u = 1 elif controls.attack() or controls.ui_accept() or controls.joy_attack( ): result = cursorPos else: u = 0 # one last draw. Later on, there's a blurfade that can take advantage of this: draw() snow.draw() return result for i in range(FADE_TIME): draw() ika.Video.DrawRect(0, 0, ika.Video.xres, ika.Video.yres, ika.RGB(0, 0, 0, i * 255 / FADE_TIME), True) snow.update() snow.draw() ika.Video.ShowPage() ika.Input.Update() ika.Delay(1)
def run(self): self.show() selectmode = 0 #nothing selected done = False selected = -1 ika.Input.Unpress() while not done: self.draw(selected) ika.Video.ShowPage() ika.Input.Update() if selectmode == 0: #selecting which control to map if controls.cancel() or controls.ui_cancel(): done = True result = self.control_menu.update() if isinstance(result, int): if result >= 0 and result <= self.control_menu.lastcontrol: selectmode = 1 #now poll for input! selected = result #-3 because top options controls.UnpressAllKeys() self.text = 'Press Escape to Cancel, Backspace to clear.' elif result == self.control_menu.default: controls.setConfig(controls.defaultControls) self.text = 'Default Controls Loaded' elif result == self.control_menu.save: c = controls.currentConfig try: controls.writeConfig('controls.cfg', c) self.text = 'Controls Saved' except: self.text = 'Error saving controls.cfg' elif result == self.control_menu.restore: try: c = controls.readConfig('controls.cfg') controls.setConfig(c) self.text = 'Controls Loaded' except: self.text = 'Error loading controls.cfg' elif result == self.control_menu.exit: done = True else: #polling for input now polling = True controls.UnpressAllKeys() joyconfirm = controls.joy_attack() unpress = False while polling: self.draw(selected) ika.Video.ShowPage() ika.Input.Update() for k in keyNames: #check keyboard key = ika.Input.keyboard[k] if key.Pressed(): #key pressed! if k == 'ESCAPE': #skip escape polling = False self.text = '' break elif k == 'BACKSPACE': controls.currentConfig[ controls. configcontrolsList[selected]] = 'None' controls.currentConfig[ 'joy_' + controls. configcontrolsList[selected]] = 'None' controls.setConfig(controls.currentConfig) self.text = 'Control cleared' polling = False break else: badkey = False for c in controls.configcontrolsDict.keys(): d = controls.currentConfig[c] if k == d: sound.menuBuzz.Play() self.text = 'Key ' + k + ' already assigned.' badkey = True polling = False break if not badkey: controls.currentConfig[ controls. configcontrolsList[selected]] = k controls.setConfig(controls.currentConfig) polling = False self.text = '' break if joyconfirm and not controls.joy_attack(): joyconfirm = False if unpress: #hack hack hack repress = False for joyIndex in range(len(ika.Input.joysticks)): for axisIndex in range( len(ika.Input.joysticks[joyIndex].axes)): if ika.Input.joysticks[joyIndex].axes[ axisIndex].Position() > 0.5: repress = True #if any axis is pressed, continue to "ignore" it for axisIndex in range( len(ika.Input.joysticks[joyIndex]. reverseAxes)): if ika.Input.joysticks[joyIndex].reverseAxes[ axisIndex].Position() > 0.5: repress = True unpress = repress if len( ika.Input.joysticks ) > 0 and not joyconfirm: #check gamepad only if gamepad confirm button was unpressed badkey = False for joyIndex in range(len(ika.Input.joysticks)): for axisIndex in range( len(ika.Input.joysticks[joyIndex].axes)): if ika.Input.joysticks[joyIndex].axes[ axisIndex].Position( ) > 0.5 and not unpress: unpress = True ax = 'joy%iaxis%i+' % (joyIndex, axisIndex) for c in controls.configcontrolsDict.keys( ): d = controls.currentConfig[c] if ax == d: sound.menuBuzz.Play() self.text = controls.buttonmapping[ str(axisIndex) + '+'] + ' already assigned.' badkey = True break if not badkey: controls.currentConfig[ 'joy_' + controls. configcontrolsList[selected]] = ax controls.setConfig( controls.currentConfig) polling = False self.text = '' break for axisIndex in range( len(ika.Input.joysticks[joyIndex]. reverseAxes)): if ika.Input.joysticks[joyIndex].reverseAxes[ axisIndex].Position( ) > 0.5 and not unpress: unpress = True ax = 'joy%iaxis%i-' % (joyIndex, axisIndex) for c in controls.configcontrolsDict.keys( ): d = controls.currentConfig[c] if ax == d: sound.menuBuzz.Play() self.text = controls.buttonmapping[ str(axisIndex) + '-'] + ' already assigned.' badkey = True break if not badkey: controls.currentConfig[ 'joy_' + controls. configcontrolsList[selected]] = ax controls.setConfig( controls.currentConfig) polling = False self.text = '' break for buttonIndex in range( len(ika.Input.joysticks[joyIndex].buttons) ): if ika.Input.joysticks[joyIndex].buttons[ buttonIndex].Pressed(): btn = 'joy%ibutton%i' % (joyIndex, buttonIndex) for c in controls.configcontrolsDict.keys( ): d = controls.currentConfig[c] if btn == d: sound.menuBuzz.Play() self.text = controls.buttonmapping[ str(buttonIndex )] + ' already assigned.' badkey = True break if not badkey: controls.currentConfig[ 'joy_' + controls. configcontrolsList[selected]] = btn controls.setConfig( controls.currentConfig) polling = False self.text = '' break controls.UnpressAllKeys() selectmode = 0 selected = -1 self.control_menu.unpress = True self.hide()