Homework 2

Please complete this homework before next class. I will be posting the
solutions after next class.


Problem set A

1. Create a function that returns an array with the following values :
"Moose", "Snake", "Awesome"

2. Create a function that takes 1 integer parameter, x. The function should return an array with the all of the even numbers leading up to x.
The function should work like this :

      int[] array = evenArrayFunction(10);
    

Array should look like this :
      {0, 2, 4, 6, 8, 10}
    

3. Write a function that takes in an array of integers as a parameter. The function should print out everything in the array.
You should be able to use the function like this :

          int[] arrayToPrint = new int[] { 1, 3, 3, 7 };
          printArray(arrayToPrint);
        

It should print out :
          1 3 3 7
        

4. Write a function that takes in an array as a parameter.
The function should multiply every element in the array times 2.
You should be able to use the function like this :

            int[] arrray = new int[] { 1, 3, 3, 7 };
            multiplyArray(array);
          
And then afterwards, all of the elements should be :
            2 6 6 14
          


Problem Set B

We're going to be working on a simple game for Problem Set B.
We'll be making a game where we have to use the A and D keys to dodge rain. This will test your ability to solve problems creatively
and combine ideas we talked about in class.
You may not be able to get through all this, but try to get as far as you can. Reach out for help if you need it.

1. The first step is to copy the starting code from the reference page into processng. Our game will be starting off as the rain program.

2. The next step is modifying the rain program so that it rains much less. Currently the program rains a lot. Make less rain spawn.
You're probably going to want to slow down the fall of the raindrop too.

3. Next we need to add in our character at the bottom. For now our character
will be a green circle. The idea is that the green circle has to dodge the blue
circles that are falling.
Add in a green circle on the bottom of the screen that can be controlled by
the keys A and D.

4. The next step is that we need to be able to detect when a raindrop hits
our character. We can do that by checking the distance between the raindrops
and the character. If the distance between any raindrop and the character
is less than 10, the game is over and the player lost.

In order to check the distance between two points, you can use the following function :

          https://processing.org/reference/dist_.html
        

The idea is that when we detect a collision between the raindrop and the player, we have to trigger that the game is now over.
We can do that by having a boolean variable called gameOver.
          boolean gameOver = false;
        

The variable should be created at the top of the program (so that it can be used everywhere.)
It should start off as false because when the game starts, the game is not over.
But when we detect the rain hit the character, we need to set it to be true.

Here's how :
          gameOver = true;
        

5. When the gameOver is true, we want to stop the game.
We can do this by checking if gameOver == false in the draw function.

          if (gameOver == true) {
            // Do stuff that should happen when the game is over.
          } else {
            // Do stuff that should happen when the game is still going.
          }
        

Start of by printing "Game is over!" when the game has ended.

6. Next, we should make it so that instead of printing "Game is over!" when the game
has ended it should draw "Game over" text on the screen.
It's easy to draw text in processing. As an exercise, try figuring out
how to do it by using google as an exercise :)

7. Our game should probably have a score system so that the longer you
avoid the raindrops, the more points you get.
We can accomplish this by creating a float variable called "score" at the top of the program. Every draw that we don't get hit by a raindrop,
increase score by some small amount (maybe 0.001 ?). When the game is
over, draw the score on the screen too next to the game over text :)

8. If you've made it this far, you're doing great. Try to get creative
and add more things to the game. Here's some ideas :
- Make the game get harder as it progresses.
- Use images instead of circles.
- Add powerups.