TwineFray: A Battle System for Twine

finn jake ice king pokemonIn this post, I’m going to cover one way to create a battle with an enemy in Twine. Let’s start with the variables. The only variable that is really needed is $health, but to make things more interesting, we will also create a few different ways to attack the enemy and a way to buy more health. We should set up these values at the beginning of the game. Players will start the game with the ability to punch or kick but will not have any other weapons.

Valhalla V: Odin's Revenge
by Mr. Riley

[[CLICK HERE TO START->Town]]
(set: $health to 100)
(set: $sword to 0)
(set: $ax to 0)
(set: $coins to 5)
(set: $points to 0)

When you click on CLICK HERE TO START it takes you to the Town. In the town, we will give the player the option to go to several places.

You are standing in the middle of town.
Where do you want to go?
[[Market]]
[[Castle]]
[[Graveyard]]
[[Farm]]

In the Market, you can buy a healing potion or sword, if you have enough money. Using the example code below, you should be able to figure out how to add other items for sale (an ax or a key, for example).

(if: $coins >= 10)[
	[[Buy A Healing Potion For 10 Coins]] ]
(else:)[
	Healing potions cost 10, but you only have $coins. ]
(if: $coins >= 50)[
	[[Buy A Sword For 50 Coins]] ]
(else:)[
	Swords cost 50, but you only have $coins. ]
Return to [[Town]]

The Buy A Healing Potion For 10 Coins and Buy A Sword For 50 Coins passages will almost be the same. In these passages, we are adding $health (or $sword) and subtracting $coins.

(set: $health to $health + 10)(set: $coins to $coins - 10)
(color:yellow)[+10 health!]
You now have $health health!
(color:red)[-10 coins.]
You now have $coins coins.

(display:"Market")

(set: $sword to $sword + 1)(set: $coins to $coins - 50)
(color:green)[+1 sword!]
You now have a Level $sword sword!
(color:red)[-50 coins.]
You now have $coins coins.

(display:"Market")

castleWhen you go to the Castle, you might be attacked by an enemy or the castle might just look empty. We set $location to “Castle” so that we can return to the Castle after the battle. There is a 50% chance that the player will get attacked.

You should be able to figure out how to change it so that there is only a 33% chance or a 25% chance that the player will get attacked.

(if: (random:1,2) is 1)[
	(set:$location to "Castle")
	(display: "Fight Enemy")
]
(else:)[
	The castle looks empty... //for now.// Check later.
	Go back to [[Town]]
]

epic finn jakeIf you get attacked by an enemy, then it jumps to a new passage called Fight Enemy. You have to create a new passage called Fight Enemy by clicking on the green +Passage button in the bottom right corner. In this passage, we are setting up two things: the health of this enemy and the strength of a punch and a kick against this enemy. We’re setting this enemy’s health based on a random number between 30-50 and the amount of points the player has earned. Using the amount of points the player has earned will mean that each new enemy will be stronger than the last one. It would be boring if punches were always better than kicks, so let’s randomly make either the punch or kick do more damage for this enemy.

(set: $enemyName to (either:"Giant Spider","Zombie","Ghost","Pizza Rat"))
A $enemyName crawls out of the shadows!
(set: $enemyHealth to (random:20,40) + ($points * .1))
(set: $punch to (random:1,9))
(set: $kick to 10 - $punch)
(display: "Enemy Hits You")

punch comboNext, you have to create a new passage called Enemy Hits You by clicking on the green +Passage button in the bottom right corner.

In the Enemy Hits You passage, we start by checking to see if the enemy is alive. If the enemy is alive, it attacks you, then you either Use Weapon or You Died. If the enemy is not alive, it displays that you have defeated the enemy and lets you Continue.

(if: $enemyHealth > 0)[
	(set: $enemyHit to (random:1,3)+(random:1,3))
	(color:red)[The $enemyName ATTACKS YOU! //-$enemyHit//]
	(set: $health to $health - $enemyHit)
	(set: $points to $points + $enemyHit)
	You have (color:yellow)[$health health].
	(if: $health > 0)[
		(display: "Use Weapon")
	]
	(else:)[
		(display: "You Died")
	]
]
(else:)[
	You have defeated the $enemyName. //+10 pts!//
	(set: $points to $points + 10)
	(set: $randomCoins to (random:25,50))
	(set: $coins to $coins + $randomCoins)
	//+$randomCoins coins!// You now have $coins coins.
	You have (color:yellow)[$health health].
	[[Continue]]
]

The Continue link only shows up when you have killed the enemy. In this passage, we are only returning back to the location in which we were attacked.

(goto:$location)

Next, you have to create a new passage called Use Weapon by clicking on the green +Passage button in the bottom right corner. In this passage, the player can always choose Punch or Kick, but can only choose Use Sword if they have a sword.

[[Punch]]
[[Kick]]
(if: $sword > 0)[	[[Use Sword]] ]
(if: $ax > 0)[	[[Use Ax]] ]

The Punch and Kick passages will almost be the same. In these passages, we set $hit to the strength of $punch (or $kick) plus a random number 1-6. We also set the probability of a missed punch (or kick), which is a 1-in-8 chance in this example.

(set: $hit to $punch + (random:1,6))
(if: (random:1,8) is 1)[
	You tried to PUNCH it but you missed!
]
(else:)[
	(set: $enemyHealth to $enemyHealth - $hit)
	(set: $points to $points + $hit)
	You PUNCH the $enemyName! (color:green)[-$hit]
]
(display: "Enemy Hits You")

The Use Sword link only shows up if you have a sword. In this passage, we set the strength of the $hit and the probability of a miss, which is 1-in-10 in this example.

(set: $hit to 10 + (random:1,6) + (random:1,6))
(if: (random:1,10) is 1)[
	You tried to SLASH it with your sword but you missed!
]
(else:)[
	(set: $enemyHealth to $enemyHealth - $hit)
	(set: $points to $points + $hit)
	You SLASH the $enemyName with your sword! (color:green)[-$hit]
]
(display: "Enemy Hits You")

The last passage that we will make in this example is You Died.

(color:red)[☠ You died! ☠
☠ GAME OVER ☠
(set: $points to $points + $coins)☠ //Final Score: $points// ☠]

CLICK HERE to play this example game.

Screenshot 2016-03-29 15.13.00

CONTINUE TO THE NEXT POST: How to Use Google Drive to Store High Scores for Twine Games

Comments are closed.