Snake Game Program - Making the Body of the Snake
EDIT: (QUESTION ANSWERED)
So I have a Thread controlling x and y and making it change. My problem is that when the snakes body gets put up in the GUI it puts it directly on top of the head instead of 12 spaces behind it. I know why it's doing it but I am not sure how to fix this problem.
From what I understand, it's drawing the body and then the head but it's using the same x and y spaces...what really needs to happen is that it needs to draw the head, go through the Thread, then draw the body parts so that it does it a space behind the head (because the head will have moved forward)...so on and so forth... I just can't figure out how to do it.
Here is the Thread runner which i highly doubt you will really need but just to be sure
class ThreadRunnable extends Thread implements Runnable {
private boolean allDone = false;
private boolean RIGHT;
private boolean LEFT;
private boolean UP;
private boolean DOWN;
public ThreadRunnable (boolean up, boolean right, boolean down, boolean left){
UP = up;
RIGHT = right;
DOWN = down;
LEFT = left;
}
public void run() {
if(UP == true) {
try {
while(UP) {
if (y <= yBase && y >= 0) {
y = y - 12;
}
else {
y = yBase;
}
Thread.sleep(40);
repaint();
if (allDone) {
return;
}
}
}
catch (InterruptedException exception) {
}
finally {
}
}
if(RIGHT ==开发者_StackOverflow中文版 true) {
try {
while(RIGHT) {
if (x <= xBase && x >= 0) {
x = x+12;
}
else {
x = 0;
}
Thread.sleep(40);
repaint();
if(allDone) {
return;
}
}
}
catch (InterruptedException exception) {
}
finally {
}
}
if(DOWN == true) {
try {
while(DOWN) {
if (y <= yBase && y >= 0) {
y = y+12;
}
else {
y = 0;
}
Thread.sleep(40);
repaint();
if (allDone) {
return;
}
}
}
catch (InterruptedException exception) {
}
finally {
}
}
if(LEFT == true) {
try {
while(LEFT) {
if (x <= xBase && x >= 0) {
x = x-12;
}
else {
x = xBase;
}
Thread.sleep(40);
repaint();
if (allDone) {
return;
}
}
}
catch (InterruptedException exception) {
}
finally {
}
}
}
}
Here is the "Drawing" part of the code
public void paintComponent(Graphics g) {
super.paintComponent(g);
setBorder(BorderFactory.createLineBorder(Color.WHITE));
this.setBackground(Color.black);
if (numOfFood == 1) {
g.setColor(Color.BLUE);
g.fillRect(foodX,foodY,12,12); //Paints the food
}
else {
foodX = random.nextInt(105)*12; //Random x for food position after snake eats
foodY = random.nextInt(59)*12; //Random y for food position after snake eats
numOfFood = 1;
}
Rectangle headRect = new Rectangle( x, y, 12, 12 ); //The head (not painted)
Rectangle foodRect = new Rectangle(foodX, foodY, 12, 12); //The food (not painted)
if ( body.size() >= 0) {
body.add(new BodyPart( x, y, 12, 12));
body = new ArrayList<BodyPart>( body.subList( body.size() - n, body.size() ) );
g.setColor(Color.RED);
for ( int i = body.size() - 1; i >= body.size() - (n-1); i-- ) {
g.fillRect( body.get( i ).getX(), body.get( i ).getY(), body.get( i ).getWidth(), body.get( i ).getHeight() ); //This is calling a class that will get these parts of the array list (x,y,width,height)
}
}
g.setColor(Color.RED);
g.fillRect(x,y,12,12); //Draws head
g.setColor(Color.WHITE);
g.fillRect(x+2,y+2,8,8); //This is a white square in the middle of the head to show that it is the head
if (headRect.intersects(foodRect)) {
numOfFood = 0;
n++;
}
}
EDIT: QUESTION ANSWERED BELOW
All that was required was to move the
setBorder(BorderFactory.createLineBorder(Color.WHITE));
and the
this.setBackground(Color.black);
to an earlier part of the code and not in the drawing method there...
I also changed my ArrayList into a normal Array and just made it insert the head location into the beginning of the Array and then made it print the next time around (after the head had moved)
g.setColor(Color.RED);
//Prints the body parts
if (n > 0) {
for (int i = 0;i < n; ++i) {
g.fillRect(body[i][0],body[i][1],12,12);
}
}
//Inserts the head location
for (int i = n-1;i >= 0; --i) {
body[i+1][0] = body[i][0];
body[i+1][1] = body[i][1];
if (i == 0) {
body[i][0] = x;
body[i][1] = y;
}
}
Thank you so much for all the help
- Move the head forward one.
- Put a body segment where the head was.
- Erase the last body segment.
None of the other body segments need move.
精彩评论