⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 board.java

📁 Java 2D 游戏开发贪吃蛇,较简单的实现
💻 JAVA
字号:
/* * To change this template, choose Tools | Templates * and open the template in the editor. */package game;import java.awt.Color;import java.awt.Font;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.JLabel;import javax.swing.JPanel;import javax.swing.Timer;/** * * @author ibm */public class Board extends JPanel implements ActionListener {    private Image head;    private Image ball;    private Image apple;    private int dots;    private final int MAX_DOTS = 1200;    private int x[] = new int[MAX_DOTS];    private int y[] = new int[MAX_DOTS];    private Timer time;    private final int DELAY = 145;    private boolean inGame = true;    private int apple_x;    private int apple_y;    private final int WIDTH = 300;    private final int HEIGHT = 400;    private boolean up = false;    private boolean down = false;    private boolean left = false;    private boolean right = true;    private final int SPEED_SNAKE = 10;    private boolean stop = false; //   private JLabel label; //   private int scorce=0; //   public Board(JLabel lebal) {    public Board(){   //     this.label=label;        this.addKeyListener(new Adapter());        ImageIcon h = new ImageIcon(this.getClass().getResource("head.png"));        head = h.getImage();        System.out.println(head.getWidth(this));        System.out.println(head.getHeight(this));        ImageIcon d = new ImageIcon(this.getClass().getResource("dot.png"));        ball = d.getImage();        System.out.println(ball.getWidth(this));        System.out.println(ball.getHeight(this));        ImageIcon a = new ImageIcon(this.getClass().getResource("apple.png"));        apple = a.getImage();        System.out.println(apple.getWidth(this));        System.out.println(apple.getHeight(this));        this.setBackground(Color.black);        this.setFocusable(true);                initGame();    }    private void Game_Over(Graphics g) {        String str = "GAME OVER";        Font mes = new Font(str, Font.BOLD, 30);        g.setFont(mes);        g.drawString(str, 70, 180);    }    private void LocationApple() {        int x = (int) (Math.random() * 10);        apple_x = x * 30;        int y = (int) (Math.random() * 10);        apple_y = y * 40;    }    private void checkConfilict() {        if ((x[0] > WIDTH) || (x[0] < 0)) {            inGame = false;        }        if ((y[0] > HEIGHT) || (y[0] < 0)) {            inGame = false;        }        for (int i = dots; i > 0; i--) {            if ((x[0] == x[i]) && (y[0] == y[i])) {                inGame = false;            }        }    }    private void checkapple() {        if ((x[0] == apple_x) && (y[0] == apple_y)) {            dots++;    //        scorce+=10;     //       label.setText("Score:");            LocationApple();        }    }    private void initGame() {        dots = 3;        for (int i = 0; i < dots; i++) {            x[i] = 50 - 10 * i;            y[i] = 50;        }        LocationApple();        time = new Timer(DELAY, this);        time.start();    }    private void move() {         for (int i = dots; i > 0; i--) {            x[i] = x[(i - 1)];            y[i] = y[(i - 1)];        }        if (up) {            y[0] -= SPEED_SNAKE;        }        if (down) {            y[0] += SPEED_SNAKE;        }        if (left) {            x[0] -= SPEED_SNAKE;        }        if (right) {            x[0] += SPEED_SNAKE;        }           }    public class Adapter extends KeyAdapter {        public void keyPressed(KeyEvent e) {            int key = e.getKeyCode();            if (key == KeyEvent.VK_SPACE) {                stop = !stop;                if (!stop) {                    time.stop();                } else {                    time.start();                }            }            if ((key == KeyEvent.VK_UP) && (!down)) {                up = true;                left = right = false;            }            if ((key == KeyEvent.VK_DOWN) && (!up)) {                down = true;                left = right = false;            }            if ((key == KeyEvent.VK_LEFT) && (!right)) {                left = true;                up = down = false;            }            if ((key == KeyEvent.VK_RIGHT) && (!left)) {                right = true;                up = down = false;            }        }    }    public void actionPerformed(ActionEvent e) {        if (inGame) {            checkapple();            checkConfilict();            move();        }        repaint();    }    public void paint(Graphics g) {        super.paint(g);        if (inGame) {            g.drawImage(apple, apple_x, apple_y, this);            for (int i = 0; i < dots; i++) {                if (i == 0) {                    g.drawImage(head, x[i], y[i], this);                } else {                    g.drawImage(ball, x[i], y[i], this);                }            }            Toolkit.getDefaultToolkit().sync();            g.dispose();        } else {            Game_Over(g);        }    }}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -