View Single Post
  #8 (permalink)  
Old 02-18-2008, 03:58 AM
Bluefox815 Bluefox815 is offline
Member
 
Join Date: Feb 2008
Location: Oregon, USA
Posts: 24
Bluefox815 is on a distinguished road
Send a message via MSN to Bluefox815
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.
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.
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 04:29 AM.
Reply With Quote