/** * @(#)pong.java * * * @author Michael Cecconet * @version 1.00 2007/5/2 */ import java.applet.*; import java.awt.*; import java.awt.event.*; import java.util.Random; // Java graphics really needs to stop following Window's procedure and use the Cartesian plane public class pong extends Applet implements Runnable{ int WIDTH, HEIGHT, PADDLESIZE;//Width and height of the applet, and Paddle Size int oneScore, twoScore; private Image dbImage; private Graphics dbg; ball firstBall; paddle player1, player2; //Distance method for later reference public int distance(int pointX1, int pointY1, int pointX2,int pointY2){ int temp = (int) Math.sqrt((pointX2 - pointX1)^2 + (pointY2 - pointY1)^2); return temp; } //Ball Class public class ball{ private int ballX, ballY, ballRadius;//Ball Position private double ballAngle, ballSpeed; //Ball Direction and Speed //Constructor Method public ball(int getX, int getY,int getSize, double getAngle, double getSpeed){ ballX=getX; ballY=getY; ballRadius=getSize; ballAngle=getAngle; ballSpeed=getSpeed; } //Reset Ball public void reset(){ Random generator = new Random(); ballX=WIDTH/2; ballY=HEIGHT/2; ballAngle=generator.nextInt(360); ballSpeed=generator.nextInt(20); } //Move Method public void move(){ ballX++; } //Collison Detection (Recreate new angle, and return if hit) public boolean collide(int pointX1, int pointY1, int pointX2,int pointY2, boolean changeSpeed){ //Creates the new direction for the ball on collision with a line, has the option to change speed or not(Wall/Paddle Collision) int lineLength = distance(pointX1, pointY1, pointX2, pointY2);// double deltaX, deltaY;//Change between X and Y coordinates by increments in the distance. int tempX,tempY;//Linear points deltaX = (pointX2-pointX1)/lineLength; deltaY = (pointY2-pointY1)/lineLength; for (int i=0; i WIDTH) ? 1:0; if (ballX < 0 || ballX > WIDTH){ reset(); repaint(); System.out.println("YEAYE"); } } } //Paddle Class public class paddle{ private int x, y;//X(cons), Y coordinates (centered) private int keyUp,keyDown;// Changeable Keys for Controlling the Paddle. private int SIZE;//Size(cons)(extending outwards from center) //Constructor Method public paddle(int getX, int getY, int getUp, int getDown, int getSize){ x=getX; y=getY; SIZE=getSize; keyUp=getUp; keyDown=getDown; } //Move Method (Handles KeyBoard Events) public void move(){ y++; } } //Draw Everything public void paint(Graphics g){ g.setColor( Color.red ); g.fillRect( 1, 1, WIDTH, HEIGHT);//Background g.setColor(Color.white) ; g.fillOval(firstBall.ballX, firstBall.ballY, firstBall.ballRadius, firstBall.ballRadius);//Ball //g.drawLine(player1.x, player1.y+player1.SIZE,player1.x, player1.y-player1.SIZE); //g.drawLine(player2.x, player2.y+player2.SIZE,player2.x, player2.y-player2.SIZE); //g.FillRect doesn't feel like being useful. So improvise by making my own fillRect for(int i=0;i>5;i++){ g.setColor(Color.blue); g.drawLine(player1.x-i, player1.y+player1.SIZE,player1.x-i, player1.y-player1.SIZE); g.drawLine(player2.x+i, player2.y+player2.SIZE,player2.x+i, player2.y-player2.SIZE); SYstem.out.println("FUCK"); } } //Double Buffering, as provided by the JavaBoutique Tutorial. public void update (Graphics g){ // initialize buffer if (dbImage == null){ dbImage = createImage (WIDTH, HEIGHT); dbg = dbImage.getGraphics (); } // clear screen in background dbg.setColor (getBackground ()); dbg.fillRect (0, 0, WIDTH, HEIGHT); // draw elements in background dbg.setColor (getForeground()); paint (dbg); // draw image on the screen g.drawImage (dbImage, 0, 0, this); } //Initialize all the variables, ball, and paddles. public void init(){ WIDTH = getSize().width; HEIGHT = getSize().height; oneScore=0; twoScore=0; Random generator = new Random(); int angle = generator.nextInt( 360 ); int speed = generator.nextInt(20); firstBall = new ball(WIDTH/2, HEIGHT/2, 10, angle, speed); player1 = new paddle(20, HEIGHT/2, 119, 115, 20); player2 = new paddle(WIDTH-20, HEIGHT/2, 200, 208, 20); //addKeyListener( this ); repaint(); } /* // method to handle key - down events public boolean keyDown (Event e, int key){ // user presses left cursor key if (key == Event.UP){ // changing x - speed so that ball moves to the left side (x_speed negative) x_speed = -1; } // user presses right cursor key else if (key == Event.DOWN){ // changing x - speed so that ball moves to the right side (x_speed positive) x_speed = 1; } // user presses space bar (value = 32!) else if (key == 32){ // Stop ball (x_speed = 0) x_speed = 0; }else{ // Additionally the method prints out the ASCII - value if an other key is pressed. This is not necessary but a possibility for you to test which value a key has. System.out.println ("Charakter: " + (char)key + " Integer Value: " + key); } // DON'T FORGET (although it has no meaning here) return true; } */ public void start(){ Thread game = new Thread (this); game.start(); } public void stop(){ } public void destroy(){ } public void run(){ Thread.currentThread().setPriority(Thread.MIN_PRIORITY); while(true){ //Main Body Loop repaint(); firstBall.move(); firstBall.pointCheck(); firstBall.collide(player1.x, player1.y-player1.SIZE, player1.x, player1.y+player1.SIZE, true); firstBall.collide(player2.x, player2.y-player2.SIZE, player2.x, player1.y+player2.SIZE, true); try { Thread.sleep(20); }catch(InterruptedException ex){ } // set ThreadPriority to maximum value Thread.currentThread().setPriority(Thread.MAX_PRIORITY); } } }