The canvas will display many random images of animals at random positions, one of which will be a sausage dog. The player has to click on the sausage dog to win the game. When clicked successfully, the sausage dog will start spinning.
wheres-sausage-dogactivities folder in your repository folder (create it if necessary)The key observation for this project is that it will be useful to make classes representing a generic “animal” and the specific sausage dog we’re searching for. Because the sausage dog will be a lot like the other animals (an image displaying on the canvas) we can use inheritance.
Then, in the main script, we can add a bunch of animal images and a single sausage dog image in order to put together the game itself.
We’ll need to do something like the following:
Luckily for us, we have an appropriate set of animal images ready to go in a .zip file.
assets/images folder in your projectAnimal classWe want a class to represent a generic animal as just an image that can be displayed on the canvas. Because we will eventually want our sausage dog to rotate when found, and because rotation is a neat thing to be able to do, we’ll also make this class include the idea of rotation.
Animal.js file and add a script tag for it in index.html (remember to place the tag above the script.js one)class structure for the Animalconstructor() with parameters x, y and image and store each parameter in a property, also add an angle property set to 0.update() method that just calls a display() methoddisplay() method thatpush()pop()At this point our program doesn’t really do anything because we’re not using this wonderful Animal class just yet
Half of the magic of our program is going to be display lots of animals on the canvas at the same time. To do this, we need to load the animal images and we need to create the right number of animal objects and display them.
const) for the number of animal images (10) and the number of animals to display (100)preload() use a for loop to load all the animal images by using the iterator (i) as part of the filename to load (that’s why they’re numbered from 0 to 9), adding each image to the animal images arraysetup()windowWidth and windowHeight)for loop to create all the animals at random positions and with a random image from the animal images array, adding each one to the animals arraydraw()for loop to go through the animals array and call the update() method on each animal in itAfter this we should be able to run the program and see 100 animal images display randomly all over the canvas!
SausageDog classThe star of our program is going to be a single sausage dog the player is trying to find. It will just be another image, but they can click on it to make it spin as a kind of victory. Because it’s very similar to the Animal class, we’ll extend that class and add some extra elements that are specific to the sausage dog.
SausageDog.js file and add a script tag for it in index.html (remember to place the tag above the script.js one and below the Animal.js one)class structure for the SausageDogconstructor() with parameters x, y and image andsuper() constructor, passing along the parametersfound property set to false (for tracking finding!)rotationSpeed property set to something like 0.25 (for spinning!).update() method thatupdate() methodfound property is true and adds the rotationSpeed to the angle if it is (so that dog spins when it’s found!)mousePressed() method thatwidth and height properties, and remember the image is centered when it’s displayed)found property to trueThe other half of the magic of our program is that when we find the sausage dog and click on it, it reacts! To do this, we need to load the sausage dog image, we need to create a single sausage dog object, and we need to display it and also tell it when the mouse is clicked.
preload() load the sausage dog image into the sausage dog image variablesetup() create a new SausageDog object at a random position and using the loaded sausage dog image and store it in the sausage dog object variabledraw() call the update() method of the sausage dog objectmousePressed() function and call the mousePressed() method of the sausage dog object inside itAfter this we should be able to run the program and see 101 animal images display randomly all over the canvas, with the sausage dog somewhere among them. If we click the sausage dog we should see it start to spin!
We have met the brief!
Currently our program works, but there are some places where we could make it more modular and add some potential for reuse.
script.js
assets/images/animal) and use it when loading themassets/images/sausage-dog.png) and use it when loading itsetup() into a separate function called createAnimals()createRandomAnimal() that returns the created animalsetup() into a function called createSausageDog()draw() into a function called updateAnimals()draw() into a function called updateSausageDog()Animal.js
overlap(x,y) that checks whether the provided coordinates are inside the animal’s image dimensions and returns true if they are and false otherwiseSausageDog.js
overlap() method in mousePressed() to check whether the mouse position was clicked inside the sausage dogWell that’s one nice little program and quite a fun game in the end. You can probably imagine all kinds of ways to expand on this basic ideas here, such as…