GAME DEVELOPMENT TUTORIAL: DAY 3-8: What is Artificial Intelligence?
- GAME DEVELOPMENT TUTORIAL: DAY 3-8: What is Artificial Intelligence?
Welcome to Day 8, the final lesson of Unit 3.
In today's lesson, we will be answering the following: What is Artificial Intelligence?
If you are starting with this lesson, feel free to grab the source code at the end of Day 7!
As this is the last lesson of Unit 3, and since we will be rebuilding this game in Unit 4 for Android, I did not think that we needed a complex Artificial Intelligence System. Therefore, this short lesson will first discuss what AI is and how we would implement it into a game.
Then, we will finish by adding just add one method to the Enemy class as an example and framework for a more compelling AI system that will follow.
In today's lesson, we will be answering the following: What is Artificial Intelligence?
If you are starting with this lesson, feel free to grab the source code at the end of Day 7!
As this is the last lesson of Unit 3, and since we will be rebuilding this game in Unit 4 for Android, I did not think that we needed a complex Artificial Intelligence System. Therefore, this short lesson will first discuss what AI is and how we would implement it into a game.
Then, we will finish by adding just add one method to the Enemy class as an example and framework for a more compelling AI system that will follow.
Artificial Intelligence
Artificial Intelligence, more commonly known as AI, refers to the mimicking of human intelligence in computers or robots.
In the realm of game development, AI refers to the exhibited "intelligence" of non-player characters that can make the game more believable or challenging. For example, in the game Diablo 3, a player will walk into a group of enemies, and the mob may engage or flee after assessing the situation.
In the realm of game development, AI refers to the exhibited "intelligence" of non-player characters that can make the game more believable or challenging. For example, in the game Diablo 3, a player will walk into a group of enemies, and the mob may engage or flee after assessing the situation.
Similarly, in Metal Slug, a game with a "great AI system," enemies will use different weapons depending on the proximity of the player, will attempt to use grenades from covers, run away from certain situations, and so on.
All of these little touches make the game more enjoyable (potentially) and challenging.
Various Forms of AI
Some of the more popular forms of AI are the following:
Scripted AI: Enemies or NPC's that move in a pattern (found in early Pokemon games).
Situationally Aware AI: Enemies or NPC's that react realistically to scenarios (such as people expressing distress to a corpse in the streets of Skyrim).
Rubberband AI: Also known as dynamic game difficulty balancing, this refers to AI that adjusts difficulty based on player performance (for example speeding up when the player gets ahead in a race).
Scripted AI: Enemies or NPC's that move in a pattern (found in early Pokemon games).
Situationally Aware AI: Enemies or NPC's that react realistically to scenarios (such as people expressing distress to a corpse in the streets of Skyrim).
Rubberband AI: Also known as dynamic game difficulty balancing, this refers to AI that adjusts difficulty based on player performance (for example speeding up when the player gets ahead in a race).
2D Platformers
As we are creating a 2D platformer, we will focus on path-finding (movement) and combat.
Path-finding refers to an enemy's ability to navigate to the player's location. In our game, we would want our enemies to approach the character when it comes near. If the character is on top of a platform, we would want certain enemies to be able to follow.
Combat is self-explanatory. It would refer to the kind of AI that is found in Metal Slug like above, in which enemies are able to use weapons effectively against you in various ways.
In our game, we will have certain enemies that move randomly (zombie-like path-finding). These enemies will not care whether you are shooting at them or not. We will also have enemies that follow the player (we will be incorporating this in this lesson). Thirdly, we will also have enemies that attack when the player comes too close.
I think that's enough theory for now. When we reach Unit 4, some of these concepts will come in handy. Until then, this extremely simple implementation will have to suffice.
Path-finding refers to an enemy's ability to navigate to the player's location. In our game, we would want our enemies to approach the character when it comes near. If the character is on top of a platform, we would want certain enemies to be able to follow.
Combat is self-explanatory. It would refer to the kind of AI that is found in Metal Slug like above, in which enemies are able to use weapons effectively against you in various ways.
In our game, we will have certain enemies that move randomly (zombie-like path-finding). These enemies will not care whether you are shooting at them or not. We will also have enemies that follow the player (we will be incorporating this in this lesson). Thirdly, we will also have enemies that attack when the player comes too close.
I think that's enough theory for now. When we reach Unit 4, some of these concepts will come in handy. Until then, this extremely simple implementation will have to suffice.
Implementing a Following AI
Make these changes to your Enemy Class (changes in BOLD):
package kiloboltgame;
import java.awt.Rectangle;
public class Enemy {
private int power, centerX, speedX, centerY;
private Background bg = StartingClass.getBg1();
private Robot robot = StartingClass.getRobot();
public Rectangle r = new Rectangle(0, 0, 0, 0);
public int health = 5;
private int movementSpeed;
// Behavioral Methods
public void update() {
follow();
centerX += speedX;
speedX = bg.getSpeedX() * 5 + movementSpeed;
r.setBounds(centerX - 25, centerY - 25, 50, 60);
if (r.intersects(Robot.yellowRed)) {
checkCollision();
}
}
private void checkCollision() {
if (r.intersects(Robot.rect) || r.intersects(Robot.rect2)
|| r.intersects(Robot.rect3) || r.intersects(Robot.rect4)) {
System.out.println("collision");
}
}
public void follow() {
if (centerX < -95 || centerX > 810){
movementSpeed = 0;
}
else if (Math.abs(robot.getCenterX() - centerX) < 5) {
movementSpeed = 0;
}
else {
if (robot.getCenterX() >= centerX) {
movementSpeed = 1;
} else {
movementSpeed = -1;
}
}
}
public void die() {
}
public void attack() {
}
public int getPower() {
return power;
}
public int getSpeedX() {
return speedX;
}
public int getCenterX() {
return centerX;
}
public int getCenterY() {
return centerY;
}
public Background getBg() {
return bg;
}
public void setPower(int power) {
this.power = power;
}
public void setSpeedX(int speedX) {
this.speedX = speedX;
}
public void setCenterX(int centerX) {
this.centerX = centerX;
}
public void setCenterY(int centerY) {
this.centerY = centerY;
}
public void setBg(Background bg) {
this.bg = bg;
}
}
import java.awt.Rectangle;
public class Enemy {
private int power, centerX, speedX, centerY;
private Background bg = StartingClass.getBg1();
private Robot robot = StartingClass.getRobot();
public Rectangle r = new Rectangle(0, 0, 0, 0);
public int health = 5;
private int movementSpeed;
// Behavioral Methods
public void update() {
follow();
centerX += speedX;
speedX = bg.getSpeedX() * 5 + movementSpeed;
r.setBounds(centerX - 25, centerY - 25, 50, 60);
if (r.intersects(Robot.yellowRed)) {
checkCollision();
}
}
private void checkCollision() {
if (r.intersects(Robot.rect) || r.intersects(Robot.rect2)
|| r.intersects(Robot.rect3) || r.intersects(Robot.rect4)) {
System.out.println("collision");
}
}
public void follow() {
if (centerX < -95 || centerX > 810){
movementSpeed = 0;
}
else if (Math.abs(robot.getCenterX() - centerX) < 5) {
movementSpeed = 0;
}
else {
if (robot.getCenterX() >= centerX) {
movementSpeed = 1;
} else {
movementSpeed = -1;
}
}
}
public void die() {
}
public void attack() {
}
public int getPower() {
return power;
}
public int getSpeedX() {
return speedX;
}
public int getCenterX() {
return centerX;
}
public int getCenterY() {
return centerY;
}
public Background getBg() {
return bg;
}
public void setPower(int power) {
this.power = power;
}
public void setSpeedX(int speedX) {
this.speedX = speedX;
}
public void setCenterX(int centerX) {
this.centerX = centerX;
}
public void setCenterY(int centerY) {
this.centerY = centerY;
}
public void setBg(Background bg) {
this.bg = bg;
}
}
As there is no new code here that needs explanation, I will allow you to figure out what changes have been made.
This is the last thing we will change for Unit 3. We will be starting from scratch in Unit 4.
This is the last thing we will change for Unit 3. We will be starting from scratch in Unit 4.
Unit 4: What's coming next?
Most of you will be following this lesson with Unit 4, which will teach you how to create/port a game for Android.
In Unit 4, we will discuss Android development as a whole. We will talk about the tools, the market, and Android's limitations, among other things.
Then we will create a full game development framework that you will be able to reuse in your future games.
Finally, using this framework, I will port the Unit 2-3 game to Android as a demonstration.
In Unit 4, we will discuss Android development as a whole. We will talk about the tools, the market, and Android's limitations, among other things.
Then we will create a full game development framework that you will be able to reuse in your future games.
Finally, using this framework, I will port the Unit 2-3 game to Android as a demonstration.
That's it for Unit 3. Thanks for reading, and remember to Like us on Facebook to stay updated!
I am looking forward to seeing you in Unit 4.
Feel free to email me questions at jamescho7@kilobolt.com
I am looking forward to seeing you in Unit 4.
Feel free to email me questions at jamescho7@kilobolt.com
|
|
Very minor changes were made since Day 7. If you would like the source code, simply click here to download the Day 7 Source Code and replace its Enemy class with the one above.
Learn how to embed this game into HTML by visiting the Reference Sheet.
Learn how to embed this game into HTML by visiting the Reference Sheet.