Game Programming for Teens
[ LiB ] |
Chasing and Evading
Well, now that we know how to use random variables and timers, we need to learn how to create artificial intelligence that actually works for a reason. Chasing and evading are very good ways to demonstrate this. Both are easy and interesting: chasing makes one object fol-low another, and evading makes one object run away from another.
Without further ado, chasing!
Chasing
Chasing entailes finding the coordinates of one object and moving another object based on where the first one is located. That may be a complex explanation, so let's break it down.
Let's say you have a spaceship following another spaceship; in fact, we will be writing a program that does this in a few minutes. Well, the program starts with the two ships onscreen in such a manner that ship A is following ship B. When B is to the left of A, A begins to move left. When B is to the right of ship A, ship A moves right. The same thing happens when B is above or below ship A: when it's above, ship A moves up; when it's below, ship A moves down. Figure 12.3 demonstrates chasing.
Figure 12.3. Following, chasing, tracking, or stalking? You be the judge.
So, because everything we do in this book uses spaceships, demo12-03bb is a chasing game in Blitz Basic. Check out the program and the source code on the CD. Seriously, check it out.
So, did you check it out? This program is a lot of fun to watch when run on the screen. No matter where you go, that tenacious spaceship won't go away!
There is only one section of the program I want to discuss: the tracking section. The tracking code looks like this:
;Now, we move the enemy depending on where the player is ;If the player is above the enemy, move the enemy up If playery > enemyy enemyy = enemyy + ENEMYSPEED EndIf ;If the player is to the left of the enemy, move the enemy left If playerx < enemyx enemyx = enemyx - ENEMYSPEED EndIf ;If the player is to the right of the enemy, move the enemy right If playerx > enemyx enemyx = enemyx + ENEMYSPEED EndIf ;if the player is below the enemy, move the enemy down If playery < enemyy enemyy = enemyy - ENEMYSPEED EndIf
Let's start off with the first line, If playery > enemyy . What does this do? Well, this just checks the y coordinate of the player against the y coordinate of the enemy. Because the higher on the screen the object is, the lower the y coordinate is (remember that the top of the screen is y = 0 ), when playery is greater than enemy y (this is tested in the first If EndIf statement), the player is below the enemy. Therefore, the enemy moves down a little bit.
The same thing happens in the following If EndIf statements. When playerx is less than enemyx , the player is to the left of the enemy, and the enemy moves left. When playerx is more than enemyx , the player is to the right of the enemy, and the enemy moves right. Finally, when playery < enemyy , the enemy moves down.
Well, that's it for tracking. All that's left in this chapter is the extremely difficult concept of evasion.
Evading
A lot of times, when I tell a joke, my friends say they can't tell whether what I said was true or if I was simply kidding them. The final sentence of the last section would be an example of that. Just to let you know, evading isn't the "extremely" difficult concept I made it out to be. But you probably already knew that, right?
Either way, I'm sure you want to know all the ins and outs of evasion. Actually, you already do. Evasion is the opposite of chasing, because the enemy is running away from you. Check out Figure 12.4 to see how evasion works, with the player being the ship marked "A." As you can see, the ship always moves away from the player.
Figure 12.4. Evasion.
Anyway, take a guess as to how you would use evasion. You're right! (That is, you are right if you guessed the following answer.) All you do is take the tracking algorithm and change the pluses to minuses and the minuses to pluses. Demo12-04.bb shows evasion, and it is almost exactly the same as its predecessor, demo12-03. In fact, I only made two changes. Here is the first one.
;Now, we move the enemy depending on where the player is ;If the player is above the enemy, move the enemy down If playery > enemyy enemyy = enemyy - ENEMYSPEED EndIf ;If the player is to the left of the enemy, move the enemy right If playerx < enemyx enemyx = enemyx + ENEMYSPEED EndIf ;If the player is to the right of the enemy, move the enemy left If playerx > enemyx enemyx = enemyx - ENEMYSPEED EndIf ;if the player is below the enemy, move the enemy up If playery < enemyy enemyy = enemyy + ENEMYSPEED EndIf
Does this look familiar? As I said, it is exactly the same as demo12-03.bb, but the pluses and minuses have been flipped . Now, when the enemy is to the right of the player, it continues to move right. When it is to the left, it moves even further left. When above, it moves up, and when below, it moves down.
I also added one new section to the program. This section makes sure that the enemy ship doesn't run offscreen , as you would expect it to because it is fleeing the player.
;if enemy goes offscreen, move him back onscreen
If enemyx <= 0 enemyx = 0 ElseIf enemyx >= 800 enemyx = 800 EndIf If enemyy <= 0 enemyy = 0 ElseIf enemyy >= 600 enemyy = 600 EndIf
This code checks the enemy's coordinates to see if he is onscreen or offscreen. If he is off-screen , the code makes sure he cannot move any further in that direction and keeps him onscreen.
[ LiB ] |