Introduction
I
really wanted to write a tutorial about a game technology I like to
use, so here it is. In this story, we will start making a little
shoot’em up game with PixiJS, a really simple and cool Javascript
library.
What
we are going to do exactly is to make a spaceship able to move and
shoot, enemy waves coming through and a beautiful animated background
with moving clouds. The first part (this story) will focus on the
background.
Ready guys? Let’s nail it!
Getting started
Let’s
start by setting up our project: I uploaded a code structure already
set so we are all working with the same base. However if you want to
make it yourself, I put a picture of my folder just below:
We will need a local server to run the game: I invite you to download WAMP if you’re working with Windows, or MAMP for macOS, they are free and easy to use. Let’s put your game folder in the server one (htdocs for MAMP / www for WAMP) and type the localhost link in your favorite browser (for me: http://localhost:8888/Spaceship/bin/)
In index.html, we are importing the javascript files in the header:
<script src="../src/lib/pixi.min.js"></script>
<script src="“../src/main.js”"></script>
… and we need to do the same for every file we make. Then comes the initialization of Pixi (which is based on the WebGL render engine):
var renderer = PIXI.autoDetectRenderer(window.innerWidth, window.innerHeight);
For
this tutorial, we tell the game to cover the whole browser window, so
if you try it now you will get an all-black background.
The main.js
file is the place where all the game starts. It’s like a manager, with
the first-executed function of the game, and the loop where we can tell
the game what it needs to do on each frame. What we want is a blue
background for the sky when the game starts, so let’s update the init function:
function init()
{
renderer.backgroundColor = 0x22A7F0;
renderer.render(stage);
loop();
}
|
Pixi is using the hex color format, so you need to write your color code preceded by 0x. Let’s save and see the result in your browser!
Clouds everywhere
This background is very boring, let’s add some floating clouds.
First, let’s add a new CloudManager class file in the src folder (which is going to create and move the clouds):
class CloudManager
{
constructor()
{
}
update()
{
}
}
Don’t forget to add it in the index.html file as we did for main.js:
<script src="../src/lib/pixi.min.js"></script>
<script src="../src/CloudManager.js"></script>
<script src="../src/main.js"></script>
The
constructor is the entry point of this class where we can add the spawn
function for our clouds. What we want is basically a method able to
create a cloud every X seconds, and it’s fine because there is a
javascript thing for this:
window.setInterval(function()
{
}
, 1000);
This piece of code, placed in the constructor, will call what’s inside the moustache brackets every 1000 milliseconds (= 1 second).
Let’s add cloud sprites in the assets folder, and because it’s better we have 2 different images: (the clouds are white with transparent background so they were invisible on this page, but here are the links on GitHub ;)
https://github.com/Karzam/Spaceship_Tutorial_Part_1/blob/master/bin/assets/cloud_1.png
https://github.com/Karzam/Spaceship_Tutorial_Part_1/blob/master/bin/assets/cloud_2.png
We need to load the sprites before the game starts, so add them in the Pixi.loader.add function:
PIXI.loader.add([
"assets/cloud_1.png",
"assets/cloud_2.png"
]).load(init);
Ok, now we can display the clouds in the setInterval method of the CloudManager:
window.setInterval(function()
{
const sprite = (Math.random() > 0.5 ? "cloud_1" : "cloud_2");
this.cloud = new PIXI.Sprite(PIXI.loader.resources["assets/" + sprite + ".png"].texture);
this.cloud.anchor.set(0.5, 0.5);
this.cloud.position.set(renderer.width * 1.2, renderer.height * Math.random());
stage.addChild(this.cloud);
}
, 1000);
To resume this code:
- First, we are computing a random number between 0 and 1, and either it’s less than 0.5 so we store the first sprite in a constant or otherwise it’s the second one.
- Then, we create a new sprite object with the image we got in the previous line.
- The origin point of this sprite is going to be its top left corner, so we set its anchor point in the middle.
- We have to display the cloud beyond the right border of the screen, so it can move to the left through the screen: renderer.width * 1.2 is the left border position + the width of the screen + 20% of its width. We can be sure that we won’t see it spawning. For the y position, renderer.height * Math.random() is a number between 0 and the window height. So the cloud vertical position will be located between the top and the bottom of the screen.
- Finally, we addChild this cloud to the stage.
If
you run this code, nothing should appear, and it’s on purpose because
they have to pop out of sight. So now we have to make them move.
The
update function is the place to do it. But we need to store the clouds
in an array so we can iterate through and set their positions. Let’s initialize a new array in CloudManager constructor:
this.cloudsList = [];
… and push the clouds inside after the stage.addChild function:
this.cloudsList.push(this.cloud);
Now we can iterate the array in update and set the position of each cloud:
this.cloudsList.forEach(function(element) {
element.position.x -= 4;
});
Now it’s working!

Oh wait, something should actually annoy us: where are all those clouds going?
Yeah, if we don’t remove them after they left the screen, they will continue to exist and it may cause some performance troubles. Let’s add a statement in forEach
that delete them when their horizontal position is a little bit
inferior to the left border of the screen (so we can’t see them popping
out):
We’re done with the clouds!
What about making a random variation on the clouds size? Add this to the cloud creation block:
let minScale = 0.2;
let maxScale = 1.2;
let scale = Math.random() * (maxScale - minScale) + minScale;
this.cloud.scale.set(scale, scale);

If you something’s missing / not working in your code, you can check the result here.
Thank you for reading!
If you want to build a website just like Trivago and Tripadvisor then try our Hotel price comparison API, which compares more than 200 websites to generate JSON results .
Thanks for sharing with us your wisdom.This will absolutely going to help me in my projects .
ReplyDeleteCloud Computing Classes in Chennai
Cloud Computing Institutes in Chennai
Great Article. Thank you for sharing! Really an awesome post for every one.
DeleteIEEE Final Year projects Project Centers in Chennai are consistently sought after. Final Year Students Projects take a shot at them to improve their aptitudes, while specialists like the enjoyment in interfering with innovation. For experts, it's an alternate ball game through and through. Smaller than expected IEEE Final Year project centers ground for all fragments of CSE & IT engineers hoping to assemble. Final Year Project Domains for IT It gives you tips and rules that is progressively critical to consider while choosing any final year project point.
JavaScript Training in Chennai
JavaScript Training in Chennai
This is an awesome post. Really very informative and creative contents. This concept is a good way to enhance knowledge. I like it and help me to development very well. Thank you for this brief explanation and very nice information. Well, got good knowledge.
ReplyDeleteWordPress development company in Chennai
in this content i got a lot information about that javascript thanks for a helpful information and its assist for me to study..once again thanks for useful information
ReplyDeleteJavascript Classes in Bangalore
Advanced Javascript Training in Bangalore
Best Institute for Javascript in Bangalore
Javascript Course in Bangalore
Javascript Training Courses in Bangalore
in this content i got a lot information about that javascript thanks for a helpful information and its assist for me to study..once again thanks for useful information
ReplyDeleteJavascript Classes in Bangalore
Advanced Javascript Training in Bangalore
Best Institute for Javascript in Bangalore
Javascript Course in Bangalore
Javascript Training Courses in Bangalore
The blog is good.Hope you continue to share more of your ideas.I will definitely love to read.
ReplyDeleteLoadRunner Training in Chennai
hp loadrunner training
Loadrunner Training in Anna Nagar
QTP Training in Chennai
Best QTP Training Institutes in Chennai
clinical sas training in chennai
SAS Course in Chennai
LoadRunner Training in Chennai
Thanks for your post which gather more knowledge about the topic. I read your blog everything is helpful and effective.
ReplyDeleteCloud computing courses in chennai
Cloud computing training in chennai
Cloud computing training in anna nagar
Cloud computing training in adyar
Cloud computing course in tambaram
Data science training in chennai
Data science course in chennai
Data science training in anna nagar
Thanks for posting this article it has really a good content.
ReplyDeleteSpoken English Classes in Chennai
Spoken English in Chennai
German Classes in Chennai
TOEFL Coaching in Chennai
IELTS Coaching in Chennai
spanish language in chennai
Spoken English Classes in Velachery
Spoken English Classes in Tambaram
It is surely a very unique post and the admin was putting the large of efforts. I appreciate for this sharing and keep blogging...
ReplyDeleteTableau Training in Chennai
Tableau Course in Chennai
Excel Training in Chennai
Oracle Training in Chennai
Oracle DBA Training in Chennai
Power BI Training in Chennai
Embedded System Course Chennai
Linux Training in Chennai
Tableau Training in Chennai
Tableau Course in Chennai
Thanks for sharing this valuable information to our vision. You have posted a worthy blog keep sharing.
ReplyDeleteTally Course in Chennai
Tally Classes in Chennai
ui design course in chennai
CCNA Training in Chennai
ReactJS Training in Chennai
microsoft dynamics crm training in chennai
Tally Training in Chennai
This information is quite useful, I truly enjoyed. Thanks for this blog.
ReplyDeleteAppium Training in Chennai
Appium Training in Porur
Appium Training in Adyar
Appium Training in Velachery
Appium Training in Tambaram
core java training in chennai
C C++ Training in Chennai
javascript training in chennai
Very nice post with lots of information. Thanks for sharing this updates.
ReplyDeleteCloud Computing Training in Chennai
Cloud Training in Chennai
Cloud Certification in Chennai
AWS Training in Chennai
Amazon Web Services Training in Chennai
Machine Learning Training in Chennai
Machine Learning course in Chennai
Azure Training in Chennai
AWS course in Chennai
This comment has been removed by the author.
ReplyDeleteVery useful information, Keep posting more blog likes this, Thank you.
ReplyDeleteAviation Academy in Chennai
Air hostess training in Chennai
Airport management courses in Chennai
Ground staff training in Chennai
Aviation Courses in Chennai
cabin crew training institute in Chennai
airlines training Chennai
Ground staff training in Chennai
Good blog!!! It is more impressive... thanks for sharing with us...
ReplyDeleteSelenium Training in Chennai
Selenium Training
selenium classes in chennai
Selenium Course in Chennai
Selenium Training in Annanagar
Selenium training in vadapalani
Digital Marketing Course in Chennai
Python Training in Chennai
Big data training in chennai
JAVA Training in Chennai
Thanks for giving excellent Message.Waiting for next article
ReplyDeleteQTP Training in Chennai
QTP classes
QTP Training
QTP Training in Velachery
QTP Training in Tambaram
LoadRunner Training in Chennai
Html5 Training in Chennai
clinical sas training in chennai
Spring Training in Chennai
Photoshop Classes in Chennai
Thank you for excellent article.You made an article that is interesting.
ReplyDeleteBest AWS certification training courses. Build your AWS cloud skills with expert instructor- led classes. Live projects, Hands-on training,24/7 support.
https://onlineidealab.com/aws-certification/
Great Article
ReplyDeleteData Mining Projects
Python Training in Chennai
Project Centers in Chennai
Python Training in Chennai
I have been reading for the past two days about your blogs and topics, still on fetching! Wondering about your words on each line was massively effective. Techno-based information has been fetched in each of your topics. Sure it will enhance and fill the queries of the public needs. Feeling so glad about your article. Thanks…!
ReplyDeletebest software testing training in chennai
best software testing training institute in chennai with placement
software testing training
courses
software testing training and placement
software testing training online
software testing class
software testing classes in chennai
best software testing courses in chennai
automation testing courses in chennai
I like the helpful info you provide in your articles. I’ll bookmark your weblog and check again here regularly. I am quite sure I will learn much new stuff right here! Good luck for the next!
ReplyDeleteWeb Designing Training Institute in Chennai | web design training class in chennai | web designing course in chennai with placement | Web Designing and Development Course in Chennai | Web Designer Training Course in Chennai
Mobile Application Development Courses in chennai
Data Science Training in Chennai | Data Science courses in Chennai
Professional packers and movers in chennai | PDY Packers | Household Goods Shifting
Web Designing Training Institute in Chennai | Web Designing courses in Chennai
Google ads services | Google Ads Management agency
Web Designing Course in Chennai | Web Designing Training in Chennai
nice :)...
ReplyDeleteEthical hacking training in chennai
Internship for automobile engineering students
cse internship in chennai
Kaashiv infotech pune
Industrial training for diploma eee students in hyderabad
Internships in chennai
Inplant training in chennai for mechanical engineering students
Data science training in chennai
Internship for aeronautical engineering students in chennai
Python internship in chennai
Your new valuable key points imply much a person like me and extremely more to my office workers. With thanks; from every one of us.
ReplyDeleteAngularjs Training in Chennai
Java Training in Chennai
Bigdata Hadoop Training in Chennai
SAS Training in Chennai
Python Training in Chennai
Software Testing Training in Chennai
excellent...!!!
ReplyDeleteSelenium training in chennai
Industrial visit in chennai
Internship
Internships in bangalore for cse students 2019
Free internship in chennai for cse students
Network security projects for cse
Ccna course in chennai
Inplant training in chennai for cse
Inplant training for eee students
Kaashiv infotech chennai
Thanks for taking time to share this useful blog...
ReplyDeletespoken english classes in bangalore
Spoken English Classes in Chennai
spoken english class in coimbatore
spoken english class in madurai
Spoken English Classes in BTM
Spoken English Classes in Marathahalli
Spoken English Classes near Marathahalli
Spoken English Marathahalli
DevOps Training in Bangalore
DOT NET Training in Bangalore
I really enjoyed this article. I need more information to learn so kindly update it.
ReplyDeleteSalesforce Training in Chennai
salesforce training in bangalore
Salesforce Course in bangalore
best salesforce training in bangalore
salesforce institute in bangalore
salesforce developer training in bangalore
Big Data Course in Coimbatore
Python Training in Bangalore
salesforce training in marathahalli
salesforce institutes in marathahalli
Nice post.it is really interesting to read this article...Find latest Sarkari Job for freshers & experienced graduates across India & get free Naukri/Job alerts on recent & upcoming Sarkari jobs vacancies through Sarkari Job, as a jobs source we provides present and latest recruitment details for all Government/Sarkari Naukri across India.
ReplyDeleteThis article is a creative one and the concept is good to enhance our knowledge. Waiting for more updates
ReplyDeleteHadoop Training In OMR
CCNA course In T Nagar
IELTS Coaching In OMR
Tally Course in Anna Nagar
Dot Net training in Velachery
Python Training in Tambaram
Android Training in Anna Nagar
DevOps Course in Tambaram
German Classes in Chennai
RPA Training in T Nagar
I would like to thank you for the efforts you have made in writing this article.
ReplyDeleteSelenium Training in chennai | Selenium Training in anna nagar | Selenium Training in omr | Selenium Training in porur | Selenium Training in tambaram | Selenium Training in velachery
more about the blog is good but if you was seen any other blog the content should be failed to fulfill the users requests on that field,but in this blog the content will fulfill the users who related to search this field.... thanks a for ur efforts
ReplyDeleteAi & Artificial Intelligence Course in Chennai
PHP Training in Chennai
Ethical Hacking Course in Chennai Blue Prism Training in Chennai
UiPath Training in Chennai
ReplyDeleteA Great read for me, Well written technically on recent technology
Are you looking for Ethical hacking related job with unexpected Pay, then visit below link
Ethical Hacking Course in Chennai
Ethical Hacking Online Course
Ethical Hacking Course
Hacking Course
Hacking Course in Chennai
Ethical Hacking Training in Chennai
hacking course online
learn ethical hacking online
hacking classes online
best ethical hacking course online
best hacking course online
ethical hacking online training
certified ethical hacker course online
Informative content,thanks for sharing...waiting for next update...
ReplyDeleteExcel Training in Chennai
Advanced Excel Training in Chennai
Embedded System Course Chennai
Excel classes in Chennai
Corporate Training Companies in Chennai
Excel classes in Chennai
embedded course in coimbatore
Excel course in Chennai
Advanced Excel Course in Chennai
Really an informative blog...Thanks for sharing an informative article with us.
ReplyDeletegoogle go training in Chennai
google go course in Chennai
google go training
google go course
mvc course in chennai
jbpm Training in Chennai
Thanks for giving excellent Message.Waiting for next article
ReplyDeletegoogle analytics certification online
google analytics online training
google analytics certification online
content writing course online
google analytics online training
I was following your blog regularly and this one is very interesting and knowledge attaining. Great effort ahead. you can also reach us for
ReplyDeleteweb development company in chennai
website design company in chennai
Website builder in chennai
Web designing in Chennai
Web Development in chennai
Web design and Development Company in Chennai
Great post, thanks for sharing wonderful information about java script.
ReplyDeleteJava Online Training
Java Online Training In Chennai
Core Java Online Training
I liked this blog.. I got some clear information from this blog.. Thanks for taking a time to share this blog...
ReplyDeleteEthical Hacking Course in Porur
Ethical Hacking Course in OMR
Ethical Hacking Course in Tambaram
Ethical Hacking Course in Anna Nagar
Ethical Hacking Course in T Nagar
Really, it’s a useful blog. Thanks for sharing this information.
ReplyDeleteIonic Online Course
Kotlin Online Course
social media marketing Online Training
React Native Online Training
R programming Training in Chennai
Xamarin Course in Chennai
Ionic Course in Chennai
Nice blog, Thanks for sharing this useful information.
ReplyDeleteMobile Application Testing Online Training
Mobile App Development Online Course
Mobile App Development Courses in Chennai
Google Analytics Online Course
Google Analytics Training in Chennai
Content Writing Course in Chennai
Online Content Writing Course
Thanks for sharing this, I actually appreciate you taking the time to share with everybody.
ReplyDeleteData Science Course In Hyderabad With Placements
I just loved your article on the beginners guide to starting a blog.If somebody take this blog article seriously
ReplyDeletein their life, he/she can earn his living by doing blogging.Thank you for this article.
java online training
Great Information shared related to massage, Visit here or Click here for more information.
ReplyDeletecurrent version of php
digital marketing tricks
big data examples in real life
community cloud salesforce
android developer interview questions
Cloudi5 is the Web Design Company in Coimbatore. Cloudi5 also offers ecommerce website development
ReplyDelete