★ wanayoo — archive 1999 http://javaboutique.webdeveloper.com/tutorials/Java_Game_Programming/PongKIEng.htmlNouvelle recherche | Portail wanayoo
Computer Digital Expo

Find the lowest price for a variety of products:


Featured Categories:
Computers
Digital Cameras
Home Theatre
Video Games
Laptops
Software
Electronics
PDA's


Find a Web Host With:
CGI Access
DB Support
NT Servers
UNIX Servers
Telnet Access

advanced search
Internet News
Internet Investing
IT
Windows Technology
Linux/Open Source
Developer
Interactive Marketing
xSP Resources
Small Business
Wireless Internet
Downloads
Internet Resources
Internet Lists
International
EarthWeb
Career Resources

Search internet.com
Advertise
Corporate Info
Newsletters
E-mail Offers
Be a Commerce Partner
Find a Web Host
Web Hosting
Track Defects Online
Send a Press Release
Build an Online Store
Shop Digital Cameras
Free Market Research
Is Your Credit Bad?
Promote Your Website
Business Search
FlashKit
Gif.com
HierMenusCentral
JavaBoutique
JavaScript.com
JavaScriptSource
WebDev Jobs
JustSMIL
ScriptSearch
StreamingMediaWorld
Web Hosts
WebDevelopersJournal WDVL
WebDeveloper.com
WebReference.com
XMLFiles.com
CGI
HTML
Java
JavaScript
Web Design and Promotion
Web Site Development
E-mail Address:


Can you receive
HTML e-mail?

Yes   No

Zip Code:


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


Applet Index
(sorted alphabetically)

A B C D E F G H I J K
L M N O P Q R S T U
V W X Y Z #s
The Java Source
(applets w/source code)

A B C D E F G H I J K
L M N O P Q R S T U
V W X Y Z

How to Add Java Applets to Your Site

New on the Java Boutique:

New Article:
A Barebones Guide to Usability Testing
At first glance, usability testing may seem like unnecessary, or additional work. In todays budget slashed world, it is not uncommon for projects not to have the luxury of this valuable phase. This is a dangerous path to tread. Let's look at how we can incorporate usability testing into our design process.
... [more articles]

New Tutorial:
Java Certification Path API
Certificate based security forms the basis for SSL (Secure socket layer) transactions? Every year billions of transactions involving trillions of dollars are done safely using the SSL protocol. This week we'll look at the role of the Java certification path API and see how it provides a mechanism to implement certification path functionality.
... [more Tutorials]

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.

internet.com logo






Developer News

Apache Updates Popular HTTP Server

Is Adobe Targeting Microsoft's InfoPath?

Oracle Takes on Collaboration Market



Database Journal Launched!
Jupiter Media has announced the launch of Database Journal. DBJ offers SQL courses and other database related resources for beginner to expert developers.



Scrolling Text
This is a suite of three applets that displays scrolling text using an array of images. The detailed description of array of each applet is provided.
Download of the Week
DS PageFlipText
This applet displays a page-flip effect between texts. Over the animation you can also insert an image and a scroll-text.

JB User Poll
Which version of Java do you use the most?
The JavaBoutique Top 15:
1. ChompText
2. PulseText
3. DJPopMenu
4. NavBar
5. RushHour
6. PingPong
7. 3DMaze
8. AScroll2
9. Viewer
10. AnimLetters_anim
11. Encyclo
12. Distorter
13. BMI
14. MusicKeyboard
15. AdvPano
Want more? Check out our Top 100!

Refer-It
Affiliate Program and Referral Directory.
New on internet.com
Cisco Exploit Is Active
Security researchers have increased the threat level on a vulnerability affecting Cisco routers and switches after an exploit was posted to a popular mailing list.

The 3G Tokyo Time Machine
What will a 3G wireless future be like? Step into DoCoMo's time machine and find out.

SBA's Whistling Dixie
Small businesses throughout the South are extended the opportunity to meet one-on-one with government agencies and large corporations to obtain near-term contract opportunities for their products and services.


The Enterprise IM Strategies & Solutions Event

Jupitermedia is publisher of the internet.com and EarthWeb.com networks.
Copyright 2003 Jupitermedia Corporation All Rights Reserved.

Legal Notices,  Licensing, Reprints, & Permissions,  Privacy Policy.
http://www.earthweb.com/
http://www.internet.com/