-
Collsions help
can sombody pleas help me with my Java program. i have created a Tron game (light cycles). i have all the code finished except the collisons. i need help fast as this due soon
// The "Tron" class.
import java.applet.*;
import java.awt.event.*;
import java.awt.*;
import GameLib.*;
public class Tron extends GameApplet
{
int speed = 0;
Image sprImg, expl[];
Gfx gfx; //The GAMELIB - Graphix class (double-buffering)
Sprite spr, spr2, spr3;
boolean game = true;
int lastkey;
int count;
int p2x = 160, p2y = 240;
int p1x = 480, p1y = 240;
int direct;
public void init ()
{
setSize (640, 480);
setSleepTime (100 - speed); // a new method (GameApplet), set the sleep time (millisec)
sprImg = getImage (getCodeBase (), "dot1.gif"); //the sprite image
spr = new Sprite (this, sprImg, 160, 240, 1, true); //create sprite
sprImg = getImage (getCodeBase (), "dot2.gif"); //the sprite image
spr3 = new Sprite (this, sprImg, 480, 240, 1, true); //create sprite
loadExplosion (); //this will load an array of images
gfx = new Gfx (this, new Color (250, 250, 250));
spr2.setAnimOnlyOnce (true);
spr.setDirection (new Point (0, 0));
spr.setAutoMoving (true);
spr3.setDirection (new Point (0, 0));
spr3.setAutoMoving (true);
spr.setCollisionRect (new Rectangle (0, 0, 10, 10));
spr3.setCollisionRect (new Rectangle (0, 0, 10, 10));
} // init method
public void paint (Graphics g)
{
count++;
gfx.cls (); //clear the screen
gfx.drawSprite (spr); //draw the first Sprite
gfx.drawSprite (spr2);
gfx.drawSprite (spr3);
//gfx.setColor (Color.white);
gfx.drawString ("PLayer 1 X: " + p1x, 10, 20);
gfx.drawString ("PLayer 1 Y: " + p1y, 10, 30);
gfx.drawString ("PLayer 2 X: " + p2x, 10, 40);
gfx.drawString ("PLayer 2 Y: " + p2y, 10, 50);
gfx.drawString ("LastKeyCode: " + lastkey, 10, 10);
if (direct == 1)
{
p1x -= 10;
}
if (direct == 2)
{
p1x += 10;
}
if (direct == 3)
{
p1y -= 10;
}
if (direct == 4)
{
p1y += 10;
}
// player 2
if (direct == 5)
{
p2x -= 10;
}
if (direct == 6)
{
p2x += 10;
}
if (direct == 7)
{
p2y -= 10;
}
if (direct == 8)
{
p2y += 10;
}
gfx.refresh ();
} // paint method
public void game (int id) //this method is the most important of the GameApplet
//the run() method of the GameApplet calls it two times each run
{
switch (id)
{
case 1: //game(1) is called before the Applet sleeps
{
if (spr.getPosition ().x > 640)
spr.setDirection (new Point (-10, 0));
if (spr.getPosition ().x < 0)
spr.setDirection (new Point (10, 0));
if (spr3.getPosition ().x > 640)
spr3.setDirection (new Point (-10, 0));
if (spr3.getPosition ().x < 0)
spr3.setDirection (new Point (10, 0));
if (spr.getPosition ().y > 480)
spr.setDirection (new Point (0, -10));
if (spr.getPosition ().y < 0)
spr.setDirection (new Point (0, 10));
if (spr3.getPosition ().y > 480)
spr3.setDirection (new Point (0, -10));
if (spr3.getPosition ().y < 0)
spr3.setDirection (new Point (0, 10));
if (spr.rectCollision (spr3)) //if spr collides with spr3
{
spr3.setDirection (new Point (0, 0));
spr.setDirection (new Point (0, 0));
spr2.runAnim (); //show the explosion
spr2.moveTo (spr3.getPosition ()); //the explosion is alway on the same position
//like the second sprite.
}
repaint (); // It's necessary to add repaint(); somewhere or else you won't see anything.
break;
}
case 2: //game(2) is called after the thread had slept.
{
break;
}
}
}
public void loadExplosion ()
{
expl = new Image [12]; //create an array of 12 images
//load all images:
expl [0] = getImage (getCodeBase (), "ex1.gif");
expl [1] = getImage (getCodeBase (), "ex2.gif");
expl [2] = getImage (getCodeBase (), "ex3.gif");
expl [3] = getImage (getCodeBase (), "ex4.gif");
expl [4] = getImage (getCodeBase (), "ex5.gif");
expl [5] = getImage (getCodeBase (), "ex6.gif");
expl [6] = getImage (getCodeBase (), "ex7.gif");
expl [7] = getImage (getCodeBase (), "ex8.gif");
expl [8] = getImage (getCodeBase (), "ex9.gif");
expl [9] = getImage (getCodeBase (), "ex10.gif");
expl [10] = getImage (getCodeBase (), "ex11.gif");
expl [11] = getImage (getCodeBase (), "ex12.gif");
//create the sprite:
spr2 = new Sprite (this, expl, 100, 100, 0, 0, 1, true, false);
//the constructor for an array of images, explaind in the
//tutorial and the Sprite.java file
}
public boolean keyDown (Event e, int key)
{
switch (key)
{
case Event.LEFT:
spr3.setDirection (new Point (-10, 0));
p1x -= 10;
direct = 1;
//spr2.moveBy (-5, 0);
break;
case Event.RIGHT:
spr3.setDirection (new Point (10, 0));
p1x += 10;
direct = 2;
//spr2.moveBy (5, 0);
break;
case Event.UP:
spr3.setDirection (new Point (0, -10));
p1y -= 10;
direct = 3;
//spr2.moveBy (0, -5);
break;
case Event.DOWN:
spr3.setDirection (new Point (0, 10));
p1y += 10;
direct = 4;
//spr2.moveBy (0, 5);
break;
case 97:
spr.setDirection (new Point (-10, 0));
p2x -= 10;
direct = 5;
//spr2.moveBy (-5, 0);
break;
case 100:
spr.setDirection (new Point (10, 0));
p2x += 10;
direct = 6;
//spr2.moveBy (5, 0);
break;
case 119:
spr.setDirection (new Point (0, -10));
p2y -= 10;
direct = 7;
//spr2.moveBy (0, -5);
break;
case 115:
spr.setDirection (new Point (0, 10));
p2y += 10;
direct = 8;
//spr2.moveBy (0, 5);
break;
}
lastkey = key;
return true;
}
} // Tron class
here is my code
you can take out the explotion and dot1 and dot2 are 10x10 .gifs
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
Forum Rules
|
Top DevX Stories
Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL
|
Bookmarks