ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

Java贪吃蛇游戏

2022-05-14 16:35:08  阅读:8  来源: 互联网

标签:25 Java 游戏 fx snakeX snakeY 贪吃蛇 new public


目录

贪吃蛇游戏

要求:

  • 碰到边框失败
  • 碰到身体失败
  • 随机刷吃的
import javax.swing.*;

public class StartGame {

    public static void main(String[] args) {
        //1.新建一个窗口
        JFrame frame = new JFrame("贪吃蛇小游戏");
        //frame.setBounds(100,100,900,720);  // 设置窗口的位置和大小
        frame.setBounds(350,10,1250,1020);
        frame.setResizable(false); //窗口大小不可调整,即固定窗口大小
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // 设置关闭事件,游戏可以关闭

        //2.添加我们自己编写的画布背景
        frame.add(new GamePanel());

        frame.setVisible(true); //将窗口展示出来
    }

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

public class Data {
    //头部图片
    public static URL headerUrl = Data.class.getResource("/statics/header.png");
    public static ImageIcon header = new ImageIcon(headerUrl);
    //头部:上下左右
    public static URL upUrl = Data.class.getResource("/statics/up.png");
    public static URL downUrl = Data.class.getResource("/statics/down.png");
    public static URL leftUrl = Data.class.getResource("/statics/left.png");
    public static URL rightUrl = Data.class.getResource("/statics/right.png");
    public static ImageIcon up = new ImageIcon(upUrl);
    public static ImageIcon down = new ImageIcon(downUrl);
    public static ImageIcon left = new ImageIcon(leftUrl);
    public static ImageIcon right = new ImageIcon(rightUrl);
    //身体
    public static URL bodyUrl = Data.class.getResource("/statics/body.png");
    public static ImageIcon body = new ImageIcon(bodyUrl);
    //食物
    public static URL foodUrl = Data.class.getResource("/statics/food.png");
    public static ImageIcon food = new ImageIcon(foodUrl);
}
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;

public class GamePanel extends JPanel implements KeyListener, ActionListener {
    int lenth; //蛇的长度
    int[] snakeX = new int[600];  //蛇的坐标x
    int[] snakeY = new int[500];  //蛇的坐标y
    String fx = "R"; //蛇的方向 : R:右  L:左  U:上  D:下
    boolean isStart = false; //游戏是否开始
    Timer timer = new Timer(110, this); //定时器:第一个参数,就是定时执行时间
    //食物
    int foodx;
    int foody;
    Random random = new Random();
    boolean isFail = false; //游戏是否结束
    int score; //游戏分数!

    //构造方法
    public GamePanel(){
        init();//初始化
        this.setFocusable(true); //获取焦点事件
        this.addKeyListener(this); //键盘监听事件
        timer.start();
    }
    //初始化方法
    public void init(){
        lenth = 3;//初始小蛇有三节,包括小脑袋
        //初始化开始的蛇,给蛇定位,
//        snakeX[0] = 100; snakeY[0] = 100;
//        snakeX[1] = 75; snakeY[1] = 100;
//        snakeX[2] = 50; snakeY[2] = 100;
        snakeX[0] = 300; snakeY[0] = 300;
        snakeX[1] = 275; snakeY[1] = 300;
        snakeX[2] = 250; snakeY[2] = 300;

        //初始化食物数据
//        foodx = 25 + 25* random.nextInt(34);
//        foody = 75 + 25* random.nextInt(24);
        foodx = 25 + 25* random.nextInt(48);
        foody = 75 + 25* random.nextInt(36);

        score = 0; //初始化游戏分数
    }


    //画组件
    public void paintComponent(Graphics g){
        super.paintComponent(g);//清屏
        this.setBackground(Color.WHITE); //设置面板的背景色
        Data.header.paintIcon(this,g,25,11); //绘制头部信息区域
        //g.fillRect(25,75,850,600); //绘制游戏区域
        g.fillRect(25,75,1200,900);

        //画食物
        Data.food.paintIcon(this,g,foodx,foody);

        //把小蛇画上去
        if (fx.equals("R")){ //蛇的头通过方向变量来判断
            Data.right.paintIcon(this,g,snakeX[0],snakeY[0]);
        }else if (fx.equals("L")){
            Data.left.paintIcon(this,g,snakeX[0],snakeY[0]);
        }else if (fx.equals("U")){
            Data.up.paintIcon(this,g,snakeX[0],snakeY[0]);
        }else if (fx.equals("D")){
            Data.down.paintIcon(this,g,snakeX[0],snakeY[0]);
        }
        for (int i = 1; i < lenth; i++) {
            Data.body.paintIcon(this,g,snakeX[i],snakeY[i]); //蛇的身体长度根据lenth来控制
        }
        //分数
        g.setColor(Color.white);
        g.setFont(new Font("微软雅黑",Font.BOLD,14));
        g.setColor(Color.magenta);
        g.drawString("长度 " + lenth,750,35);
        g.drawString("分数 " + score,750,54);

        //游戏提示
        if (isStart==false){
            g.setColor(Color.white);
            g.setFont(new Font("微软雅黑",Font.BOLD,40));
            g.drawString("按下空格开始游戏!",450,450);
        }
        //失败判断
        if (isFail){
            g.setColor(Color.RED);
            g.setFont(new Font("微软雅黑",Font.BOLD,40));
            g.drawString("失败, 按下空格重新开始",400,500);
        }

    }



    //键盘监听事件
    @Override
    public void keyPressed(KeyEvent e) {
        int keyCode = e.getKeyCode(); //获取按下的键盘

        if (keyCode==KeyEvent.VK_SPACE){ //如果是空格
            if (isFail){ //如果游戏失败,从头再来!
                isFail = false;
                fx = "R";
                init(); //重新初始化
            }else { //否则,暂停游戏
                isStart = !isStart;
            }
            repaint();
        }

        //键盘控制走向
        //不能往反方向走
        if (fx.equals("L")){
            if (keyCode==KeyEvent.VK_UP){
                fx = "U";
            }else if (keyCode==KeyEvent.VK_DOWN){
                fx = "D";
            }
        }else if (fx.equals("R")){
            if (keyCode==KeyEvent.VK_UP){
                fx = "U";
            }else if (keyCode==KeyEvent.VK_DOWN){
                fx = "D";
            }
        }else if (fx.equals("U")){
            if (keyCode==KeyEvent.VK_LEFT){
                fx = "L";
            }else if (keyCode==KeyEvent.VK_RIGHT) {
                fx = "R";
            }
        }else if (fx.equals("D")){
            if (keyCode==KeyEvent.VK_LEFT){
                fx = "L";
            }else if (keyCode==KeyEvent.VK_RIGHT) {
                fx = "R";
            }
        }


    }
    @Override
    public void keyTyped(KeyEvent e) {

    }
    @Override
    public void keyReleased(KeyEvent e) {

    }

    //定时执行的操作
    @Override
    public void actionPerformed(ActionEvent e) {
        //如果游戏处于开始状态,并且没有结束,则小蛇可以移动
        if (isStart && isFail==false){
            //右移:即让后一个移到前一个的位置即可 !
            for (int i = lenth-1; i > 0; i--) { //除了脑袋都往前移:身体移动
                snakeX[i] = snakeX[i-1]; //即第i节(后一节)的位置变为(i-1:前一节)节的位置!
                snakeY[i] = snakeY[i-1];
            }
            //通过方向控制,头部移动
//            if (fx.equals("R")){
//                snakeX[0] = snakeX[0]+25;
//                if (snakeX[0]>850) snakeX[0] = 25;
//            }else if (fx.equals("L")){
//                snakeX[0] = snakeX[0]-25;
//                if (snakeX[0]<25) snakeX[0] = 850;
//            }else if (fx.equals("U")){
//                snakeY[0] = snakeY[0]-25;
//                if (snakeY[0]<75) snakeY[0] = 650;
//            }else if (fx.equals("D")){
//                snakeY[0] = snakeY[0]+25;
//                if (snakeY[0]>650) snakeY[0] = 75;
//            }

            //通过方向控制,头部移动
            //碰到边缘失败
            if (fx.equals("R")){
                snakeX[0] = snakeX[0]+25;
                if (snakeX[0]>1200) isFail=true;
            }else if (fx.equals("L")){
                snakeX[0] = snakeX[0]-25;
                if (snakeX[0]<25) isFail=true;
            }else if (fx.equals("U")){
                snakeY[0] = snakeY[0]-25;
                if (snakeY[0]<75) isFail=true;
            }else if (fx.equals("D")){
                snakeY[0] = snakeY[0]+25;
                if (snakeY[0]>950) isFail=true;
            }


            //吃食物:当蛇的头和食物一样时,算吃到食物!
            if (snakeX[0]==foodx && snakeY[0]==foody){
                //1.长度加一
                lenth++;
                //每吃一个食物,增加积分
                score = score + 10;
                //2.重新生成食物
//                foodx = 25 + 25* random.nextInt(34);
//                foody = 75 + 25* random.nextInt(24);
                foodx = 25 + 25* random.nextInt(48);
                foody = 75 + 25* random.nextInt(36);
            }

            //结束判断,头和身体撞到了
            for (int i = 1; i < lenth; i++) {
                //如果头和身体碰撞,那就说明游戏失败
                if (snakeX[i]==snakeX[0] && snakeY[i]==snakeY[0] ){
                    isFail = true;
                }
            }

            repaint(); //需要不断的更新页面实现动画
        }
        timer.start();//让时间动起来!

    }

}

贪吃蛇打包

  • 点击文件,选择项目结构

  • 选择Artifacts(工件),点击+号

  • 选择JAR,选择From modules with...(来自具有依赖的模块)

  • 选择main方法,点击Apply

  • 选择Build(构建),选择Build Artifacts...(构建工件),Build(构建)

  • 打开jar文件路径,打开cmd

  • 输入

    java -jar snake.jar
    
  • 完成

标签:25,Java,游戏,fx,snakeX,snakeY,贪吃蛇,new,public
来源: https://www.cnblogs.com/yingxu/p/16270457.html

专注分享技术,共同学习,共同进步。侵权联系[admin#icode9.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有