Results 1 to 7 of 7
  1. #1
    DballerP is offline Member
    Join Date
    Jan 2012
    Posts
    4
    Rep Power
    0

    Default Having small problem with my hockey game

    Hi I'm new to these forums.

    I'm trying to get the player to pass to one another when their x values are the same. Player 2 can pass fine, but player 1 cannot pass to the right ever (but he can pass anywhere else)

    Here's the code:


    else if (key == KeyEvent.VK_P){ //passing
    xspeed=10;
    yspeed=10;
    if (xpuck==bluex && bluex2>bluex){
    if (xspeed>0){
    xspeed=-xspeed;
    yspeed=0;
    }
    xpuck = xpuck + xspeed + 20;
    ypuck = ypuck + yspeed;
    }
    else if (xpuck==bluex && bluex2<bluex){
    if (xspeed>0){
    xspeed=-xspeed;
    yspeed=0;
    }
    xpuck = xpuck + xspeed - 20;
    ypuck = ypuck + yspeed;
    }


    As far as I know it should be working cause the codes are exactly the same except for the opposite values for both players.

    xpuck and ypuck are the positions of the puck
    xspeed and yspeed is how fast the puck moves up and down
    bluex and bluex2 are the x positions for player 1 and 2

    Any help would be appreciated
    Last edited by DballerP; 01-15-2012 at 01:38 AM.

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,308
    Rep Power
    25

    Default Re: Having small problem with my hockey game

    Can you show some output from the program that shows the problem?
    Add some printlns to display the values of the variables, run the program, copy the printed output and add comments describing what is wrong with the values and show what the values of the variables should be.

  3. #3
    DballerP is offline Member
    Join Date
    Jan 2012
    Posts
    4
    Rep Power
    0

    Default Re: Having small problem with my hockey game

    here is the entire program:
    Java Code:
    import java.awt.event.*;
    import javax.swing.*;
    import java.awt.*;
       public class HockeyGame extends JFrame implements KeyListener, Runnable, WindowListener  //, ActionListener
            {
            Thread t;
            int xpuck, ypuck,xspeed,yspeed,gx,gy,redx2,redy2,lg,rg, redscore, bluescore,redx,redy, gx2, gy2, bluex, bluey,bluex2,bluey2,rg2,lg2;
            //int SQUARE_SIZE=10;
            public HockeyGame ( ) {
                }
            
            public static void main ( String [ ] commandLine ) {
                HockeyGame hg=new HockeyGame();
                hg.init();
            }
            
            public void init() {
                t=new Thread(this);
                this.addKeyListener(this);
                this.setFocusable(true);
                setSize(700,700);
                setVisible ( true );
                xpuck = 200;
                ypuck = 80;
                redx=100;
                redy=100;
                xspeed = 5;
                yspeed = 5;
                redx2 = 500;
                redy2 = 100;
                redscore = 0;
                bluescore = 0;
                gx = 300;
                gy = 50;
                lg=gx;
                rg=gx+39;
                gx2=300;
                gy2=610;
                bluex=100;
                bluey=500;
                bluex2=500;
                bluey2=500;
                lg2=gx2;   
                rg2=gx2+30;
                t.start();            
           }  
           public void paint (Graphics g) {
                g.setColor(Color.white);
                
                g.fillRect(1, 1,700 , 700);
                setBackground(Color.lightGray);
                
                g.setColor (Color.black); //Puck
                g.fillOval(xpuck,ypuck,10,10);
                
                g.setColor(Color.orange); //Bounds
                g.fillRect(10, 30, 10, 610);
                g.fillRect(10, 30, 190, 10);
                g.fillRect(400, 30, 190, 10);
                g.fillRect(590, 30, 10, 610);
                g.fillRect(10, 640, 200, 10);
                g.fillRect(400, 640, 200, 10);
                
                
                g.setColor(Color.red);
                g.drawString("RED:  " + redscore, 100,37); //Red Score
                
                g.setColor(Color.blue);
                g.drawString("BLUE: " + bluescore, 500,37); //Blue Score
                
                g.setColor(Color.blue); //Blue Player
                g.fillRect(bluex, bluey, 20, 20);
                
                g.setColor(Color.blue); //Blue Player 2
                g.fillRect(bluex2, bluey2, 20, 20);
                
                g.setColor(Color.red); //Red Player
                g.fillRect(redx, redy, 20, 20);
                 
                g.setColor(Color.yellow); //Net
                g.fillRect(200, 10, 200, 40);
                
                g.setColor(Color.yellow); //Net 2
                g.fillRect(200, 630, 200, 40);
                 
                g.setColor(Color.red); //Goalie
                g.fillRect(gx,gy, 40, 20);
                g.setColor(Color.blue); //Goalie2
                g.fillRect(gx2,gy2, 40, 20);
                 
                g.setColor(Color.red); //Red Player 2
                g.fillRect(redx2, redy2, 20, 20);
                 
                //g.fillOval(300, 200, 10, 10);
                
                if (xpuck < 30 || xpuck > 575)
                xspeed = -xspeed;
                if (ypuck > 610 || ypuck < 50)
                yspeed = -yspeed;
                int nsize=15;
                if ((xpuck<=redx+nsize && xpuck>=redx-nsize)&&(ypuck>=redy-nsize && ypuck<=redy+nsize)) { //red hold puck
                xpuck=redx;
                ypuck=redy;
                xspeed=0;
                yspeed=0;
                }
                if ((xpuck<=bluex+nsize && xpuck>=bluex-nsize)&&(ypuck>=bluey-nsize && ypuck<=bluey+nsize)) { //blue hold puck
                    xpuck=bluex;
                    ypuck=bluey;
                    xspeed=0;
                    yspeed=0;
                    }
                if ((xpuck<=bluex2+nsize && xpuck>=bluex2-nsize)&&(ypuck>=bluey2-nsize && ypuck<=bluey2+nsize)) { //blue2 hold puck
                    xpuck=bluex2;
                    ypuck=bluey2;
                    xspeed=0;
                    yspeed=0;
                    }
                if ((xpuck<=redx2+nsize && xpuck>=redx2-nsize)&&(ypuck>=redy2-nsize && ypuck<=redy2+nsize)){ //red2 hold puck
                	xpuck=redx2;
                	ypuck=redy2;
                	xspeed=0;
                	yspeed=0;
                }
                if (ypuck <= 40 && xpuck >= 200 && xpuck <= 400){ //Goal
                	bluescore = bluescore + 1;
                	ypuck= 300;
                	xpuck= 300;
                	xspeed=0;
                	yspeed=0;
                }
                if (ypuck >= 610 && xpuck >= 200 && xpuck <= 400){ //Goal2
                	redscore = redscore + 1;
                	ypuck= 300;
                	xpuck= 300;
                	xspeed=0;
                	yspeed=0;
                }
                if (ypuck <= 75 && xpuck >= lg && xpuck <= rg || ypuck < 0){ //Save goalie1
                	yspeed=-yspeed;
                	xspeed=0;
                	xpuck = xpuck + xspeed;
            		ypuck = ypuck + yspeed;
                }
                if (ypuck >= 590 && xpuck >= lg2 && xpuck <= rg2 || ypuck < 0){ //Save goalie2
                	yspeed=-yspeed;
                	xspeed=0;
                	xpuck = xpuck + xspeed;
            		ypuck = ypuck + yspeed;
                }
                else{ //keep puck moving
                xpuck = xpuck + xspeed;
                ypuck = ypuck + yspeed;
                }
            }
        public void run() {
        try {
        int ctr=-1; while (true){
        Thread.sleep(200);
        this.repaint();
        }   
        }
        catch(Exception e){}
        }
        /** Handle the key-pressed event from the text field. */
        
        public void keyPressed(KeyEvent evt) {
        			int key = evt.getKeyCode();
                    if (key == KeyEvent.VK_A) {
                    	if (redx > 3)
                    		redx = redx - 10;
                    		}
                    
                    else if (key == KeyEvent.VK_E){		//shooting
    
                    	if ((xpuck == redx && ypuck == redy) || (xpuck == redx2 && ypuck == redy2)){
                    		xspeed=20;
    		                yspeed=20;
    	                	if (yspeed>0){
    		                	yspeed=+yspeed;
    		                	xspeed=0;
    		                }
    	                		xpuck = xpuck + xspeed;
    	                		ypuck = ypuck + yspeed +20;
                    	}
    	            }
                    else if (key == KeyEvent.VK_M){		//shooting
    
                    	if ((xpuck == bluex && ypuck == bluey) || (xpuck == bluex2 && ypuck == bluey2)){
                    		xspeed=20;
    		                yspeed=20;
    	                	if (yspeed>0){
    		                	yspeed=-yspeed;
    		                	xspeed=0;
    		                }
    	                		xpuck = xpuck + xspeed;
    	                		ypuck = ypuck + yspeed -20;
                    	}
    	            }
                    else if (key == KeyEvent.VK_Q){		//passing
                    	xspeed=10;
    	                yspeed=10;
                            if (xpuck==redx && redx2>redx){
                            	if (xspeed>0){
        		                	yspeed=0;
        		                	xpuck = xpuck + xspeed + 20;
        		                	ypuck = ypuck + yspeed;
                            	}
                            }
                            else if (xpuck==redx && redx2<redx){
                            	if (xspeed>0){
        		                	xspeed=-xspeed;
        		                	yspeed=0;
                            	}
                            		xpuck = xpuck + xspeed - 20;
                            		ypuck = ypuck + yspeed;
                            }
                            else if (ypuck==redy && redy2<redy){
                            	if (yspeed>0){
        		                	yspeed=-yspeed;
        		                	xspeed=0;
        		                }
                            
                            	xpuck = xpuck + xspeed;
                            	ypuck = ypuck + yspeed - 20;
                            }
                            else if (ypuck==redy && redy2>redy){
                            	if (yspeed>0){
        		                	xspeed=0;
        		                }
                            
                            	xpuck = xpuck + xspeed;
                            	ypuck = ypuck + yspeed + 20;
                            }
                            
                            if (xpuck==redx2 && redx>redx2){
                            	if (xspeed>0){
        		                	yspeed=0;
        		                	xpuck = xpuck + xspeed + 20;
        		                	ypuck = ypuck + yspeed;
                            	}
                            }
                            else if (xpuck==redx2 && redx<redx2){
                            	if (xspeed>0){
        		                	xspeed=-xspeed;
        		                	yspeed=0;
                            	}
                            		xpuck = xpuck + xspeed - 20;
                            		ypuck = ypuck + yspeed;
                            }
                            else if (ypuck==redy2 && redy<redy2){
                            	if (yspeed>0){
        		                	yspeed=-yspeed;
        		                	xspeed=0;
        		                }
                            
                            	xpuck = xpuck + xspeed;
                            	ypuck = ypuck + yspeed - 20;
                            }
                            else if (ypuck==redy2 && redy>redy2){
                            	if (yspeed>0){
        		                	xspeed=0;
        		                }
                            
                            	xpuck = xpuck + xspeed;
                            	ypuck = ypuck + yspeed + 20;
                            }
                    }
                    else if (key == KeyEvent.VK_P){		//passing
                    	xspeed=10;
    	                yspeed=10;
                            if (xpuck==redx && bluex2>bluex){
                            	if (xspeed>0){
        		                	yspeed=0;
        		                	xpuck = xpuck + xspeed + 20;
        		                	ypuck = ypuck + yspeed;
                            	}
                            }
                            else if (xpuck==bluex && bluex2<bluex){
                            	if (xspeed>0){
        		                	xspeed=-xspeed;
        		                	yspeed=0;
                            	}
                            		xpuck = xpuck + xspeed - 20;
                            		ypuck = ypuck + yspeed;
                            }
                            else if (ypuck==bluey && bluey2<bluey){
                            	if (yspeed>0){
        		                	yspeed=-yspeed;
        		                	xspeed=0;
        		                }
                            
                            	xpuck = xpuck + xspeed;
                            	ypuck = ypuck + yspeed - 20;
                            }
                            else if (ypuck==bluey && bluey2>bluey){
                            	if (yspeed>0){
        		                	xspeed=0;
        		                }
                            
                            	xpuck = xpuck + xspeed;
                            	ypuck = ypuck + yspeed + 20;
                            }
                            
                            if (xpuck==bluex2 && bluex>bluex2){
                            	if (xspeed>0){
        		                	yspeed=0;
        		                	xpuck = xpuck + xspeed + 20;
        		                	ypuck = ypuck + yspeed;
                            	}
                            }
                            else if (xpuck==bluex2 && bluex<bluex2){
                            	if (xspeed>0){
        		                	xspeed=-xspeed;
        		                	yspeed=0;
                            	}
                            		xpuck = xpuck + xspeed - 20;
                            		ypuck = ypuck + yspeed;
                            }
                            else if (ypuck==bluey2 && bluey<bluey2){
                            	if (yspeed>0){
        		                	yspeed=-yspeed;
        		                	xspeed=0;
        		                }
                            
                            	xpuck = xpuck + xspeed;
                            	ypuck = ypuck + yspeed - 20;
                            }
                            else if (ypuck==bluey2 && bluey>bluey2){
                            	if (yspeed>0){
        		                	xspeed=0;
        		                }
                            
                            	xpuck = xpuck + xspeed;
                            	ypuck = ypuck + yspeed + 20;
                            }
                    }
                    else if (key == KeyEvent.VK_H) {
                    	redx2 = redx2 + 10;
                    }
                    else if (key == KeyEvent.VK_F) {
                    	redx2 = redx2 - 10;
                    }
                    else if (key == KeyEvent.VK_T) {
                    	redy2 = redy2 - 10;
                    }
                    else if (key == KeyEvent.VK_G) {
                    	redy2 = redy2 + 10;
                    }
                    else if (key == KeyEvent.VK_D) {
                    	redx = redx + 10;
                    }
                    else if (key == KeyEvent.VK_W) {
                    	if (redy > 3)
                    		redy = redy - 10;
                    }
                    else if (key == KeyEvent.VK_S) {
                    	redy = redy + 10;
                    }
                    else if (key == KeyEvent.VK_Y) {
                    	gx = gx+5;
                    }
                    else if (key == KeyEvent.VK_R) {
                    	gx = gx-5;
                    }
                    else if (key == KeyEvent.VK_U){
                    	gx2=gx2-5;
                    }
                    else if (key == KeyEvent.VK_O){
                    	gx2=gx2+5;
                    }
                    else if (key == KeyEvent.VK_J){
                    	bluex=bluex-10;
                    }
                    else if (key == KeyEvent.VK_L){
                    	bluex=bluex+10;
                    }
                    else if (key == KeyEvent.VK_K){
                    	bluey=bluey+10;
                    }
                    else if (key == KeyEvent.VK_I){
                    	bluey=bluey-10;
                    }
                    else if (key == KeyEvent.VK_LEFT){
                    	bluex2=bluex2-10;
                    }
                    else if (key == KeyEvent.VK_RIGHT){
                    	bluex2=bluex2+10;
                    }
                    else if (key == KeyEvent.VK_DOWN){
                    	bluey2=bluey2+10;
                    }
                    else if (key == KeyEvent.VK_UP){
                    	bluey2=bluey2-10;
                    }
                    
                 }  
                            
                /** Handle the key-released event from the text field. */
                public void keyReleased(KeyEvent e) {
             
                }
    
    
                public void keyTyped(KeyEvent e) {
                    }
    
    
        public class WindowExiter extends WindowAdapter {
        public void windowClosing ( WindowEvent e ) {
        	e.getWindow ( ) .dispose ( );
        	System.exit (0);
        	}
        }
    /*
         public void actionPerformed(ActionEvent evt) {
        //System.out.print("action preformed");
             // This routine is called by the system when the user clicks
             // on the button.  The response is to call the Thread  start which calls run
         // keyDown.start();
          t.start();
          }                                            
      */                                     
         
    
    
    @Override
    public void windowActivated(WindowEvent arg0) {
    // TODO Auto-generated method stub 
    }
    
    
    @Override
    public void windowClosed(WindowEvent arg0) {
    // TODO Auto-generated method stub 
    }
    @Override
    public void windowClosing(WindowEvent arg0) {
    // TODO Auto-generated method stub
    }
    @Override
    public void windowDeactivated(WindowEvent arg0) {
    // TODO Auto-generated method stub 
    }
    
    
    @Override
    public void windowDeiconified(WindowEvent arg0) {
    // TODO Auto-generated method stub
    }
    @Override
    public void windowIconified(WindowEvent arg0) {
    // TODO Auto-generated method stub
    }
    @Override
    public void windowOpened(WindowEvent arg0) {
    // TODO Auto-generated method stub
    }
    }





    __________________________________________________ ______--

    The problem is that the line that's supposed to add 20 to x (passing to the right) was not executing, even when the statements said they were true. So I really am confused. The values of the variables should just be adding when player 1 passes to the right, which it doesn't
    Last edited by Norm; 01-15-2012 at 01:55 AM. Reason: added code tags

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,308
    Rep Power
    25

    Default Re: Having small problem with my hockey game

    the line that's supposed to add 20 to x (passing to the right) was not executing
    Add some printlns to the code to show the values of the variables that controls if those statements execute.
    Make sure you add enough to show all you need to show.
    Also add an id String to the print out so you know which println printed the output. Make the ids unique.

    System.out.println("var=" + var);
    Last edited by Norm; 01-15-2012 at 02:00 AM.

  5. #5
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,193
    Rep Power
    19

    Default Re: Having small problem with my hockey game

    Moved from 'Advanced Java'

    Nothing advanced about the question.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  6. #6
    DballerP is offline Member
    Join Date
    Jan 2012
    Posts
    4
    Rep Power
    0

    Default Re: Having small problem with my hockey game

    close thread please, i found out the problem using the println in the passing code.

    Thanks so much.

  7. #7
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,308
    Rep Power
    25

    Default Re: Having small problem with my hockey game

    Glad you figured it out and got it working.

    Some suggestions for the code:
    1)You should split the paint() method and remove the code that computes the postions. Move that code to the timer code. Only do drawing and painting in the paint method. Do the computations in the timer method before calling repaint().
    2) Create an inner class that extends JPanel and move the paint method into that new class and rename it to paintComponent.
    Add an instance of that class to the class that extends JFrame .

Similar Threads

  1. Small problem with problem with Java, C++ parse program.
    By dragstang86 in forum New To Java
    Replies: 4
    Last Post: 10-30-2011, 03:43 AM
  2. Small game
    By ivlatt in forum Java Applets
    Replies: 5
    Last Post: 04-12-2011, 02:15 PM
  3. small problem
    By barusk in forum Networking
    Replies: 4
    Last Post: 03-21-2009, 06:19 AM
  4. Small problem
    By ayoood in forum New To Java
    Replies: 2
    Last Post: 06-06-2008, 12:27 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
  •