Results 1 to 3 of 3
  1. #1
    Shadaw is offline Member
    Join Date
    Dec 2008
    Posts
    4
    Rep Power
    0

    Default Can't find out how to get this method working :)

    I'm programming the game Pong for java practice. I have three classes in the project: ball, paddle, and Pong. I have the game fully functioning, including the ball bouncing off the walls, except I can't get it to bounce off the paddles. This is a school project, and according to my teacher I need a few "if statements" which I will point out in the source code below:

    Pong Class:

    /**
    * @(#)Pong.java
    *
    * Sample Applet application
    *
    * @author Jeff
    * @version 1.00 06/11/02
    */

    import java.awt.*;
    import java.applet.*;
    import java.awt.event.*;


    public class Pong extends Applet implements Runnable, KeyListener
    {
    Thread animation; //declares a thread called animation
    static final int REFRESH_RATE = 50; //Constant for “delay”
    ball myBall = new ball(500,300);
    paddle leftPad = new paddle(20, 20, 20, 20);
    paddle rightPad = new paddle(20, 480, 20, 20);
    boolean bLeftUp=false, bLeftDown=false, bRightUp=false, bRightDown=false;


    //declare other class data members

    public void init()
    {
    //initialize class data members
    setBackground( Color.black);
    addKeyListener(this);
    this.requestFocus();
    }
    public void paint(Graphics g)
    {


    myBall.draw(g);
    leftPad.draw(g);
    rightPad.draw(g);
    }
    public void start()
    {
    animation = new Thread(this);
    if(animation != null)
    {
    animation.start();
    }
    }
    public void run()
    {
    while(true)
    {
    //put code that runs in loop here
    repaint();
    try
    {
    myBall.move();
    //myBall.bounce();//(20, leftPad.yPos(), 480, rightPad.yPos()); The method commented out called myBall.bounce();//(20, etc...) is the line which I have to modify in order for the paddles to function. The parameters are in the paddle class below.
    //Left Paddle;
    myBall.bounce();
    if(bLeftUp)
    {
    leftPad.moveUp();
    }
    else if(bLeftDown)
    {
    leftPad.moveDown();
    }

    //Right Paddle;
    if(bRightUp)
    {
    rightPad.moveUp();
    }
    else if(bRightDown)
    {
    rightPad.moveDown();
    }

    repaint();
    Thread.sleep(REFRESH_RATE);
    }catch(Exception exc){};
    }
    }

    public void stop()
    {
    animation = null;
    }

    //Pick up key pressed
    public void keyPressed (KeyEvent e)
    {
    char key = e.getKeyChar();
    if((key == 'a') || (key == 'A'))
    {
    bLeftUp = true;
    leftPad.moveUp();
    }
    else if((key == 'z') || (key == 'Z'))
    {
    bLeftDown = true;
    leftPad.moveDown();
    }


    if((key == 'k') || (key == 'K'))
    {
    bRightUp = true;
    rightPad.moveUp();
    }
    else if((key == 'm') || (key == 'M'))
    {
    bRightDown = true;
    rightPad.moveDown();
    }

    }

    public void keyReleased (KeyEvent ke)
    {
    char key = ke.getKeyChar();
    if((key == 'a') || (key == 'A'))
    {
    bLeftUp = false;
    }
    else if((key == 'z') || (key == 'Z'))
    {
    bLeftDown = false;
    }

    if((key == 'k') || (key == 'K'))
    {
    bRightUp = false;
    }
    else if((key == 'm') || (key == 'M'))
    {
    bRightDown = false;
    }
    }

    public void keyTyped (KeyEvent ke)
    {

    }

    }


    Ball Class:

    import java.awt.*;
    public class ball
    {
    private int x;
    private int y;
    private boolean bUp;
    private boolean bRight;
    private int distance;
    private int appletWidth;
    private int appletHeight;

    public ball(int myWidth, int myHeight)
    {
    x = 20 + (int)(Math.random()*200);
    y = 20 + (int)(Math.random()*200);
    bUp = false;
    bRight = true;
    distance = 4;
    appletWidth=myWidth;
    appletHeight=myHeight;

    }
    public void move()
    {
    if(bUp == true)
    {
    y+=distance;
    }
    else
    {
    y--;
    }
    if (bRight == true)
    {
    x+=distance;
    }
    else
    {
    x--;
    }

    }
    public void bounce(leftX, leftY, rightX, rightY)
    {
    if (y>=appletHeight || y<=0)
    {
    bUp=!bUp;
    System.out.println("lol bounce");
    }
    if (x>=appletWidth || x<=0)
    {
    bRight=!bRight;
    System.out.println("lol bounce");
    }

    }

    void draw(Graphics g)
    {
    g.setColor(Color.white);
    g.fillOval(x,y,10,10);
    }
    public int getX()
    {
    return x;
    }





    }

    [B]And lastly (sorry so long):
    Paddle Class:[/B

    import java.awt.*;
    public class paddle
    {
    private int x;
    private int y;
    private int appWidth;
    private int appHeight;
    private int distance;

    public paddle(int nXpos, int nYpos, int appletHeight, int appletWidth)
    {
    appHeight=appletHeight;
    appWidth=appletWidth;
    x=nYpos;
    y=nXpos;
    distance=6;
    }
    public void moveUp()
    {
    if (y<=appHeight || y>=0)
    {
    y-=distance;
    }
    }
    public void moveDown()
    {
    if (x<=appWidth || x>=0)
    {
    y+=distance;
    }
    }
    void draw (Graphics g)
    {
    g.setColor(Color.red);
    g.fillRect(x,y,5,15);
    }
    public int yPos()
    {
    return y;
    }
    public int xPos()
    {
    return x;
    }
    }


    NOTE: I temporarily added parameters to the ball class to show what should be there. for the applet to compile correctly you must remove the parameters from public void bounce() in the ball class.

    I hope I'll be able to get some help, but it's a lot of code to look through and (for me at least) a seemingly complex if statement that needs to be inserted :D
    If I was too broad in my depiction of my problem, just ask and I will clear up any questions.
    Last edited by Shadaw; 12-19-2008 at 02:59 AM. Reason: mistake in explanation

  2. #2
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

    Java Code:
    // The compiler won't like this method signature because
    // the parameters do not have any type declarations.
    public void bounce(leftX, leftY, rightX, rightY)
    
    // If the parameter types are all primitive type [i]int[/i]s
    // you could write it like this:
    public void bounce(int leftX, int leftY, int rightX, int rightY)
    To call such a method you would do:
    Java Code:
    int leftX, leftY, rightX, rightY;
    ...
    // values assigned to these declared variables...
    ...
    myBall.bounce(leftX, leftY, rightX, rightY);

  3. #3
    Durnus is offline Member
    Join Date
    Dec 2008
    Posts
    6
    Rep Power
    0

    Default

    I didn't read through all of your code, but given your bounce method I extrapolated.
    PHP Code:
    //Aside from edge bounces, this actually isn't that hard. In other words, assume your ball is square. (That's how pong was anyway, right?)
    	public void bounce(int leftX, int leftY, int rightX, int rightY)
    	{
    		if (y>=appletHeight || y<=0)
    		{
    			bUp=!bUp;
    			System.out.println("lol bounce");
    		}
    		if (x>=appletWidth || x<=0)
    		{
    			bRight=!bRight;
    			System.out.println("lol bounce");
    		}
    		
    		//This'll get if it is past the left bumper, by checking the X vs the right side.
    		if(x < leftX + 5)
    			//Then we check to see if the ball's too high.
    			if(y < leftY)
    				//Then if it's too low.
    				if(y > leftY + 15)
    				{
    					//This means it's a hit on the right side!
    					bRight=!bRight;
    					System.out.println("woot bounce");
    				}
    		//You can put all those ifs in one statement if you want.
    		//Now you just do the same sort of thing with the other paddle, and other sides of the paddle.
    	}

Similar Threads

  1. makeButton method not working
    By ljk8950 in forum AWT / Swing
    Replies: 8
    Last Post: 08-10-2008, 11:20 PM
  2. Replies: 1
    Last Post: 02-16-2008, 10:10 PM
  3. Replies: 2
    Last Post: 02-16-2008, 09:52 PM
  4. problems to find the main method
    By christina in forum Eclipse
    Replies: 2
    Last Post: 08-06-2007, 08:51 PM
  5. Problems with Find method in EJB
    By Nick15 in forum Enterprise JavaBeans (EJB)
    Replies: 0
    Last Post: 05-14-2007, 02:29 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •