Untitled
unknown
plain_text
3 years ago
2.3 kB
4
Indexable
# Set the background color to blue app.background = 'blue' # Use a global variable to keep track of the player's score var score = 0 # Use an if-else statement to check which arrow key is pressed # and change the player's direction accordingly function onKeyPress(key) { if (key === 'up') { player.dy = -player.speed; } else if (key === 'down') { player.dy = player.speed; } else if (key === 'left') { player.dx = -player.speed; } else if (key === 'right') { player.dx = player.speed; } } # Use the onKeyRelease() method to stop the player's movement # when the arrow key is released function onKeyRelease(key) { if (key === 'up' || key === 'down') { player.dy = 0; } else if (key === 'left' || key === 'right') { player.dx = 0; } } # The onFrame() method is called repeatedly, with the frame rate specified # in the app.fps property. Use it to update the player's position and check # for collisions with the obstacles or the edges of the screen. function onFrame() { # Update the player's position based on its dx and dy properties player.x += player.dx; player.y += player.dy; # Use a nested if-else statement to check if the player has collided # with any of the obstacles. If the player hits an obstacle, stop # the app and display a game over message. if (player.hits(obstacle1)) { app.stop(); app.text(500, 200, "Game over!", 'red', 100); } else if (player.hits(obstacle2)) { app.stop(); app.text(500, 200, "Game over!", 'red', 100); } else if (player.hits(obstacle3)) { app.stop(); app.text(500, 200, "Game over!", 'red', 100); } # Use a compound conditional with the and and or operators to check if the # player has collided with the edge of the screen. If the player hits the # edge of the screen, stop the app and display a game over message. if ((player.x < 0 || player.x > app.width) || (player.y < 0 || player.y > app.height)) { app.stop(); app.text(500, 200, "Game over!", 'red', 100); } # Use the not operator (!) to negate the result of the contains() method # to check if the player has collided with the edge of the screen. # If the player has not hit the edge of the screen, increment the score. if (!app.contains(player))
Editor is loading...