Big Green Chair

Where Love and Learning Meet

  • Email
  • Facebook
  • Pinterest
  • Twitter
  • What Is Big Green Chair?
  • CONSTRUCT!
  • Homeschool Voices
Home About Technology Learn to Program Artillery! IV
Learn to Program Artillery! IV

David March 21, 2014

Learn to Program Artillery! IV

A tutorial to learn to program a simple graphic version of the game Artillery.

For this installment of Artillery, we will add a cannon and a target and randomize the locations in which they are rendered.  We will also randomize the mountain and give it a slightly smoother look.

Let’s start by adding the cannon and target images to the page.  We will add them right under the impact crater image we added last time.  The code should look like this:

Artillery4-1

Next we will add the cannon and target to the Environment function and give them a set position on the scene.  To do this, I created two functions AddCannon(xLocation) and AddTarget(xLocation).

Artillery4-2

These functions let us specify exactly where on the x axis we want to place our cannon and target.  Remember the origin (0,0) for everything is the upper left of the object.  So we subtract 15 from groundY for the cannon so that the top of it is a little above ground.  We could also raise the target as well, but I think it looks better on the green background.  You should also notice that we rendered the images as different sizes which you can see in the parameter ‘30,30); and ‘30,15);’. Next we add the following lines to the bottom of the Environment function right before ‘context.stroke();’.

Artillery4-3

Now we should have something that looks like this:

Artillery4-4

Play around with this a little and see if you can hit the target before we move on to the next steps of randomizing locations and mountain heights.

Done?  Ok, let’s randomize.

Random numbers are simple in javascript.  You just call ‘Math.random()’ which will return a decimal between 0 and 1.  We will leverage this by creating a function named ‘randomRange’ and pass it a value of ‘maxRange’.

Artillery4-5

Now that we have this we can update the calls to ‘AddCannon’ and ‘AddTarget’.  We will also need to add a variable for the cannon location called ‘cannonLocation’.  We will use this when we call fire so that we know where to start drawing.  The new code at the end of environment should look like this:

Artillery4-6

We use 170 as the range because the image is 30 pixels wide and we don’t want them to go off the screen or be buried in the mountain.  We added 600 to ‘AddTarget’ because the mountains is 400 pixels wide and starts 200 pixels from the left side of the screen.

We also need to update how we initialize xn in the fire function.  After a little experimentation I decided that ‘cannonLocation + 19’ gave a pretty good result as a starting point for the projectile.

Artillery4-7

Lastly we will tackle the mountain.  First, we will increase the number of rectangles that make up the mountain.  This is done by setting ‘mountainSectionWidth’ to 4.
Artillery4-8Second we want to make two new variables inside the Environment function called ‘lastHeight’ and ‘newHeight’ so that we can record where we left off on drawing the mountain.

Artillery4-9

Now that we have our reference for the last point on the mountain we need to update the ‘for loops’ that lay it out.

Artillery4-10

You will notice I also added a variable for ‘rangemax’ to make sure the mountain is different every time we draw it.  The other changes to note are where we set ‘newHeight’.  Once these changes are made in the environment function we can run it again and we should have something that looks like this:

Artillery4-11

Or this:

Artillery4-12

Or even this;

Artillery4-13

Thanks for taking the time to read this tutorial.  If you have any questions please leave a comment below.  As always you can view the complete source code here or view the page here.   I’d also like to hear if you have any suggestions for improvement.

In the next installment we will add a second player and keep track of how many times you successfully hit your opponent.

If you found this post interesting and are following along with our tutorial take a moment to click the ‘Like Us’ button and sign up for our newsletter in the sidebar. 

Filed Under: About Technology, How to be a...

Sign up for our FREE newsletter!

Big Green Chair's CONSTRUCT! Newsletter Logo

CONSTRUCT!

For Kids Who Like To Build CONSTRUCT! is a free eNewsletter for families interested in building … Keep Reading

Find It

CONSTRUCT!

CONSTRUCT! is an eNewsletter for families interested in building techniques, ideas and projects. Issues are random surprises in your email inbox! Each issue includes:

...featured indepth article
...reviews of books, websites, or projects
...project challenges and contests

We are working on ways for you to share your projects with our community. Fill out the form below to sign up!

Upcoming Issue: Animal Architects

Post Categories

  • About Technology (21)
  • Big Green Chair (4)
  • Challenges (1)
  • Construct (5)
  • Handcrafts (3)
  • Homeschool Libraries (5)
  • Homeschooling Voices (6)
  • How to be a… (17)
  • Inside Voices (4)
  • Local (3)
  • Math (6)
  • MeetUps (1)
  • Philosophies (2)
  • Reviews (5)
  • Science (2)
  • Uncategorized (6)

Recent Posts

  • Tello! May 14, 2018
  • Build a Crossbow from Legos July 30, 2016
  • We’re Now On Scratch! April 22, 2016
  • 3D Printing Over the Network Using Sketch-up and Octoprint September 11, 2015
  • What is a Robot? August 14, 2015

Tags

3D printer 3D printing Agile Family Manifesto Agile Family TED Talk bit Charlotte Mason Crewton Ramone's House of Math design process five in a row History History According to Bob History Podcast homeschool Homeschool Dad homeschoolgeek Homeschool History homeschool library Homeschool Math homeschool organizational tool Homeschool Resources How To Be A Coder How To Homeschool iterative design iterative learning jan bloom kids html learn to code living books Living Math Math math games my first book of code Pi Day Podcast printrbot simple metal programming literacy programming toys Raspberry Pi Robotics teach kids to code teach kids to program used books valeries living books Why Homeschool YouTube

Copyright © 2025 · Streamline Pro Theme on Genesis Framework · WordPress · Log in