Once again, I really struggle to think of a task to do for this homework. I thought of improving my bouncing ball and modify it so I can make it more interactive with user through some kind of game. I turned down this idea because the game I thought of making will take me a lot of time to implement because I will need to work with classes and objects. I decided to implement this kind of game for a later time. Another idea that came up was making a Sudoko game but when I realized how complicated the winning conditions of Sudoko I instantly turned down the idea because I will need at least two-three days to implement it. After a few minutes from turning down the Sudoko game idea, I remembered how tough next semester will be, (basically the spring semester of the sophomore year for the Engineering students is hell) I decided to make a game that links the new stuff we covered today in class (functions, if statements plus I used some keyCodes) with how NYU is a very tough school especially with issuing homeworks and tasks.
Basically the game consists of a ball that bounces off the edges of the screen and the user’s tasks is to use the arrows of the keyboard to move a rectangle at the bottom of the screen to catch the ball before it bounces off the bottom edge of the screen. The ball represents the huge amount of homeworks that NYU students receive on a daily basis. The rectangle represents the poor NYU students who are trying their best to deal with all these homeworks they are receiving from every class they are in. The edges of the wall represent the classes (or the courses).
Most parts of code that were in the bouncing ball program were inherited to this game code. So, I still have the same variables with the addition of a few variables that describe the x and y position of the rectangle, the speed of the rectangle on the x-axis and the width and height of the rectangle. I also added a variable (gameStatus) which is (sometimes) called a ‘flag’. I created this variable so that I can let the program know when should the game continue and when should it stop running (when the user loses).
Here are the list of variable:
<script src=”https://gist.github.com/rha257/2a0902bd73ac2815fc91.js”></script>
Notice that I have commented out one variable off the list. This variable was supposed to measure the time elapsed since the program started and depending on how much time elapsed until the user have lost the game, the program would issue the user a score. However, I was not able to do that because p5 is not recognizing what is ‘second()’ although this function is included in the list of references on the p5 website.
Moving on with the code, I have created a function called KeyPressed() so that I can add the conditional statements required to move the rectangle using the keyboard arrows. After that was done, I created another function called simpleGameLogic() which defines the logic of the game and its rules.
I had some issues when I was writing the code. For example, my main issue was to make the ball bounce off the rectangle instead of just continuing to fall to the ground. The issue was regarding the syntax I used in the if statement which I am not sure if it’s a p5 issue or this is how javascript works. The code did not work when I used this:
if (((y+(r/2) > height-recth) && (rectx<x<(rectx+rectw))) {
But it worked for me when I changed it to this:
if (((y+(r/2) > height-recth) && (rectx<x) && (x<(rectx+rectw)))) {
It seems weird to me and I don’t get why wouldn’t it work for the first piece of code.
In conclusion, it was a pretty interesting task for me and it went smoothly with some minor issues here and there but in general writing the code went smoothly.