All pastes #755538 Raw Edit

Unbenannt

public text v1 · immutable
#755538 ·published 2007-10-30 19:27 UTC
rendered paste body
package chasingevading;

import javax.swing.*;

import java.awt.*;
import java.awt.event.*;
import java.awt.geom.Line2D;
import java.awt.geom.Ellipse2D;

import java.util.LinkedList;
import java.util.ListIterator;
import java.util.Random;

public class ChasingEvadingPanel extends JPanel implements MouseMotionListener,
		MouseListener, Runnable, KeyListener, MouseWheelListener  {
	private int[][] grid;
	
	private Thread animator;

	//private int prey[] = new int[2];
	private Mobile prey = new Mobile();
	private Mobile hunter = new Mobile();
	
	private boolean running = false;
	private boolean gameOver = false;
	
	private boolean turnLeft = false;
	private boolean turnRight = false;
	private boolean speedUp = false;
	private boolean speedDown = false;
	
	private Graphics dbg;
	private Image dbImage = null;

	//private int hunter[] = new int[2];

	private int width, height;

	public ChasingEvadingPanel(int r, int c) {

		width = r;
		height = c;
/*
		grid = new int[width][height];

		for (int i = 0; i < width; i++) {
			for (int j = 0; j < height; j++)
				grid[i][j] = 0;
		}
*/
		setPreferredSize(new Dimension(r, c));
		addMouseMotionListener(this);
		addMouseListener(this);
		addKeyListener(this);
		addMouseWheelListener(this);
		
		setDoubleBuffered(false);

		Random rand = new Random();

		// lets have a fair start

		int rndx = (int) (rand.nextFloat() * width);
		while (rndx <= 10 || rndx >= width - 10) {
			rndx = (int) (rand.nextFloat() * width);
		}

		int rndy = (int) (rand.nextFloat() * height);
		while (rndy <= 10 || rndy >= height - 10
				|| Math.abs(rndx - hunter.getY()) <= 20) {
			rndy = (int) (rand.nextFloat() * height);
		}

		//prey[0] = rndx;
		//prey[1] = rndy;
		prey = new Mobile(rndx, rndy);
		//grid[prey.getX()][prey.getY()] = 1;

		System.out.println("Prey is at: " + rndx + " and " + rndy);

	}
	
	public void startGame() {
		System.out.println("in method startGame");
		if (animator == null || !running) {
			System.out.println("New animator Thread about to start");
			animator = new Thread(this);
			System.out.println("New animator Thread started");
			animator.start();
		}
	}
	
	public void stopGame() {
		running = false;
	}
	
	public void run() {
		running = true;
		while (running) {
			gameUpdate();
			gameRender();
			repaint();
		}
		try {
			Thread.sleep(20);
		}
		catch (InterruptedException ex) {}
	}
	
	public void gameUpdate() {
		
		prey.setX(prey.getX() + prey.getSpeed()*Math.cos(prey.getDir())*0.01);
		prey.setY(prey.getY() + prey.getSpeed()*Math.sin(prey.getDir())*0.01);
		
		hunter.setX(hunter.getX() + hunter.getSpeed()*Math.cos(hunter.getDir())*0.01);
		hunter.setY(hunter.getY() + hunter.getSpeed()*Math.sin(hunter.getDir())*0.01);
		
		//hunter is always faster than the prey
		hunter.setSpeed(prey.getSpeed()+1);
		
		if(turnLeft == true)
			prey.setDir(prey.getDir()-(float)0.005);
		
		if(turnRight == true)
			prey.setDir(prey.getDir()+(float)0.005);
		
		if(speedUp == true) {
			prey.setSpeed(prey.getSpeed()+1);
			speedUp = false;
		}
		
		if(speedDown == true) {
			if(prey.getSpeed() <= 1) {
				prey.setSpeed(1);
				speedDown = false;
			}
			else {
				prey.setSpeed(prey.getSpeed()-1);
				speedDown = false;
			}
			
		}
		
		
		
		
		
		float xNeu = prey.getX()-hunter.getX();
		System.out.println("X Position of prey: " + prey.getX());
		System.out.println("X Position of hunter: " + hunter.getX());
		System.out.println("X Position difference: " + xNeu);
		float yNeu = prey.getY()-hunter.getY();
		
		/*
		double phi = 0;
		
		if(xNeu != 0) {
			phi = Math.atan(yNeu/xNeu);
		}
		
		System.out.println("Phi:" + phi);
		*/
		
		float relX = (float)(Math.cos(hunter.getDir())*xNeu+Math.sin(hunter.getDir())*yNeu);
		System.out.println("RelX:" + relX);
		float relY = (float)(Math.sin(hunter.getDir())*(-1)*xNeu+Math.cos(hunter.getDir())*yNeu);
		System.out.println("RelY:" + relY);
		System.out.println("Hunter Direction:" + hunter.getDir());
		
		if(relX > 0)
			hunter.setDir(hunter.getDir()-(float)0.05);
		
		else if(relX < 0)
			hunter.setDir(hunter.getDir()+(float)0.05);
		/*
		else if (relX >= 0 && relY < 0)
			hunter.setDir(hunter.getDir()-(float)0.005);
		
		else if (relX < 0 && relY < 0)
			hunter.setDir(hunter.getDir()-(float)0.005);
		*/
		
		

	}
	
	public void gameRender() {
		if (dbImage == null) {
			dbImage = createImage(width, height);
			if(dbImage == null) {
				System.out.println("cannot create dbImage");
				return;
			}
			else
				dbg = dbImage.getGraphics();
		}
		dbg.setColor(Color.yellow);
		dbg.fillRect(0,0,width,height);
		
		dbg.setColor(Color.red);
		
		dbg.fillOval(Math.round(hunter.getX()) - 7, Math.round(hunter.getY()) - 7, 15, 15);
		
		dbg.drawLine(20, 20, (int)Math.round(20+15*Math.cos(hunter.getDir())), (int)Math.round(20+15*Math.sin(hunter.getDir())));
		dbg.drawLine(20, 20, (int)Math.round(20+15*Math.cos(hunter.getDir()+90*(float)Math.PI/180)), (int)Math.round(20+15*Math.sin(hunter.getDir()+90*(float)Math.PI/180)));

		
		dbg.setColor(Color.black);
		
		// lets give the player some aid to see the direction
		dbg.drawLine(Math.round(hunter.getX()), Math.round(hunter.getY()), (int)Math.round(Math.round(hunter.getX())+8*Math.cos(hunter.getDir())), (int)Math.round(Math.round(hunter.getY())+8*Math.sin(hunter.getDir())));
		
		dbg.setColor(Color.blue);

		dbg.fillOval(Math.round(prey.getX()) - 7, Math.round(prey.getY()) - 7, 15, 15);
		
		dbg.drawLine(60, 20, (int)Math.round(60+15*Math.cos(prey.getDir())), (int)Math.round(20+15*Math.sin(prey.getDir())));
		dbg.drawLine(60, 20, (int)Math.round(60+15*Math.cos(prey.getDir()+90*(float)Math.PI/180)), (int)Math.round(20+15*Math.sin(prey.getDir()+90*(float)Math.PI/180)));
		
		dbg.setColor(Color.black);
		
		// lets give the player some aid to see the direction
		dbg.drawLine(Math.round(prey.getX()), Math.round(prey.getY()), (int)Math.round(Math.round(prey.getX())+8*Math.cos(prey.getDir())), (int)Math.round(Math.round(prey.getY())+8*Math.sin(prey.getDir())));
	

		
		if (gameOver) {
			gameOverMessage(dbg);
		}
	}
	
	  public void repaint()
	  { 
	    Graphics g;
	    try {
	      g = this.getGraphics();
	      if ((g != null) && (dbImage != null))
	        g.drawImage(dbImage, 0, 0, null);
	      Toolkit.getDefaultToolkit().sync();  // sync the display on some systems
	      g.dispose();
	    }
	    catch (Exception e)   // quite commonly seen at applet destruction
	    { System.out.println("Graphics Context error: " + e);  }
	  } // end of paintScreen()
	
	public void gameOverMessage(Graphics g) {
		g.drawString("You SUCK!", 10, 10);
	}

	public void paint(Graphics g) {
		System.out.println(System.currentTimeMillis());
		Graphics2D g2 = (Graphics2D) g;
/*
		g2.setPaint(Color.yellow);

		g2.fill(new Rectangle(0, 0, width, height));

		g2.setPaint(Color.red);

		g2.fillRoundRect(hunter.getX() - 7, hunter.getY() - 7, 15, 15, 3, 3);

		g2.setPaint(Color.green);

		g2.fillRoundRect(prey.getX() - 7, prey.getY() - 7, 15, 15, 3, 3);

*/
	}

	public void mouseClicked(MouseEvent event) {

	}

	public void mouseEntered(MouseEvent e) {
	}

	public void mouseExited(MouseEvent e) {
	}

	public void mousePressed(MouseEvent e) {
		System.out.println(e.getButton());
		if(e.getButton()==1) {
			turnLeft = true;
			
		}
		if(e.getButton()==3) {
			turnRight = true;
			//prey.setDir(prey.getDir()-(float)0.5);
		}
	}

	public void mouseReleased(MouseEvent e) {
		if(e.getButton()==1) {
			turnLeft = false;	
		}
		if(e.getButton()==3) {
			turnRight = false;	
		}
	}

	public void mouseDragged(MouseEvent e) {
		/*speedDown = false;
		speedUp=true;*/
	}

	public void mouseMoved(MouseEvent event) {
		/*speedUp=false;
		speedDown = true;*/
	}
	
    public void mouseWheelMoved(MouseWheelEvent e) {
        int notches = e.getWheelRotation();
        if (notches < 0) {
            System.out.println("Mouse wheel moved UP " + -notches + " notch(es)");
            speedUp = true;
        } else {
        	System.out.println("Mouse wheel moved DOWN " + notches + " notch(es)");
        	speedDown = true;
        	}
        }
	
    public void keyTyped(KeyEvent e) {
    	System.out.println("Key typed");

        }


        public void keyPressed(KeyEvent e) {
        	System.out.println("Key pressed");

        }


        public void keyReleased(KeyEvent e) {
        	System.out.println("Key released");
        }
     
	public void runGame(int startx, int starty) {
		// System.out.println(startx + " " + starty);
		//hunter[0] = startx;
		//hunter[1] = starty;
		hunter.setX(startx);
		hunter.setY(starty);
		//grid[hunter.getX()][hunter.getY()] = 2;

		System.out.println("Hunter is at: " + hunter.getX() + " and " + hunter.getY());
		System.out.println("Before startgame");		
		startGame();
		System.out.println("After startgame");
		//paint(this.getGraphics());
	}
	
	public Mobile getPrey(){
		return prey;
	}
}