Java教程

JAVA实现贪吃蛇

本文主要是介绍JAVA实现贪吃蛇,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

废话少说,直接怼上代码:

  • stratGame.java
package com.game.snake;

import javax.swing.*;
import java.awt.*;

public class startGame {
    public static void main(String[] args) {
        JFrame gameFrame = new JFrame("贪吃蛇");

        gameFrame.add(new gamePanel());
        gameFrame.setVisible(true);
        gameFrame.setBounds(100,100,900,720);
        gameFrame.setResizable(false);
        gameFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }
}
  • myData.java
package com.game.snake;

import javax.swing.*;
import java.net.URL;

public class myData {
    public static URL headerurl = myData.class.getResource("image/header.png");
    public static ImageIcon headicon = new ImageIcon(headerurl);

    public static URL bodyurl = myData.class.getResource("image/body.png");
    public static ImageIcon bodyicon = new ImageIcon(bodyurl);

    public static URL upurl = myData.class.getResource("image/up.png");
    public static ImageIcon upicon = new ImageIcon(upurl);
    public static URL righturl = myData.class.getResource("image/right.png");
    public static ImageIcon righticon = new ImageIcon(righturl);
    public static URL downurl = myData.class.getResource("image/down.png");
    public static ImageIcon downicon = new ImageIcon(downurl);
    public static URL lefturl = myData.class.getResource("image/left.png");
    public static ImageIcon lefticon = new ImageIcon(lefturl);

    public static URL foodurl = myData.class.getResource("image/food.png");
    public static ImageIcon foddicon = new ImageIcon(foodurl);

}
  • gamePanel.java
package com.game.snake;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.Random;
/*
1.定义数据
2.画
3.监听:a.键盘 b.事件
* */
public class gamePanel extends JPanel implements KeyListener, ActionListener {
    int nLen = 0;//蛇的长度
    int[] snakeX = new int[600];//存放蛇的X坐标
    int[] snakeY = new int[600];//存放蛇的Y坐标

    //食物
    int foodX = 0;
    int foodY = 0;
    Random random = new Random();

    //分数显示
    int grade = 0;

    boolean isGameStart = false;//游戏状态,默认不开始
    boolean isGameOver = false;//判断是否失败

    String dir = null;
    //设置一个定时器
    Timer timer = new Timer(100,this);//100毫秒刷新一次

    public gamePanel(){
        init();
        //获得焦点和键盘事件
        this.setFocusable(true);//焦点事件,专注游戏窗口
        this.addKeyListener(this);//键盘事件,当前类中实现了,所以用this
        timer.start();//游戏开始,定时器启动
    }

    public void init(){
        nLen = 3;
        snakeX[0] = 100;snakeY[0] = 100;
        snakeX[1] = 75;snakeY[1] = 100;
        snakeX[2] = 50;snakeY[2] = 100;
        dir = "R";

        //初始化食物
        foodX = 25 + 25 * random.nextInt(33);
        foodY = 25 + 25 * random.nextInt(20);
    }

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        //绘制静态界面
        this.setBackground(Color.white);
        myData.headicon.paintIcon(this,g,25,11);
        g.fillRect(25,75,850,600);

        //绘制食物
        myData.foddicon.paintIcon(this,g,foodX,foodY);

        for (int i = 1/*i = 1蛇头固定的*/; i < nLen; i++) {
            myData.bodyicon.paintIcon(this,g,snakeX[i],snakeY[i]);
        }

        //绘制小蛇
        switch (dir) {
            case "R":
                myData.righticon.paintIcon(this, g, snakeX[0], snakeY[0]);//蛇头的方向会变化,初始是向右
                break;
            case "L":
                myData.lefticon.paintIcon(this, g, snakeX[0], snakeY[0]);
                break;
            case "U":
                myData.upicon.paintIcon(this, g, snakeX[0], snakeY[0]);
                break;
            case "D":
                myData.downicon.paintIcon(this, g, snakeX[0], snakeY[0]);
                break;
        }

        //游戏状态绘制
        if (isGameStart == false){
            g.setColor(Color.white);//设置画笔颜色白色
            g.setFont(new Font("微软雅黑",Font.BOLD,40));
            g.drawString("空格开始游戏",325,300);
        }
        if (isGameOver == true){
            g.setColor(Color.CYAN);//设置画笔颜色白色
            g.setFont(new Font("微软雅黑",Font.BOLD,40));
            g.drawString("空格重新开始游戏",325,300);
        }
        //显示分数与蛇长度
            g.setColor(Color.white);
            g.setFont(new Font("微软雅黑",Font.BOLD,18));
            g.drawString("蛇长 "+nLen,750,25);
            g.drawString("分数 "+grade,750,45);
    }

    //键盘监听事件
    @Override
    public void keyPressed(KeyEvent e) {
        int keycode = e.getKeyCode();//获取键盘按键
        if (keycode == KeyEvent.VK_SPACE){
            if (isGameOver == true) {
                isGameOver = false;
                init();//重新初始化开始游戏
            }else {isGameStart = !isGameStart;}
            //改变了状态,重新画
            repaint();
        }
        if (keycode == KeyEvent.VK_W){
            dir = "U";
        }else if (keycode == KeyEvent.VK_D){
            dir = "R";
        }else if (keycode == KeyEvent.VK_S){
            dir = "D";
        }else if (keycode == KeyEvent.VK_A){
            dir = "L";
        }
    }

    //事件监听
    @Override
    public void actionPerformed(ActionEvent e) {
        if (isGameStart == true &&
            isGameOver == false){//如果游戏是进行状态,就让小蛇动起来
            //吃食物
            if (snakeX[0] == foodX &&
                snakeY[0] == foodY){
                nLen++;
                grade = grade + 10;
                //重新生成食物
                foodX = 25 + 25 * random.nextInt(34);
                foodY = 25 + 25 * random.nextInt(24);
            }
            //小蛇运动
            for (int i = nLen - 1; i > 0 ; i--) {
                snakeX[i] = snakeX[i - 1];
                snakeY[i] = snakeY[i - 1];
            }
            //方向判断
            //向右移动
            if (dir.equals("R")){
                snakeX[0] = snakeX[0] + 25;
                //边界判断
                if (snakeX[0] > 850){
                    snakeX[0] = 25;
                }
            }else if (dir.equals("L")){
                snakeX[0] = snakeX[0] - 25;
                if (snakeX[0] < 25){
                    snakeX[0] = 850;
                }
            }else if (dir.equals("U")){
                snakeY[0] = snakeY[0] - 25;
                if (snakeY[0] < 75){
                    snakeY[0] = 650;
                }
            }else if (dir.equals("D")){
                snakeY[0] = snakeY[0] + 25;
                if (snakeY[0] > 650){
                    snakeY[0] = 75;
                }
            }

            //失败判断
            for (int i = 1; i < nLen; i++) {
                if (snakeX[0] == snakeX[i] &&
                    snakeY[0] == snakeY[i]){
                    isGameOver = true;
                }
            }

            repaint();
        }
        timer.start();
    }

    @Override
    public void keyTyped(KeyEvent e) {

    }
    @Override
    public void keyReleased(KeyEvent e) {

    }
}
这篇关于JAVA实现贪吃蛇的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!