蛇游戏 - 不能引导蛇

Actual question on the bottom of the post!
起初,我想解释一下我的问题 .

我正在写一个基本的Snake游戏,我让蛇自动移动 . 当您执行代码时,它会自动移动到窗口的右侧,就像预期的那样 . 但是,我不能按照我想要的方式操纵我的蛇,它根本不会改变它的方向 .


为避免混淆, player 是类 Snake 的一个实例 .


解释蛇的运动:

Snake 对象具有 coordinates 属性,该属性是包含 SnakePart 对象的ArrayList . 每个 SnakePart 对象都具有 xy 属性 . 使用此ArrayList,蛇通过在画布的y轴和x轴上使用 xy 属性在画布上绘制小矩形来移动 .

Snake 对象还具有 dxdy 属性,该属性被添加(或减去 - 取决于蛇的方向)到 SnakePart 对象的 xy 属性,以使蛇向一个方向移动 .


要更新 Snake.java 中的ArrayList:

public void move() {
  SnakePart head = new SnakePart(this.coordinates.get(0).x + this.dx, this.coordinates.get(0).y + this.dy);

  this.coordinates.add(0, head);
  this.coordinates.remove(this.coordinates.size() - 1);
}

要在 Board.java 画布上绘制蛇(部分原因,此时方法的其余部分不是必需的):

@Override
public void paintComponent(Graphics g) {
  this.player.coordinates.forEach(snakePart -> {
    g.setColor(Color.BLUE);
    g.fillRect(snakePart.x, snakePart.y, 10, 10);
  });
}

为了操纵蛇,我想使用箭头键 . 根据按下的箭头键,蛇的 xy 坐标/属性被修改( Board.java ):

@Override
public void keyPressed(KeyEvent event) {
  int keyCode = event.getKeyCode();

  if (keyCode == 37) {
    this.player.dx = -10;
    this.player.dy = 0;
  } else if (keyCode == 38) {
    this.player.dx = 0;
    this.player.dy = -10;
  } else if (keyCode == 39) {
    this.player.dx = 10;
    this.player.dy = 0;
  } else if (keyCode == 40) {
    this.player.dx = 0;
    this.player.dy = 10;
  }
}

整码:

Snake.java

package com.codef0x.snake;
import java.util.ArrayList;

public class Snake {
  ArrayList < SnakePart > coordinates;
  int dx = 10;
  int dy = 0;

  public Snake(ArrayList < SnakePart > coords) {
    this.coordinates = coords;
  }

  public void move() {
    SnakePart head = new SnakePart(this.coordinates.get(0).x + this.dx, this.coordinates.get(0).y + this.dy);

    this.coordinates.add(0, head);
    this.coordinates.remove(this.coordinates.size() - 1);
  }

  public void grow() {
    SnakePart newPart = new SnakePart(0, 0);
    newPart.x = this.coordinates.get(this.coordinates.size() - 1).x - 10;
    newPart.y = this.coordinates.get(this.coordinates.size() - 1).y;

    this.coordinates.add(this.coordinates.size() - 1, newPart);
  }
}

Board.java (仅显示相关部分,否则代码太多)

package com.codef0x.snake;

import javax.swing.*;
import java.awt.*;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.ArrayList;
import java.util.Timer;
import java.util.TimerTask;


public class Board extends JPanel implements KeyListener {
  Snake player;
  ArrayList<SnakePart> snakeCoordinates;

  public Board() {

    this.snakeCoordinates = new ArrayList<>();

    snakeCoordinates.add(new SnakePart(150, 150));
    snakeCoordinates.add(new SnakePart(140, 150));
    snakeCoordinates.add(new SnakePart(130, 150));
    snakeCoordinates.add(new SnakePart(120, 150));

    this.player = new Snake(snakeCoordinates);

    this.food = new Food();
  }

  @Override
  public void paintComponent(Graphics g) {
    clear(g);

    this.player.coordinates.forEach(snakePart - > {
      g.setColor(Color.BLUE);
      g.fillRect(snakePart.x, snakePart.y, 10, 10);
    });
  }

  public void clear(Graphics g) {
    g.clearRect(0, 0, getHeight(), getWidth());
  }

  @Override
  public void update(Graphics g) {
    paintComponent(g);
  }

  @Override
  public void keyPressed(KeyEvent event) {
    int keyCode = event.getKeyCode();

    if (keyCode == 37) {
      this.player.dx = -10;
      this.player.dy = 0;
    } else if (keyCode == 38) {
      this.player.dx = 0;
      this.player.dy = -10;
    } else if (keyCode == 39) {
      this.player.dx = 10;
      this.player.dy = 0;
    } else if (keyCode == 40) {
      this.player.dx = 0;
      this.player.dy = 10;
    }
  }

  @Override
  public void keyTyped(KeyEvent event) {
    return;
  }

  @Override
  public void keyReleased(KeyEvent event) {
    return;
  }

  public void run(Board board) {
    Timer game = new Timer();
    game.schedule(new TimerTask() {
      boolean initiallySpawned = false;
      @Override
      public void run() {
        Graphics g = board.getGraphics();

        if (hitSomething()) { // removed method hitSomething, not relevant
          game.cancel();
          return;
        }

        player.move();
        update(g);
      }
    }, 0, 500);
  }
}

SnakePart.java

package com.codef0x.snake;

public class SnakePart {
  int x;
  int y;

  public SnakePart(int x, int y) {
    this.x = x;
    this.y = y;
  }
}

我做错了什么,我需要改变什么才能正确地操纵蛇?


如果您仍然希望/需要查看所有文件,请在此处查看它们:

Snake.java
Board.java
SnakePart.java
Food.java < - 不相关,但可以防止对 Food 对象的混淆

回答(1)

3 years ago

问题是主要的 . 您创建一个 board 来托管您的游戏状态,并创建不同的一个来聆听键盘 .

public static void main(String[] args) {

    JFrame frame = new JFrame("Snake");
    frame.setDefaultCloseOperation(3);

    Board board = new Board();
    frame.add(board);
    frame.setSize(500, 500);
    frame.addKeyListener(new Board());
    frame.setVisible(true);

    board.run(board);
}

它应该是:

public static void main(String[] args) {

    JFrame frame = new JFrame("Snake");
    frame.setDefaultCloseOperation(3);

    Board board = new Board();
    frame.add(board);
    frame.setSize(500, 500);
    frame.addKeyListener(board);
    frame.setVisible(true);


    board.run(board);
}

另外 board.run(board) 没什么意义,在 run 方法的范围内,板子可以交换到 this (并省略)...