Kollisionsabfrage Ball <-> Paddle

Status
Nicht offen für weitere Antworten.
J

JaVa

Gast
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


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
 
Status
Nicht offen für weitere Antworten.
Ähnliche Java Themen
  Titel Forum Antworten Datum
T Kollisionsabfrage von einem Stein mit einem Ball Spiele- und Multimedia-Programmierung 5
A DoodleJump programmieren: Kollisionsabfrage Spiele- und Multimedia-Programmierung 6
T Problem bei Kollisionsabfrage Spiele- und Multimedia-Programmierung 4
S Polygon Kollisionsabfrage Spiele- und Multimedia-Programmierung 2
RalleYTN Erweiterte Kollisionsabfrage Spiele- und Multimedia-Programmierung 7
S Kollisionsabfrage zwischen Rechteck und Polygon Spiele- und Multimedia-Programmierung 1
J Java Kollisionsabfrage Spiele- und Multimedia-Programmierung 21
kaoZ Kollisionsabfrage implementieren Spiele- und Multimedia-Programmierung 63
T Problem mit Kollisionsabfrage der NPC Spiele- und Multimedia-Programmierung 1
F Kollisionsabfrage bei schnellen Objekten Spiele- und Multimedia-Programmierung 2
J Problem bei pixelgenauer Kollisionsabfrage Spiele- und Multimedia-Programmierung 10
M Kollisionsabfrage Spiele- und Multimedia-Programmierung 7
N Quake - Kollisionsabfrage Spiele- und Multimedia-Programmierung 21
N Problem mit Kollisionsabfrage beim Fallen Jump & Run Spiele- und Multimedia-Programmierung 5
R Kollisionsabfrage haut nicht hin Spiele- und Multimedia-Programmierung 15
Gossi Quaxlis 2D Tutorial....Probleme nach hinzufügen der Kollisionsabfrage Spiele- und Multimedia-Programmierung 16
U Jump n' Run 2D Geometrie und Kollisionsabfrage? Spiele- und Multimedia-Programmierung 11
baddestpoet Problem mit Kollisionsabfrage Spiele- und Multimedia-Programmierung 18
D Kollisionsabfrage von 2 Autos Spiele- und Multimedia-Programmierung 2
G Kollisionsabfrage (Mario klon) Spiele- und Multimedia-Programmierung 6
gieser Buggy Kollisionsabfrage Spiele- und Multimedia-Programmierung 4
masta // thomas Kollisionsabfrage - inspiriert durch "pixelgenaue Kolli Spiele- und Multimedia-Programmierung 13
gieser pixelgenaue Kollisionsabfrage der Kreise Spiele- und Multimedia-Programmierung 9
N Kollisionsabfrage Spiele- und Multimedia-Programmierung 6
D Jump and Run Game -- Kollisionsabfrage Spiele- und Multimedia-Programmierung 30
K Ball abschießen? Spiele- und Multimedia-Programmierung 1
K Ball abschießen Spiele- und Multimedia-Programmierung 1
J ping pong ball abprallen lassen Spiele- und Multimedia-Programmierung 12
P Ball mit schwerkraft hüpfen lassen Spiele- und Multimedia-Programmierung 9
Developer_X THE BALL (needs Java3D) Spiele- und Multimedia-Programmierung 34
S Ball Problem Spiele- und Multimedia-Programmierung 7
S Ball is Kaputt Spiele- und Multimedia-Programmierung 17
U Ball programmieren Spiele- und Multimedia-Programmierung 26
G Balken, der Ball zurückschießt Spiele- und Multimedia-Programmierung 7

Ähnliche Java Themen

Neue Themen


Oben