Thread: 2d Pong HELP!!
View Single Post
Old Feb 20th, 2008, 6:02 PM   #1
Alittlelost
Newbie
 
Join Date: Jan 2008
Location: Belfast, Northern Ireland
Posts: 7
Rep Power: 0 Alittlelost is on a distinguished road
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;

}
}
Alittlelost is offline   Reply With Quote