arrow_back Play the Computer
This add-on is your introduction to artificial intelligence. Just like what computer opponents in video games and self-driving cars use. The example in the video will show you two ways of programming the opponent. The first is easier to program, but much harder to defeat.
The second involves more code, but it gives the player a fighting chance.
First, duplicate the athlete. If you’ve already programmed a second player, you can use that sprite as the computer player. Either way, disconnect the second player’s code from the “when flag clicked” event, so that it’s still there but doesn’t run.
Next, go to costumes, click “flip up-down,” and move the new sprite to an opposing position at the top of the screen. The first way to program this is to make the sprite always go to the same left-right position as the ball sprite. In the “scripts” tab, from the “motion” menu, drag out a “set x to” block. The number in the x value represents the sprite’s position from left to right on the stage. For instance, zero is anywhere along this middle line of the stage, 240 is the right edge of the stage, and negative 240 is the left edge of the stage. Go to the “sensing” menu. Into the “set x to” block, put the rounded block that says, "x position of sprite." From the second dropdown in the sprite, select the ball sprite. This block now reads, “set the computer player’s left-right position to be the same as the left-right position of the ball sprite.”
Click it to see if the computer sprite changes position.
Just like in the “bounce reaction” add-on and the “keep score” add-on, it’s not enough to do this once. The sprite needs to keep doing this to be a worthy opponent. Wrap the block in a “forever” loop. Then, place a “set rotation style” block, on top of the block stack, and select “left-right” from the dropdown. Finally, start the stack off with a “when flag clicked” block. You just created the first computer opponent.
Try it out! Then, try programming a more realistic computer opponent. To do that, follow the rest of this video. This next part is more challenging, but it will result in a cooler project that more closely resembles a real game! Start by disconnecting the last code stack from the event so it doesn’t run. Then, duplicate the original block stack.
Here’s what you’re about to program: The computer sprite will act a lot like a real human player. It will move in the direction of the ball just as fast as your player.
Modify the new code stack to make it automatic. Start by removing and discarding the “key pressed” sensing blocks. Replace them with blocks from the “operators” menu. Place a “greater than” block in the first blank. Into the second blank, drag a “less than” block. Drag another "position of sprite" block from the “sensing” menu into the first blank in each of the “operators” blocks. Check that “x position” and “tennis ball” (or the ball sprite in your program) are selected in the dropdowns. In both of the second blanks, you’ll refer to the x position of the sprite - the one you’re currently programming. You’ll find the block that gives the x position of the current sprite in “motion” Drag out two of them, and snap them into the two remaining blanks.
Finally, snap “when flag clicked” on the top and test. Okay, wait. The computer is just running away from the ball. It’s going the wrong direction! So the code in the example reads, “if the ball is to the right of the athlete, move left.” Yeah that’s backward.
If that happens to you just pull out the motion blocks and switch them around in the “if” blocks. Great, now watch the computer sprite move in the direction of the ball sprite. If your computer sprite seems to be moving in the wrong direction, you may have switched the conditions in the conditionals.
There is one last bug. Notice that the new sprite constantly flips when it moves back one direction. The forever block checks both “if” conditions very quickly, giving the appearance that both conditions are being met at the same time. This makes the sprite look like its flipping. Fix this by putting a wait block under the second “if” block.
Tinker with this value until you find something that works well for you program. Test it out!
Great job! Once you’re finished, you’ll have programmed a computer opponent with artificial intelligence! Here’s the game plan: Program the computer sprite to forever move to the x position of the ball sprite.
Then, program the computer sprite to detect whether to move left or right, then to move in that direction like the human player.