![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Newbie
Join Date: Jan 2008
Location: Belfast, Northern Ireland
Posts: 7
Rep Power: 0
![]() |
2d Pong HELP!!
Hey everyone,
I tried looking around pretty much everywhere i could think of but i havnt been able to come up with a solution to my problem, which ill explain in a minute. This is my first time making any sort of videogame so im a complete beginner to some of the methods involved. I am attempting to make a 2d pong java applet. So far I have the background colored. The paddle and ball on the screen. User input works to move the paddle up and down. The ball moves randomly and resets to its original position after reaching a certain x-coordinate (best way i could think of). The ball also bounces off the right,top and bottom 'walls'. This idea of the game is basically a single player game of squash, so that for now, i can avoid AI. Sorry for going on so much, but my problem is that I cannot find a way to create collsion detection between the paddle (a rectangle made with g.fillRect) and the ball (made with g.fillOval). I have tried using the Rectangle class and creating an intersects boolean but with no success, my code compiled but it seemed to have no effect. Here is my code, thanks a lot for any help and thanks for reading this. import java.applet.*;
import java.awt.*;
import java.util.*;
import java.math.*;
public class Ball extends Applet implements Runnable
{
int x_pos = 590, y_pos = 100, radius = 20, appletsize_x = 800, appletsize_y = 600, x_speed=0, y_speed=0;
int x_pos1 = 100, y_pos1 = 100, x_speed1, y_speed1;
int limit = 100;
Random rnd = new Random();
private Image dbImage;
private Graphics dbg;
public void init()
{
setBackground (Color.blue);
}
public void start ()
{
Thread th = new Thread (this);
th.start ();
}
public void stop()
{
}
public void destroy()
{
}
public void run ()
{
Thread.currentThread().setPriority(Thread.MIN_PRIORITY);
while (true)
{
if (x_pos > appletsize_x - radius)
{
x_speed = -10;
}
else if (x_pos < radius)
{
x_speed = +10;
}
else if (y_pos > appletsize_y - radius)
{
y_speed = -10;
}
else if (y_pos < radius)
{
y_speed = +10;
}
else if (x_pos < x_pos1-50)///////////////////////////
{
x_pos = 590;
y_pos = rnd.nextInt(600);
}
x_pos += x_speed;
y_pos += y_speed;
if (x_pos1 > appletsize_x - radius)
{
x_speed1 = -10;
}
else if (x_pos1 < radius)
{
x_speed1 = +10;
}
else if (y_pos1 > appletsize_y - radius*4)
{
y_speed1 = -10;
}
else if (y_pos1 < radius)
{
y_speed1 = +10;
}
x_pos1 += x_speed1;
y_pos1 += y_speed1;
repaint();
try
{
Thread.sleep (20);
}
catch (InterruptedException ex)
{
}
Thread.currentThread().setPriority(Thread.MAX_PRIORITY);
}
}
public void update (Graphics g)
{
if (dbImage == null)
{
dbImage = createImage (this.getSize().width, this.getSize().height);
dbg = dbImage.getGraphics ();
}
dbg.setColor (getBackground ());
dbg.fillRect (0, 0, this.getSize().width, this.getSize().height);
dbg.setColor (getForeground());
paint (dbg);
g.drawImage (dbImage, 0, 0, this);
}
public void paint (Graphics g)
{
g.setColor (Color.red);
g.fillOval (x_pos - radius, y_pos - radius, 2 * radius, 2 * radius);
g.setColor (Color.green);
g.fillRect (x_pos1 - radius, y_pos1 - radius, 2 * radius, 2 * radius+50);
}
public boolean keyDown (Event e, int key)
{
if (key == Event.LEFT)
{
x_speed = -10;
y_speed = 0;
}
else if (key == Event.RIGHT)
{
x_speed = 10;
y_speed = 0;
}
else if (key == Event.DOWN)
{
y_speed = 10;
x_speed = 0;
}
else if (key == Event.UP)
{
y_speed = -10;
x_speed = 0;
}
else if (key == 32)
{
x_speed = -10;
y_speed = +10;
}
else
{
System.out.println ("Character: " + (char)key + " Integer Value: " + key);
}
if (key == 113)
{
x_speed1 = 0;
y_speed1 = -10;
}
else if (key == 97)
{
x_speed1 = 0;
y_speed1 = +10;
}
//else if (key == 32)
//{
// x_speed1 = 0;
// y_speed1 = 0;
//}
else
{
System.out.println ("Character: " + (char)key + " Integer Value: " + key);
}
return true;
}
} |
|
|
|
|
|
#2 |
|
Newbie
Join Date: Jan 2008
Location: Belfast, Northern Ireland
Posts: 7
Rep Power: 0
![]() |
Re: 2d Pong HELP!!
I've now got the ball being reset after it gets passed the paddle (not that it even hits off it but still). so it resets back to a random y coordinate at the same x coordinate. I've also included a "computer score" which is updated each time the ball goes past the paddle.
Has anybody ne ideas on the collision detection, i am quickly running out of ideas and none of em seem to work. This is the updated code, please HELP!!!!
import java.applet.*;
import java.awt.*;
import java.util.*;
import java.math.*;
public class Pong extends Applet implements Runnable
{
/////BALL/////
int xball = 590;
int yball = 100;
int radius = 20;
int x_speed;
int y_speed;
int xc,yc;
/////PADDLE////
int xpad = 100;
int ypad = 100;
int x_speed1;
int y_speed1;
int xpadc,ypadc;
/////MISC///////////////
int appletsize_x = 800;
int appletsize_y = 600;
int playerscore;
int i,j;
final int PI = 3.14;
private Image dbImage;
private Graphics dbg;
Random rnd = new Random();
int ballarea = PI*(radius*radius);
public void init()
{
setBackground (Color.blue);
}
public void start ()
{
Thread th = new Thread (this);
th.start ();
}
public void stop()
{
}
public void destroy()
{
}
public void run ()
{
Thread.currentThread().setPriority(Thread.MIN_PRIORITY);
while (true)
{
if (xball > appletsize_x - radius)
{
x_speed = +10;
}
else if (xball < radius)
{
x_speed = +10;
}
else if (yball > appletsize_y - radius)
{
y_speed = -10;
}
else if (yball < radius)
{
y_speed = +10;
}
else if (xball < xpad-50)
{
xball = 700;
yball = rnd.nextInt(600);
playerscore++;
yc = yball;
}
else {}
xball += x_speed;
yball += y_speed;
////////////////////////////////////////////////////////
if (ypad > appletsize_y - radius*4)
{
y_speed1 = -10;
}
else if (ypad < radius)
{
y_speed1 = 10;
}
xpad += x_speed1;
ypad += y_speed1;
repaint();
try
{
Thread.sleep (20);
}
catch (InterruptedException ex)
{
}
Thread.currentThread().setPriority(Thread.MAX_PRIORITY);
}
}
public void update (Graphics g)
{
if (dbImage == null)
{
dbImage = createImage (this.getSize().width, this.getSize().height);
dbg = dbImage.getGraphics ();
}
dbg.setColor (getBackground ());
dbg.fillRect (0, 0, this.getSize().width, this.getSize().height);
dbg.setColor (getForeground());
paint (dbg);
g.drawImage (dbImage, 0, 0, this);
}
public void paint (Graphics g)
{
g.setColor (Color.red);
g.fillOval (xball - radius, yball - radius, 2 * radius, 2 * radius);
g.setColor (Color.green);
g.fillRect (xpad - radius, ypad - radius, 2 * radius, 2 * radius+50);
g.setColor (Color.white);
g.drawString("Computer Score = " + playerscore,0,10);
}
public boolean keyDown (Event e, int key)
{
if (key == 32)
{
x_speed = -10;
y_speed = +10;
}
else if (key == 113)// key 'Q' = move paddle up
{
x_speed1 = 0;
y_speed1 = -10;
}
else if (key == 97)// key 'A' = move paddle down
{
x_speed1 = 0;
y_speed1 = +10;
//System.out.println("ycoord: " + ypad);
//System.out.println("xcoord: " + xpad);
}
else
{
System.out.println ("Charakter: " + (char)key + " Integer Value: " + key);
}
return true;
}
}//end class |
|
|
|
|
|
#3 |
|
Hobbyist Programmer
|
Re: 2d Pong HELP!!
You could try to use some triginomitry by using sin and cos with right triangles.
__________________
i dont know much about programming but i try to help |
|
|
|
|
|
#4 |
|
Newbie
Join Date: Nov 2007
Location: Ireland
Posts: 18
Rep Power: 0
![]() |
Re: 2d Pong HELP!!
Possibly by using the distance formula. If the distance between the paddle and ball is less than such a number, you've got a collision. But I guess it depends on the coordinate system you're using.
http://www.purplemath.com/modules/distform.htm |
|
|
|
|
|
#5 |
|
Professional Programmer
|
Re: 2d Pong HELP!!
There is a couple of ways you can do this. First I would simply check if the balls x coordinate is less than (x_paddle_position + paddle_width).
You can get more info here: http://www.edenwaith.com/products/pi.../collision.php
__________________
JG-Webdesign |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| 2D Graphics in Java | truBlu | Java | 5 | Feb 3rd, 2008 2:49 PM |
| 2d array | michelle_789 | C | 3 | Nov 27th, 2007 8:23 AM |
| Original Pong circuit | Klarre | Coder's Corner Lounge | 3 | Aug 21st, 2006 10:30 PM |
| Pong game complete | teencoder | Show Off Your Open Source Projects | 6 | Aug 9th, 2006 2:27 PM |
| Pong AI | Markpy | Delphi | 1 | Oct 13th, 2005 5:50 AM |