Results 1 to 2 of 2

Thread: [RESOLVED] Matching Game

Hybrid View

  1. #1

    Thread Starter
    Member
    Join Date
    Mar 2007
    Posts
    50

    Resolved [RESOLVED] Matching Game

    I am trying to create a simple matching game that allows the user to click on a box and the card is revealed and if 2 of the cards match then they stay dissplayed else the card returns to its original state the code i have so far is:

    Code:
    import java.applet.*;
    import java.awt.*;
    import java.awt.event.*;
    import java.awt.image.*;
    import java.util.Date;
    public class MatchingPairs extends Applet implements MouseListener//, Runnable
    {
    	int xpos =- 1;
    	int ypos =- 1;
    	int noTries, noMatches;
    	int elapsedTime;
    	int gameStatus;
    	//String gameStatus;
    	//Thread elapsedTime;
    
    	final int IMAGES = 5;
    	Image[][] cards = new Image[IMAGES][IMAGES];
    
    	public void init()
    	{
    		setBackground(Color.white);
    		// Add the MouseListener to your applet
      		addMouseListener(this);
    		for(int i = 0; i < IMAGES; i++)
    		{
    			cards[i][i] = getImage(getDocumentBase(), "pic" +i+ ".jpg");
    		}
    	}
    
    	public void paint(Graphics g)
    	{
    		int xt = 0;
    		int row;
    		int col;
    		int x, y;
    		for (row = 0;  row < IMAGES;  row++) 
    		{
    			for ( col = 0;  col < IMAGES;  col++) 
    			{
    			//	x = col * IMAGES;
                    	//	y = row * IMAGES;
    				//if((row % 1) == (col % 1))
    					g.drawImage(cards[col][col], xt, 0, this);
    					xt+=cards[row][row].getWidth(null) + 10;
    					xt+=cards[col][col].getHeight(null) + 10;
    					//cards[i][i] = (int) (Math.random()*5);
    				//if((row % 2) == (col % 2))
    				//	g.drawImage(cards[col][col], xt, 0, this);
    				//	xt+=cards[col][col].getWidth(null) + 10;	
    			}
    		}
    	}
    
    	public void mouseClicked(MouseEvent e) 
    	{
    		xpos = e.getX()/20;
    		ypos = e.getY()/20;
    		repaint();
    	}
    
    	public void mousePressed(MouseEvent e){}
    	public void mouseEntered(MouseEvent e){}
    	public void mouseReleased(MouseEvent e){}
    	public void mouseExited(MouseEvent e){}
    
    } // end class
    but the images dont form a grid it displays in a single row
    i want it to appear something like:
    oooo
    oooo
    oooo
    instead of ooooooooooo ooooooo
    the "o" represents the images. any help would be appreciated.
    thanks in advance.

  2. #2
    Arabic Poster ComputerJy's Avatar
    Join Date
    Nov 2005
    Location
    Happily misplaced
    Posts
    2,513

    Re: Matching Game

    I just added g.drawRect to your paint method, I think it's obvious why it's not working if you try this code
    Code:
    	@Override
    	public void paint(final Graphics g)
    	{
    		int xt = 0;
    		int row;
    		int col;
    		int x, y;
    		for (row = 0; row < IMAGES; row++)
    			for (col = 0; col < IMAGES; col++)
    			{
    				x = col * IMAGES;
    				y = row * IMAGES;
    				// if((row % 1) == (col % 1))
    				// g.drawImage(cards[col][col], xt, 0, this);
    				g.drawRect(x, y, 100, 100);
    				xt += cards[row][row].getWidth(null) + 10;
    				xt += cards[col][col].getHeight(null) + 10;
    				// cards[i][i] = (int) (Math.random()*5);
    				// if((row % 2) == (col % 2))
    				// g.drawImage(cards[col][col], xt, 0, this);
    				// xt+=cards[col][col].getWidth(null) + 10;
    			}
    	}
    "I'm not normally a praying man, but if you're up there, save me... Superman!" - Homer Simpson
    My Blog

Posting Permissions

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



Click Here to Expand Forum to Full Width