image Computational Thinking - Artificial intelligence

Artificial Intelligence, or AI, is the study of using computer algorithms to perform tasks that humans do. In video games, the control of non-player characters are a form of AI. For instance, in a pacman-like game, the ghosts can either chase or flee the pacman exhibit a certain level of AI, similar to a game of tag by humans.

In this tutorial, we will look at a game in which the player plays against the computer. The computer uses AI and acts as another human player. The player character and the NPC character hit a ball back and forth. Just like Ping Pong, the object of the game is to hit to ball past the other player and into the border behind them. The computer needs a way to know whether to move left or right with the ball, so we have the computer character track the embedded position_x attribute:

There is a fox near the top.
There is a rabbit near the bottom.
There is a fast ball near the middle of the canvas.
The player controls the rabbit.
When left arrow is pressed, the rabbit moves left.
When right arrow is pressed, the rabbit moves right.
The ball starts out moving in a random direction.
When the ball touches the left border, it reverses direction.
When the ball touches the right border, it reverses direction.
When the ball collides with the rabbit, it reverses direction.
When the ball collides with the fox, it reverses direction.
When the ball reaches the top border, you win.
When the ball reaches the bottom border, you lose.
When the position_x of the ball is less than the position_x of the fox, the fox moves left.
Otherwise, the fox moves right.


Try it yourself:


Complete Sample Games for Artificial Intelligence

// Game #1. Kick Ball Against the Turtle.

You control the rabbit.
When an arrow key is pressed, the rabbit moves in the same direction.

The speed of the rabbit is 3 pixel per frame.
The speed of the turtle is 1 pixel per frame.
The rocks are invisible. The bones are invisible. The pearl is invisible.
The speed of the ball is 5 pixels per frame.
The ball starts out moving in a random direction.
When the ball collides with the wall, the ball reverses direction.
When the ball collides with the rabbit, the ball reverses direction.
When the rabbit collides with the rock, the rabbit reverses direction.
When the ball collides with the turtle, the ball reverses direction.
When the ball touches the topaz, the ball reverses direction.
When the ball touches the gem, the ball reverses direction.
When the ball touches the border, the score increases by 1.
When the ball touches the border, the ball disappears.
When the ball disappears, the pearl is notified for 0.5 second.
When the pearl is notified, the pearl inserts a ball.
When the rabbit touches the bone, the rabbit reverses direction.
When the rabbit touches the wall, the rabbit reverses direction.
When the rabbit touches the gem, the rabbit reverses direction.
When the position_y of the ball is less than the position_y of the turtle, the turtle moves up.
Otherwise, the turtle moves down.

When the score is equal to 2, you win.
When the ball touches the bone, gameover.

Map:

-
-
-
---------s
wwwwwwwwwwwwwwwwwwww
G--------o---------F
G--------oR--------F
G--------o---------F
e--------o----------
e--------o----------
er-------ol-----T---
e--------o----------
e--------o----------
G--------o---------F
G--------o---------F
G--------o---------F
wwwwwwwwwwwwwwwwwwww
---------s

// Game #2. Touch the Spinstars and Push the Dino.

The speed of the pointer is 3.
The speed of the dino is 2.
The speed of the kitten is 1.4.

//Pointer control.
You control the pointer.
The angle of the pointer is 180.
When you press the down arrow, the pointer moves down.
When you press the up arrow, it moves up.
When you press the right arrow, the pointer becomes facing_east.
When the pointer is facing_east, the angle of the pointer decreases by 5.
When you press the left arrow, the pointer becomes facing_west.
When the pointer is facing_west, the angle of the pointer increases by 5.
When the angle of the pointer is less than 180, it moves right.
When the angle of the pointer is greater than 180, it moves left.

When the angle of the pointer is greater than 355, the angle of the pointer becomes 0.
When the angle of the pointer is less than 0, the angle of the pointer becomes 355.
When the pointer collides with a border, it wraps around.
When the pointer collides with the dino, it pushes the dino.

//Kitty Run away.
When the kitten sees the dino, it flees the dino.
When the kitten collides with the border, it wraps around.
The kitten wanders around.
When the kitten collides with the dino, it explodes.

//Dino AI.
When the dino is not dead, it moves down with 50 percent probability.
When the position_x of the kitten is less than the position_x of the dino, the dino moves left with 60 percent probability.
Otherwise, the dino moves right with 60 percent probability.
When the dino collides with a border, it wraps around.

//Random generation of spinstars.
There is an apricot.
The apricot is invisible.
The speed of apricot is 9.
The apricot starts by moving in a random direction.
When the apricot collides with a border, it reverses direction.
When a spinstar collides with pointer, it explodes and score increases by 1.
When a spinstar explodes, the apricot becomes empowered for 0.3 second.
When the apricot is empowered, it inserts spinstar.

//Game over.
When the kitten is gone you lose.
When score is 10, you win.

Map:

-
-
----------D
----------s
-
-
-
-
-
----------p
----------i


This is a brief tutorial which should be sufficient for you to get started with creating fun games!

Next page

Tutorials Home