This forum is now read-only. Please use our new forums! Go to forums
Pokemon Javascript
Hi guys I’ve worked through the javascript lessons and have built a pokemon script
Currently you cannot choose your pokemon but the attacking pokemon is randomly picked from an array as well as the attack and level being random
Would love to hear what you guys think and I’ll be adding new features such as experience locations and pokemon specific moves as well as the ability to pick your pokemon
Copy and paste the code into your scratch pad
//////////////////////////////////////////////
//////////////////////////////////////////////
//CALL WILD POKEMON
function wildPokemon (type) {
this.type = type;
}
//POKEMON ARRAY
var pokemonStats = new Array();
pokemonStats[0] = new wildPokemon("Zubat");
pokemonStats[1] = new wildPokemon("Oddish");
pokemonStats[2] = new wildPokemon("Geodude");
pokemonStats[3] = new wildPokemon("Slowpoke");
//POKEMON ATTACK ARRAY
function move (move, basedmg) {
this.move = move;
this.basedmg = basedmg;
}
var moves = new Array();
moves[0] = new move("Slam", 5);
moves[1] = new move("Headbutt", 4);
moves[2] = new move("Tackle", 3);
moves[3] = new move("Cut", 4);
///////////////////////////////////////////////////
//////////////////////////////////////////////////
//CALL WILD POKEMON STATS
//CALL WILD POKEMON ID
var wildPokemonid;
callWildPokemonid = function(){
wildPokemonid = Math.floor(Math.random() * pokemonStats.length);
};
//CALL WILD POKEMON LEVEL
var wildPokemonLevel;
callWildPokemonLevel = function(){
wildPokemonLevel = Math.floor(Math.random() * 6 + 1);
};
//CALL WILD POKEMON HEALTH
var wildPokemonHealth;
callWildPokemonHealth = function(){
wildPokemonHealth = Math.floor(Math.random() + wildPokemonLevel + 3);
};
//CALL WILD POKEMON ATTACK
var moveid;
callMoveid = function(){
moveid = Math.floor(Math.random() * moves.length);
};
//CALL DAMAGE
var damage;
//CALL WILD POKEMON ATTACK DAMAGE
callMoveDamage = function(){
damage = Math.floor(Math.random() * moves[callMoveid].basedmg + 3);
};
//CALL PLAYER ATTACK DAMAGE
callPlayerMoveDamage = function() {
damage = Math.floor(Math.random() * moves[playerMove].basedmg + 3);
};
//CALL PLAYER ATTACK
function selectMove(){
playerMove = prompt("You have 4 moves, " + moves[0].move + ", " + moves[1].move + ", " + moves[2].move + " and " + moves[3].move + ", which move do you want to use?").toUpperCase();
if (playerMove === ""){
selectMove();
}
else
{
switch(playerMove){
case 'SLAM':
playerMove = 0;
callPlayerMoveDamage();
playerAttack();
break;
case 'HEADBUTT':
playerMove = 1;
callPlayerMoveDamage();
playerAttack();
break;
case 'TACKLE':
playerMove = 2;
callPlayerMoveDamage();
playerAttack();
break;
case 'CUT':
playerMove = 3;
callPlayerMoveDamage();
playerAttack();
break;
default :
alert('Move not found');
selectMove();
break;
}
}
}
///////////////////////////////////////////////////
//////////////////////////////////////////////////
//WILD POKEMON ATTACK
function wildPokemonAttack(){
if (pokemonHealth > 0){
pokemonHealth = pokemonHealth - damage;
alert("Wild " + pokemonStats[wildPokemonid].type + " uses " + moves[moveid].move + " dealing " + damage + " damage!");
attackLoop();}
else
{
alert("Pokemon fainted");
}
}
//PLAYER ATTACK
function playerAttack(){
alert ("Player uses " + moves[playerMove].move + " dealing " + damage + " damage!");
if (wildPokemonHealth > 0)
{
wildPokemonHealth = wildPokemonHealth - damage;
alert(pokemonStats[wildPokemonid].type + " has " + wildPokemonHealth + " health remaining!");
playerTurn = false;
wildPokemonFaint()
}
else
{
alert(pokemonStats[wildPokemonid].type + " fainted");
}
}
//RANDOMIZE WILD POKEMON
function randomPokemon(){
callWildPokemonid();
callWildPokemonHealth();
callWildPokemonLevel();
callMoveid();
}
var pokemonHealth = 50;
//START FIGHT
function startFight(){
randomPokemon();
alert("Wild " + pokemonStats[wildPokemonid].type + " appears!");
alert("Level : " + wildPokemonLevel + " Health : " + wildPokemonHealth);
playerTurn = false;
attackLoop();
}
function attackLoop(){
if (playerTurn === false)
{
playerTurn = true;
wildPokemonAttack();
}
else
{
playerTurn = false;
selectMove();
}
}
function wildPokemonFaint(){
if (wildPokemonHealth < 1){
alert(pokemonStats[wildPokemonid].type + " has fainted");}
else
{ attackLoop();}
}
startFight();
Answer 50c9d54e1f9dd24ae4002527
works like a dream, i just tried it out. Would be cool if you had it so that you could put your own name, but its not absolutely nescesary. great job! :D
Answer 531b84068c1ccc0ecd002841
I have started learning Java script on Code Academy myself and have so far got around 28%. This game encourages me to carry on the course because I’d love to be coding games like that once I have finished. So, great job! Hope you carry on making games like this.
Popular free courses
- Free Course
Learn SQL
In this SQL course, you'll learn how to manage large datasets and analyze real data using the standard data management language.Beginner friendly,4 LessonsLanguage Fluency - Free Course
Learn JavaScript
Learn how to use JavaScript — a powerful and flexible programming language for adding website interactivity.Beginner friendly,11 LessonsLanguage Fluency - Free Course
Learn HTML
Start at the beginning by learning HTML basics — an important foundation for building and editing web pages.Beginner friendly,6 LessonsLanguage Fluency