Skip to content

Instantly share code, notes, and snippets.

@zeimusu
Last active January 3, 2016 12:39
Show Gist options
  • Save zeimusu/8464101 to your computer and use it in GitHub Desktop.
Save zeimusu/8464101 to your computer and use it in GitHub Desktop.
Fighting Fantasy are a series of "choose your own adventure" books. This is a kivy app that implements the fighting protocol.
#:kivy 1.0.0
<Battle>:
hp_player: player_stam
hp_monster: monster_stam
sk_player: player_skill
sk_monster: monster_skill
GridLayout:
size: root.size
rows: 4
cols: 4
Label:
text: "Stamina"
Label:
text: "Skill"
Label:
text: "Monster\nStamina"
Label:
text: "Monster\nSkill"
Slider:
id: player_stam
min: 0
max: 24
value: 24
orientation: "vertical"
step: 1
Slider:
id: player_skill
min: 0
max: 12
value: 10
orientation: "vertical"
step: 1
Slider:
id: monster_stam
min: 0
max: 24
value: 10
orientation: "vertical"
step: 1
Slider:
id: monster_skill
min: 0
max: 12
value: 12
orientation: "vertical"
step: 1
Label:
text: str(int(root.hp_player.value))
Label:
text: str(int(root.sk_player.value))
Label:
text: str(int(root.hp_monster.value))
Label:
text: str(int(root.sk_monster.value))
Button:
id: battlebut
text: "Battle"
on_press: root.battle()
size_hint_y: 0.2
Label:
text: ""
Label:
text: ""
Button:
id: attackbut
text: "Attack once"
on_press: root.attack()
size_hint_y: 0.2
import kivy
kivy.require("1.0.6")
from kivy.app import App
from kivy.uix.widget import Widget
from random import randint
class Battle(Widget):
def roll(self):
while True:
player_sk = self.sk_player.value + randint(1,6)
monster_sk = self.sk_monster.value + randint(1,6)
if player_sk != monster_sk: break
return player_sk > monster_sk
def attack(self):
if self.roll():
self.hp_monster.value -= 2
if self.hp_monster.value < 0 : self.hp_monster.value=0
else:
self.hp_player.value -= 2
if self.hp_player.value < 0 : self.hp_player.value=0
def battle(self):
while self.hp_player.value > 0 and self.hp_monster.value >0:
self.attack()
class BattleApp(App):
def build(self):
return Battle()
if __name__ == "__main__":
BattleApp().run()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment