Board.Java(程序的逻辑部分)
package snake;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.ArrayList;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Board extends JPanel implements KeyListener, Runnable{
private final int WIDTH = 500, HEIGHT = 500,TWIDTH = WIDTH/25, THEIGHT = HEIGHT/25, RAND_POS = 29;
private int foodLocX, foodLocY, snakeHeadX = 240, snakeHeadY = 240;;
private boolean running = true, up = false,down = false,left = false,right = true;
private JFrame frame;
private Thread t;
private ArrayList<BodyPart> body;
public Board() {
frame = new JFrame("Snake");
initBoard();
foodLocX = snakeHeadX + 8*TWIDTH;
foodLocY = snakeHeadY;
body = new ArrayList<BodyPart>();
body.add(new BodyPart(snakeHeadX,snakeHeadY, TWIDTH));
t = new Thread(this);
}
public void initBoard() {
frame.setPreferredSize(new Dimension(518,540));
this.setPreferredSize(new Dimension(WIDTH,HEIGHT));
frame.addKeyListener(this);
frame.pack();
frame.add(this);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
private void locateApple() {
int r = (int) (Math.random() * RAND_POS);
foodLocX = ((r * TWIDTH));
r = (int) (Math.random() * RAND_POS);
foodLocY = ((r * THEIGHT));
while(foodLocX >= WIDTH || foodLocY >= HEIGHT || foodLocX < 0 || foodLocY < 0) {
r = (int) (Math.random() * RAND_POS);
foodLocX = ((r * TWIDTH));
r = (int) (Math.random() * RAND_POS);
foodLocY = ((r * THEIGHT));
}
}
public void paint(Graphics g) {
g.clearRect(0, 0, WIDTH, HEIGHT);
g.setColor(Color.GREEN);
for(int i = 0; i<WIDTH;i+=TWIDTH) {
for(int ii = 0; ii<HEIGHT;ii+=THEIGHT) {
g.fillRect(i, ii, TWIDTH, THEIGHT);
}
}
if(foodLocX == body.get(0).getX() && foodLocY == body.get(0).getY()) {
int x = body.get(body.size()-1).getX();
int y = body.get(body.size()-1).getY();
if(right) {
body.add(new BodyPart(x+20,y,TWIDTH));
}if(left) {
body.add(new BodyPart(x-20,y,TWIDTH));
}if(down) {
body.add(new BodyPart(x,y+20,TWIDTH));
}if(up) {
body.add(new BodyPart(x,y-20,TWIDTH));
}
locateApple();
}
g.setColor(Color.RED);
g.fillRect(foodLocX, foodLocY, TWIDTH, THEIGHT);
g.setColor(Color.BLACK);
for(int i = 0; i< body.size();i++) {
body.get(i).draw(g);
}
repaint();
}
@Override
public void keyPressed(KeyEvent e) {
if(e.getKeyCode() == KeyEvent.VK_UP && !down) {
up = true;
down = false;
left = false;
right = false;
try {
Thread.sleep(55,666);
} catch (InterruptedException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}else if(e.getKeyCode() == KeyEvent.VK_DOWN && !up) {
up = false;
down = true;
left = false;
right = false;
try {
Thread.sleep(55,666);
} catch (InterruptedException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}else if(e.getKeyCode() == KeyEvent.VK_RIGHT && !left) {
up = false;
down = false;
left = false;
right = true;
try {
Thread.sleep(55,666);
} catch (InterruptedException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}else if(e.getKeyCode() == KeyEvent.VK_LEFT && !right) {
up = false;
down = false;
left = true;
right = false;
try {
Thread.sleep(100,666);
} catch (InterruptedException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
}
@Override
public void keyReleased(KeyEvent arg0){};
@Override
public void keyTyped(KeyEvent arg0){}
//public void addBodyPart() {
//body.add(previousHead); // Part that I left off on
//}
public void moveSnake() {
if(right) {
for(int i = 0; i<body.size();i++) {
body.get(i).setX(body.get(i).getX()+20);
}
}if(left) {
for(int i = 0; i<body.size();i++) {
body.get(i).setX(body.get(i).getX()-20);
}
}if(down) {
for(int i = 0; i<body.size();i++) {
body.get(i).setY(body.get(i).getY()+20);
}
}if(up) {
for(int i = 0; i<body.size();i++) {
body.get(i).setY(body.get(i).getY()-20);
}
}
}
@Override
public void run() {
while(running) {
if(body.get(0).getX()<0 || body.get(0).getX()>480 || body.get(0).getY()<0 || body.get(0).getY()>480){
System.out.println("Game Over!\nYour snake was " + body.size() + " blocks long");
endGame();
}
moveSnake();
repaint();
try {
Thread.sleep(75,666);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public void startGame() {
t.start();
}
public void endGame() {
t.stop();
}
}
Main.java(可运行程序)
package snake;
public class Main {
public static void main(String[] args) {
Board b = new Board();
b.startGame();
}
}
BodyPart.java
package snake;
import java.awt.Color;
import java.awt.Graphics;
public class BodyPart {
private int x,y,tilesize;
public BodyPart(int x, int y, int tilesize) {
this.x = x;
this.y = y;
this.tilesize = tilesize;
}
public void draw(Graphics g) {
g.setColor(Color.BLACK);
g.fillRect(x,y,tilesize,tilesize);
}
public int getX() {
return x;
}
public int getY() {
return y;
}
public void setX(int x) {
this.x = x;
}
public void setY(int y) {
this.y = y;
}
}
这很麻烦,因为它将主体部分添加到数组列表中,但是一旦我移动,这些部分就不会跟随主体。我迷路了,不知道该怎么办。任何帮助将不胜感激。非常感谢大家。
-AbysssCoder
因为在moveSnake()
方法中,您要遍历整个身体并移动身体的各个部分相同的量(您要拾取整条蛇并移动它,而使其没有“ slither”)。
而不是移动整个蛇-您需要箭头键仅移动“头”或“父”。然后,您需要将其余的蛇编码为“子级”,其中蛇的每个新部分都将其新位置设置为其父级的当前位置。
例如,如果您的蛇只有2个单位长,则箭头键将确定第一个单位(仅是“头部”)的位置。然后,将for each bodypart : yoursnake
(伪代码)设置为bodyPart.getParentBodyPart.getLocation()
或类似逻辑的新位置。
这很丑,因为我只是将它与您拥有的东西一起扔了-您绝对可以清理它。但这会起作用:
public void moveSnake() {
if(right) {
// iterate from the tail forward so we don't overwrite positions that we need
for(int i = body.size()-1; i>=0;i--) {
if (i == 0) {
body.get(i).setX(body.get(i).getX()+20);
} else {
// we dont know what direct we are moving, so we need x and y
body.get(i).setX(body.get(i-1).getX());
body.get(i).setY(body.get(i-1).getY());
}
}
}if(left) {
for(int i = body.size()-1; i>=0;i--) {
if (i == 0) {
body.get(i).setX(body.get(i).getX()-20);
} else {
body.get(i).setX(body.get(i-1).getX());
body.get(i).setY(body.get(i-1).getY());
}
}
}if(down) {
for(int i = body.size()-1; i>=0;i--) {
if (i == 0) {
body.get(i).setY(body.get(i).getY()+20);
} else {
body.get(i).setY(body.get(i-1).getY());
body.get(i).setX(body.get(i-1).getX());
}
}
}if(up) {
for(int i = body.size()-1; i>=0;i--) {
if (i == 0) {
body.get(i).setY(body.get(i).getY()-20);
} else {
body.get(i).setY(body.get(i-1).getY());
body.get(i).setX(body.get(i-1).getX());
}
}
}
}