This add-on is your introduction to artificial intelligence. Just like what computer opponentsin video games and self-driving cars use. The example in the video will show you twoways 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 canuse that sprite as the computer player. Either way, disconnect the second player’s codefrom 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 positionat the top of the screen. The first way to program this is to make thesprite 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 representsthe sprite’s position from left to right on the stage. For instance, zero is anywherealong this middle line of the stage, 240 is the right edge of the stage, and negative240 is the left edge of the stage. Go to the “sensing” menu. Into the “setx to” block, put the rounded block that says, "x position of sprite." From the seconddropdown in the sprite, select the ball sprite. This block now reads, “set the computerplayer’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 notenough to do this once. The sprite needs to keep doing this to be a worthy opponent. Wrapthe 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 stackoff 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 therest of this video. This next part is more challenging, but itwill result in a cooler project that more closely resembles a real game! Start by disconnectingthe 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 realhuman 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 “keypressed” sensing blocks. Replace them with blocks from the “operators”menu. Place a “greater than” block in the first blank. Into the second blank, draga “less than” block. Drag another "position of sprite" block fromthe “sensing” menu into the first blank in each of the “operators” blocks. Checkthat “x position” and “tennis ball” (or the ball sprite in your program) are selectedin the dropdowns. In both of the second blanks, you’ll referto the x position of the sprite - the one you’re currently programming. You’ll findthe block that gives the x position of the current sprite in “motion” Drag out twoof them, and snap them into the two remaining blanks.
Finally, snap “when flag clicked” on the top and test. Okay, wait. The computer isjust running away from the ball. It’s going the wrong direction! So the code in the examplereads, “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 movein the direction of the ball sprite. If your computer sprite seems to be moving in thewrong 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 backone direction. The forever block checks both “if” conditions very quickly, giving theappearance that both conditions are being met at the same time. This makes the spritelook like its flipping. Fix this by putting a wait block under the second “if” block.
Tinker with this value until you find something that workswell for you program. Test it out!
Great job! Once you’re finished, you’ll have programmeda 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 movein that direction like the human player.
Scegli un’estensione
play_circle_filled
Reazione al rimbalzo
Ordina allo sprite Palla di fare o dire qualcosa quando rimbalza.
arrow_forward
play_circle_filled
La maglia della squadra
Fai indossare all'atleta la maglia della tua squadra del cuore.
arrow_forward
play_circle_filled
Secondo giocatore
Aggiungi un altro atleta per creare una partita a due.