To start off, we can create 5 objects, a player, an AI, a players bullets, the ai’s bullets, and a platform for the player to move on.
Names I’ll use for each clip as an example:
Platform: “pf”
Player: “player”
Player’s bullet: “shot1”
AI: “bot”
Ai’s bullet: “shot2”
Coding the player:
To begin with, we want the player to be able to move right and left.
Add this code to the update script of the player:
if(isKeyDown("right")){
this.x+=15;
}
if(isKeyDown("left")){
this.x-=15;
}
Now, we want gravity.
I’ll make a gravity variable, attach it to player, and set it to zero in the default script like this:
player.gravity=0; // you can also use this.gravity = 0;
Another thing I’ll add to player’s default script is a health variable:
this.health=100;
Now, going back to the update script, we’ll want gravity to get stronger, so we can add this line:
player.gravity+=1; // the 1 here is the gravity force
Of course, we have gravity ready, so I’ll just let the program now that this gravity is for the player. So I’ll add this line to the update script:
player.y+=player.gravity;
Now, the player’s y will change based on the gravity force.
The player will always fall though, and we don’t want that, so I’ll add this to the update script:
if(this.hits(pf)){ // If the player hits the platform
if(isKeyDown("up")){ // If the player is clicking the jumping key
player.gravity=-15; // Make gravity force weaker
}else{ // else (if player isn't trying to jump)
player.gravity=0; // Have player's gravity set to 0
player.y=(pf.y-(pf.height/2))-(player.height/2)+1; // Snap player on top of platform
}
}
And now, we completed the following:
Player moving right/ left
Basic gravity
Platform
Now, let’s go on to the bullet…
Before we do that, let’s make sure to add 3 more line to the player’s update script:
if(isKeyJustPressed('space')){ // If "space" key is pressed
shot1.clone(); // Clone the bullet
}
Coding shot1:
First thing to do is to hide this object outside the canvas.
Add this to the default script:
this.x=-project.width; // Take object outside canvas
this.opacity=0; // Make object non- visible
Now, the next step is to have the bullet face the correct direction (this is the update script):
if(!this.isClone){ // Make sure this object is NOT a clone
if(isKeyDown('right')){ // If right key is clocked down
this.scaleX=1; // Set scaleX to positive 1
}
if(isKeyDown('left')){ // If player moving left
this.scaleX=-1; // Set scaleX to negative 1
}
}
Now we don’t want the clones of this bullet to be hidden, we want them to start in the player’s position and move towards the right direction when they’re used, so we can add this to the load script:
if(this.isClone){ // If this object IS a clone
this.x=player.x; // Set x to player x
this.y=player.y; // Set y to player y
this.opacity=1; // Make it visible
}
And to make them move, add this to the update script:
if(this.isClone){ // If this is a clone
this.x+=10*this.scaleX; // Move towards correct direction
}
Make sure to know when to remove this object, and who it effects. Add this code in the update script to let the program know to damage the ai and when to remove the shot:
if(this.isClone){
if(this.hits(bot)){
bot.health-=5;
this.remove();
}
if(this.x>project.width*2||this.x<-project.width){
this.remove();
}
}
Coding ai:
Now, we can a very similar default script for the bot as the one we had for the player:
bot.gravity=0; // you can also use this.gravity = 0;
this.health=100; // set health to 100
We can also use the same update script, but change the key events with variables:
if(bot.Right){
this.x+=15;
}
if(bot.Left){
this.x-=15;
}
bot.gravity+=1; // the 1 here is the gravity force
bot.y+=bot.gravity;
if(this.hits(pf)){ // If the bot hits the platform
if(bot.Up){ // If the bot is clicking the jumping key
bot.gravity=-15; // Make gravity force weaker
}else{ // else (if bot isn't trying to jump)
bot.gravity=0; // Have bot's gravity set to 0
bot.y=(pf.y-(pf.height/2))-(bot.height/2)+1; // Snap bot on top of platform
}
}
if(bot.Shoot){ // If "e" key is pressed
shot2.clone(); // Clone the bullet
}
Now, I’ll add this code to the bot so that it knows when to move, depending on if it’s an ai or a real player. Add these lines to the update script:
if(bot.realPlayer){
bot.Right = isKeyDown("d");
bot.Left= isKeyDown("a");
bot.Up = isKeyDown("w");
bot.Shoot = isKeyJustPressed("e")
}else{
if(player.x>this.x+250){
bot.Right=true;
}else{
bot.Right=false;
}
if(player.x<this.x-250){
bot.Left=true
}else{
bot.Left=false;
}
if(Math.round(this.num/10)*10===(this.num/10)*10){
bot.Shoot=true;
}else{
bot.Shoot=false;
}
if(Math.round(this.num/15)*15===(this.num/15)*15){
bot.Up=true;
}else{
bot.Up=false;
}
}
this.num++;
You probably noticed I added “this.num,” that’s just to make sure that the bot doesn’t shoot bullets non-stop.
Now, I’ll add this to the default script to see if the player is a bot or a real person:
bot.realPlayer = confirm("Two players?");
All that’s left is making shot 2 now.
Coding shot2:
Same default script as shot1:
this.x=-project.width; // Take object outside canvas
this.opacity=0; // Make object non- visible
Slightly different load script:
if(this.isClone){ // If this object IS a clone
this.x=bot.x; // Set x to bot x
this.y=bot.y; // Set y to bot y
this.opacity=1; // Make it visible
}
And a slightly different update script:
if(!this.isClone){ // Make sure this object is NOT a clone
if(bot.Right){ // If bot is moving right
this.scaleX=1; // Set scaleX to positive 1
}
if(bot.Left){ // If bot is moving left
this.scaleX=-1; // Set scaleX to negative 1
}
}
if(this.isClone){ // If this is a clone
this.x+=10*this.scaleX; // Move towards correct direction
}
if(this.isClone){
if(this.hits(player)){
player.health-=5;
this.remove();
}
if(this.x>project.width*2||this.x<-project.width){
this.remove();
}
}
Lastly, now all that we really might add is a text object to indicate the health of the players.
Add a text object, and name it “text,” then in the frames update script, add this code in:
text.setText("Player 1's health: "+player.health+'\nPlayer 2's health: "+bot.health);
Note:
I did this all in my head, so I never really made this project.
It’s not the best shooter game since it’s supposed to be simple to understand.
Let me know of any possible errors or if you need any help with any step, thanks!