Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

 
 
Thread Tools Display Modes
Prev Previous Post in Thread   Next Post in Thread Next
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
 

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

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




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 10:46 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC