Need help with function code:

Hi I am trying to make my score go up by 1 if it touches apple and go down one if it touches the pizza. I have set the code up but when it touches it, the score goes up really quickly. I also need to create a function or code where if the score is >8 game ends and says “You win” or health is <4 and says “You lose” using an if/else statement.

Any help is apperciated. Thank you

[Posts in the debugging category need to have the following pieces of information included in order to help other teachers or our moderator team best help you. Feel free to delete everything in brackets before posting your request for debugging help.]

Link to the project or level: [replace with link to the curriculum or get the “Share” link to a project]
What I expect to happen: [replace with a detailed description of the behavior you’re trying to create]
What actually happens: [replace with a detailed description of what actually happens when the code runs including any errors or unexpected behavior]
What I’ve tried: [replace with a detailed description of what you’ve already tried to do to solve the problem]

The counter goes up each frame if the ghost is touching the pizza or the apple. In order to mitigate this, the sprite that was touched should have it’s y component reset to 0 (or lower) and it’s x component randomized. On line 83, this snippet should be inserted into the if-statement.

apple_1.x = randomNumber(50, 350);
apple_1.y = randomNumber(-30, -60);

As for the pizza, on line 88, this snippet should be inserted.

ghost_1.x = randomNumber(5, 200);
ghost_1.y = randomNumber(-30, -100);

As for the win/lose conditions, you could put if-statements at the start of the draw function that checks whether the score is over 8 or the health is below 4. If either of those happen to be true, you could add a return statement (which prevents the rest of the code from executing). Here is an example:

//add at the start of the function
if (health<4) { //prioritize lower health over higher score
  text("You Lose", 0, 15);
} else if (score>8) {
    text("You Win", 0, 15);

Some other tips are

  • setAnimation only needs to be called once at the start of execution.
  • You can better constrain the x values by doing randomNumber(ghost_1.width/2, World.width-ghost_1.width/2)
1 Like

Thank you soo much!. That code worked and helped a lot!.

I don’t get the two other tips you gave?
Also how do you create an intro screen so before they play they can read the instructions? I have a function called ‘intro’ and defined it but doesn’t work.

function enemy() {
if (ghost_1.isTouching(pizza_1)) {
health = health - 1;
pizza_1.x = randomNumber(5, 200);
pizza_1.y = randomNumber(-30, -100);

This the code on line 95 where you told me to add the two variables for pizza. When I did my sprite would disappear when it touches the pizza. So I changed the ghost_1.x & y to pizza_1.x &y. At the start the pizza falls and repeats as it should, but then it stops appearing?

That is the updated game.

Thanks once again

Here is the most recent one. When I start the game, they fall from random places but then it keeps falling from the same place. I have played with the random number commands but can’t get it to work.

The tips I gave were just ways to optimize the game. For games that are simple, this isn’t really necessary. As long as the game works, I believe it is the best it can be.
To clarify, once you call setAnimation, it stays permanent. The draw function runs the code inside every frame. If there was a lot of code to be called, there would be frame issues. As for the random numbers, I noticed that some sprites were not completely inside the canvas.
If you wanted to keep them inside the screen without having to manually find the correct values, you could have automated it by replacing something such as randomNumber(50, 350) with randomNumber(sprite.width/2, World.width-sprite.width/2), which guaranteed that the sprite’s position would always be completely within the canvas.
However the game works fine, (except for the sound repeating) so there’s no need for any of this.

Thank you for the feedback! It helped a lot. I took out the setanimation code and put it underneath each variable for the sprite.
I am still having issues with the sound though, I just want it to play for a second or two then stop. I used the stopsound command but didn’t help.