Computer Science Canada

Ready to Program Java Pong Game moving paddles help

Author:  FigNeutron [ Fri Jun 12, 2015 10:07 pm ]
Post subject:  Ready to Program Java Pong Game moving paddles help

Hi, im trying to get my paddle to move up and down, and wondered if someone can tell me why its only moving once and then stops, i will paste my code for my main game class and my paddle class.

I am very new to java, this being my first non hsa console game, all help is greatly appreciated


Java:

import javax.swing.*;
import java.awt.*;
import java.awt.image.*;
import java.awt.event.*;
import usefulstuff.*;
import java.util.*;


public class Pong
{
    public static void main (String[] args)
    {
        JFrame j = new JFrame ("Patrick's Pong");
        j.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
        j.getContentPane ().add (new PongPanel (), BorderLayout.CENTER);
        j.setSize (800, 500);
        j.setVisible (true);

        // Loading l = new Loading ();
        // l.splashScreen ();
        // l.mainMenu ();




    }
}

class PongPanel extends JPanel implements KeyListener, ActionListener, MouseListener
{
    private javax.swing.Timer timer;


    int i = 0;
    int state = 1;
    ImageShape Play;
    ImageShape Pong;

    paddle p1;
    paddle p2;
    ball b;



    public PongPanel ()
    {
        setBackground (Color.black);
        Play = new ImageShape (360, 200, 75, "photos/startbutton.jpg");
        Pong = new ImageShape (1, 1, 800, "photos/mainmenu.jpg");

        p1 = new paddle ();
        p2 = new paddle ();
        b = new ball ();


        // m = new UFO ();
        // m.setPosition (300, 50);
        // m.setColor (Color.green);
        // m.setSize (50);

        timer = new javax.swing.Timer (1, this); // instantiate the timer, set the delay, and add the Listener
        timer.start ();

        addKeyListener (this);
        addMouseListener (this);
    }


    public void paintComponent (Graphics g)
    {
        super.paintComponent (g);


        if (state == 1)
        {
            Pong.draw (g);
            g.setColor (Color.white);
            g.drawString ("Loading...", 15, 15);
            g.setColor (Color.green);

            g.fillRect (0, 400, i, 200);
        }

        else if (state == 2)
        {
            g.setColor (Color.white);
            g.setFont (new Font ("calibri", 1, 60));
            g.drawString ("Main Menu!!!", 225, 60);
            Play.draw (g);

            g.setColor (Color.white);
            g.setFont (new Font ("Arial", 1, 30));
            g.drawString ("Player on the right is Player 1, use arrow keys", 5, 350);
            g.drawString ("Player on the left is Player 2, use 'w' and 's' keys", 5, 385);
            g.drawString ("First player to score 10 Wins!", 5, 420);


        }

        else if (state == 3)
        {

            // p1.setPosition (770, 250);
            p1.draw (g);

        }


        // //g.clear ();
        // g.drawString ("Main Menu", 15,30);
        //
        // // m.draw (g);
        requestFocus ();
    }






    // To fulfill our obligations as a KeyListener, we implement the following...
    public void keyPressed (KeyEvent e)
    {
    }


    public void keyReleased (KeyEvent e)
    {
   
   
   
        if (e.getKeyChar () == 'w') // up
        {
            p2.setPosition (p1.getX (), p1.getY () - 10);
        }
        else if (e.getKeyChar () == 's') // down
        {
            p2.setPosition (p1.getX (), p1.getY () + 10);
           
        }
        if (e.getKeyCode () == KeyEvent.VK_UP) // up
        {
            p1.setPosition (p1.getX (), p1.getY () - 10);
           
        }
        else if (e.getKeyChar () == KeyEvent.VK_DOWN) // down
        {
            p1.setPosition (p1.getX (), p1.getY () + 10);
           
        }
        repaint ();
   
    }


    public void keyTyped (KeyEvent e)
    {

    }


    public void actionPerformed (ActionEvent e)
    {



        if (e.getSource () == timer)
        {
            i = i+1;
            if (i > 800)
            {
                timer.stop ();
                state = 2;
            }


        }

        repaint ();
    }


    // To fulfill our obligations as a MouseListener, we implement the following...
    public void mouseClicked (MouseEvent e)
    {

        if (Play.contains (e.getX (), e.getY ()))
            state = 3;





        repaint ();

    }



    public void mousePressed (MouseEvent e)
    {
    }


    public void mouseReleased (MouseEvent e)
    {
    }


    public void mouseEntered (MouseEvent e)
    {

    }


    public void mouseExited (MouseEvent e)
    {

    }
}


and the paddle class...

Java:

package usefulstuff;
/**
 * paddle Class (Grade 11)
 *
 * @author Patrick
 * @version 1
 */

import java.awt.*;
import javax.swing.*;
import hsa.Console;

public class paddle
{
    // INSTANCE VARIABLES
    private int h; // height of the mine
    private int x; //x coordinate of the upper left corner of the line
    private int y; //y coordinate of the upper left corner of the mine
private Color color; //color of paddle
    /**
     * Constructor for objects of class ImageShape
     */

    public paddle ()
    {
        //declare all initital values
        h = 100;
        x = 100;
        y = 100;
        color = Color.white;
    }



    /**
    * Draws the ImageShape
    *
    * @param g   the Graphics context to draw on
    */

    public void draw (Console c)
    {
        c.setColor (color);
        c.fillRect (x,y,10,50);

       
    }
 public void draw (Graphics g)
    {
        g.setColor (color);
        g.fillRect (x,y,10,80);

       
       
    }

    /**
     * Sets the height of a ImageShape
     *
     * @param  newh   new height of the ImageShape
     */

    public void setHeight (int newh)
    {
        h = newh;
    }


    /**
     * Sets the color of a mine
     *
     * @param  newh   new color of the paddle
     */

    public void setColor (Color newc)
    {
        color = newc;
    }
   
    /**
     * Sets the color of a mine
     *
     * @param  newh   new color of the paddle
     */

    public Color getColor ()
    {
        return color;
    }
   
   
    // public boolean contains (int ptx, int pty)
    // {
    // //if point is withi the square surrounding pacman- return true
    // //if (x< otx < x+h) AND (y < pty < y + h)
    // if (x < ptx < x + h && y
    //
   
   
   
   
   
   
   
   
   
   
   
   
   
   


    /**
    * Returns the height of the ImageShape
    *
    * @return  the height
    */

    public int getHeight ()
    {
        return h;
    }


    /**
    *Returns the X of the paddle
    *
    *@return  x coord of the upper left corner of the mine
    */

    public int getX ()
    {
        return h;
    }


    /**
    *Returns the y of the mine
    *
    *@return  the y coordinate of the upper left corner
    */

    public int getY ()
    {
        return h;
    }
   
     /**
    *Returns the center X of the mine
    *
    *@return  the center X
    */

    public int getCenterX ()
    {
        return x+h/2;
    }

     /**
    // *Returns the center Y of the mine
    // *
    // *@return  the center Y
    // */

    // public int getCenterX ()
    // {
    //     return x+h/2;
    // }

    /**
     * Sets the (x,y) coordinates of upper left corner of the ImageShape
     *
     * @param  newx   new x coordinate
     * @param  newy   new y coordinate
     */

    public void setPosition (int newx, int newy)
    {
        x = newx;
        y = newy;

    }






} // ImageShape class


the problem is that the paddle only moves up or down once, and then becomes unresponsive, the 3 states are for my loading, menu, and game screens.
Again, thanks alot. this has been really bugging me, and sorry for any mistakes in advance, first time poster.

Author:  Zren [ Fri Jun 12, 2015 11:25 pm ]
Post subject:  RE:Ready to Program Java Pong Game moving paddles help

What's wrong with this picture?

Posted Image, might have been reduced in size. Click Image to view fullscreen.

Author:  FigNeutron [ Fri Jun 12, 2015 11:28 pm ]
Post subject:  RE:Ready to Program Java Pong Game moving paddles help

OMG ahahhaha, thanks for that, i cant believe i kept missing that, and to whoever arranged my code to picture format, is there a link explaining how to do that?

Author:  Zren [ Fri Jun 12, 2015 11:31 pm ]
Post subject:  RE:Ready to Program Java Pong Game moving paddles help

You wrap code like so:

code:

[syntax="java"]
class Hello {}
[/syntax]

Author:  FigNeutron [ Fri Jun 12, 2015 11:33 pm ]
Post subject:  RE:Ready to Program Java Pong Game moving paddles help

Ok, i fixed the above^^ picture in my code, but that did not solve the problem, the rectangle moves up 10 once, but then wont respond, and again sorry, im a real noob

--ok, thanks for letting me know about the wrapping

Author:  FigNeutron [ Fri Jun 12, 2015 11:40 pm ]
Post subject:  RE:Ready to Program Java Pong Game moving paddles help

oh....just realized i have key char instead of code...

-- so now it can move up or down (lol) but still only once, i know this is a waste of all your time, but id appreciate if someone could show me why this is, and thanks for the help so far!

Author:  FigNeutron [ Sun Jun 14, 2015 4:22 pm ]
Post subject:  RE:Ready to Program Java Pong Game moving paddles help

i figured it out guys, had to set position variables and add or subtract from them, thanks anyways


: