JAVA--GUI--总结(贪吃蛇游戏)

在这里插入图片描述

贪吃蛇游戏

note

1.定义数据
2.画到面板上
3.监听事件:键盘,事件


游戏启动类

package snake;

import javax.swing.*;

//游戏的启动类
public class StartDemo extends JFrame {
    public static void main(String[] args) {
        StartDemo startDemo = new StartDemo();
        startDemo.setBounds(10,10,900,720);
        startDemo.setResizable(false);//设置不可变大小
        startDemo.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

        //正常游戏界面应该在panel上

        startDemo.setVisible(true);
        startDemo.add(new GamePanel());
    }
}

游戏数据中心

package snake;

import javax.swing.*;
import java.net.URL;
//数据中心,用来存储图片素材
public class Data {
    //相对路径 "tx.jpg"
    //绝对路径 "/...." 相对于当前的项目
    public static URL headerURL = Data.class.getResource("/snake/statics/header.png");
    public static ImageIcon header = new ImageIcon(headerURL);

    public static URL upURL = Data.class.getResource("/snake/statics/up.png");
    public static ImageIcon up = new ImageIcon(upURL);

    public static URL downURL = Data.class.getResource("/snake/statics/down.png");
    public static ImageIcon down = new ImageIcon(downURL);

    public static URL leftURL = Data.class.getResource("/snake/statics/left.png");
    public static ImageIcon left = new ImageIcon(leftURL);

    public static URL rightURL = Data.class.getResource("/snake/statics/right.png");
    public static ImageIcon right = new ImageIcon(rightURL);

    public static URL bodyURL = Data.class.getResource("/snake/statics/body.png");
    public static ImageIcon body = new ImageIcon(bodyURL);

    public static URL foodURL = Data.class.getResource("/snake/statics/food.png");
    public static ImageIcon food = new ImageIcon(foodURL);
}

游戏界面

package snake;

import javafx.scene.input.KeyCode;

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 length;//蛇的长度
    int[] x = new int[600];//蛇的x坐标
    int[] y = new int[600];//蛇的Y坐标
    String fx;
    int score;//分数

    //食物的坐标
    int foodx;
    int foody;
    Random random = new Random();

    boolean status; //游戏状态:开始,停止
    boolean isFail = false;//游戏失败
    Timer timer = new Timer(100,this);//每100ms执行一次

    public GamePanel(){
        init();
        //获得焦点和键盘监听
        setFocusable(true);//获得焦点事件
        addKeyListener(this);//获取键盘事件
        timer.start();//游戏一开始,定时器启动

    }

    public void init(){
        length = 3;
        x[0] = 100;y[0] = 100;
        x[1] = 75;y[1] = 100;
        x[2] = 50;y[2] = 100;
        fx = "R";
        status = false;
        //把食物随机放到界面上
        foodx = 25+25*random.nextInt(34);
        foody = 75+25*random.nextInt(24);
        score=0;
    }


    //绘制面板,游戏中的所有东西都用画笔来画
    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);//清屏
        //绘制静态的面板
        setBackground(Color.white);
        Data.header.paintIcon(this,g,25,11);//头部广告栏
        g.fillRect(25,75,850,600);//默认的游戏界面
        //绘制食物
        Data.food.paintIcon(this,g,foodx,foody);
        //绘制静态的蛇
        if(fx.equals("R")){     //绘制头部,按初始化方向来绘制
            Data.right.paintIcon(this,g,x[0],y[0]);
        }else if (fx.equals("L")){
            Data.left.paintIcon(this,g,x[0],y[0]);
        }else if (fx.equals("U")){
            Data.up.paintIcon(this,g,x[0],y[0]);
        }else if (fx.equals("D")){
            Data.down.paintIcon(this,g,x[0],y[0]);
        }
        for (int i=1;i<length;i++){     //绘制身体,按长度绘制
            Data.body.paintIcon(this,g,x[i],y[i]);
        }
        //绘制分数和长度
        g.setColor(Color.white);
        g.setFont(new Font("微软雅黑",Font.BOLD,18));
        g.drawString("长度:"+length,750,35);
        g.drawString("分数:"+score,750,50);

        //绘制游戏状态
        if(status == false){
            g.setColor(Color.white);
            g.setFont(new Font("微软雅黑",Font.BOLD,40));
            g.drawString("按下空格开始游戏",300,300);
        }
        if(isFail){
            g.setColor(Color.red);
            g.setFont(new Font("微软雅黑",Font.BOLD,40));
            g.drawString("失败,按下空格开始游戏",300,300);
        }
    }
    //键盘监听事件
    @Override
    public void keyPressed(KeyEvent e) {
        int keyCode = e.getKeyCode();   //获得键盘的按键
        if(keyCode==KeyEvent.VK_SPACE){     //如果按下的是空格
            if(isFail){
                isFail=false;
                init();
            }else {
                status = !status;   //取反
            }
            repaint();
        }
        //小蛇移动
        if(keyCode==KeyEvent.VK_UP){
            fx="U";
        }else if(keyCode==KeyEvent.VK_DOWN){
            fx="D";
        }else if(keyCode==KeyEvent.VK_LEFT){
            fx="L";
        }else if(keyCode==KeyEvent.VK_RIGHT){
            fx="R";
        }
    }
    //事件监听  需要通过固定事件来刷新,1s=10次
    @Override
    public void actionPerformed(ActionEvent e) {
        if(status&&isFail==false){     //如果游戏是开始状态,让小蛇动起来
            //吃食物
            if(x[0]==foodx&&y[0]==foody){
                //长度加一
                length++;
                //分数加十
                score+=10;
                //随机食物位置
                foodx = 25+25*random.nextInt(34);
                foody = 75+25*random.nextInt(24);
            }

            //移动
            for(int i = length-1;i>0;i--){  //后一节移到前一节的位置
                x[i] = x[i-1];
                y[i] = y[i-1];
            }
            //走向
            if(fx=="R"){
                x[0]+=25;
                if(x[0]>850){ x[0]=25; }
            }else if(fx=="L"){
                x[0]-=25;
                if(x[0]<25){ x[0]=850; }
            }else if(fx=="U"){
                y[0]-=25;
                if(y[0]<75){ y[0]=650; }
            }else if(fx=="D"){
                y[0]+=25;
                if(y[0]>650){ y[0]=75; }
            }
            repaint();
            //失败判定,撞到自己就算失败
            for (int i = 1; i < length ; i++) {
                if(x[0]==x[i]&&y[0]==y[i]){
                    isFail=true;
                }
            }
        }
        timer.start();
    }

    @Override
    public void keyTyped(KeyEvent e) { }
    @Override
    public void keyReleased(KeyEvent e) { }
}

效果
在这里插入图片描述

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值