如何在蛇游戏中创建运动?

时间:2017-03-08 19:09:00

标签: java

我正在尝试制作吃苹果的经典蛇游戏,但我对这些动作有问题。蛇能够对角线移动。例如,当蛇向右移动并且我点击向上箭头键时,它将对角线而不是直线向上y坐标。我该如何解决这个问题?谢谢。

Game.java

public class Game extends Canvas implements Runnable, KeyListener {

public static int width = 300;
public static int height = width / 16 * 9;
public static int scale = 3;

private Thread thread;
private JFrame frame;
private Snake snake;
private boolean running = false;


public Game(){
    Dimension size = new Dimension(width * scale, height * scale);
    setPreferredSize(size);
    setFocusable(true);
    setFocusTraversalKeysEnabled(false);
    addKeyListener(this);

    frame = new JFrame();
    snake = new Snake(200, 200);
}

public synchronized void start(){
    running = true;
    thread = new Thread(this, "Display");
    thread.start();
}

public synchronized void stop(){
    running = false;
    try{
        thread.join();
    }catch (InterruptedException e){
        e.printStackTrace();
    }
}

@Override
public void run(){
    while (running){
        update();
        render();
    }
}

public void update(){

}

public void render(){
    BufferStrategy bs = getBufferStrategy();
        if(bs == null){
            createBufferStrategy(3);
            return;
        }

        Graphics g = bs.getDrawGraphics();
        g.setColor(Color.black);
        g.fillRect(0, 0, getWidth(), getHeight());

        snake.draw(g);

        g.dispose();
        bs.show();
}

@Override
public void keyPressed(KeyEvent e) {
    int key = e.getKeyCode();

    if (key == KeyEvent.VK_W)  {
        snake.setYD(-1);
    }

    if(key == KeyEvent.VK_S){
        snake.setYD(1);
    }


    if(key == KeyEvent.VK_A){
        snake.setXD(-1);
    }

    if(key == KeyEvent.VK_D){
        snake.setXD(1);
    }

}

@Override
public void keyReleased(KeyEvent e) {

}

@Override
public void keyTyped(KeyEvent e) {
    // TODO Auto-generated method stub

}

public static void main (String[] args){
    Game game = new Game(); 

    game.frame.setResizable(false);
    game.frame.setTitle("Snake");
    game.frame.add(game);
    game.frame.pack();
    game.frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    game.frame.setLocationRelativeTo(null);
    game.frame.setVisible(true);

    game.start();
}
}

Entity.java

public abstract class Entity {

protected int x, y;

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

public void draw(Graphics g){
}

public int getX() {return x;}
public int getY() {return y;}

}

Snake.java

public class Snake extends Entity {

private int xDirection, yDirection;

public Snake(int x, int y) {
    super(x, y);
}

public void draw(Graphics g){
    move();

    g.setColor(Color.blue);
    g.fillRect(x, y, 15, 15);
}

public void move(){
    x += xDirection;
    y += yDirection;
}



public void setXD(int value){
    xDirection = value;
}

public void setYD(int value){
    yDirection = value;
}

2 个答案:

答案 0 :(得分:1)

你需要阻止蛇在另一个轴上移动。这应该可以解决问题。

if (key == KeyEvent.VK_W)  {
    snake.setYD(-1);
    snake.setXD(0);
}

if(key == KeyEvent.VK_S){
    snake.setYD(1);
    snake.setXD(0);
}


if(key == KeyEvent.VK_A){
    snake.setXD(-1);
    snake.setYD(0);
}

if(key == KeyEvent.VK_D){
    snake.setXD(1);
    snake.setYD(0);
}

答案 1 :(得分:1)

您将坐标设置为1和-1,但是您从未将其设置为0.因此,如果您将y设置为1,则将x设置为1,那么它将是对角线移动(1,1)。如果您不想移动对角线,则在将x设置为1或-1时应将y设置为零。

所以

CREATE DEFINER=`root`@`localhost` PROCEDURE `Proc_selectvideo`(IN `type` VARCHAR(500), IN `id` VARCHAR(500))

select * from tbl_news,tbl_video where  tbl_news.nid  = tbl_video.cid and tbl_news.ntype=type and id

在此之后你只能直接移动。 (可能它不是最美丽的解决方案,但这是你的问题的答案)