Java Draw不动蛇

我正在尝试创建一个蛇克隆作为练习。我画了蛇并添加了运动模式,但是当我按任意键移动时蛇会自己吃掉它。但它不动。该阵列收回起点上的反应堆并且什么也不做。


这是我的蛇类我已经删除了我的评论,因为它们不仅仅是代码而且发布系统不允许我发布


编辑


如果您需要其他班级的任何东西,请告诉我。但我认为我的错误就在这里


编辑 2


添加了整个代码,您只需在新项目中复制粘贴即可重现我的错误。


public class Snake {


    List<Point> sPoints;

    int xDir,yDir;

    boolean isMoving,addTail;

    final int sSize = 20, startX = 150 , startY = 150;

    public Snake(){


        sPoints = new ArrayList<Point>();


        xDir = 0;

        yDir = 0;


        isMoving = false;


        addTail = false;

        sPoints.add(new Point(startX,startY));


        for(int i=1; i<sSize; i++) {



            sPoints.add(new Point(startX - i * 4,startY));

        }

    }


    public void draw(Graphics g){


        g.setColor(Color.white);


        for(Point p : sPoints) {


            g.fillRect(p.getX(),p.getY(),4,4);

        }

    }


    public void move(){

        if (isMoving) {

            Point temp = sPoints.get(0);

            Point last = sPoints.get(sPoints.size() - 1);


            Point newstart = new Point(temp.getX() + xDir * 4, temp.getY() + yDir * 4);



            for (int i = sPoints.size() - 1; i >= 1; i--) {


                sPoints.set(i, sPoints.get(i - 1));

            }


            sPoints.set(0, newstart);

        }

    }


    public int getxDir() {

        return xDir;

    }


    public void setxDir(int x) {

        this.xDir = xDir;

    }


    public int getyDir() {

        return yDir;

    }


    public void setyDir(int y) {

        this.yDir = yDir;

    }


    public  int getX(){

        return sPoints.get(0).getX();

    }


    public int getY(){

        return sPoints.get(0).getY();

    }


    public boolean isMoving() {

        return isMoving;

    }


    public void setIsMoving(boolean b) {

        isMoving = b;

    }

}

下面是点类。只是一些点的 getter setter,对于那些我使用 IntelliJ 自动生成它们的点..(我再次删除了评论)


喵喔喔
浏览 131回答 2
2回答

繁星点点滴滴

这是我阅读您的代码的一些意见。你的蛇不会移动的原因是因为你的snake.setyDir()and&nbsp;snake.setxDir()没有接受输入来覆盖&nbsp;xDirand&nbsp;yDir。他们正在分配给自己。JDK 中已经Point2D为你准备好了一个类移动蛇时,只需要去掉尾巴,在蛇头前多加一个方块即可。你可以保持身体紧绷(据我对蛇的常识)。&nbsp;考虑左侧的 L 形蛇,底端是头部,它目前正向右移动。要移动蛇,请移除尾巴(绿色块)并根据其方向(红色块)在头部添加一条。它的最终状态变成了右边的蛇。LinkedList适合需要。如果使用两个 int(xDir和yDir)来控制蛇的方向令人困惑,您可以通过创建一个enum.&nbsp;那些带有 x 和 y 的 -1、0、1 可能会让您感到困惑。声明常量而不是幻数。例如块 4 的宽度,图像大小 400是Snake.addTail不必要的吗?属性应该有可访问性修饰符最终结果:https://i.stack.imgur.com/IbbND.gif Game.javaimport java.applet.Applet;import java.awt.*;import java.awt.event.KeyEvent;import java.awt.event.KeyListener;import java.util.Arrays;public class Game extends Applet implements Runnable, KeyListener {&nbsp; &nbsp; private final int GAMEBOARD_WIDTH = 400;&nbsp; &nbsp; // setting up double buffering.&nbsp; &nbsp; private Graphics graphics;&nbsp; &nbsp; private Image img;&nbsp; &nbsp; private Thread thread;&nbsp; &nbsp; private Snake snake;&nbsp; &nbsp; public void init() {&nbsp; &nbsp; &nbsp; &nbsp; // setting the size of our Applet&nbsp; &nbsp; &nbsp; &nbsp; this.resize(GAMEBOARD_WIDTH, GAMEBOARD_WIDTH);&nbsp; &nbsp; &nbsp; &nbsp; // we gonna create the image just the same size as our applet.&nbsp; &nbsp; &nbsp; &nbsp; img = createImage(GAMEBOARD_WIDTH, GAMEBOARD_WIDTH);&nbsp; &nbsp; &nbsp; &nbsp; // this represents our offscreen image that we will draw&nbsp; &nbsp; &nbsp; &nbsp; graphics = img.getGraphics();&nbsp; &nbsp; &nbsp; &nbsp; this.addKeyListener(this);&nbsp; &nbsp; &nbsp; &nbsp; snake = new Snake();&nbsp; &nbsp; &nbsp; &nbsp; thread = new Thread(this);&nbsp; &nbsp; &nbsp; &nbsp; thread.start();&nbsp; &nbsp; }&nbsp; &nbsp; public void paint(Graphics g) {&nbsp; &nbsp; &nbsp; &nbsp; // Setting the background of our applet to black&nbsp; &nbsp; &nbsp; &nbsp; graphics.setColor(Color.BLACK);&nbsp; &nbsp; &nbsp; &nbsp; // Fill rectangle 0 , 0 (starts from) for top left corner and then 400,400 to&nbsp; &nbsp; &nbsp; &nbsp; // fill our entire background to black&nbsp; &nbsp; &nbsp; &nbsp; graphics.fillRect(0, 0, GAMEBOARD_WIDTH, GAMEBOARD_WIDTH);&nbsp; &nbsp; &nbsp; &nbsp; snake.draw(graphics);&nbsp; &nbsp; &nbsp; &nbsp; // painting the entire image&nbsp; &nbsp; &nbsp; &nbsp; g.drawImage(img, 0, 0, null);&nbsp; &nbsp; }&nbsp; &nbsp; // Update will call on Paint(g)&nbsp; &nbsp; public void update(Graphics g) {&nbsp; &nbsp; &nbsp; &nbsp; paint(g);&nbsp; &nbsp; }&nbsp; &nbsp; // Repaint will call on Paint(g)&nbsp; &nbsp; public void repaint(Graphics g) {&nbsp; &nbsp; &nbsp; &nbsp; paint(g);&nbsp; &nbsp; }&nbsp; &nbsp; public void run() {&nbsp; &nbsp; &nbsp; &nbsp; // infinite loop&nbsp; &nbsp; &nbsp; &nbsp; for (;;) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; snake.move();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // drawing snake&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; this.repaint();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Creating a time delay&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; try {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Thread.sleep(40);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } catch (InterruptedException e) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; e.printStackTrace();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; public void keyTyped(KeyEvent keyEvent) {&nbsp; &nbsp; }&nbsp; &nbsp; public void keyPressed(KeyEvent keyEvent) {&nbsp; &nbsp; &nbsp; &nbsp; int keyCode = keyEvent.getKeyCode();&nbsp; &nbsp; &nbsp; &nbsp; if (!snake.isMoving()) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // this will allow the snake to start moving, but will disable LEFT for just the&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // 1st move&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (matchKey(keyCode, KeyEvent.VK_UP, KeyEvent.VK_RIGHT, KeyEvent.VK_DOWN)) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; snake.setIsMoving(true);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; // setting up Key mapping so when the user presses UP,RIGHT,DOWN,LEFT. the Snake&nbsp; &nbsp; &nbsp; &nbsp; // will move accordingly&nbsp; &nbsp; &nbsp; &nbsp; if (matchKey(keyCode, KeyEvent.VK_UP)) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; snake.setDirection(Direction.UP);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; if (matchKey(keyCode, KeyEvent.VK_RIGHT)) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; snake.setDirection(Direction.RIGHT);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; if (matchKey(keyCode, KeyEvent.VK_DOWN)) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; snake.setDirection(Direction.DOWN);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; if (matchKey(keyCode, KeyEvent.VK_LEFT)) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; snake.setDirection(Direction.LEFT);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; // return true if targetKey contains the provided keyCode&nbsp; &nbsp; private boolean matchKey(int keyCode, int... targetKey) {&nbsp; &nbsp; &nbsp; &nbsp; return Arrays.stream(targetKey).anyMatch(i -> i == keyCode);&nbsp; &nbsp; }&nbsp; &nbsp; public void keyReleased(KeyEvent keyEvent) {&nbsp; &nbsp; }}Snake.javaimport java.awt.Color;import java.awt.Graphics;import java.awt.geom.Point2D;import java.util.LinkedList;public class Snake {&nbsp; &nbsp; private final int sSize = 20, startX = 150, startY = 150;&nbsp; &nbsp; private final int BLOCK_WIDTH = 4;&nbsp; &nbsp; private LinkedList<Point2D.Float> sPoints;&nbsp; &nbsp; private boolean isMoving;&nbsp; &nbsp; private Direction direction;&nbsp; &nbsp; public Snake() {&nbsp; &nbsp; &nbsp; &nbsp; sPoints = new LinkedList<Point2D.Float>();&nbsp; &nbsp; &nbsp; &nbsp; isMoving = false;&nbsp; &nbsp; &nbsp; &nbsp; sPoints.add(new Point2D.Float(startX, startY));&nbsp; &nbsp; &nbsp; &nbsp; for (int i = 1; i < sSize; i++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; sPoints.add(new Point2D.Float(startX - i * BLOCK_WIDTH, startY));&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; public void draw(Graphics g) {&nbsp; &nbsp; &nbsp; &nbsp; g.setColor(Color.white);&nbsp; &nbsp; &nbsp; &nbsp; for (Point2D p : sPoints) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; g.fillRect((int) p.getX(), (int) p.getY(), BLOCK_WIDTH, BLOCK_WIDTH);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; public void move() {&nbsp; &nbsp; &nbsp; &nbsp; if (isMoving) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; sPoints.removeLast();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; steer(sPoints.getFirst());&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; private void steer(Point2D head) {&nbsp; &nbsp; &nbsp; &nbsp; Point2D.Float newHead = new Point2D.Float();&nbsp; &nbsp; &nbsp; &nbsp; switch (this.getDirection()) {&nbsp; &nbsp; &nbsp; &nbsp; case UP:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; newHead.setLocation(head.getX(), head.getY() - BLOCK_WIDTH);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; case DOWN:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; newHead.setLocation(head.getX(), head.getY() + BLOCK_WIDTH);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; case LEFT:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; newHead.setLocation(head.getX() - BLOCK_WIDTH, head.getY());&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; case RIGHT:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; newHead.setLocation(head.getX() + BLOCK_WIDTH, head.getY());&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; this.sPoints.addFirst(newHead);&nbsp; &nbsp; }&nbsp; &nbsp; public int getX() {&nbsp; &nbsp; &nbsp; &nbsp; return (int) sPoints.get(0).getX();&nbsp; &nbsp; }&nbsp; &nbsp; public int getY() {&nbsp; &nbsp; &nbsp; &nbsp; return (int) sPoints.get(0).getY();&nbsp; &nbsp; }&nbsp; &nbsp; public boolean isMoving() {&nbsp; &nbsp; &nbsp; &nbsp; return isMoving;&nbsp; &nbsp; }&nbsp; &nbsp; public void setIsMoving(boolean b) {&nbsp; &nbsp; &nbsp; &nbsp; isMoving = b;&nbsp; &nbsp; }&nbsp; &nbsp; public Direction getDirection() {&nbsp; &nbsp; &nbsp; &nbsp; return direction;&nbsp; &nbsp; }&nbsp; &nbsp; public void setDirection(Direction d) {&nbsp; &nbsp; &nbsp; &nbsp; if (this.getDirection() == null) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; this.direction = d;&nbsp; &nbsp; &nbsp; &nbsp; } else if (!this.getDirection().isOpposite(d)) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; this.direction = d;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}Direction.javapublic enum Direction {&nbsp; &nbsp; UP(-1), DOWN(1), LEFT(-2), RIGHT(2);&nbsp; &nbsp; int vector;&nbsp; &nbsp; Direction(int i) {&nbsp; &nbsp; &nbsp; &nbsp; this.vector = i;&nbsp; &nbsp; }&nbsp; &nbsp; public boolean isOpposite(Direction d) {&nbsp; &nbsp; &nbsp; &nbsp; return this.vector + d.vector == 0;&nbsp; &nbsp; }}

慕码人8056858

Snack.javaimport java.awt.EventQueue;import javax.swing.JFrame;public class Snake extends JFrame {&nbsp; &nbsp; public Snake() {&nbsp; &nbsp; &nbsp; &nbsp; initUI();&nbsp; &nbsp; }&nbsp; &nbsp; private void initUI() {&nbsp; &nbsp; &nbsp; &nbsp; add(new Board());&nbsp; &nbsp; &nbsp; &nbsp; setResizable(false);&nbsp; &nbsp; &nbsp; &nbsp; pack();&nbsp; &nbsp; &nbsp; &nbsp; setTitle("Snake");&nbsp; &nbsp; &nbsp; &nbsp; setLocationRelativeTo(null);&nbsp; &nbsp; &nbsp; &nbsp; setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);&nbsp; &nbsp; }&nbsp; &nbsp; public static void main(String[] args) {&nbsp; &nbsp; &nbsp; &nbsp; EventQueue.invokeLater(() -> {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; JFrame ex = new Snake();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ex.setVisible(true);&nbsp; &nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; }}Board.javaimport java.awt.Color;import java.awt.Dimension;import java.awt.Font;import java.awt.FontMetrics;import java.awt.Graphics;import java.awt.Image;import java.awt.Toolkit;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.awt.event.KeyAdapter;import java.awt.event.KeyEvent;import javax.swing.ImageIcon;import javax.swing.JPanel;import javax.swing.Timer;public class Board extends JPanel implements ActionListener {&nbsp; &nbsp; private final int B_WIDTH = 300;&nbsp; &nbsp; private final int B_HEIGHT = 300;&nbsp; &nbsp; private final int DOT_SIZE = 10;&nbsp; &nbsp; private final int ALL_DOTS = 900;&nbsp; &nbsp; private final int RAND_POS = 29;&nbsp; &nbsp; private final int DELAY = 140;&nbsp; &nbsp; private final int x\[\] = new int\[ALL_DOTS\];&nbsp; &nbsp; private final int y\[\] = new int\[ALL_DOTS\];&nbsp; &nbsp; private int dots;&nbsp; &nbsp; private int apple_x;&nbsp; &nbsp; private int apple_y;&nbsp; &nbsp; private boolean leftDirection = false;&nbsp; &nbsp; private boolean rightDirection = true;&nbsp; &nbsp; private boolean upDirection = false;&nbsp; &nbsp; private boolean downDirection = false;&nbsp; &nbsp; private boolean inGame = true;&nbsp; &nbsp; private Timer timer;&nbsp; &nbsp; private Image ball;&nbsp; &nbsp; private Image apple;&nbsp; &nbsp; private Image head;&nbsp; &nbsp; public Board() {&nbsp; &nbsp; &nbsp; &nbsp; initBoard();&nbsp; &nbsp; }&nbsp; &nbsp; private void initBoard() {&nbsp; &nbsp; &nbsp; &nbsp; addKeyListener(new TAdapter());&nbsp; &nbsp; &nbsp; &nbsp; setBackground(Color.black);&nbsp; &nbsp; &nbsp; &nbsp; setFocusable(true);&nbsp; &nbsp; &nbsp; &nbsp; setPreferredSize(new Dimension(B_WIDTH, B_HEIGHT));&nbsp; &nbsp; &nbsp; &nbsp; loadImages();&nbsp; &nbsp; &nbsp; &nbsp; initGame();&nbsp; &nbsp; }&nbsp; &nbsp; private void loadImages() {&nbsp; &nbsp; &nbsp; &nbsp; ImageIcon iid = new ImageIcon("src/resources/dot.png");&nbsp; &nbsp; &nbsp; &nbsp; ball = iid.getImage();&nbsp; &nbsp; &nbsp; &nbsp; ImageIcon iia = new ImageIcon("src/resources/apple.png");&nbsp; &nbsp; &nbsp; &nbsp; apple = iia.getImage();&nbsp; &nbsp; &nbsp; &nbsp; ImageIcon iih = new ImageIcon("src/resources/head.png");&nbsp; &nbsp; &nbsp; &nbsp; head = iih.getImage();&nbsp; &nbsp; }&nbsp; &nbsp; private void initGame() {&nbsp; &nbsp; &nbsp; &nbsp; dots = 3;&nbsp; &nbsp; &nbsp; &nbsp; for (int z = 0; z < dots; z++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; x\[z\] = 50 - z * 10;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; y\[z\] = 50;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; locateApple();&nbsp; &nbsp; &nbsp; &nbsp; timer = new Timer(DELAY, this);&nbsp; &nbsp; &nbsp; &nbsp; timer.start();&nbsp; &nbsp; }&nbsp; &nbsp; @Override&nbsp; &nbsp; public void paintComponent(Graphics g) {&nbsp; &nbsp; &nbsp; &nbsp; super.paintComponent(g);&nbsp; &nbsp; &nbsp; &nbsp; doDrawing(g);&nbsp; &nbsp; }&nbsp; &nbsp; private void doDrawing(Graphics g) {&nbsp; &nbsp; &nbsp; &nbsp; if (inGame) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; g.drawImage(apple, apple_x, apple_y, this);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for (int z = 0; z < dots; z++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (z == 0) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; g.drawImage(head, x\[z\], y\[z\], this);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; g.drawImage(ball, x\[z\], y\[z\], this);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Toolkit.getDefaultToolkit().sync();&nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; gameOver(g);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; }&nbsp; &nbsp; private void gameOver(Graphics g) {&nbsp; &nbsp; &nbsp; &nbsp; String msg = "Game Over";&nbsp; &nbsp; &nbsp; &nbsp; Font small = new Font("Helvetica", Font.BOLD, 14);&nbsp; &nbsp; &nbsp; &nbsp; FontMetrics metr = getFontMetrics(small);&nbsp; &nbsp; &nbsp; &nbsp; g.setColor(Color.white);&nbsp; &nbsp; &nbsp; &nbsp; g.setFont(small);&nbsp; &nbsp; &nbsp; &nbsp; g.drawString(msg, (B_WIDTH - metr.stringWidth(msg)) / 2, B_HEIGHT / 2);&nbsp; &nbsp; }&nbsp; &nbsp; private void checkApple() {&nbsp; &nbsp; &nbsp; &nbsp; if ((x\[0\] == apple_x) && (y\[0\] == apple_y)) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; dots++;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; locateApple();&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; private void move() {&nbsp; &nbsp; &nbsp; &nbsp; for (int z = dots; z > 0; z--) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; x\[z\] = x\[(z - 1)\];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; y\[z\] = y\[(z - 1)\];&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; if (leftDirection) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; x\[0\] -= DOT_SIZE;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; if (rightDirection) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; x\[0\] += DOT_SIZE;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; if (upDirection) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; y\[0\] -= DOT_SIZE;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; if (downDirection) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; y\[0\] += DOT_SIZE;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; private void checkCollision() {&nbsp; &nbsp; &nbsp; &nbsp; for (int z = dots; z > 0; z--) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if ((z > 4) && (x\[0\] == x\[z\]) && (y\[0\] == y\[z\])) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; inGame = false;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; if (y\[0\] >= B_HEIGHT) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; inGame = false;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; if (y\[0\] < 0) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; inGame = false;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; if (x\[0\] >= B_WIDTH) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; inGame = false;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; if (x\[0\] < 0) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; inGame = false;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; if (!inGame) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; timer.stop();&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; private void locateApple() {&nbsp; &nbsp; &nbsp; &nbsp; int r = (int) (Math.random() * RAND_POS);&nbsp; &nbsp; &nbsp; &nbsp; apple_x = ((r * DOT_SIZE));&nbsp; &nbsp; &nbsp; &nbsp; r = (int) (Math.random() * RAND_POS);&nbsp; &nbsp; &nbsp; &nbsp; apple_y = ((r * DOT_SIZE));&nbsp; &nbsp; }&nbsp; &nbsp; @Override&nbsp; &nbsp; public void actionPerformed(ActionEvent e) {&nbsp; &nbsp; &nbsp; &nbsp; if (inGame) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; checkApple();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; checkCollision();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; move();&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; repaint();&nbsp; &nbsp; }&nbsp; &nbsp; private class TAdapter extends KeyAdapter {&nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; public void keyPressed(KeyEvent e) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; int key = e.getKeyCode();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if ((key == KeyEvent.VK_LEFT) && (!rightDirection)) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; leftDirection = true;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; upDirection = false;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; downDirection = false;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if ((key == KeyEvent.VK_RIGHT) && (!leftDirection)) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; rightDirection = true;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; upDirection = false;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; downDirection = false;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if ((key == KeyEvent.VK_UP) && (!downDirection)) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; upDirection = true;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; rightDirection = false;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; leftDirection = false;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if ((key == KeyEvent.VK_DOWN) && (!upDirection)) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; downDirection = true;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; rightDirection = false;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; leftDirection = false;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java