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


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
Web Hosting
Best Deals on PDAs!
Online Credit Reports
Tech Magazines - FREE
Compare Prices & Shop
Find a Web Host
Track Defects Online
Is Your Credit Bad?
FREE Credit Report
Hacker's Secrets
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

Bounce a ball

Well Ok we are now able to move a ball across the applet without any flickering of the screen. This means that we know the two most important steps to program a game with animated objects. In this chapter I want to tell you how to change the movement direction of the ball. This is the first time that we can't solve a problem by a "standard" technique, as we could do in the chapters before (Double buffering...). But I'm sure if you understand the solution, you will have your own ideas once you are in a similar situation. In our last two applets, the ball is moving across the applet area just one time. But now we want to avoid, that the ball is leaving the applet area, instead the ball should bounce in the opposite direction if it hits a wall. What shall we do? It would be good, if you would stop reading now and try to find a solution by yourself. In the following chapter you'll get to know my solution (which is not the only one!). Please use the "Ball" - applet with double buffering as the basic code for this chapter!

How to move a ball in different x - directions

First of all we have to think of the technique we used to move the ball in the second chapter. What did we do? Well, we increased the x - position of the ball every time the thread was executed and and repainted the ball at the new position. Maybe you didn't realize it, but we used a "speed vector" for the x - direction of the ball, everytime we changed the x - position of the ball by calling x_pos ++ in the run - method. That means that we can also define a variable called x_speed that stores the value which will be added to x_pos by calling now x_pos += x_speed. Imagine, x_speed has the value 1, this means, that the ball is moving from the left to the right hand side (same direction as in the chapter before), because x_pos will be increased by one everytime the run - method is executed. Now it is really easy to change the direction of the ball. We only have to set the value of x_speed to -1! This means, that everytime we call x_pos += x_speed -1 is "added" to the x - position of the ball. That way the value of x_pos is decreased by one and the ball is moving from the right to the left hand side!

Now we are able to change the direction of the ball! And there is just one "problem" left! How can we (the computer) decide, when to change the direction? Well, we have to test everytime we execute the run method, at what x - position our ball is. The code to do this looks like this:

// Ball is bounced if its x - position reaches the right border of the applet
if (x_pos > appletsize_x - radius)
{

    // Change direction of ball movement
    x_speed = -1;
}
// Ball is bounced if its x - position reaches the left border of the applet
else if (x_pos < radius)
{
    // Change direction of ball movement
    x_speed = +1;
}

Everytime we execute the run - method we test if the ball has reached one of the borders (left or right). If this is the case, we change the value of x_speed and that way we are able to change the direction of the ball movement.

You might be asking why I use the radius of the ball in the code above. The x - position of the ball is its middle point, which means that the left or the right border of the ball is its x - position plus or less the radius. To make us believe that the ball would be bouncing when it hits the wall, we have to add or substract the radius to / from the the value of x_pos.

How to "beam" the ball

Once you understand the technique of how to test where the ball is, you can implement many other things. Imagine we want to let the ball appear on the other side of the applet once the ball has left the applet area. That's very easy to do, we have to test again, where the ball is and then we have to set a new x - position for the ball instead of changing the value of x_speed.

// Test if ball has left applet area if (x_pos > appletsize_x + radius)
{
    // Set a new x_pos value for the ball
    x_pos = -20;
}

Later, in chapter 5, I will talk about how to move a ball not only in a one dimensional way but in a two dimensional area (which means in x and y direction). That is really easy to do. You will have not only one vector (x_speed) but two (x_speed, y_speed), and a y_pos and a x_pos. If one changes both values everytime run() is called the ball will be moving over the whole area. But of course we have to test 4 different possibilities, where the ball could leave the applet. If you want to, you can program an applet where the ball can move in different directions (2D) and is bouncing from every border of the applet (would be a good practice ;-). Ok, that's it, take a look at the applets and download the code!

SourceCode BallReverse download
SourceCode BallBeamen download
Applet Ball Reverse ansehen
Applet Ball Beamen ansehen

Nächstes Kapitel

Using sounds in applets


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.


Find the lowest price for a variety of products:


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

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/