贪吃蛇小游戏

ScakeGame类

package snake;

import javax.swing.*;

public class ScakeGame {
    public static void main(String[] args) {

        JFrame frame = new JFrame();

        frame.setBounds(10,10,900,750);

        frame.setResizable(false);
        frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

        // 面板
        frame.add(new GamePanel());

        frame.setVisible(true);
    }

}

Data类

package snake;

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 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);

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

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

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

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

GamePanel类

package snake;

import com.sun.corba.se.spi.orbutil.fsm.Action;
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.sql.Time;
import java.util.Random;

public class GamePanel extends JPanel  implements KeyListener, ActionListener {

    // 定义蛇的数据结构
    int length;
    int [] snakeX = new int [600];
    int [] snakeY = new int [600];
    String fx;
    // 游戏状态
    boolean isStart = false ;
    // 食物坐标
    int footx;
    int footy;
    Random random = new Random();
    // 游戏失败
    boolean isFail = false;
    //积分
    int score;
    public  GamePanel(){
        init();
        this.setFocusable(true);
        this.addKeyListener(this);
        timer.start();
    }
    // 定时器
    Timer timer = new Timer(100,this);
    // 初始化方法
    public void init(){
        length=3;
        snakeX[0]=100;snakeY[0]=100;
        snakeX[1]=75;snakeY[1]=100;
        snakeX[2]=50;snakeY[2]=100;
        fx="R";
        score=0;
        isFail=false;
        isStart=false;

        // 食物随机分配
        footx = 25+25*random.nextInt(34);
        footy = 25+25*random.nextInt(24);
    }

    // 绘制面板
    @Override
    protected 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.setColor(Color.white);
        g.drawString("长度"+length,750,30);
        g.drawString("分数"+score,750,50);
         // 画小蛇
        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,footx,footy);
        //游戏状态
        if(isStart==false){
            g.setColor(Color.white);
            g.drawString("开始游戏",300,300);
        }
        if(isFail){
            g.setColor(Color.red);
            g.drawString("失败",300,300);
        }
    }

    @Override
    public void keyTyped(KeyEvent e) {
    }

    // 键盘监听
    @Override
    public void keyPressed(KeyEvent e) {
        int KeyCode = e.getKeyCode();
        if(KeyCode == KeyEvent.VK_SPACE){
            if(isFail){
                init();
            } else{
                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 keyReleased(KeyEvent e) {
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        if(isStart&&isFail==false){
            //吃食物
            if(snakeX[0]==footx&&snakeY[0]==footy){
                length++;
                score=score+10;
                //再次随机食物
                footx = 25+25*random.nextInt(32);
                footy = 25+25*random.nextInt(18);
            }
            // 移动
            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]<75) snakeY[0]=650;
            }else if(fx.equals("D")){
                snakeY[0]=snakeY[0]+25;
                if(snakeY[0]>650) snakeY[0]=75;
            }
            // 失败判定
            for (int i=1;i<length;i++) {
                if(snakeX[0]==snakeX[i]&&snakeY[0]==snakeY[i]){
                    isFail=true;break;
                }
            }
            repaint();
        }
        timer.start();
    }
}

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值