• Home
  • Tutorials
    • Game Development Tutorial>
      • Unit 1: Beginning Java>
        • Before you begin...
        • Day 1: Setting Up
        • Day 2: Java Basics
        • Day 3: More Basics
        • Day 4: Java Math
        • Day 5: More Math
        • Day 6: If... else...
        • Day 7: More Control Flow
        • Day 8: Looping
        • Day 9: More on Looping
        • Day 10: Inheritance, Interface
        • Day 11: Threads and Graphics
      • Unit 2: Creating a Game I>
        • Day 1: Foundations
        • Day 2: Basic Framework
        • Day 3: Taking User Input
        • Day 4: Enter the Robot
        • Day 5: Background and Sprites
        • Day 6: Adding Enemies
        • Day 7: Shooting Bullets
        • Day 8: Animations
        • Day 9: 2D-Arrays
        • Day 10: Painting the Tilemap
      • Unit 3: Creating a Game II>
        • Day 1: Level Creation - Part 1
        • Day 2: Level Creation - Part 2
        • Day 3: Level Creation - Part 3
        • Collision Detection Basics
        • Day 4: Collision Detection Part 1
        • Day 5: Collision Detection Part 2
        • Day 6: Collision Detection Part 3
        • Day 7: Health System & Death
        • Day 8: Basic AI & Final Touches
      • Unit 4: Android Game Development>
        • Day 1: Introduction to Android
        • Day 2: Setting up for Development
        • Day 3: Creating our First Android Application
        • Day 4: Parts of an Android Application
        • Day 5: The Android Game Framework: Part I
        • Day 6: The Android Game Framework: Part II
        • Create an Android Game From Scratch (or port your existing game)
        • Day 7: Creating an Android Game (From Start to Finish)
      • Reference Sheet
    • Zombie Bird Tutorial (Flappy Bird Remake)>
      • Unit 1: Building the Game>
        • Introduction
        • Day 1: Flappy Bird - An In-depth Analysis
        • Day 2: Setting up libGDX
        • Day 3: Understanding the libGDX Framework
        • Day 4: GameWorld and GameRenderer and the Orthographic Camera
        • Day 5: The Flight of the Dead - Adding the Bird
        • Day 6: Adding Graphics - Welcome to the Necropolis
        • Day 7: The Grass, the Bird and the Skull Pipe
        • Day 8: Collision Detection and Sound Effects
        • Day 9: Finishing Gameplay and Basic UI
        • Day 10: GameStates and High Score
        • Day 11: Supporting iOS/Android + SplashScreen, Menus and Tweening
        • Day 12: Completed UI & Source Code
    • Android Application Development Tutorial>
      • Unit 1: Writing Basic Android Apps>
        • Before you begin...
        • Day 1: Android 101
        • Day 2: Getting to Know the Android Project
        • Day 3: The Development Machine
        • Day 4: Building a Music App - Part 1: Building Blocks
        • Day 5: Building a Music App - Part 2: Intents
        • Day 6: Building a Music App - Part 3: Activity Lifecycles
  • New Forum
  • About Us
    • Contact Us
  • Our Games
    • TUMBL: FallDown
  • Facebook
  • Twitter

      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.

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. 
Picture
A group of Fallen engage a Monk in Diablo 3
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. 
Picture
Enemy Troops utilize various weapons against the player in Metal Slug
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).

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.

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;
}

}

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.

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.
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
Picture
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.
Go to Unit 3: Day 7
Continue to Unit 4
© 2014 Kilobolt, LLC. All rights reserved.