Pong Game Kollisionsabfrage versagt zusammen mit AI

KGD92

Mitglied
Schönen Abend Leute,
ich hab gestern mal angefangen eine kleines Pong Game zu schreiben (in vorbereitung auf ein größeres Projekt, d.h. wollte erstmal verstehen wie ein Spiel grundsätzliche funktioniert).

Nun habe schön meine Kollisionsabfrage gebastelt und es geht soweit auch alles. Als ich dann die AI (= Inner Class implemts Runnable) implementiert habe und diese Ihren Schläger bewegt kollidiert meine Ball nicht mehr mit dem Schläger der AI (= Objekt PlayerTwo). Nun weis ich nicht genau, ob es daran liegt, dass die Animation des Balles als auch die AI selbst beides prallel laufende Threads sind, oder ob es mit was anderem zu tun hat.

Hier ist mal der Source, ich hoffe ihr könnt meine Fehler finden(, falls es den den gibt)

Java:
package pongfengine;

/* Simple Pong Game
 * Author: Kai Dölger
 * Date: 28.12.2009
 * Version: 0.1
 */

import java.awt.*;
import java.awt.event.*;
import java.awt.color.*;

public class F2D extends Frame implements KeyListener, WindowListener
{

    //Main Variables
    int x = 0;
    int y = 0;
    int height = 500;
    int width = 800;

    BatEntity playerOne;
    BatEntity playerTwo;
    BallEntity ball;
    int direction;
    Image dbImage;
    Graphics dbg;
    Thread movingBall;
    Thread ai;


    public static void main(String[] args)
    {
        F2D Engine = new F2D ();
        Engine.initGame();
    }


    public void initGame ()
    {
        this.setBounds(x, y, width, height);
        this.setTitle("Pong - FEngine");
        playerOne = new BatEntity ();
        playerTwo = new BatEntity ();
        ball = new BallEntity();
        initPlayerOne ();
        initPlayerTwo ();
        initBall ();
        this.addKeyListener(this);
        direction = 5;
        System.out.println(direction);
        movingBall = new Thread (new movingBall ());
        movingBall.start();
        ai = new Thread (new AI());
        ai.start();
        this.addWindowListener(this);
        this.setVisible(true);
    }

    @Override
    public void paint (Graphics g)
    {
        g.drawRect(playerOne.getLeft(), playerOne.getTop(), playerOne.getWidth(), playerOne.getHeight());
        g.drawRect(playerTwo.getLeft(), playerTwo.getTop(), playerTwo.getWidth(), playerTwo.getHeight());
        g.drawOval(ball.getLeft(), ball.getTop(), ball.getWidth(), ball.getHeight());
    }

    public void initPlayerOne ()
    {
        playerOne.setLeft(10);
        playerOne.setTop(225);
        playerOne.setWidth(10);
        playerOne.setHeight(50);
    }

    public void initPlayerTwo ()
    {
        playerTwo.setLeft(780);
        playerTwo.setTop(225);
        playerTwo.setWidth(10);
        playerTwo.setHeight(50);

    }

    public void initBall ()
    {
        ball.setHeight(20);
        ball.setWidth(20);
        ball.setLeft(390);
        ball.setTop(230);
    }

    public void keyTyped(KeyEvent e) { }

    public void keyPressed(KeyEvent e)
    {
        int keyCode = e.getKeyCode();


        int tempMove=playerOne.getTop();

        
        switch(keyCode)
        {
            case java.awt.event.KeyEvent.VK_UP:
            tempMove-=15;
            break;
        case java.awt.event.KeyEvent.VK_DOWN:    
            tempMove+=15;
            break;
        }
        if (tempMove >= 445 || tempMove <= 25)
        {
            if (tempMove > 445)
            {
                tempMove = 445;
            }
            if (tempMove < 25)
            {
                tempMove = 25;
            }
        }
        playerOne.setTop(tempMove);
        this.repaint();
    }

    public void keyReleased(KeyEvent e) { }

    public void windowOpened(WindowEvent e) { }

    public void windowClosing(WindowEvent e)
    {
        System.exit(0);
    }

    public void windowClosed(WindowEvent e) { }

    public void windowIconified(WindowEvent e) { }

    public void windowDeiconified(WindowEvent e) { }

    public void windowActivated(WindowEvent e) { }

    public void windowDeactivated(WindowEvent e) { }

    public class movingBall implements Runnable
    {

        
        int distance=10;
        int counter =0;
        int speed=100;


        public void run ()
        {

        //=====DIRECTIONS=====
        //1 north
        //2 south
        //3 east
        //4 west
        //5 north east
        //6 north west
        //7 south east
        //8 south west

        int dist1=10;
        int dist2=5;

        //continue till the program ends
        while(true)
        {
            //just a temporary variable for storing intermediate ball position
            int tempTop=0;
            int tempLeft=0;
            //check and see which direction the ball is going
            //if direction is set to go up i.e. 2 then subtract distance
            //if direction is set to go down then add distance
            switch(direction)
            {
            case 1:    //north
                tempTop=ball.getTop()-dist1;
                break;
            case 2 : //south
                tempTop=ball.getTop()+dist1;
                break;
            case 3:    //east
                tempTop=ball.getLeft()+dist2;
                break;
            case 4:    //west
                tempTop=ball.getLeft()-dist2;
                break;
            case 5:    //north east
                tempTop=ball.getTop()-dist1;
                tempLeft=ball.getLeft()+dist2;
                break;
            case 6:    //north west
                tempTop=ball.getTop()-dist1;
                tempLeft=ball.getLeft()-dist2;
                break;
            case 7:    //south east
                tempTop=ball.getTop()+dist1;
                tempLeft=ball.getLeft()+dist2;
                break;
            case 8:    //south west
                tempTop=ball.getTop()+dist1;
                tempLeft=ball.getLeft()-dist2;
                break;
            }
            //update the balls position in the datastructure
            ball.setTop(tempTop);
            ball.setLeft(tempLeft);
           
            //=====TOP COLLISIONS=====
            if(ball.getTop()<=20 &&  direction==5)
                //coming north east now go south east
                direction=7;
            if(ball.getTop()<=20 && direction==6)
                //coming north west now go south west
                direction=8;
            //=====BOTTOM COLLISIONS=====
            if(ball.getTop()>=480 && direction==7)
                //coming south east now go north east
                direction=5;
            if(ball.getTop()>=480 && direction==8)
                //coming north east now go south east
                direction=6;
            //====LEFT COLLISIONS=====
            if(ball.getLeft() <=0 && direction == 6)
                //coming north west going north east
                direction=5;
            if(ball.getLeft() <=0 && direction == 8)
                //coming south west going south east
                direction=7;
            //=====RIGHT COLLISIONS=====
            if(ball.getLeft() >=780 && direction == 5)
                //coming north east going north west
                direction=6;
            if(ball.getLeft() >=780 && direction == 7)
                //coming south east going south west
                direction=8;
            //=====BAT (LEFT) COLLISIONS=====
            if ((ball.getLeft() <= playerOne.getLeft()+20 && playerOne.getTop()<=ball.getTop() && ball.getTop()>=playerOne.getTop()+50) && direction == 8)
                {//coming south west going south east
                direction=7;
                counter++;
                }
            if ((ball.getLeft() <= playerOne.getLeft()+20 && playerOne.getTop()<=ball.getTop() && ball.getTop()>=playerOne.getTop()+50) && direction == 6)
                {//coming north west going north east
                direction=5;
                counter++;
                }
            //=====BAT (RIGHT) COLLISIONS=====
            if ((ball.getLeft() >= playerTwo.getLeft()-10 && playerTwo.getTop()<=ball.getTop() && ball.getTop()>=playerTwo.getTop()+50) && direction == 5)
                {//coming south east going south west
                direction=6;
                counter++;
                }
            if ((ball.getLeft() >= playerTwo.getLeft()-10 && playerTwo.getTop()<=ball.getTop() && ball.getTop()>=playerTwo.getTop()+50) && direction == 7)
                {//coming south east going south west
                direction=5;
                counter++;
                }
            //redraw the ball at new locations
            repaint();
            //Increases Speed of the Game when the Ball hits a Bat
            if (counter <= 90)
            {
                speed = speed - counter;
               //System.out.println (speed);
            }
            //putting the thread to sleep so we can create some delay
            //so the ball's motion looks natural.
            try
            {
                Thread.sleep(speed);
            }
            catch(Exception e)
            {
                System.out.println("Error in running thread " + e);
            }
        }
        }
        


    }

    public class AI implements Runnable
    {

        int tempTop;

        public void run ()
        {
            while (true)
            {
                if (direction == 5 || direction == 7)
                if (ball.getTop()+10 < playerTwo.getTop()+25)
                {
                    tempTop = playerTwo.getTop();
                    tempTop-= 15;
                    playerTwo.setTop(tempTop);
                }
                if (ball.getTop()+10 > playerTwo.getTop()+25)
                {
                    tempTop = playerTwo.getTop();
                    tempTop+= 15;
                    playerTwo.setTop(tempTop);
                }

                repaint();
                
                try
                {
                    Thread.sleep(100);
                }
                catch (Exception e)
                {
                    System.out.println("Error in running Thread " + e);
                }
            }
        }
    }


}

Grüße Kai
 
Hmm deine Zugriffe auf die Methoden die von den Threads verwendet werden, sollten synchronized sein, damit immer der aktuelle Wert gelesen werden kann.
 
Hab ich jetzt gemacht, aber nach dem ich den Bat vom AI mal per hand gesteuert und trotzdem gab es keine Kollision, also hab ich mal den Debugger bemüht und festgestellt, dass er überhaupt nicht in den entsprechenden if-Block kommt. Komisch, allerdings weis ich nicht, was an den Bedingungen falsch sein soll.

Hoffe ihr findets, werde jetzt erstmal an anderer Stellen an dem Game weiterschreiben.

Grüße Kai
 
Kein Wunder dass das ganze nicht funktioniert hat. Benutze niemals irgendwelche ints um dir einen Status zu merken. Das mit den directions konnte so ja nur schief gehen. Bei dem Ball um dem Player ist es auch sinnvoll noch getRight und getBottom Methoden hinzuzufügen. Deine Berechnungen sollten auch nicht mit festen Zahlen arbeiten, da du sonst nie die Größe von Player und Ball ändern kannst ohne alles umzuschreiben. So ist es einigermaßen lesbar und funktioniert. Noch nicht super aber du sollst ja auch noch was zu tun haben :D

Java:
package de;

import java.awt.Frame;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.util.concurrent.TimeUnit;

public class F2D extends Frame implements KeyListener, WindowListener {

	private static final int PLAYER_STEP = 15;
	// Main Variables
	int x = 0;
	int y = 0;
	int height = 500;
	int width = 800;

	BatEntity playerOne;
	BatEntity playerTwo;
	BallEntity ball;
	Direction direction;
	Image dbImage;
	Graphics dbg;
	Thread movingBall;
	Thread ai;

	public static void main(String[] args) {
		F2D Engine = new F2D( );
		Engine.initGame( );
	}

	public void initGame() {
		this.setBounds(x, y, width, height);
		this.setTitle("Pong - FEngine");
		playerOne = new BatEntity( );
		playerTwo = new BatEntity( );
		ball = new BallEntity( );
		initPlayerOne( );
		initPlayerTwo( );
		initBall( );
		this.addKeyListener(this);
		direction = Direction.NORTH_EAST;
		System.out.println(direction);
		movingBall = new Thread(new MovingBall( ));
		movingBall.start( );
		ai = new Thread(new AI( ));
		ai.start( );
		this.addWindowListener(this);
		this.setVisible(true);
	}

	@Override
	public void paint(Graphics g) {
		g.drawRect(playerOne.getLeft( ), playerOne.getTop( ), playerOne
				.getWidth( ), playerOne.getHeight( ));
		g.drawRect(playerTwo.getLeft( ), playerTwo.getTop( ), playerTwo
				.getWidth( ), playerTwo.getHeight( ));
		g.drawOval(ball.getLeft( ), ball.getTop( ), ball.getWidth( ), ball
				.getHeight( ));
	}

	public void initPlayerOne() {
		playerOne.setLeft(10);
		playerOne.setTop(225);
		playerOne.setWidth(10);
		playerOne.setHeight(50);
	}

	public void initPlayerTwo() {
		playerTwo.setLeft(780);
		playerTwo.setTop(225);
		playerTwo.setWidth(10);
		playerTwo.setHeight(50);

	}

	public void initBall() {
		ball.setHeight(20);
		ball.setWidth(20);
		ball.setLeft(390);
		ball.setTop(230);
	}

	public void keyTyped(KeyEvent e) {
	}

	public void keyPressed(KeyEvent e) {
		int keyCode = e.getKeyCode( );

		int tempMove = playerOne.getTop( );

		switch (keyCode) {
			case java.awt.event.KeyEvent.VK_UP:
				tempMove -= PLAYER_STEP;
				break;
			case java.awt.event.KeyEvent.VK_DOWN:
				tempMove += PLAYER_STEP;
				break;
		}

		int minMove = 0;
		int maxMove = height - playerOne.getHeight( );
		if (tempMove >= maxMove || tempMove <= minMove) {
			if (tempMove > maxMove) {
				tempMove = maxMove;
			}
			if (tempMove < minMove) {
				tempMove = minMove;
			}
		}
		playerOne.setTop(tempMove);
		this.repaint( );
	}

	public void keyReleased(KeyEvent e) {
	}

	public void windowOpened(WindowEvent e) {
	}

	public void windowClosing(WindowEvent e) {
		System.exit(0);
	}

	public void windowClosed(WindowEvent e) {
	}

	public void windowIconified(WindowEvent e) {
	}

	public void windowDeiconified(WindowEvent e) {
	}

	public void windowActivated(WindowEvent e) {
	}

	public void windowDeactivated(WindowEvent e) {
	}

	public class MovingBall implements Runnable {

		private static final int COUNTER_THRESHOLD = 90;
		private static final int INITIAL_SPEED = 100;
		int distance = 10;
		int counter = 0;
		int speed = INITIAL_SPEED;

		public void run() {

			int dist1 = 10;
			int dist2 = 5;

			// continue till the program ends
			while (true) {
				int tempCounter = counter;
				// just a temporary variable for storing intermediate
				// ball position
				int tempTop = 0;
				int tempLeft = 0;
				// check and see which direction the ball is going
				// if direction is set to go up i.e. 2 then subtract
				// distance
				// if direction is set to go down then add distance
				
				if(direction.isNorth( )){
					tempTop = ball.getTop( ) - dist1;
				}
				if(direction.isSouth( )){
					tempTop = ball.getTop( ) + dist1;
				}
				if(direction.isWest( )){
					tempLeft = ball.getLeft( ) - dist2;
				}
				if(direction.isEast( )){
					tempLeft = ball.getLeft( ) + dist2;
				}
				// update the balls position in the datastructure
				ball.setTop(tempTop);
				ball.setLeft(tempLeft);

				int ballLeftCollision = x;
				int ballRightCollision = width - ball.getWidth( );
				int ballTopCollision = y + ball.getHeight( );
				int ballBottomCollision = height;

				// =====TOP COLLISIONS=====
				if (direction.isNorth( ) && ball.getTop( ) <= ballTopCollision) {
					direction = direction.toggleNorthSouth( );
				}
				// =====BOTTOM COLLISIONS=====
				if (direction.isSouth( )
						&& ball.getBottom( ) >= ballBottomCollision) {
					// coming south east now go north east
					direction = direction.toggleNorthSouth( );
				}
				// ====LEFT COLLISIONS=====
				if (direction.isWest( ) && ball.getLeft( ) <= ballLeftCollision) {
					// coming north west going north east
					direction = direction.toggle( );
				}
				// =====RIGHT COLLISIONS=====
				if (direction.isEast( )
						&& ball.getRight( ) >= ballRightCollision) {
					// coming north east going north west
					direction = direction.toggle( );
				}

				// =====BAT (LEFT) COLLISIONS=====
				if (direction.isWest( )
						&& ball.getLeft( ) <= playerOne.getRight( )
						&& ball.getTop( ) >= playerOne.getTop( )
						&& ball.getTop( ) <= playerOne.getBottom( )) {
					direction = direction.toggle( );
					tempCounter++;
				}
				// =====BAT (RIGHT) COLLISIONS=====
				if (direction.isEast( )
						&& ball.getRight( ) >= playerTwo.getLeft( )
						&& ball.getTop( ) >= playerTwo.getTop( )
						&& ball.getTop( ) <= playerTwo.getBottom( )) {
					// coming
					// south
					// east
					// going
					// south west
					direction = direction.toggle( );
					tempCounter++;
				}

				// redraw the ball at new locations
				repaint( );
				// Increases Speed of the Game when the Ball hits a
				// Bat
				if (tempCounter != counter && tempCounter <= COUNTER_THRESHOLD) {
					speed = INITIAL_SPEED - tempCounter;
					// System.out.println (speed);
				}
				if (speed < 0) {
					speed = 0;
				}

				counter = tempCounter;

				// putting the thread to sleep so we can create some
				// delay
				// so the ball's motion looks natural.
				try {
					TimeUnit.MILLISECONDS.sleep(speed);
				}
				catch (Exception e) {
					System.out.println("Error in running thread " + e);
				}
			}
		}

	}

	public class AI implements Runnable {

		int tempTop;

		public void run() {
			while (true) {
				int ballTop = ball.getHeight( ) / 2;
				int playerTop = playerTwo.getHeight( ) / 2;

				if (direction == Direction.NORTH_EAST
						|| direction == Direction.SOUTH_EAST) {
					if (ball.getTop( ) + ballTop < playerTwo.getTop( )
							+ playerTop) {
						tempTop = playerTwo.getTop( ) - PLAYER_STEP;
					}
				}
				if (ball.getTop( ) + ballTop > playerTwo.getTop( ) + playerTop) {
					tempTop = playerTwo.getTop( ) + PLAYER_STEP;
				}

				int minMove = 0;
				int maxMove = height - playerTwo.getHeight( );
				if (tempTop >= maxMove || tempTop <= minMove) {
					if (tempTop > maxMove) {
						tempTop = maxMove;
					}
					if (tempTop < minMove) {
						tempTop = minMove;
					}
				}

				playerTwo.setTop(tempTop);

				repaint( );

				try {
					TimeUnit.MILLISECONDS.sleep(100);
				}
				catch (Exception e) {
					System.out.println("Error in running Thread " + e);
				}
			}
		}
	}

}

Java:
package de;

public enum Direction {
	NORTH, SOUTH, EAST, WEST, NORTH_EAST, NORTH_WEST, SOUTH_EAST, SOUTH_WEST;

	public boolean isNorth() {
		return this == NORTH || this == NORTH_EAST || this == NORTH_WEST;
	}

	public boolean isSouth() {
		return this == SOUTH || this == SOUTH_EAST || this == SOUTH_WEST;
	}

	public boolean isWest() {
		return this == WEST || this == SOUTH_WEST || this == NORTH_WEST;
	}

	public boolean isEast() {
		return this == EAST || this == SOUTH_EAST || this == NORTH_EAST;
	}

	public Direction toggleNorthSouth() {
		if (isNorth( )) {
			if (this == NORTH_EAST) {
				return SOUTH_EAST;
			}
			else {
				return SOUTH_WEST;
			}
		}
		else {
			if (this == SOUTH_EAST) {
				return NORTH_EAST;
			}
			else {
				return NORTH_WEST;
			}
		}
	}

	public Direction toggleEastWest() {
		if (isWest( )) {
			if (this == NORTH_WEST) {
				return NORTH_EAST;
			}
			else {
				return SOUTH_EAST;
			}
		}
		else {
			if (this == NORTH_EAST) {
				return NORTH_WEST;
			}
			else {
				return SOUTH_WEST;
			}
		}
	}

	public Direction toggle() {
		return this.toggleNorthSouth( ).toggleEastWest( );
	}
}

Java:
package de;

public class Entity {

	private int left;
	private int top;
	private int width;
	private int height;

	/**
	 * @return the left
	 */
	public synchronized int getLeft() {
		return left;
	}

	/**
	 * @param left
	 *            the left to set
	 */
	public synchronized void setLeft(int left) {
		this.left = left;
	}

	/**
	 * @return the top
	 */
	public synchronized int getTop() {
		return top;
	}

	/**
	 * @param top
	 *            the top to set
	 */
	public synchronized void setTop(int top) {
		this.top = top;
	}

	/**
	 * @return the width
	 */
	public synchronized int getWidth() {
		return width;
	}

	/**
	 * @param width
	 *            the width to set
	 */
	public synchronized void setWidth(int width) {
		this.width = width;
	}

	/**
	 * @return the height
	 */
	public synchronized int getHeight() {
		return height;
	}

	/**
	 * @param height
	 *            the height to set
	 */
	public synchronized void setHeight(int height) {
		this.height = height;
	}

	public synchronized int getRight() {
		return this.left + this.width;
	}

	public synchronized int getBottom() {
		return this.top + this.height;
	}
}

Java:
package de;

public class BallEntity extends Entity {

}

Java:
package de;

public class BatEntity extends Entity {

}
 
Sorry, aber irgendwie verstehe ich die Klasse Direction noch nicht so ganz. Könntest du da vielleicht noch ein, zwei Worte drüber los werden?

Schönen Nachmittag noch, Kai
 
Okay, danke ich hab den Fehler gefunden, der mich verzeifeln ließ und zwar habe ich bei RIGHT,LEFT,BAT(LEFT),BAT(RIGHT) den Methodenaufruf direction.toogle() mit directio.toogleEastWest() eresetzt, denn vorher ist der Ball in die Richtung abgeprallt, aus der er kam, weil mit direction.toogle() erst toogleNorthSouth() aufgerufen wurde und er dann in die Schleife

Java:
if (this == SOUTH_EAST)
            {
                return NORTH_EAST;
            }
            else
            {
                return NORTH_WEST;
            }

reinlief und wieder nach oben abprallte.

Ich hoffe du verstehst was ich meine :D
 
Zurück