Create a Simple Turn-Based Game

Well I’ve noticed my tutorials seem very complicated and long-winded, so here’s a simple tutorial for you beginners!

Lets create a new document at 30 FPS, and draw two characters. Turn them both into symbols and give one the instance name of “player” and the other “enemy”. Now underneath them we’ll need four buttons: attack, magic, item and run. Then give them the exact same instance names.

Firstly we’ll need to organise turns. Therefore we can only do stuff during our turn. We also want to make variables for whether we’re attacking or using magic etc. Oh, and add two text boxes - one with the Var of “player_health” and the other with “enemy_health”. Now put this in your actions!

var players_turn:Boolean = true;
//whether its the player's turn or not
var player_attack:Boolean = false;
//whether the player is attacking or not
var player_magic:Boolean = false;
//whether the player is using magic
var player_item:Boolean = false;
//whether the player is using an item
var player_run:Boolean = false;
//whether the player ran away!
var player_health:Number = 10;
//our player's health
var enemy_health:Number = 8;
//the enemy's health
function onEnterFrame() {
	if (players_turn) {
		if (player_attack) {
			//if the player is attacking
			enemy_health--;
			//damage the enemy
			player_attack = false;
			//the attack is over
			players_turn = false;
			//the turn is over
		} else if (player_magic) {
			//if the player uses magic
			enemy_health -= 2;
			//damage the enemy a little bit more
			player_magic = false;
			players_turn = false;
		}
	} else {
		//when it's the enemy's turn
		switch (random(2)) {
			//for now we're going to randomly make them attack or use magic
		case 0 :
			player_health--;
			//hurt the player
			players_turn = true;
			//it's now the player's turn
			break;
		case 1 :
			player_health -= 2;
			//hurt them even more!
			players_turn = true;
			//it's now the player's turn
			break;
		}
	}
}
attack.onRelease = function() {
	//when our attack button is clicked
	if (players_turn) {
		//if it's our turn
		player_attack = true;
		//attack!
	}
}
magic.onRelease = function() {
	if (players_turn) {
		player_magic = true;
	}
}

Well, we’re definitely fighting the enemy, but it’s not very interesting. Let’s increase the health and make the damage a bit more random! Another essential thing is some animation. Firstly add a stop ( stop(); ) action on the first frame of your player. Then make the second frame an attacking animation. Make that a movie clip and give it the instance name of “attack_mc”. Then on the third frame make a movie clip of the player doing magic and give it the instance name of “magic_mc”. Now do the same for enemy (with the same instance names). Here’s our code!

var players_turn:Boolean = true;
//whether its the player's turn or not
var player_attack:Boolean = false;
//whether the player is attacking or not
var player_magic:Boolean = false;
//whether the player is using magic
var player_item:Boolean = false;
//whether the player is using an item
var player_run:Boolean = false;
//whether the player ran away!
var player_health:Number = 100;
//our player's health
var enemy_health:Number = 80;
//the enemy's health
var enemy_attack:Boolean = false;
//whether the enemy is attacking or not
var enemy_magic:Boolean = false;
//whether the enemy is using magic
function onEnterFrame() {
	if (players_turn) {
		if (player_attack && player.attack_mc._currentframe == player.attack_mc._totalframes) {
			//if the player is attacking
			enemy_health -= 10+random(10);
			//damage the enemy
			player_attack = false;
			//the attack is over
			players_turn = false;
			//the turn is over
			player.gotoAndStop(1);
			//player look normal again
		} else if (player_magic && player.magic_mc._currentframe == player.magic_mc._totalframes) {
			//if the player uses magic
			enemy_health -= 3*random(10);
			//damage the enemy with a bit of risk
			player_magic = false;
			players_turn = false;
			player.gotoAndStop(1);
		}
	} else {
		if (!enemy_attack && !enemy_magic) {
			//when it's the enemy's turn
			switch (random(2)) {
				//for now we're going to randomly make them attack or use magic
			case 0 :
				enemy.gotoAndStop(2);
				//enemy go to attack frame
				enemy_attack = true;
				//enemy is attacking
				break;
			case 1 :
				enemy.gotoAndStop(3);
				//enemy go to magic frame
				enemy_magic = true;
				//enemy is using magic
				break;
			}
		}
		if (enemy_attack && enemy.attack_mc._currentframe == enemy.attack_mc._totalframes) {
			player_health -= 10+random(10);
			//hurt the player
			players_turn = true;
			//it's now the player's turn
			enemy_attack = false;
			//no longer attacking
			enemy.gotoAndStop(1);
			//make the enemy go to normal frame
		} else if (enemy_magic && enemy.magic_mc._currentframe == enemy.magic_mc._totalframes) {
			player_health -= 3*random(10);
			//hurt them with risk!
			players_turn = true;
			enemy_magic = false;
			//no longer using magic
			enemy.gotoAndStop(1);
		}
	}
}
attack.onRelease = function() {
	//when our attack button is clicked
	if (players_turn && !player_attack && !player_magic) {
		//if it's our turn, and we're not attacking or using magic
		player_attack = true;
		//attack!
		player.gotoAndStop(2);
		//our attacking frame
	}
};
magic.onRelease = function() {
	if (players_turn && !player_attack && !player_magic) {
		player_magic = true;
		player.gotoAndStop(3);
		//our magic frame
	}
};

(Either JavaScript is not active or you are using an old version of Adobe Flash Player. Please install the newest Flash Player.)

Looks good. Nothing special, but it’s simple and easy to build on. Maybe there’ll be a part 2…

Source Code

20 Responses to “Create a Simple Turn-Based Game”

  1. eaglevisionproductions says:

    Very Useful for beginners! :D I found your blog yesterday, and I am very impressed. :)
    PS:
    Email me…email found here.. eaglevision.890m.com/email.php
    Thanks.
    EV

  2. Gabriel Bianconi says:

    Nice tutorial… No… Amazing tutorial…

    =D Keep like this…

  3. Bloodyboy says:

    how the hell do you do that??? i sort of need ALOT of help………….. im just a beginner but i find that hard. i need more detailed instructions:(

  4. FrozenHaddock says:

    This is an excellent tutorial!

  5. MindlessGames says:

    Bloodyboy, what part are you struggling with?

  6. mrxim says:

    I’m having trouble with the text box var I’m not sure what you mean.

  7. mrxim says:

    sorry never mind i worked it out i was using action script 0.3

  8. Chris says:

    ok this is kinda messed up! i need help with the bit about putting animations in the attack. Could you maybe exsplain a bit more CLEARLY its a bit messed up for beginners lol. its all so confusing.

  9. mindlessgames says:

    Chris, basically you just animate it yourself inside the movie clip. Have a look at the source files to help you understand.

  10. ??? says:

    The point of the items and run buttons was…?

  11. MindlessGames says:

    … for future parts? :)

  12. raj says:

    it is very usefulflah learners and also experts.i sugest it is give simple language

  13. theriekardie says:

    what about the item and run?
    i’m kinda messed upt here….

  14. Quincy says:

    Heey when i saw this tutorial i tried to make my own battle.

    eventualy when i finished it it didnt work everything wrks exept for the animation of the enemy magic.

    E-Mail : quincy_1994@live.nl

    Could you send me a email and then il send my fla to you cuz i have tried in a forum but nobody knows why

  15. MindlessGames says:

    Quincy, check out the source files and compare them. Make sure all your instance names are right, too.

  16. casey says:

    hey there.. just finished the tutorial… but once i tried playing again none of the animations took effect. it was the same as the first code prettymuch… email me and help me out

  17. Flash n00b says:

    How do you give the buttons instance names?

  18. Bob says:

    How do you make an animation in ONE frame? I downloaded the source and I don’t see where stuff moves!

  19. Someone says:

    Bob, Click on the movie clip, the animation is NESTED within

  20. Max says:

    How would I end the fight when either the enemy or the player wins

Leave a Reply