# Kollisionsabfrage Ball <-> Paddle



## JaVa (5. Aug 2004)

Hi!

Ich bin jetzt schon weiter mit meinem Spielchen!
Aber wie kann ich die width und die height von meinem Paddle abfragen und in Variablen speicher???

Hier der Code



```
import java.awt.*;
import java.applet.*;

public class SampleThread extends Applet implements Runnable 
{
	Thread t;
    //int i;
    int x;
    int y;
    int ball_x;
    int ball_y;
    int x_vector = 1;
    int y_vector = 1;
    
    boolean play = true;
    
    Image PCpaddle;
    Image ball;
    
    Image dbImage;
    Graphics dbg;
    
 
	
	public void init()
	{
		t = new Thread(this);
		
		t.start();
		
		//i = 0;
		x = 0;
		y = 250;
		ball_x = 250;
		ball_y = 250;
		
		PCpaddle = getImage(getCodeBase(), "paddlePC.GIF");
		ball = getImage(getCodeBase(), "ball.GIF");
	}
	
	
	public void run()
	{
		while(true)
		{
			//i++;
	
			repaint();
			
			if(y > 410)
			{
				//y = y-1;
				y = 410;
			}
			
			if(y < 0)
			{
				//y = y+1;
				y = 0;
			}
			
			ball_x = ball_x + x_vector;
			ball_y = ball_y + y_vector;
			if(ball_x > 470)
			{
				x_vector = -3;
				y_vector = +3;
			}
			if(ball_x < 0)
			{
				x_vector = +1;
				y_vector = -1;
			}
			
			if(ball_y > 470)
			{
				y_vector = -1;
			}
			
			if(ball_y < 0)
			{
				y_vector = +1;
			}
			
			if(ball_x == widthPaddle)
			{
				x_vector = +3;
			}
			
			try
			{
				t.sleep(10);
			}	
			catch (InterruptedException e)
			{
			}	
		}
	}
	
	public void update (Graphics g)
    {
	  // init of Double Buffer
	  if (dbImage == null)
	  {
        dbImage = createImage (this.getSize().width, this.getSize().height);
		dbg = dbImage.getGraphics ();
	  }

	  // delete image in the background
	  dbg.setColor (getBackground ());
	  dbg.fillRect (0, 0, this.getSize().width, this.getSize().height);

	  // draw foreground on deleted background
	  dbg.setColor (getForeground());
	  paint (dbg);
	
	  // displays complete image on screen
	  g.drawImage (dbImage, 0, 0, this);
    }
	
	
	public void paint(Graphics g)
    {
      //g.drawString("i = "+i, 10, 20);
      
      g.drawImage(PCpaddle, x, y, this);
      g.drawImage(ball, ball_x, ball_y, this);
    }
    
    
    public boolean keyDown(Event e, int key)
    {
      if(key == 1005)
      {
      	y = y+4;
      }
      if(key == 1004)
      {
      	y = y-4;
      }
	  
      return true;
    }

}
```


Grüße Java


----------



## Beni (5. Aug 2004)

> Image PCpaddle;


Guck mal in der API unter "Image" nach...


----------



## bevyunceboobe (29. Mrz 2008)

Hello. Let's get acquainted! 
My name is Jessika.


----------

