一个用JAVA写的贪食蛇实例

  1. import java.awt.*;
  2. import javax.swing.*;
  3. import java.util.*;
  4. import javax.swing.Timer;
  5. import java.awt.event.*;
  6. import javax.swing.border.*;
  7. public class Snake {
  8.     public static void main(String[] args) {
  9.         SnakeFrame frame = new SnakeFrame();
  10.         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  11.         frame.setResizable(false);
  12.         frame.show();
  13.     }
  14. }
  15. class SnakeFrame extends JFrame {
  16.     public static SnakePanel p = new SnakePanel();
  17.     public static Timer t;
  18.     public act a;
  19.     public static JTextField number;
  20.     public SnakeFrame() {
  21.         setTitle("Snake");
  22.         setSize(449490);
  23.         Container contentPane = getContentPane();
  24.         a = new act();
  25.         number = new JTextField();
  26.         number.setBackground(Color.CYAN);
  27.         number.setEditable(false);
  28.         number.setBounds(20054020);
  29.         JPanel panel = new JPanel();
  30.         JLabel lab = new JLabel();
  31.         lab.setText("已吃了");
  32.         lab.setBounds(16034020);
  33.         JLabel lab0 = new JLabel();
  34.         lab0.setText("个");
  35.         lab0.setBounds(24034020);
  36.         panel.setLayout(null);
  37.         panel.add(lab0);
  38.         panel.add(lab);
  39.         number.setFocusable(false);
  40.         number.setText("0");
  41.         panel.add(number);
  42.         panel.setBounds(0048030);
  43.         contentPane.setLayout(null);
  44.         p.setBounds(2040400400);
  45.         Border b = BorderFactory.createEtchedBorder(Color.BLUE, Color.RED);
  46.         panel.setBorder(b);
  47.         contentPane.add(panel);
  48.         contentPane.add(p);
  49.         a.Test();
  50.         t = new Timer(130, a);
  51.         t.start();
  52.     }
  53. }
  54. class act implements ActionListener {
  55.     public static int num = 2;
  56.     int i = 20;
  57.     int j = 2;
  58.     public int m;
  59.     public int n;
  60.     public static int k = 0;
  61.     public LinkedList list = new LinkedList();
  62.     
  63.     {
  64.         Iterator iter = list.iterator();
  65.         list.add(SnakeFrame.p.b[0][1]);
  66.         list.add(SnakeFrame.p.b[0][2]);
  67.         list.add(SnakeFrame.p.b[0][3]);
  68.     }
  69.     public void actionPerformed(ActionEvent e) {
  70.         if (k == 0) {
  71.             if (i == m && j == n) {
  72.                 list.add(SnakeFrame.p.b[0][4]);
  73.                 Test();
  74.                 SnakeFrame.t.setDelay(100 + (int) (100 * Math.pow(0.97, list.size())));
  75.                 SnakeFrame.number.setText(addnum());
  76.             }
  77.             if (list.contains(SnakeFrame.p.b[i][j])) {
  78.                 SnakeFrame.t.stop();
  79.                 JOptionPane.showMessageDialog(null"You Lost!");
  80.             }
  81.             SnakeFrame.p.b[i][j].setBackground(Color.RED);
  82.             list.addFirst(SnakeFrame.p.b[i][j]);
  83.             ((SnakeButton) list.removeLast()).setBackground(Color.WHITE);
  84.             j++;
  85.             if (j > 39) {
  86.                 SnakeFrame.t.stop();
  87.                 JOptionPane.showMessageDialog(null"You Lost!");
  88.             }
  89. //else if(j<=0) 
  90.         } else if (k == 1) {
  91.             if (i == m && j == n) {
  92.                 list.add(SnakeFrame.p.b[0][4]);
  93.                 Test();
  94.                 SnakeFrame.t.setDelay(100 + (int) (100 * Math.pow(0.97, list.size())));
  95.                 SnakeFrame.number.setText(addnum());
  96.             }
  97.             if (list.contains(SnakeFrame.p.b[i][j])) {
  98.                 SnakeFrame.t.stop();
  99.                 JOptionPane.showMessageDialog(null"You Lost!");
  100.             }
  101.             SnakeFrame.p.b[i][j].setBackground(Color.RED);
  102.             list.addFirst(SnakeFrame.p.b[i][j]);
  103.             ((SnakeButton) list.removeLast()).setBackground(Color.WHITE);
  104.             i--;
  105.             if (i < 0) {
  106.                 SnakeFrame.t.stop();
  107.                 JOptionPane.showMessageDialog(null"You Lost!");
  108.             }
  109.         } else if (k == 2) {
  110.             if (i == m && j == n) {
  111.                 list.add(SnakeFrame.p.b[0][4]);
  112.                 Test();
  113.                 SnakeFrame.t.setDelay(100 + (int) (100 * Math.pow(0.97, list.size())));
  114.                 SnakeFrame.number.setText(addnum());
  115.             }
  116.             if (list.contains(SnakeFrame.p.b[i][j])) {
  117.                 SnakeFrame.t.stop();
  118.                 JOptionPane.showMessageDialog(null"You Lost!");
  119.             }
  120.             SnakeFrame.p.b[i][j].setBackground(Color.RED);
  121.             list.addFirst(SnakeFrame.p.b[i][j]);
  122.             ((SnakeButton) list.removeLast()).setBackground(Color.WHITE);
  123.             i++;
  124.             if (i > 39) {
  125.                 SnakeFrame.t.stop();
  126.                 JOptionPane.showMessageDialog(null"You Lost!");
  127.             }
  128.         } else if (k == 3) {
  129.             if (i == m && j == n) {
  130.                 list.add(SnakeFrame.p.b[0][4]);
  131.                 Test();
  132.                 SnakeFrame.t.setDelay(100 + (int) (100 * Math.pow(0.97, list.size())));
  133.                 SnakeFrame.number.setText(addnum());
  134.             }
  135.             if (list.contains(SnakeFrame.p.b[i][j])) {
  136.                 SnakeFrame.t.stop();
  137.                 JOptionPane.showMessageDialog(null"You Lost!");
  138.             }
  139.             SnakeFrame.p.b[i][j].setBackground(Color.RED);
  140.             list.addFirst(SnakeFrame.p.b[i][j]);
  141.             ((SnakeButton) list.removeLast()).setBackground(Color.WHITE);
  142.             j--;
  143.             if (j < 0) {
  144.                 SnakeFrame.t.stop();
  145.                 JOptionPane.showMessageDialog(null"You Lost!");
  146.             }
  147.         }
  148.     }
  149.     public static void changDirect() {
  150.         if (k == 0) {
  151.             k = 1;
  152.         } else if (k == 1) {
  153.             k = 0;
  154.         }
  155.     }
  156.     public void Test() {
  157.         Random rand = new Random();
  158.         m = rand.nextInt(39);
  159.         n = rand.nextInt(39);
  160.         SnakeFrame.p.b[m][n].setBackground(Color.BLUE);
  161.     }
  162.     private String addnum() {
  163.         Integer c = new Integer(list.size());
  164.         int t = c.intValue() - 3;
  165.         Integer d = new Integer(t);
  166.         return d.toString();
  167.     }
  168. }
  169. class SnakePanel extends JPanel {
  170.     SnakeButton[][] b = new SnakeButton[40][40];
  171.     public SnakePanel() {
  172.         setLayout(null);
  173.         for (int i = 0; i <= 39; i++) {
  174.             for (int j = 0; j <= 39; j++) {
  175.                 b[i][j] = new SnakeButton();
  176.                 b[i][j].i = i;
  177.                 b[i][j].j = j;
  178.                 b[i][j].setBounds(i * 10, j * 101010);
  179.                 b[i][j].setBackground(Color.WHITE);
  180.                 b[i][j].setEnabled(false);
  181.                 add(b[i][j]);
  182.             }
  183.         }
  184.         KeyHandle keylist = new KeyHandle();
  185.         addKeyListener(keylist);
  186.         setFocusable(true);
  187.     }
  188.     public static void convert(SnakeButton b1, SnakeButton b2) {
  189.         b1.setBounds(b2.i * 5, b2.j * 555);
  190.         b2.setBounds(b1.i * 5, b1.j * 555);
  191.         int k = b1.i;
  192.         int m = b1.j;
  193.         b1.i = b2.i;
  194.         b2.i = k;
  195.         b1.j = b2.j;
  196.         b2.j = m;
  197.     }
  198. }
  199. class KeyHandle implements KeyListener {
  200.     public void keyPressed(KeyEvent event) {
  201.         int keyCode = event.getKeyCode();
  202.         if (keyCode == KeyEvent.VK_UP && act.k != 0) {
  203.             act.k = 3;
  204.         } else if (keyCode == KeyEvent.VK_LEFT && act.k != 2) {
  205.             act.k = 1;
  206.         } else if (keyCode == KeyEvent.VK_DOWN && act.k != 3) {
  207.             act.k = 0;
  208.         } else if (keyCode == KeyEvent.VK_RIGHT && act.k != 1) {
  209.             act.k = 2;
  210.         }
  211.     }
  212.     public void keyReleased(KeyEvent event) {
  213.     }
  214.     public void keyTyped(KeyEvent event) {
  215.         char keyChar = event.getKeyChar();
  216.         if (keyChar == 'i') {
  217.             System.out.println("hello");
  218.         }
  219.     }
  220. }
  221. class SnakeButton extends JButton {
  222.     public int i;
  223.     public int j;
  224.     public SnakeButton() {
  225.     }
  226. }
以下是一个简单的贪食蛇游戏,使用Java。它包括一个游戏循环,可以处理用户输入,更新游戏状态并绘制游戏画面。 ```java import javax.swing.*; import java.awt.*; import java.awt.event.*; import java.util.*; public class SnakeGame extends JFrame implements ActionListener { private static final int WIDTH = 500; private static final int HEIGHT = 500; private static final int DOT_SIZE = 20; private static final int ALL_DOTS = (WIDTH * HEIGHT) / (DOT_SIZE * DOT_SIZE); private static final int RAND_POS = WIDTH / DOT_SIZE; private static final int DELAY = 100; private ArrayList<Point> snake; private Point food; private Timer timer; private boolean inGame; private boolean leftDirection; private boolean rightDirection; private boolean upDirection; private boolean downDirection; public SnakeGame() { setTitle("Snake Game"); setSize(WIDTH, HEIGHT); setDefaultCloseOperation(EXIT_ON_CLOSE); setResizable(false); setVisible(true); addKeyListener(new TAdapter()); snake = new ArrayList<Point>(); inGame = true; leftDirection = false; rightDirection = true; upDirection = false; downDirection = false; initGame(); } public void initGame() { snake.clear(); snake.add(new Point(100, 100)); snake.add(new Point(80, 100)); snake.add(new Point(60, 100)); generateFood(); timer = new Timer(DELAY, this); timer.start(); } public void generateFood() { int x = (int) (Math.random() * RAND_POS) * DOT_SIZE; int y = (int) (Math.random() * RAND_POS) * DOT_SIZE; food = new Point(x, y); } public void checkFood() { if (snake.get(0).equals(food)) { snake.add(0, new Point(food)); generateFood(); } } public void checkCollision() { for (int i = 1; i < snake.size(); i++) { if (snake.get(0).equals(snake.get(i))) { inGame = false; } } if (snake.get(0).x < 0 || snake.get(0).x >= WIDTH || snake.get(0).y < 0 || snake.get(0).y >= HEIGHT) { inGame = false; } if (!inGame) { timer.stop(); } } public void move() { for (int i = snake.size() - 1; i > 0; i--) { snake.set(i, new Point(snake.get(i-1))); } if (leftDirection) { snake.get(0).x -= DOT_SIZE; } else if (rightDirection) { snake.get(0).x += DOT_SIZE; } else if (upDirection) { snake.get(0).y -= DOT_SIZE; } else if (downDirection) { snake.get(0).y += DOT_SIZE; } } public void paint(Graphics g) { super.paint(g); if (inGame) { g.setColor(Color.RED); g.fillOval(food.x, food.y, DOT_SIZE, DOT_SIZE); g.setColor(Color.BLACK); for (Point p : snake) { g.fillOval(p.x, p.y, DOT_SIZE, DOT_SIZE); } Toolkit.getDefaultToolkit().sync(); } else { gameOver(g); } } public void gameOver(Graphics g) { String msg = "Game Over"; Font font = new Font("Arial", Font.BOLD, 16); FontMetrics metrics = getFontMetrics(font); g.setColor(Color.BLACK); g.setFont(font); g.drawString(msg, (WIDTH - metrics.stringWidth(msg)) / 2, HEIGHT / 2); } public void actionPerformed(ActionEvent e) { if (inGame) { checkFood(); checkCollision(); move(); repaint(); } } private class TAdapter extends KeyAdapter { public void keyPressed(KeyEvent e) { int key = e.getKeyCode(); if (key == KeyEvent.VK_LEFT && !rightDirection) { leftDirection = true; upDirection = false; downDirection = false; } else if (key == KeyEvent.VK_RIGHT && !leftDirection) { rightDirection = true; upDirection = false; downDirection = false; } else if (key == KeyEvent.VK_UP && !downDirection) { upDirection = true; leftDirection = false; rightDirection = false; } else if (key == KeyEvent.VK_DOWN && !upDirection) { downDirection = true; leftDirection = false; rightDirection = false; } } } public static void main(String[] args) { new SnakeGame(); } } ``` 这个游戏使用Java Swing库来处理窗口和绘制。它使用一个ArrayList来跟踪蛇的身体,一个Point来表示食物的位置,以及一些布尔变量来跟踪蛇的方向。游戏循环在ActionListener中实现,并在每个时钟周期中更新游戏状态并重新绘制游戏画面。键盘事件处理程序允许玩家使用箭头键来控制蛇的移动。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值