手机版 欢迎访问it开发者社区(www.mfbz.cn)网站

当前位置: > 开发

java贪吃蛇

时间:2021/5/9 17:39:02|来源:|点击: 次
import javax.swing.*;
//游戏的主启动类
public class StartGame {
    public static void main(String[] args) {
        JFrame frame = new JFrame("一只倒霉仔");

        frame.setResizable(false);//窗口大小不可变
        frame.setBounds(10,10,900,795);
        frame.setVisible(true);
        frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

        //正常游戏界面都应该在面板上面
        frame.add(new PanelGame());
    }
}
import javax.swing.*;
import javax.xml.crypto.Data;
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 PanelGame extends JPanel implements KeyListener, ActionListener {

    //定义蛇的数据结构
    int length;//蛇的长度,一个方块25*25
    int[] snakex = new int[1000];//蛇的x坐标 1000 > 25*25
    int[] snakey = new int[1000];//蛇的y坐标 1000 > 25*25
    String fx;

    //游戏状态
    boolean gameStart = false;//默认是不开始

    boolean gamefail = false;//游戏失败
    //食物的坐标
    int foodx;
    int foody;
    Random random = new Random();

    //定时器,以ms为单位 1000ms = 1s
    Timer timer = new Timer(100,this);

    //初始化
    public void init(){
        length = 3;
        snakex[0] = 100;snakey[0] = 175;//脑袋的坐标
        snakex[1] = 75;snakey[1] = 175;//身体的坐标
        snakex[2] = 50;snakey[2] = 175;//身体的坐标
        fx = "r";

        //随机位置产生食物
        foodx =25 + 25* random.nextInt(34);// 850/25=34
        foody =150 + 25* random.nextInt(24);// 600/25=24
    }
    //构造器
    public PanelGame() {
        init();

        this.setFocusable(true);//获得焦点事件
        this.addKeyListener(this);//获得键盘监听事件
        timer.start();//游戏一开始定时器就启动
    }

    //绘制面板,我们游戏中的所有东西,都是用这个画笔来画
    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);//清屏,没有的话会闪烁
        //绘制静态的面板
        this.setBackground(Color.WHITE);
        //Date.header.paintIcon(this,g,25,11);//头部的背景
        Date.background.paintIcon(this,g,25,150);//蛇运动的区域的背景
        g.fillRect(25,11,850,100);

        //把食物画上去
        Date.food.paintIcon(this,g,foodx,foody);//放在画蛇代码的前面使蛇头压住食物
        //把蛇画上去
        if(fx.equals("r")){
            Date.right.paintIcon(this,g,snakex[0],snakey[0]);//蛇头向右
        }else if (fx.equals("l")){
            Date.left.paintIcon(this,g,snakex[0],snakey[0]);//蛇头向左
        }else if (fx.equals("u")){
            Date.up.paintIcon(this,g,snakex[0],snakey[0]);//蛇头向上
        }else if (fx.equals("d")){
            Date.down.paintIcon(this,g,snakex[0],snakey[0]);//蛇头向下
        }
        for (int i = 1; i < length; i++) {  //蛇的身体
            Date.body.paintIcon(this,g,snakex[i],snakey[i]);
        }

        //积分
        g.setColor(Color.WHITE);
        g.setFont(new Font("微软雅黑",Font.BOLD,40));
        g.drawString("长度"+length,400,75);


        //游戏状态
        if (!gameStart){
            g.setColor(Color.orange);
            g.setFont(new Font("微软雅黑",Font.BOLD,40));
            g.drawString("按下空格开始游戏",350,400);
        }

        if (gamefail){
            g.setColor(Color.RED);
            g.setFont(new Font("微软雅黑",Font.BOLD,40));
            g.drawString("游戏失败",350,400);
        }
    }
    //键盘监听器
    @Override
    public void keyPressed(KeyEvent e) {
        int keyCode = e.getKeyCode();//获得的是键盘的哪一个键

        if (keyCode == KeyEvent.VK_SPACE){//监听空格
           if (gamefail){//重新开始
               gamefail = true;//取反
               init();//初始化,重新开始
           }else {
               gameStart =! gameStart;//取反
               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";
        }
    }

    //事件监听---需要固定事件来刷新,比如:一秒24次
    @Override
    public void actionPerformed(ActionEvent e) {
        if (gameStart && gamefail == false){//如果游戏是开始状态,就让蛇动起来

            //吃食物
            if (snakex[0] == foodx && snakey[0] == foody){
                length++;//蛇长度加一
                //重新生成食物
                foodx =25 + 25* random.nextInt(34);// 850/25=34
                foody =150 + 25* random.nextInt(24);// 600/25=24
            }
            //身体移动
            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]>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] < 150) {//边界判断
                    snakey[0] = 725;
                }
            }else if (fx.equals("d")) {
                snakey[0] = snakey[0] + 25;
                if (snakey[0] > 725) {//边界判断
                    snakey[0] = 150;
                }
            }
            //蛇撞到自己死亡,游戏失败
            for (int i = 1 ; i < length; i++){
                if (snakex[0] == snakex[i] && snakey[0] == snakey[i]){
                    gamefail = true;
                }
            }


            repaint();//重画
        }

        timer.start();//定时器开启
    }

    @Override
    public void keyReleased(KeyEvent e) {

    }

    @Override
    public void keyTyped(KeyEvent e) {

    }

}

图片

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

//数据中心
public class Date {

    //相对路径 tx,jpg
    //绝对路径  /  相当于当前的项目
    public static URL headerURL = Date.class.getResource("staticsPicture/header.jpeg");
    public static URL upURL = Date.class.getResource("staticsPicture/up.png");
    public static URL downURL = Date.class.getResource("staticsPicture/down.png");
    public static URL leftURL = Date.class.getResource("staticsPicture/left.png");
    public static URL rightURL = Date.class.getResource("staticsPicture/right.png");
    public static URL bodyURL = Date.class.getResource("staticsPicture/body.png");
    public static URL foodURL = Date.class.getResource("staticsPicture/food.png");
    public static URL backgroundURL = Date.class.getResource("staticsPicture/background.jpeg");

    public static ImageIcon header = new ImageIcon(headerURL);
    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 ImageIcon body = new ImageIcon(bodyURL);
    public static ImageIcon food = new ImageIcon(foodURL);
    public static ImageIcon background = new ImageIcon(backgroundURL);
}

素材图片来自狂神说java
https://gitee.com/kuangstudy/openclass?_from=gitee_search

Copyright © 2002-2019 某某自媒体运营 版权所有