Java一个简单的贪吃蛇

Java一个简单的贪吃蛇

虽然GUI已经要淘汰了,但是手动写写界面还是有助于理解语法的,像构造函数 ,函数调用,内部类,继承,接口。有助于半初学者强化理解。

直接上代码

游戏主体类:

package com.chenghu.snake;
import javax.swing.*;
public class StartGame {
    public static void main(String[] args) {
       JFrame frame = new JFrame(); //new一个窗体
       frame.setBounds(10,10,1000,1000);//开一个窗体
       frame.setResizable(false);//是否可拉伸
       frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); //点击×关闭
       frame.add(new GamePanel());//窗体上添加一个画布
       frame.setVisible(true);//窗体可见
    }
}

画布类:

package com.chenghu.snake;
import com.sun.xml.internal.ws.addressing.model.ActionNotSupportedException;

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 {//画布继承两个接口 KeyListener ,ActionListener监听键盘和事件
    //下面定义一些变量
    int length; //蛇身长度
    int[] snakeX=new int[600];//蛇的坐标
    int[] snakeY=new int[600];//蛇的坐标
    String fx;//蛇头方向
    boolean isStart;//游戏状态
    Timer timer = new Timer(100,  this);//定时器,100ms刷新
    int foodx;//食物坐标
    int foody;//食物坐标
    Random random=new Random();//食物随机 、位置
    public GamePanel(){//画布构造器  就是构造方法,里面初始化一些量
        init();//初始化函数 初始化蛇的位置,游戏状态,食物位置
        this.setFocusable(true);
        this.addKeyListener(this);
    }
    public void init(){
        length=3;
        snakeX[0]=100;snakeY[0] =250;
        snakeX[1]=75;snakeY[1] =250;
        snakeX[2]=50;snakeY[2] =250;
        fx="R";
        foodx=25+25*random.nextInt(38);
        foody=150+25*random.nextInt(28);
        isStart=false;
        timer.start();


    }

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);//清屏
        this.setBackground(Color.white);
        Data.header.paintIcon(this,g,25,11);//头部广告
        g.fillRect(25,150,950,700);//游戏界面

        //放小蛇
        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<length;i++)
     Data.body.paintIcon(this,g,snakeX[i],snakeY[i]);
        Data.food.paintIcon(this,g,foodx,foody);
        if(isStart==false){
            g.setColor(Color.white);
            g.setFont(new Font("微软雅黑",Font.BOLD,40));
            g.drawString("按下空格开始游戏",300,400);
        }
    }

    @Override
    public void keyPressed(KeyEvent e) {  //监听键盘
    int keyCode=e.getKeyCode();
    if(keyCode==KeyEvent.VK_SPACE) {//监听空格
        isStart=!isStart;
        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";}
    }
    @Override
    public void actionPerformed(ActionEvent e) { //监听事件

        if(isStart){
            for(int i=length-1;i>0;i--){
                snakeX[i]=snakeX[i-1];
                snakeY[i]=snakeY[i-1];
            }
            if(fx.equals("R")){
                snakeX[0]=snakeX[0]+25;
                if(snakeX[0]>950) snakeX[0]=25;
            }else if(fx.equals("L")){
                snakeX[0]=snakeX[0]-25;
                if(snakeX[0]<25) snakeX[0]=950;
            }else if(fx.equals("U")){
                snakeY[0]-=25;
                if(snakeY[0]<150) snakeY[0]=825;
            }else if(fx.equals("D")){
                snakeY[0]+=25;
                if(snakeY[0]>825) snakeY[0]=150;
            }
            if(snakeX[0]==foodx&&snakeY[0]==foody) {
                length++;
                foodx=25+25*random.nextInt(38);
                foody=150+25*random.nextInt(28);
            }
            repaint();
        }
        timer.start();
    }
    @Override
    public void keyReleased(KeyEvent e) { }
    @Override
    public void keyTyped(KeyEvent e) {}
}

素材类:

package com.chenghu.snake;
import javax.swing.*;
import java.net.URL;
public class Data {  //这个类是定位需要的素材 图片
    public static URL bodyurl=Data.class.getResource("snake/body.jpg");
    public static ImageIcon body=new ImageIcon(bodyurl);
    public static URL downurl=Data.class.getResource("snake/down.jpg");
    public static ImageIcon down=new ImageIcon(downurl);
    public static URL lefturl=Data.class.getResource("snake/left.jpg");
    public static ImageIcon left=new ImageIcon(lefturl);
    public static URL righturl=Data.class.getResource("snake/right.jpg");
    public static ImageIcon right=new ImageIcon(righturl);
    public static URL foodurl=Data.class.getResource("snake/body.jpg");
    public static ImageIcon food=new ImageIcon(foodurl);
    public static URL headerurl=Data.class.getResource("snake/timg.jpg");
    public static ImageIcon header=new ImageIcon(headerurl);
    public static URL upurl=Data.class.getResource("snake/up.jpg");
    public static ImageIcon up=new ImageIcon(upurl);
}

​ 总结一下:简单实现了贪吃蛇,总体来说,想要的功能分三步实现,第一步:定义变量初始化数据,第二步:用画笔在画布上画出来,第三步:监听这个事件,有变化更改数据。

上一篇:如何使用java的WindowBuilder编写贪吃蛇小程序


下一篇:贪吃蛇GUI游戏设计