当我在Java上的主机Snake游戏中吃东西时如何添加尾巴?
                            来源:爱站网时间:2021-12-01编辑:网友分享
                        
                        
                        我创建了一个控制台蛇游戏,并且一些基础知识运行良好。但是,我无法添加拖尾功能。于是爱站技术小编产生了一个问题:当我在Java上的主机Snake游戏中吃东西时如何添加尾巴?面对这个问题,小编翻遍资料,终于找到了答案,现在发出来供大家参考参考。
                        问题描述
我创建了一个控制台蛇游戏,并且一些基础知识运行良好。但是,我无法添加拖尾功能。这是我的代码:
Game.java(Main class)
import java.util.Scanner;
class Game{
public static void main(String[] args){
    Scanner input = new Scanner(System.in);
    World world = new World(10, 5);
    world.init();
    world.draw();
    while(true){
        //get input 
        char direction = input.next().charAt(0);
        //apply input 
        world.getSnake().direction = direction;
        //simulate 
        world.tick();
        //rendering
        world.draw();
    }
}}
World.java
public class World{
private char[][] field;
private int width;
private int height;
private Snake snake;
public World(int width, int height){
    //+2 for borders
    this.width  = width + 2;
    this.height = height + 2;
}
public void init(){
    this.field = new char[height][width];
    //background
    for(int x = 0; x 
Snake.java
public class Snake{
public Point head;
public char direction;
public void init(int x, int y){
    this.head      = new Point();
    this.head.x    = x;
    this.head.y    = y;
    this.direction = 'w';
}
}
Point.java
public class Point{
int x;
int y; }
思路:
尝试添加?那就是您如何显示它。如果蛇吃食物food将为空,并且您在尾巴上加+1 [x] [y]
以上内容就是爱站技术频道小编为大家分享的当我在Java上的主机Snake游戏中吃东西时如何添加尾巴?看完以上分享之后,大家应该都知道如何添加尾巴了吧。
 
                    