gpt4 book ai didi

java - 蠕虫(蛇)游戏,棋子生长/move

转载 作者:行者123 更新时间:2023-12-01 10:39:01 24 4
gpt4 key购买 nike

我正在编写一款类似于贪吃蛇的游戏。目前,我正在努力编写 move() 和 Growth() 方法。这个游戏的运作方式是,蠕虫从 1 block 开始,每 move 一步就增加 1 block ,直到达到 3 block 。达到3 block 后,只能吃一个苹果才能成长。该游戏中的运动是通过删除第一 block 并添加位于蠕虫前进方向的新 block 来完成的。 Move() 应该将蠕虫 move 一件,grow() 应该将蠕虫增长一件。然而,当调用grow()时,蠕虫在调用move()之前不会生长。我添加了一个私有(private)变量“growPiece”来在调用grow()时存储一个片段,然后在调用move()时添加它。但是,我没有得到应有的结果。

public class Worm {

private int x;
private int y;
private Direction direction;
private List<Piece> pieces;
private Piece growPiece;

public Worm(int originalX, int originalY, Direction originalDirection) {
this.x = originalX;
this.y = originalY;
this.direction = originalDirection;
this.pieces = new ArrayList<Piece>();
this.pieces.add(new Piece(this.x, this.y));
this.growPiece = null;
}

public List<Piece> getPieces() {
return this.pieces;
}

public void move() {
if (this.direction == Direction.RIGHT) {
this.pieces.add(new Piece(this.pieces.get(this.pieces.size() - 1).getX() + 1, this.pieces.get(this.pieces.size() - 1).getY()));
} else if (this.direction == Direction.LEFT) {
this.pieces.add(new Piece(this.pieces.get(this.pieces.size() - 1).getX() + 1, this.pieces.get(this.pieces.size() - 1).getY()));
} else if (this.direction == Direction.DOWN) {
this.pieces.add(new Piece(this.pieces.get(this.pieces.size() - 1).getX(), this.pieces.get(this.pieces.size() - 1).getY() + 1));
} else if (this.direction == Direction.UP) {
this.pieces.add(new Piece(this.pieces.get(this.pieces.size() - 1).getX(), this.pieces.get(this.pieces.size() - 1).getY() - 1));
}
if (this.growPiece != null) {
this.pieces.add(growPiece);
this.growPiece = null;
}
if (this.pieces.size() > 3 && this.growPiece == null) {
this.pieces.remove(0);
}
}

public void grow() {
if (this.direction == Direction.RIGHT) {
this.growPiece = new Piece(this.pieces.get(this.pieces.size() - 1).getX() + 2, this.pieces.get(this.pieces.size() - 1).getY());
} else if (this.direction == Direction.LEFT) {
this.growPiece = new Piece(this.pieces.get(this.pieces.size() - 1).getX() - 2, this.pieces.get(this.pieces.size() - 1).getY());
} else if (this.direction == Direction.DOWN) {
this.growPiece = new Piece(this.pieces.get(this.pieces.size() - 1).getX(), this.pieces.get(this.pieces.size() - 1).getY() + 2);
} else if (this.direction == Direction.UP) {
this.growPiece = new Piece(this.pieces.get(this.pieces.size() - 1).getX(), this.pieces.get(this.pieces.size() - 1).getY() - 2);
}
}

我无法在不使用参数的情况下让方法相互通信。

最佳答案

在函数 move() 中,如果 this.growPiece 在函数开头为 null 或不为 null,并且pieces.size > 3,则增长 block 将被添加,然后立即删除,因为无论哪种方式,当你得到如果,growPiece 为空。

我的意思是,看看 move() 末尾的两个 if 语句。在第一个中,如果 this.growPiece 已被分配,growPiece 将被添加到 ArrayList 中,我必须假设它会达到您想要的效果。添加后,您将growPiece 设置为空。

在下一个 if 条件部分中,growPiece 为 null,现在它始终为 null。

尝试将其设为 else if,看看效果是否更好。

关于java - 蠕虫(蛇)游戏,棋子生长/move ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34521256/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com