plz help me scoring part in this little game!


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 4 of 4

Thread: plz help me scoring part in this little game!

Hybrid View

  1. #1
    Join Date
    Mar 2006
    Posts
    4

    plz help me scoring part in this little game!

    I have a game where an object falls down when pressed Enter and hits the moving object at the bottom. When it hit's the moving object at the bottom score should be displayed somewhere(I want it in JLabel as I've done in code) I Have done the condition where dropping object touches the moving object but can't get the score displayed in Jlabel.

    I have actually problem in scoring method in SmashGame Class And every thing else is working just fine.
    Here is my code:


    //SmashGAme Class
    Code:
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    import java.util.*;
    
    
    public class SmashGame extends JFrame
    {
    	private Container contents;
    	private final int WINDOW_SIZE = 400;
    	private JPanel p1;
    	private JButton button;
        private JButton start;
        
       
        private JLabel score;
       public int counter;
    	DropObject dropper = new DropObject();
    	SmashObject target = new SmashObject( (WINDOW_SIZE/2), (WINDOW_SIZE - 10),0,(WINDOW_SIZE) );
    	public SmashGame()
    	{		
    	super("Start by Pressing Enter to drop and arrow key to move!");
        p1= new JPanel();
        p1.setLayout(new BoxLayout(p1,BoxLayout.Y_AXIS));
        JButton start = new JButton("Start");
        JButton reset=new JButton("Reset");
        JLabel score=new JLabel("Score "+counter);
        start = new JButton("Start");
    	p1.add(start); 
       
        p1.add(reset);
       
        p1.add(score); 
       
        SmashObject target = new SmashObject( (WINDOW_SIZE/2), (WINDOW_SIZE - 10),0, (WINDOW_SIZE) );      
       
        contents = getContentPane( );
      
        contents.add(p1,"East");
      
        ButtonHandler bh=new ButtonHandler();
        
        start.addKeyListener(bh);
    	
    	setSize(WINDOW_SIZE, WINDOW_SIZE);
    
    	setResizable(false);
    
    	setVisible(true);
    	  }
    	  
    	  
    	/////PROBLEM PROBLEM PROBLEM PROBLEM  
          	public void scoring()
       	{
       
      	 if ((dropper.getY()+dropper.getHeight() >=target.getY()) && 
    		   (dropper.getX()<= target.getX()+target.getWidth()) &&
    		  (dropper.getX()+dropper.getWidth() >= target.getX()))
    		
       	{
       		counter++;
       	}
    score.setText("Score  :"+counter);
    
    		}
       	
      	
       
       
      
    private class ButtonHandler implements KeyListener
      {
     
    	public void keyPressed(KeyEvent e) 
    	{
    	
    			if (e.getKeyCode() == KeyEvent.VK_ENTER) 
    		{
            dropper.falling=true;
    			repaint();
    
    	}
    		
    		 if(e.getKeyCode() == (KeyEvent.VK_LEFT))
           {
           		dropper.moveLeft();
           		repaint();
           }
           
           if(e.getKeyCode() == (KeyEvent.VK_RIGHT))
           {
           	dropper.moveRight();
           		repaint();
           		       }
    		
    	}
    	public void keyReleased(KeyEvent e) {}
    	public void keyTyped(KeyEvent e) {}
    	}
    	
    	public void paint(Graphics g)
    	{
    	super.paint(g);
    	target.draw(g);
    	dropper.draw(g);
    	try
    	{
    			Thread.sleep( 100 );
    		}
    				
    		catch (InterruptedException ie)
    		{
    			ie.printStackTrace();
    		}				
    		
    repaint();
    		
    	}
    	
    	
    	
    	public static void main(String [] args)
    	{
    		SmashGame newGame = new SmashGame();
    		
    		newGame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    	}
    	
    }
    //DropObject Class//
    Code:
    // This is the class for the object to be dropped
    // This is the class for the object to be dropped
    
    import java.awt.*;
    
    
    
    public class DropObject
    {
    	
    	public int x;
    private int y; 
    private int height=10;
    private int width=10;	
    	private int yMax; 
    	
    	private int dropSpeed; 
    	public boolean falling;
    	
    
    	public DropObject()
    	{
    		
    		x = 200;
    		y = 30;
    		
    		dropSpeed = 4;
    		
    		yMax = 400;
    		falling=false;	
    		
    	}
    
    
    
    	
    	public void setX(int newX)
    	{
    		
    		x = newX;
    		
    	}
    	
    	public void setYMax(int newYMax)
    	{
    		yMax = newYMax;
    	}
    	
    	
    	
    	public void changeSpeed(int newSpeed)
    	{
    		dropSpeed = newSpeed;
    	}
    	
    	
    	public int getX()
    	{
    		return x;
    	}
    	
    	
    	public int getY()
    	{
    		return y;
    	}
    	
    		public int getHeight()
    	{
    		return height;
    	}
    		public int getWidth()
    	{
    		return width;
    	}
    	
    	
    	public void drop()
    	{
    	
    	
    			y += dropSpeed;
    		
    	}
    	
    		public void moveRight()
    	{
    		if( x+width+1 > 400 ){}
    		else
    		{
    			x+=10;
    		}
    	}
    	
    	//If the paddle can be moved left, move it left, should call from key listener in Pong.java
    	public void moveLeft()
    	{
    		if(x - 1 < 0){}
    		else
    		{
    			x -= 10;
    		}
    	}
    	
    	
    
    	public void draw(Graphics g)
    	{
    	
    		g.setColor(Color.red);
    		g.fillRect(x,y,width,height);
    		if(falling==true)
    		drop();
    		
    	}
    
    }
    //Object moving at the buttom:
    Code:
    import java.awt.*;
    
    
    public class SmashObject
    {
    	
    public int x;
    	private int y;
    private int height=30;
    private int width=50;
    	private int xMax;
    	private int xMin;
    	
    	private int moveSpeed;
    	
    	public SmashObject()
    	{
    		x = 20;
    		y = 20;
    		xMax = 40;
    		xMin = 0;
    		moveSpeed = 6;
    		
    	}
    	
    	
    	public SmashObject(int xIn, int yIn, int xMinIn, int xMaxIn)
    	{
    	
    	x = xIn;
    	y = yIn;
    	xMin = xMinIn;
    	xMax = xMaxIn;
    	
    	moveSpeed=4;
    	}
    	
    	
    	
    	public void setX(int newX)
    	{
    		x = newX;
    	}
    
    	public void setY(int newY)
    	{
    		y = newY;
    		
    	}
    
    	
    	public int getX()
    		{
    		return x;
    	}
    	
    	public int getY()
    	{
    		return y;
    	}
    	
    	public int getWidth()
    	{
    		return width;
    	}
    	
    	public void moveObject()
    	{
    	
    		
    		
    		
    		if (x + moveSpeed+width > xMax|| x + moveSpeed < xMin)
    		{
    		moveSpeed = - moveSpeed;
    			
    			x = ((Math.abs((xMax - x)) < Math.abs((xMin-x))) ? xMax-width : xMin);
    			
    		}
    		
    		
    	
    		else
    		{
    			x += moveSpeed;
    		} 
    	}
    	
    	
    	public void draw(Graphics g)
    	{
    		g.setColor(Color.red);
    		g.fillRect(x,y,width,height);
    		moveObject();
    		
    	}
    	
    	
    	
    }

  2. #2
    Join Date
    Feb 2006
    Location
    Cologne - Germany
    Posts
    271
    in the method, where you detect the hit / score, you just have to set the text of the jlabel. i had no time to review your code to point to the place.

  3. #3
    Join Date
    Mar 2006
    Posts
    4
    I did that too....

  4. #4
    Join Date
    Mar 2005
    Location
    UK, London
    Posts
    150
    try this with ur scoring method.

    Code:
    public void scoring(){ new Thread(new Runnable() {
    			public void run() 
    			{
    
    	 if ((dropper.getY()+dropper.getHeight() >=target.getY()) && 
    		   (dropper.getX()<= target.getX()+target.getWidth()) &&
    		  (dropper.getX()+dropper.getWidth() >= target.getX()))
    		
       	{
       		counter++;
       	}
    score.setText("Score  :"+counter);
    }
    }).start(); }

Similar Threads

  1. Replies: 3
    Last Post: 03-08-2005, 11:24 PM
  2. Framework of a complex game...
    By AGuy in forum Java
    Replies: 0
    Last Post: 07-31-2000, 07:35 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center