This week I revisited “Don’t eat the poo!” with an eye at cleaning up the code, but I also wanted to change a few images. For one thing, I feared the pink color mixed with a tongue made it look a little too much like a skin flick so I changed the background to a Caribbean blue to go along with the fabulous Beach Boys musical choice.Screen Shot 2015-09-30 at 10.12.33 PM

And you can’t do the beach without a palm tree and a tropical drink! But cosmetic changes aside, the real effort of the week was re-doing the code.

The video game itself was a very ambitious project and although we got it to work, the code looked like a complete mess. I was sure there was a better way to construct it. Lauren McCarthy, my genius professor, came to my rescue. I was determined to truly learn the best way to structure it so this time instead of hacking the code myself, I sat down with her and had her talk me through other options for the structure. With Lauren’s help, the 400 lines of code turned into under 200!

The main change was putting all of the moving parts into a single function with many parameters. To do this, all of the variables had to be introduced with their parameters:

//introduce variables with their paramters – their arguments for the function
apple = new Food(“assets/apple.png”, 7, 10, false);
cookie = new Food(“assets/cookie.png”, 5, -10, false);
donut = new Food(“assets/donut.png”, 8, 10, false);
eggplant = new Food(“assets/eggplant.png”, 7, 10, false);
fries = new Food(“assets/fries.png”, 6, -10, false);
grapes = new Food(“assets/grapes.png”, 5, 10, false);
hamburger = new Food(“assets/hamburger.png”, 5, -10, false);
pineapple = new Food(“assets/pineapple.png”, 5, 10, false);
pizza = new Food(“assets/pizza.png”, 6, -10, false);
poo = new Food(“assets/poo.png”, 5, 100, true);

//main function for the game that sets up all of the parameters for each object.
function Food(f, s, p, ip) {

this.x = int(random(0, width));
this.y = random(-50, 0);
this.img = loadImage(f);
this.speed = s;
this.points = p;
this.isPoo = ip;

There is a little more to the code to make it run, but I’d say the above part must have saved me around 100 lines of code! It’s so clean and lovely I cannot even express my gratitude to Lauren. This project helped me understand so much more about how to code AND I labeled everything clearly so I could revisit it later and know what I did.

The final important changes were to fix things we never got to work properly in the first place. First of all, I needed to use the mousePressed ( ) function¬†instead of mouseIsPressed ( ) to make the start button at the beginning work smoothly. And I was able to redo the code slightly to make the “win” screen and the “poo” screen neat and clean. Originally we were not able to clear off all of the images when the final text appeared. I believe this makes the game more satisfying.

Please enjoy the new and improved “Don’t eat the poo!” game through the link below!

PLAY GAME