LGforum (2803)
Virtual Pet Vote_lcapVirtual Pet Voting_barVirtual Pet Vote_rcap 
Mr.Easybb (1586)
Virtual Pet Vote_lcapVirtual Pet Voting_barVirtual Pet Vote_rcap 
Bloodbath (745)
Virtual Pet Vote_lcapVirtual Pet Voting_barVirtual Pet Vote_rcap 
Rukiafan (533)
Virtual Pet Vote_lcapVirtual Pet Voting_barVirtual Pet Vote_rcap 
Dom (513)
Virtual Pet Vote_lcapVirtual Pet Voting_barVirtual Pet Vote_rcap 
puppycheese (446)
Virtual Pet Vote_lcapVirtual Pet Voting_barVirtual Pet Vote_rcap 
pedro (330)
Virtual Pet Vote_lcapVirtual Pet Voting_barVirtual Pet Vote_rcap 
Neymar (301)
Virtual Pet Vote_lcapVirtual Pet Voting_barVirtual Pet Vote_rcap 
Hitsu (281)
Virtual Pet Vote_lcapVirtual Pet Voting_barVirtual Pet Vote_rcap 
Flora (275)
Virtual Pet Vote_lcapVirtual Pet Voting_barVirtual Pet Vote_rcap 


Virtual Pet

evaleval
Status : No status yet...

Posts : 9
Join date : 2015-04-11
Tue 28 Jun 2016, 13:17
Hi there.

I'm currently working on a random script called Virtual Pet. Purely build with JavaScript. This script allows you to make your own virtual pet. As simple as that Razz

Building a complete script of a huge collection of pet is pretty long, so it'll make time to build. And if you wanna suggest a pet/feature, please notify me, I'll add it directly in the script. lol!

Feel free to comment.

Code:
[panda=js]// Virtual Pet script by eval
var virtualPet = true; // it means the script's successfully imported

// object literal
var pet = {
 type: "",
 name: "",
 gender: "",
 age: ""
};

// pet's type configuration
var prom = prompt("Type your pet's type!");
switch(prom.toLowerCase()) {
 case "cat":
  pet.type = "Cat"; break;
 case "dog":
  pet.type = "Dog"; break;
 case "rabbit":
  pet.type = "Rabbit"; break;
 case "turtle":
  pet.type = "Turtle"; break;
 case "snake":
  pet.type = "Snake"; break;
 case "bird":
  pet.type = "Bird"; break;
 case "iguana":
  pet.type = "Iguana"; break;
 default:
  alert("Please enter a valid pet type!");
}

// confirmation
if(pet["type"].length) {
  alert("If you're ready, hit OK.");
}else{
  alert("There was an error.");
}

// basic data
var prom2 = prompt("Enter your pet's name!");
pet.name = prom2;
var prom3 = prompt("Enter your pet's gender!");
if(prom3.toLowerCase() != "male" || prom3.toLowerCase() != "female") alert("Invalid gender.");
pet.gender = prom3;
var prom4 = prompt("Enter your pet's age!");
pet.age = prom4;


// checking data
alert("So far you've entered this data:");
alert(pet.type + ", " + pet.name + ", " + pet.gender + ", " + pet.age);
var con = confirm("Proceed with this datas?");

// some checks
if(con) {
 document.cookie = "name=" + pet.name;
 document.cookie = "gender=" + pet.gender;
 document.cookie = "type=" + pet.type;
 document.cookie = "age=" + pet.age;
 alert("We've saved all of your pet basic datas.");
 var con2 = confirm("If you're interested in outputting more data about your pet," + pet.name + " , please hit OK.");
}else{
 pet.type = "";
 pet.name = "";
 pet.gender = "";
}

// pet's detail
if(con2 && virtualPet) {
  var prom5 = prompt("Please type your pet's data. Anything, like fur color, fur length, etc.");
  pet["details"] = prom5;
  alert("Now you've created a complete virtual pet. Congrats!");
  switch(pet.gender.toLowerCase()) {
  case "male":
    alert("Please take a good care of him. Your virtual pet can also get sick or even die. But even if your " + pet.type + "'s not sick, he cannot live forever."); break;
  case "female":
    alert("Please take a good care of her. Your virtual pet can also get sick or even die. But even if your " + pet.type + "'s not sick, she cannot live forever."); break;
  }
}

After all, even the code's pretty long, it's still on progress.. maybe it's still 10% of 100%. Feel free to correct any grammar issues (lol).
gillgill
Status : Syntax highlighter's colors are nice.

Posts : 78
Join date : 2014-04-05
Wed 29 Jun 2016, 02:23
I've found something similar to build a virtual pet (although it's a tutorial) here: http://www.wired.com/2010/02/advanced_javascript_tutorial_-_lesson_4/

It's a great idea to make a new property inside the object when you're done finishing the basic data of the pets. Something like hunger, bladder, social, and health. And simply set the health value into 10 that'll drop when bladder & hunger value's dropping. Also make a property that'd contains all of the meal names & how many hunger point they'll add into the hunger property. Then, simply alert "PET NAME's not hungry yet" if the hunger point's 10 and you'd choose to feed him/her.

Also, configuring the age of death's necessary if you want it to die at a random age. Something like deathAge. And if the hunger point hits 5 & bladder hits 5, pass the true bool inside sick property. For the social one.. something like mood might help.

Oh well. I'm explaining too much things. Razz well, here's a draft.

Code:
[panda=js]pet["hunger"] = 0;
pet["bladder"] = 10;
pet["social"] = 10;
pet["health"] = 10;
pet["meal_list"] = switch(pet["type"].toLowerCase()) { // do listing the meals. };
pet["mood"] = ["Happy", "Neutral", "Sad", "Mad"];

var dA = Math.floor((Math.random() * 10) + 1);
pet["death_age"] = while (pet["age"] > dA) {  dA++; }

pet["sick"] = false;
if(pet["bladder"] == 5 || pet["hunger"] == 5) {
  pet["sick"] = true;
}
(all right, it's a crappy code.)

It's not that simple to code. But if you'd want to use a lot of prompts, confirms and alerts, well.. who knows.
evaleval
Status : No status yet...

Posts : 9
Join date : 2015-04-11
Wed 29 Jun 2016, 04:19
Your post just give me an idea! Very Happy so, when the night comes (maybe 9 PM), your pet'll be sleeping. If you'd disturb his/her sleep, he/she'll definitely get mad. At that moment I'll set up something like this.

Code:
[panda=js]var currentHour = new Date().getHours();
if(currentHour == "21") {
  pet["sleep"] = true;
  if(pet.entertain()) {
    pet["sleep"] = false;
    pet["mood"] = "Mad";
    pet["sleep"] = true;
  }
}

if(currentHour == "7" && pet["sleep"] == true) {
  pet["sleep"] = false;
  pet["mood"] = "Great";
  pet["hunger"] = 1;
}

That's pretty funny to have. Surprised

Oh and yeah, something like this'll be also funny to have.

Code:
[panda=js]if(currentHour == "12") {
 pet["activity"] = "Watching TV";
 // pet["whatever"] lol
}

Currently working something same but different method. Mhm. I think it's better like this but.. who knows. Any suggestions?

Code:
[panda=js]function Pet(pet_name, pet_type, pet_age, pet_gen, pet_desc) {
 this.pet_name = pet_name;
 this.pet_type = pet_type;
 this.pet_age = pet_age;
 this.pet_gen = pet_gen;
 this.pet_desc = pet_desc;

 this.hunger = 10; // default value
 this.bladder = 0; // default value
 this.social = 10; // default value
 this.health = 10;
 
 this.feed = feed;
 this.play = play;
 this.activity = activity;
 this.mood = mood;
 this.speak = speak;
}

Pet.prototype.feed = function() {
 this.hunger += 5;
 if(this.hunger > 10) {
  this.health -= 5;
  this.activity("puking");
  this.speak("Your pet does not feeling well.");
  this.speak("Maybe because you're giving him/her too much food...?");
 }
}

Pet.prototype.speak = function(args) {
 alert(args);
}

Pet.prototype.activity = function(action) {
 alert("Your pet is " + action + " !");
}

// welp. it's still ongoing so.. yeah
HitsuHitsu
Status : Finished on working fmAPI.

Posts : 281
Join date : 2013-09-09
Age : 22
Location : Indonesia
Sun 15 Jan 2017, 09:13
Ok so,
Version #1 or version #2? Which is better?
I don't think I can evaluate which is better correctly, because the purposes are all the same. You started on the basic informations, like the name, gender, type, age, and all infos about your pet. You do activities with your pet, and all of it remains the same. And I think, if only you build on a HTML5 mini-game, it'll be much and much more simple rather than having a buncha prompts, confirms, arrays, and any basic configurations. But ah well, nevermind.

Honestly, I do think that Version #2 is better. A lot more simpler I think, w/o annoying prompts and anything like that.


Oh and, there's a little bit error w/ the version #1.
In this bit:
Code:
[panda=js]if(prom3.toLowerCase() != "male" || prom3.toLowerCase() != "female") alert("Invalid gender.");
pet.gender = prom3;
I do type "Female" and it'd still returns "Invalid gender."
And, even if the gender of the pet was invalid, you'd still input the wrong gender inside of the object.

Also
Code:
[panda=js]var prom4 = prompt("Enter your pet's age!");
pet.age = prom4;
Put an if condition here. You don't want someone to type "999" and the program inputs it inside the data right? That, might be fatal.
Sponsored content