advertisement
javaboutique
Search Tips
Articles  |   Tutorials  |   Reviews  |   Tools  |   by Category  |   by Date  |   by Name  |   Submit  |   Source  |   Forums  |  
javaboutique
Browse DevX


Partners & Affiliates











advertisement

Tutorials : The Java Game Development Tutorial :

Contents
Introduction
Basic structure of an applet
Move a ball
Double buffering
Ball bouncing and change the direction
Using sound in applets
Using pictures in applets
Mouse events
Keyboard events
The first complete game
Artificial intelligence for a pong like game
Generation of random 2D landscapes

Artificial inteligence used in a Pong game

Many games need two players but mostly you have to play on your own, because no one is there to play with you. For these situations you want an opponent that is not too strong but is strong enough so that the game is not boring at all. If you are programing such a game you have to write an AI (artificial inteligence), that "plays" against the human player. In this chapter I want to explain how to program a really simple AI one could use for example in a Pong game like Streethockey as I did. I will only explain how to program the AI, please take a look at the other classes and methods in the sourcecode by yourself (download at the end of the chapter).

The situation

First of all we should decide what we want our AI to do: In a Pong game the computer has to make goals against the player and has to save his own goal as a goalkeeper. A human player, who is playing this game, will always try to be at the same y - position as the ball, so that the ball will bounce when it hits the player paddle. The computer should do the same and now we want to "tell" the computer how to follow the ball's y - position.

The idea

So the computer has to "watch" the ball and tries to be at the same position (in the game Streethockey it's the y - position) as the ball. Imagine the ball object would have methods, that could tell us (the computer) it's velocity, direction and position, which could be the "eyes" of the computer. Then the AI method is really simple, as you see:

The AI - method

    /** this method moves the computer */
    public void move (Ball ball)
    {
      // calculate the middle of the paddle
      real_y_pos = y_pos + (size_y / 2);

      /* If the ball is moving in opposite direction to the paddle and is no danger for computer's goal move paddle back to the middle y - position*/
      if (ball.getXSpeed() > 0)
      {
        // if the paddle's position is over the middle y - position
        if (real_y_pos < 148)
        {
          y_pos += y_speed;
        }
        // Paddle is under the middle y - position
        else if (real_y_pos > 152)
        {
          y_pos -= y_speed;
        }
      }
      // ball is moving towards paddle
      else if (ball.getXSpeed() < 0)
      {
        // As long as ball's y - position and paddle's y - position are different
        if ( real_y_pos != ball.getYPos())
        {
          // If ball's position smaller than paddle's, move up
          if (ball.getYPos() < real_y_pos)
          {
            y_pos -= y_speed;
          }
          // If ball's position greater than padle's, move down
          else if (ball.getYPos() > real_y_pos)
          {
            y_pos += y_speed;
          }
        }
      }
    }

Explaining the method

This method is really simple, there are two different cases:
If the ball is not moving towards the paddle (it's x_speed is positive) then the paddle is moved back to the middle position of it's goal. To do this one has to know if the position is greater or smaller than the middle. If it's smaller then paddle is over the middle and you have to move the paddle down and if it's greater then move the paddle up.
Now we get to the interesting case, the "AI" case: The ball is moving towards the computer and the computer has to decide where to move. If the computers position is greater than the balls position, then the ball is over the paddle, the paddle has to be moved up. I think you know what happens if the ball's position is smaller then the position of the paddle... right, move the paddle down. That's it and it works as you can see if you watch the applet!

At least one important thing

It would be no problem to write an AI (in this case) that can not be beaten. The computer doesn't get tired and is always "watching" the ball. You as a programer have to think of the human player. A game is only fun if you can win. Winning mustn't be too easy but it has to be possible. So the computer has to be weak too. For example it has to be slower than the ball, so that the computer can't get fast balls or something like that. You have to find the right balance between a strong and a weak AI, which might take much more time than writing the AI itself.

Sourcecode download (*.zip - archive)
Take a look at the applet

Next chapter

Generation of a 2D landscape

How to Add Java Applets to Your Site

New on the Java Boutique:

New Review:

Time Management Made Easy with the Quartz Enterprise Job Scheduler
Why not just use the Java timer API? This open source scheduling API boasts simplicity, ease-of-integration, a well-rounded feature set, and it's free!

New Applet:

Reverse Complement
Reverse Complement is a simple applet that converts DNA or RNA sequences into three useful formats.

Elsewhere on internet.com:

WebDeveloper Java
Lots of Java information on webdeveloper.com

WDVL Java
Thorough Java resource at the Web Developer's Virtual Library.

ScriptSearch Java
Hundreds of free Java code files to download.

jGuru: Your View of the Java Universe
Customizable portal with online training, FAQs, regular news updates, and tutorials.

 Avaya Developer Showcase
 MSDN Spotlight
 PHP for Windows Showcase
XML error: undefined entity at line 34
advertisement
Receive Articles via our XML/RSS feed
Receive Articles via our XML/RSS feed

JavaBytes
Internet Cyclone
This powerful, easy-to-use, internet optimizer is for Windows 95, 98, ME, NT, 2000 and XP. It's designed to automatically optimize your Windows settings, boosting your Internet connection up to 200%.

IBM Brings Developers Into the Cloud
Apache at 10: You Can't Buy Us
Microsoft's CodePlex Foundation Moving Forward
Apple Claims 100,000 Apps, Google Analyzes Them
Nokia Latest to Play Opera Mobile 10 Browser
PayPal Opens Up Payment Platform to Devs
Ubuntu Linux 9.10 'Karmic Koala' Starts Its Climb
IBM Links Rational Developer Tools, Tivoli Apps
Libraries Give Vista Apps a Windows 7 Look
Ubuntu: The 'Default Alternative' to Windows?

Delivering Web-based Embedded Fonts in CSS 3
Adobe Helps PHP Developers Create Rich Internet Applications
Java Developers Finding a Home at Adobe Flex
Virtualization Delivers a Dynamic Infrastructure
Consuming XML Web Services in iPhone Applications
Build a More Agile Business with IBM
POJO-Based Solutions for LDAP Access: One Good, One Better
IBM Offers Enhanced Measurement and Management for Energy Usage
IBM Helps Transformation to an Information-Based Enterprise
Top Five Touch UI-Related Design Guidelines

Advertising Info  |   Member Services  |   Contact Us  |   Help  |   Feedback  |   Site Map  |   Network Map  |   About

internet.commediabistro.comJusttechjobs.comGraphics.com

Search:

WebMediaBrands Corporate Info

Legal Notices, Licensing, Reprints, Permissions, Privacy Policy.
Advertise | Newsletters | Shopping | E-mail Offers | Freelance Jobs