SnakeGame如何使尾巴跟随头部?

SnakeGame如何使尾巴跟随头部?,第1张

SnakeGame如何使尾巴跟随头部

基本思想是,您需要某种

List
包含蛇的所有点的东西。从概念上讲,
List
它将包含虚拟坐标,即
1x1
表示虚拟空间中的坐标,该坐标表示虚拟板上的位置(宽度和高度会有所增加)。

然后,您可以将其翻译到屏幕上,这样将允许蛇的每个部分都大于单个像素。因此,如果每个部分都是

5x5
像素,则
1x1
实际上将
5x5
在屏幕中。

每次蛇移动时,都向头部添加一个新值,并从尾部删除最后一个值(假设它没有增长)。当您需要画蛇时,您只需遍历

List
,就可以画蛇的每个点。

以下是一个简单的示例,该示例使用一个

linkedList
,将一个新的元素推
Point
到上
List
,制作一个新的头部,并在每个循环中都删除最后一个元素(尾部)。

基本上可以归结为…

snakeBody.removeLast();snakeBody.push(new Point(xPos, yPos));

作为可运行的概念

import java.awt.Color;import java.awt.Dimension;import java.awt.EventQueue;import java.awt.Graphics;import java.awt.Graphics2D;import java.awt.Point;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.awt.event.KeyEvent;import java.util.linkedList;import javax.swing.AbstractAction;import javax.swing.Action;import javax.swing.ActionMap;import javax.swing.InputMap;import javax.swing.Jframe;import javax.swing.JPanel;import javax.swing.KeyStroke;import javax.swing.Timer;import javax.swing.UIManager;import javax.swing.UnsupportedLookAndFeelException;public class Snake {    public static void main(String[] args) {        new Snake();    }    public Snake() {        EventQueue.invokeLater(new Runnable() { @Override public void run() {     try {         UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());     } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {         ex.printStackTrace();     }     Jframe frame = new Jframe("Testing");     frame.setDefaultCloseOperation(Jframe.EXIT_ON_CLOSE);     frame.add(new TestPane());     frame.pack();     frame.setLocationRelativeTo(null);     frame.setVisible(true); }        });    }    public static class TestPane extends JPanel {        public enum Direction { UP, DOWN, LEFT, RIGHT        }        private int xPos, yPos;        private Direction direction = Direction.UP;        private linkedList<Point> snakeBody = new linkedList<>();        public TestPane() { xPos = 100; yPos = 100; for (int index = 0; index < 50; index++) {     snakeBody.add(new Point(xPos, yPos)); } bindKeyStrokeTo("up.pressed", KeyStroke.getKeyStroke(KeyEvent.VK_UP, 0, false), new MoveAction(Direction.UP)); bindKeyStrokeTo("down.pressed", KeyStroke.getKeyStroke(KeyEvent.VK_DOWN, 0, false), new MoveAction(Direction.DOWN)); bindKeyStrokeTo("left.pressed", KeyStroke.getKeyStroke(KeyEvent.VK_LEFT, 0, false), new MoveAction(Direction.LEFT)); bindKeyStrokeTo("right.pressed", KeyStroke.getKeyStroke(KeyEvent.VK_RIGHT, 0, false), new MoveAction(Direction.RIGHT)); Timer timer = new Timer(40, new ActionListener() {     @Override     public void actionPerformed(ActionEvent e) {         switch (direction) {  case UP:      yPos--;      break;  case DOWN:      yPos++;      break;  case LEFT:      xPos--;      break;  case RIGHT:      xPos++;      break;         }         if (yPos < 0) {  yPos--;         } else if (yPos > getHeight() - 1) {  yPos = getHeight() - 1;         }         if (xPos < 0) {  xPos--;         } else if (xPos > getWidth() - 1) {  xPos = getWidth() - 1;         }         snakeBody.removeLast();         snakeBody.push(new Point(xPos, yPos));         repaint();     } }); timer.start();        }        public void bindKeyStrokeTo(String name, KeyStroke keyStroke, Action action) { InputMap im = getInputMap(WHEN_IN_FOCUSED_WINDOW); ActionMap am = getActionMap(); im.put(keyStroke, name); am.put(name, action);        }        @Override        public Dimension getPreferredSize() { return new Dimension(200, 200);        }        @Override        protected void paintComponent(Graphics g) { super.paintComponent(g); Graphics2D g2d = (Graphics2D) g.create(); g2d.setColor(Color.RED); for (Point p : snakeBody) {     g2d.drawLine(p.x, p.y, p.x, p.y); } g2d.dispose();        }        public class MoveAction extends AbstractAction { private Direction moveIn; public MoveAction(Direction direction) {     this.moveIn = direction; } @Override public void actionPerformed(ActionEvent e) {     direction = this.moveIn; }        }    }}

现在,它没有碰撞检测或其他功能,但是您可以移动蛇,蛇会自动跟随



欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/zaji/5489583.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-13
下一篇 2022-12-12

发表评论

登录后才能评论

评论列表(0条)

保存