Library
This slide is some quick information to help you understand coding libraries, especially our game library: SimplyJS.
In the last lesson you wrote your first function!
To create a game it can take quite a bit of code.
For this reason, there are libraries. Coding libraries store lots of functions for you to use and reuse for your programs.
For those that are more advanced 💪 in JavaScript you can Download the Library Here
Edit it 📝 and use it however you'd like.
Or see 👓 the Documentation Here.
The SimplyJS library gives you additional functions that you can use.
Here are some examples (Notice the sjs at the beginning).
See below, Click on the code for an explanation.
sjs.open();
Accesses the sjs library to run the open function! (This function opens a game window).
sjs.Image();
Accesses the sjs library to run the Image function! (This function will load up an image to your page).
sjs.keyDown();
Accesses the sjs library to run the keyDown function! (This function is for tracking keys on the keyboard being pressed).
Between the parenthesis of those functions you can customize the code for your game!
For example, the sjs.open()
can be modified to open a window of the size of your choice!
Click on the code below for an explanation!
Linking to a JavaScript Library is done inside a <script>
tag, see below.
This is the same code in your pong.html file around line 6 in your text editor.
Click on the code below for an explanation!
<script src="https://simplycodingcourses.com/files/simplyjs/simply.js"></script>
A script tag can be linked to a source (another file).Comments
Sometimes skipping is the only answer.
Comment tags are lines of code, or information, that the browser doesn't loaded as part of your game or program.
When you click 'Run the Code' nothing happens
because the sjs.open line is commented out (line 6)!
Also notice the closing bracket for function run is labeled (line 7)!
Comment Practice
Read the instructions on the left to edit the code editor on the right!
If you click 'Run the Code' two alerts will pop up.
Follow the instructions on the left to create some comment tags!
Activity
This Activity will walk you through:
function start(){
sjs.open("target",500,400);
}
Only add the code in bold.
Don't add in another start function.
function start(){
sjs.open("target",500,400);
} //end start
Only add the code in bold.
IMPORTANT! If you already have your pong.html file loaded in the browser (from the last lesson) just refresh that tab by clicking on that broswer tab then do CTRL + R!
Here are THREE ways (again) on how to open pong.html in the browser to see your game! Choose One!
Checkpoint
Compare your results with the example below.
Make sure it looks the same as the code below.
You should see a gray rectangle in the middle of your page as shown above.
See the code to make this below.
<html>
<head>
<title> Pong </title>
<script src="https://simplycodingcourses.com/files/simply.js"></script>
<script>
function start(){
sjs.open("target",500,400);
} //end start
</script>
</head>
<body onload="start()">
<h1> Pong </h1>
<div id="target" style="margin:auto;background:grey;"></div>
</body>
</html>
Remember, each time you edit your code, save the HTML file and refresh it in the browser to see the changes you have made!
Once you have everything setup correctly Click the Next button to move to the next Lesson!