Virtual Pet Feeder With Database

by jagdish sutilal in Living > Toys & Games

822 Views, 2 Favorites, 0 Comments

Virtual Pet Feeder With Database

Screenshot 2021-01-04 205831.png

Hello everyone this is my first project in which I made a virtual pet feeder game and this is made only by being inspired by my dog bruto I thought that as I had gone for my native place for some months and I had given my pet to my sister so, just as routine I used this game which had created as remembrance .For creating this game I used Javascript, and as a coding platform I used Visual Studio Code as it is free and one of the best coding platform .

Link for downloading visual studio code -Visual Studio Code

Start to Code !

Screenshot 2021-01-04 205911.png

After describing the supplies I will also instruct to write he code ,if you have noticed that I made this with database this is what makes this difficult and more exciting to play and code. First make a file called sketch.js and in that we will introduce a variable called database and use that whenever we want to make or change some value in database but the problem is that which database would you use ?, upon comparing some popular database providers on net I found Firebase database free and efficient so simply login to that and go to web app and in that complete all yes or no answers for analytics etc. then in realtime database go and create to things food stock and game state, food stock has been used to check how much food we have and the changed food stock will be changed in database and hence in all sites . The same purpose is for gameState I created 6 as for feeding , adding food etc.

Lets Add and Make a Library to Ease Coding

Screenshot 2021-01-04 211230.png
Screenshot 2021-01-04 211420.png

As we have made our account and created two variables foodS and gameState we will use some libraries such as p5.play.js which can be downloaded from google and create some own for example feeding.js for reducing the foodS when dog is fed or increasing it when food is added. Creating library makes coding easy as you don't need to again and repeat lines of code instead just write the function name will call function. Once you have added the library create some simple ways to change the values such as -

var database = firebase.database(); = for creating variable for our database .

foodStock = database.ref('foodS'); = to refer to a particular value in database.

foodStock.on('value',function (data){

var food= data.val(); = to read a value from database and give the value to a variable .

});

database.ref('/').update({'foodS:food'}); = to change the value of variable in database .

Testing Our Game !

Screenshot 2021-01-04 210121.png

The important part remaining is testing of your own virtual pet feeder game with database ! You can do this by downloading the 200 ok web server ,but one thing that must be in your game is that as the game has database whenever you open the link of 200 ok sever in two or more devices / in same device but more that one time the changes done in one window / device should be changed in database and finally in another device / window for example foodS is 25 in one device it should be also same in another device.

Web server .

Final OUTPUTS !!!!

Screenshot 2021-01-04 210333.png
Screenshot 2021-01-04 210351.png
Screenshot 2021-01-04 210410.png
Screenshot 2021-01-04 210456.png

As I have created some buttons and on pressing a button the value of gameState changes and the background also changes for eg if I press ' I want to play ' it will be changed in all windows. If you are not familiar to coding but you liked the project here is the link for code and game -

code -Code for game

game-Game online