Problem Set: Sprite Overlap

This section contains a number of selected problems for the Sprite Overlap section.

It is recommended that you review the problems, and complete a few before moving on to the next section.

Problem #1: Hidden Treasure

Create two sprites: a sprite of kind Player and a sprite of kind Treasure. Design the Player sprite so that it looks like a pirate, and make it move with buttons.

The Treasure sprite should be filled with the color the background, so that it is hard to see. Place the Treasure at a random position on the screen.

Create an overlap event between Player and Treasure sprites to represent the pirate finding the treasure.

In the overlap event, use sprite.setImage to change the image of the Treasure sprite to a treasure chest. After changing the image, pause for one second, and then end the game with game.over(true).

Problem #2: Dodge Ball

Create two sprites: a sprite of kind Player and a sprite of kind Ball. Design the Player sprite so that it looks like a student in gym class, and make it move with buttons.

Give the Ball a random initial x and y velocities. Set the BounceOnWall SpriteFlag to true, so that the Ball will bounce around the screen. Draw a ball for the image of the Ball sprite.

Create an overlap event between Player and Ball sprites to represent the player getting hit by the ball.

In the overlap event, splash “ouch!” to the screen, and then ends the game with game.over(false)

Problem #3: Stay Out of the Fire

Create two sprites: a sprite of kind Food and a sprite of kind Fire. Design the Food sprite so that it looks like a small burger, and make it move with buttons at about half speed (that is, using 50 as the vx and vy parameters for moveSprite).

Set the Fire sprite‘s image to look like a fire.

Create an overlap event between Food and Fire sprites to represent the player getting hit by the ball.

In the overlap event, move the Food up and to the left 1 pixel, and move the Fire down and to the right 1 pixel.

Now, the Food and Fire will repel each other when they touch.

Problem #4: Pushing the Food

Start with a solution to problem #3; this problem will extend from that code.

The previous problem resulted in the Fire and Food moving away from each other on contact, but they always moved in the same direction. Another option would be to have the Food move away from the Fire depending on where it is on the screen; that way, if the burger moves on top of the fire from the bottom right, the burger moves back in the direction it came from.

Fire pushing away burger

In the overlap event, remove the portion that makes the Fire move. Next, use if conditions to control the direction the Food is moved.

If the x position of the Food sprite is less than the x position of the Fire sprite, increment the Food sprite‘s x position; otherwise (else), decrement the x position.

Finally, do the same comparison and updates for the y position. With that, the Food sprite will stay away from the Fire sprite, moving away no matter the direction it comes from.