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! II
Learn to Program Artillery! II

David January 12, 2014

Learn to Program Artillery! II

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

In our last post Learn to Program Artillery we left off with entering a velocity, an angle and the “Fire” button.  The code would draw an arc on the screen based on these parameters.  This time we will add a mountain to shoot over (or through until we do collision detection in the next post).

Initial Arc

To create a mountain, we will use an array to store the heights of the rectangles that make up the mountain.  What is an array?   An array is an indexed list of objects all of the same type.  In our case, it is an indexed list of numbers that represent rectangle heights (myMountainHeights) that we will use to draw the mountain. In javascript this variable is declared as follows:

mountain height variable

There are a couple ways we can populate this array.  One way would be to populate every number by hand like this:

Add intergers on declaration

That is fine for small arrays, but if we decide to have 200 sections in our mountain, I would rather have the computer fill them in.  But before we do that, lets also go ahead and set some more variables.

All variables

By setting these variables now, we will save time later when we decide the make the mountain wider, or make each section smaller.

So now we are ready to write the code to populate our array.  For our first mountain design lets make a pyramid.

Populate Array

By placing the variables above the environment function and inserting the code inside the environment function we should end up with something like this:

All Code

When you refresh the html page you should see a mountain that looks like this:

Mountain

If we were to print out the values in the array that were used to draw the mountain they would look like this:

array

So what do all these numbers mean?  The column on the left is the index on the array that we use to look up the value.  The second number is used to draw the rectangle on the screen and represents the distance from the ground to the top of the rectangle.   (Remember from the first post that our ground is 400 and the top of the screen is 0.  So for an item to be higher than the ground we have to subtract the rectangle height from 400.  To do this we are adding the negative of the rectangle height.).

You can view the complete source code or view the Artillery page here.  If you have any questions, please leave a comment below.  I’d also like to hear if you have any suggestions for improvement.  In the next installment we will be adding collision detection.

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