Inspired by the fish eating game, I made this pacman eating game. Why pacman? I guess the reason is that it is easier to calculate the intersection distance since they are basically arcs.
So just like the fish eating game, your pacman starts from a small scale. You can only eat other pacmans which are smaller than you. And you must always watch your back. Otherwise, even if your pacman has been grown to really big, maybe the largest in the pool, you will still lose the game if it’s caught up by a tiny pacman from behind.
I actually play this game a lot of times. And you could really learn a lot from this game for your real life:
- Be patient when you’re still a tiny fish. You always start from small then dream bigger.
- The hardest part sometimes is the moment you almost success. You become bigger but also easier to be touched by other pacmans.
- Always watch your back, even when you are a big fish.
Okay, don’t be that serious. Enjoy the game!
And here’s the process I coded.(Sorry I was too focus on it and didn’t take screenshot during the process)
- I draw “mypacman” first, by creating the construction function “GoodPacman”.
- I coded two construction functions “BadPacmanL” and “BadPacmanR” to draw the enemies from left and right, and use random function to get random size, color, and moving speed.
Problem here: At first, I use random function in the construction function and got error sign. It seems like I have to write it in setup.
- Then I create addL and addR function to add new “BadPacmanL” and “BadPacmanR” in the array “enemyL” and “enemyR”, using it in draw function with loop function. And every time the object is out of canvas, I splice it from the array and use the addL, addR function to add a new one to the array. By doing that, I keep the enemy number to 8, always with different colors, sizes and speeds.
- I tried to move my pacman by using keyIsDown condition, adding it to the GoodPacman construction. I var a variable called “dir”, set it as 1 when pressing right arrow, and set it as 0 when pressing left button, so that I can record the direction. Then create a function called faceRight to return true when dir = 1, and return false for else condition. As a result, I can easily use this faceRight function as a boolean thing to do many things, like change the facing direction of my pacman.
Problem here: I forgot to add “()” after “faceRight”, then it won’t work.
- Last but not least, I need to set the game rules. First, I use dist function to detect if my pacman intersects an enermy. Then I use the faceRight function I created before to see if it’s facing to the enermy. After that, I use the “.d” variable to compare if my pacman is bigger than the enermy. All these conditions together decides if my pacman successfully eats an enemy. If so, I add 0.5 to the scale, slice the enemy object and using addL or addR function again to add a new enemy. I also created a new variable called gameStatus and set it to 1, 0, or -1 so that I can show winning, play, or losing status.