Results 1 to 8 of 8
  1. #1
    joz_12345 is offline Member
    Join Date
    Dec 2007
    Posts
    17
    Rep Power
    0

    Default Can someone check my code

    I'm making the rush hour game using a 6x6 grid so that cars will snap to each square when being moved. I have 6 images loaded in my photobucket account - a 2 square vertical, 3 square vertical, target vertical, 2 square horizontal, 3 square horizontal and target horizontal. Each of these are called as and when they are needed.

    I have 3 classes at the moment - Car, Board and GUI.

    Can someone check my code so far so that I know what i'm doing is correct and any pointers/advice would be appreciated as I'm a bit stuck now as to what to do next.
    I still need my collision detection, and mouse listeners and a couple of other bits.

    Car:
    Java Code:
    package rush;
    
    import java.awt.Image;
    import java.awt.Toolkit;
    
    public class Car_1{
        
    	public static final int	HORIZONTALTWOBLOCK	= 0;
    	public static final int	HORIZONTALTHREEBLOCK	= 1;
            public static final int	HORIZONTALTARGET	= 2;
            public static final int	VERTICALTWOBLOCK	= 3;
            public static final int	VERTICALTHREEBLOCK	= 4;
            public static final int	VERTICALTARGET  	= 5;
    	
    	private int				xPos;
    	private int				yPos;
    	private int                             orientandlength;
                   	
    	public Car_1(int x, int y, int oandl)
    	{
    		this.xPos               = x;
    		this.yPos               = y;
                    this.orientandlength    = oandl;
            }	
    	
    	public void setX(int x)
    	{
    		this.xPos = x;
    	}
    	
    	public int getX()
    	{
    		return xPos;
    	}
    	
    	public void setY(int y)
    	{
    		this.yPos = y;
    	}
    	
    	public int getY()
    	{
    		return yPos;
    	}
    	
    	public void setOrientandLength(int oandl)
            {
                if(orientandlength == HORIZONTALTWOBLOCK)
                {
                    Image image = Toolkit.getDefaultToolkit().getDefaultToolkit().createImage("http://i126.photobucket.com/albums/p85/geebs2006/2BlockCarHorizontal.jpg");
                }
                else if (orientandlength == HORIZONTALTHREEBLOCK)
                {
                    Image image = Toolkit.getDefaultToolkit().getDefaultToolkit().createImage("http://i126.photobucket.com/albums/p85/geebs2006/3BlockCarHorizontal.jpg");
                }
                else if (orientandlength == HORIZONTALTARGET)
                {
                    Image image = Toolkit.getDefaultToolkit().getDefaultToolkit().createImage("http://i126.photobucket.com/albums/p85/geebs2006/CarTargetHorizontal.jpg");
                }
                else if (orientandlength == VERTICALTWOBLOCK)
                {
                    Image image = Toolkit.getDefaultToolkit().getDefaultToolkit().createImage("http://i126.photobucket.com/albums/p85/geebs2006/2BlockCarVertical.jpg");
                }
                else if (orientandlength == VERTICALTHREEBLOCK)
                {
                    Image image = Toolkit.getDefaultToolkit().getDefaultToolkit().createImage("http://i126.photobucket.com/albums/p85/geebs2006/3BlockCarVertical.jpg");
                }
                else if (orientandlength == VERTICALTARGET)
                {
                    Image image = Toolkit.getDefaultToolkit().getDefaultToolkit().createImage("http://i126.photobucket.com/albums/p85/geebs2006/CarTargetVertical.jpg");
                }
            }
    }
    Board
    Java Code:
    package rush;
    
    import java.awt.Color;
    
    
    /**
     *
     * @author Steven Gibbs
     */
    public class Board extends Car_1 {
        
        public static final int NOOFROWS	= 6;
        public static final int NOOFCOLS	= 6;
        
        private int                         theExit;
        
        // Car Object, Name of Object, Row Number, Column Number, Image Required
        Car_1 targetCar = new Car_1(2, 3, 0);
        Car_1 car1 = new Car_1(1, 1, 0);
        Car_1 car2 = new Car_1(1, 2, 1);
        Car_1 car3 = new Car_1(1, 5, 1);
        Car_1 car4 = new Car_1(4, 2, 1);
        Car_1 car5 = new Car_1(3, 6, 0);
        Car_1 car6 = new Car_1(6, 1, 1);
        Car_1 car7 = new Car_1(5, 5, 0);     
        
        /** Creates a new instance of Board */
        public Board(int exit, int x, int y, int oandl) 
        {
            super(x, y, oandl);
            this.theExit = exit;
            
            
                 
        }
        
        public void settheExit(int theExit)
        {
            this.theExit = theExit;
        }
    	
        public int gettheExit()
        {
    	return theExit;
        }
    }
    GUI
    Java Code:
    package rush;
    
    import java.awt.event.MouseAdapter;
    import java.awt.event.MouseEvent;
    import javax.swing.JPanel;
    
    /**
     *
     * @author Steven Gibbs
     */
    public class InteractiveGUI extends Board{
        
        
        /** Creates a new instance of InteractiveGUI */
        public InteractiveGUI(int exit, int x, int y, int oandl) 
        {
            super (exit, x, y, oandl);
            JPanel jp = new JPanel();
            
        }
    }

  2. #2
    Bluefox815 is offline Member
    Join Date
    Feb 2008
    Location
    Oregon, USA
    Posts
    49
    Rep Power
    0

    Default

    How wide are your rows and columns in pixels? Because the way you called the constructor in your Board class...
    Java Code:
        Car_1 car1 = new Car_1(1, 1, 0);
        Car_1 car2 = new Car_1(1, 2, 1);
        etc.
    you have each row and column set to 1 pixel, which is really small.
    You should space out your cars for each row. Like this if each row was 50 pixels wide.
    Java Code:
        Car_1 targetCar = new Car_1(49, 99, 0);
        Car_1 car1 = new Car_1(0, 0, 0);
        Car_1 car2 = new Car_1(0, 49, 1);
    Or if you can't that or collisions to work, you could leave the constructor calls as they are and make your board out of an array, but that would require making more pictures (breaking up 100x50 pictures into two 50x50 pictures for each car and another picture of each square for each direction it would face) then you can display each square one by one.

    Just a few things to think about, I hope that everything turns out alright.
    Last edited by Bluefox815; 02-16-2008 at 07:44 PM.

  3. #3
    joz_12345 is offline Member
    Join Date
    Dec 2007
    Posts
    17
    Rep Power
    0

    Default

    Thanks for reply

    Basically where I have specified for each car something like Car_1 car1 = new Car_1(1, 1, 0);

    the first number is the column number and the second number is the row number, and then the third relates to which picture needs to be displayed at that row and column coordinate.

  4. #4
    Bluefox815 is offline Member
    Join Date
    Feb 2008
    Location
    Oregon, USA
    Posts
    49
    Rep Power
    0

    Default

    Yes, I know, but how are you going to display that? I would use arrays if you want to use that idea, and maybe start at 0 instead of 1 (makes it easier to work with arrays). If you need help with displaying, let me know what you want to do.
    Last edited by Bluefox815; 02-16-2008 at 08:52 PM.

  5. #5
    joz_12345 is offline Member
    Join Date
    Dec 2007
    Posts
    17
    Rep Power
    0

    Default

    yea any help you can give would be great as i'm a little stuck with it. Haven't done java in 2 years and just getting back into it now so i'm impressed with stage i'm at now lol.

  6. #6
    Bluefox815 is offline Member
    Join Date
    Feb 2008
    Location
    Oregon, USA
    Posts
    49
    Rep Power
    0

    Default

    Alright, well you're going to need to come up with a method that will take the cars and line them up correctly, right now you shouldn't be thinking about collisions or anything like that. You should take your pictures and split them to 50x50 pics so that you can instead display a square where you need it. An example would be this. 1 = Front car piece, 2 = back car piece (horizontal)

    0 0 0
    1 2 0
    0 0 0

    And each number is a square which can be represented by an array that holds the images, then you just select the image you want to display at each square. (You will also need a multi-dimensional array for the plotting coordinates)
    Java Code:
    image[0] = an empty square
    image[1] = horizontal front car piece
    image[2] = horizontal back car piece
    image[3] = horizontal mid car piece (for a 3-block car)
    image[4] = vertical front car piece
    etc.
    And then you need to display the array, making sure each square is 50 pixels apart (which will display them as being right next to each other).

    An example for selecting the right parts of your array to change would be
    Java Code:
    coordinate[0] = image[0]
    coordinate[1] = image[1]
    coordinate[2] = image[2]
    coordinate[3] = image[0]
    That's a few ideas to help you.

  7. #7
    joz_12345 is offline Member
    Join Date
    Dec 2007
    Posts
    17
    Rep Power
    0

    Default

    Surely there is a way of doing it keeping images the way they are?

  8. #8
    Bluefox815 is offline Member
    Join Date
    Feb 2008
    Location
    Oregon, USA
    Posts
    49
    Rep Power
    0

    Default

    Yes, that's with collisions, that was just an example of using arrays. You could check to see if a car is at a certain position, which would be a bit difficult. You would have to know the position of each car, how wide it is (if you need to check for the right side of it) how tall (for bottom).

    You will probably want a method in your GUI class that takes the numbers for row and column and converts them to pixels. You could multiply the row or column number by 50 and subtract 1 (that IS if row number = 0, it comes out as 0, and 1 as 50, so a little added math is used... yay).
    I noticed your setOrientandLength method in Car_1 is pretty worthless, as it doesn't set any field to an image, or return an image, so it does nothing. I would make image a field (member variable) and call the method inside your constructor (which requires it be static). Then add a getImage() method.
    Below is code that will work with the above.
    Java Code:
    public void paint(Graphics g) {
    g.drawImage(car1.getImage(), (car1.getX() - 1) * 50, (car1.getY() - 1) * 50, this);    /* the parenthesis used for getting the X and Y coordinates decide which math is done first, and are important */
    }
    This will draw car1 at 0, 0 (column 1, minus 1 = 0, times 50 = 0) and width and height just depend on the image used. So for collision checking you would need to know the orient&length number, so you know width and height.
    Java Code:
    public void paint(Graphics g) {
    if (checkCollision(car1.getX(), car1.getY(), car1.getOrientandLength)) {    /* getOrientandLength will need to be created, and should return an int */
    g.drawImage(car1.getImage(), (car1.getX() - 1) * 50, (car1.getY() - 1) * 50, this);
    }
    // other drawing continues
    }
    
    private static boolean checkCollision(int x, int y, int orientAndLength) {
    /* let's say that 'used' is a boolean field/member variable (boolean[] used = new boolean[36]) */
    switch (orientAndLength) {
    case 0: // for horizontal 2 block
    case 2: // for horizontal target, which is the same size as 2 block
    // assuming the number of total columns is 6
    if (used[(y * 6) + x] == false && used[(y * 6) + x + 1] == false) {
    used[(y * 6) + x] = true;
    used[(y * 6) + x + 1)] = true;
    } else {
    return false;
    } break;
    case 1: // for horizontal 3 block
    if (used[(y * 6) + x] == false && used[(y * 6) + x + 1)] == false && used[(y * 6) + x + 2)] == false) {
    used[(y * 6) + x] = true;
    used[(y * 6) + x + 1)] = true;
    used[(y * 6) + x + 2)] = true;
    } else {
    return false;
    } break;
    case 3:
    if (used[(y * 6) + x] == false && used[((y + 1) * 6) + x] == false) {
    used[(y * 6) + x] = true;
    used[((y + 1) * 6) + x)] = true;
    } else {
    return false
    } break;
    // etc.
    }  // end switch statement
    return true;
    }
    That's a start on doing it without arrays (except used, which is very useful)

    Also, when you get everything drawing itself correctly, I can help you with making the mouse work, but be advised that I work with applets and don't really know ANYTHING about JFrame, or Swing and what not, I still need to learn that.

    And one last thing, I noticed your classes extend each other, they don't need to do that at all, so you can take that out (and also don't worry about import, because they are in the same package)
    Last edited by Bluefox815; 02-18-2008 at 03:29 AM.

Similar Threads

  1. CRC check program 1 error
    By javakid9000 in forum New To Java
    Replies: 1
    Last Post: 03-19-2008, 05:04 AM
  2. Check if a web page exists or not
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 03-02-2008, 07:24 PM
  3. Spell check feature
    By ravian in forum Advanced Java
    Replies: 2
    Last Post: 12-27-2007, 09:28 AM
  4. spell check
    By kazitula in forum Java Applets
    Replies: 2
    Last Post: 12-20-2007, 11:37 AM
  5. Replies: 1
    Last Post: 04-26-2007, 03:52 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
  •