我还在学习java编程,我还有很长的路要走。我试图编写我的第一个游戏,即蛇游戏,我遇到了一些我不太懂的问题。
这是我的代码:
渲染器类
Book::Author
蛇
public class Renderer {
private static Frame frame;
private static Canvas canvas;
private final static int WIDTH = 500;
private final static int HEIGHT = 500;
private final static int tileSize = 25;
private final static int width_game = 510;
private final static int height_game = 510;
private static int ticks = 0;
static Board board = new Board(0,0,tileSize);
static Snake snake;
static int AposX = (int)(Math.random() * 20);
static int AposY = (int)(Math.random() * 20);
static Apple apple = new Apple(AposX, AposY, tileSize);
public static int posX = 7;
public static int posY = 10;
public static int snakeSize = 3;
static Control control;
static int direction = 3; //0 = up, 1 = right, 2 = down, 3 = left
public static boolean right = true, left = false, up = false, down = false;
public static ArrayList <Snake> snakeList = new ArrayList<Snake>();
public static void init(){
frame = new Frame();
canvas = new Canvas();
//Add user control
control = new Control(direction);
canvas.addKeyListener(control);
canvas.setFocusable(true);
canvas.setSize(new Dimension(WIDTH, HEIGHT));
frame.add(canvas);
frame.pack();
frame.setTitle("Snake Game");
frame.setResizable(false);
frame.setLocationRelativeTo(null);
frame.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
Main.quit();
}
});
frame.setVisible(true);
startRendering();
}
public static void startRendering(){
Thread thread = new Thread(){
public void run(){
while(true){
draw();
}
}
};
thread.start();
}
public static void draw(){
GraphicsConfiguration gc = canvas.getGraphicsConfiguration();
VolatileImage vImage = gc.createCompatibleVolatileImage(WIDTH, HEIGHT);
while(true){
ticks++;
if(vImage.validate(gc) == VolatileImage.IMAGE_INCOMPATIBLE){
vImage = gc.createCompatibleVolatileImage(WIDTH, HEIGHT);
}
Graphics g = vImage.getGraphics();
//Clear Frame
g.clearRect(0, 0, WIDTH, HEIGHT);
//To create Snake in the start inside the array
int index = 0;
while(snakeList.size() != snakeSize){
snake = new Snake(posX, posY, tileSize);
snakeList.add(snake);
}
//Frame to update the snake to move
if(ticks > 100){
direction = control.getDirection();
if(direction == 1)posX++;
if(direction == 3)posX--;
if(direction == 2)posY++;
if(direction == 0)posY--;
snake = new Snake(posX, posY, tileSize);
snakeList.add(snake);
if(snakeList.size() > snakeSize){
snakeList.remove(0);
}
ticks = 0;
//If the snake out of the area, it will go back to another side
if(posX > 20){posX = -1;}
if(posX < -1){posX = 20;}
if(posY > 20){posY = -1;}
if(posY < -1){posY = 20;}
if(snakeList.get(0).getSnakePosX() == apple.getApplePosX() && snakeList.get(0).getSnakePosY() == apple.getApplePosY()){
apple.update();
snakeSize++;
}
}
//Rendering the snake
for(int x = 0; x < snakeList.size(); x++){
snakeList.get(x).render(g);
}
//Rendering the apple
apple.render(g);
//Rendering the board grid
board.render(g);
g = canvas.getGraphics();
g.drawImage(vImage, 0, 0, width_game, height_game, null);
g.dispose();
}
}
}
好的问题是,蛇会在尾巴的末端而不是头部与苹果接触。但是在代码中
import java.awt.Color;
import java.awt.Graphics;
public class Snake {
public int posX;
public int posY;
public int tileSize;
final static public int height = 500;
final static public int width = 500;
public int direction = 1; //0 = up, 1 = right, 2 = down, 3 = left
public Snake(int posX, int posY, int tileSize){
this.posX = posX;
this.posY = posY;
this.tileSize = tileSize;
}
public void render(Graphics g){
g.setColor(Color.blue);
g.fillRect(posX*tileSize, posY*tileSize, tileSize, tileSize);
}
public void update(){
}
public int getSnakePosX(){return posX;}
public int getSnakePosY(){return posY;}
}
我清楚地定义了第一个索引,它是0,这是蛇体部分的第一个是头部。我在这里找不到问题。阵列的头部位于[2]。
我希望这真的有帮助,我很快就会得到解决方案, 谢谢。
以下是我认为可能存在问题的领域:
if(snakeList.get(0).getSnakePosX() == apple.getApplePosX() && snakeList.get(0).getSnakePosY() == apple.getApplePosY()){
apple.update();
snakeSize++;
}
上面的代码用于创建前3个身体部位。
while(snakeList.size() != snakeSize){
snake = new Snake(posX, posY, tileSize);
snakeList.add(snake);
}
这一个是这样的,如果发生碰撞,蛇的大小会增加。