java贪吃蛇小游戏开发

package cn;

import javax.swing.*;

public class Snake {
    public static void main(String[] args) {
        JFrame frame=new JFrame();
        frame.setBounds(400,200,900,700);
        frame.setResizable(false);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        SnakePanel panel=new SnakePanel();
        frame.add(panel);

        frame.setVisible(true);
    }
}

Snake类的代码

package cn;

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 SnakePanel extends JPanel implements KeyListener, ActionListener {

    ImageIcon up=new ImageIcon("C:\\Users\\Administrator\\Desktop\\up.png");
    ImageIcon down=new ImageIcon("C:\\Users\\Administrator\\Desktop\\down.png");
    ImageIcon left=new ImageIcon("C:\\Users\\Administrator\\Desktop\\left.png");
    ImageIcon right=new ImageIcon("C:\\Users\\Administrator\\Desktop\\right.png");
    ImageIcon title=new ImageIcon("C:\\Users\\Administrator\\Desktop\\title.jpg");
    ImageIcon food=new ImageIcon("C:\\Users\\Administrator\\Desktop\\food.png");
    ImageIcon body=new ImageIcon("C:\\Users\\Administrator\\Desktop\\body.png");

    //蛇的数据结构
    int[] snakeX=new int[750];
    int[] snakeY=new int[750];
    int len=3;
    int score=0;
    String direction="R";

    //食物
    Random random=new Random();
    int foodX=random.nextInt(34)*25+25;
    int foodY=random.nextInt(24)*25+75;

    //游戏是否开始
    boolean isStarted=false;

    //游戏是否结束
    boolean isGameOver=false;

    Timer timer=new Timer(150,this);

    public SnakePanel()
    {
      this.setFocusable(true);
         initSnake();
      //添加一个键盘监听事件
         this.addKeyListener(this);

         timer.start();

    }

    public void initSnake()
    {
        len=3;
        direction="R";
        snakeX[0]=100;
        snakeY[0]=100;
        snakeX[1]=75;
        snakeY[1]=100;
        snakeX[2]=50;
        snakeY[2]=100;
        isGameOver=false;

    }



    public void paint(Graphics g)
    {
        this.setBackground(Color.BLACK);
        g.fillRect(25,75,850,600);

        //设置标题
        title.paintIcon(this,g,25,11);

        //画蛇头
        if(direction.equals("R"))
        {
            right.paintIcon(this,g,snakeX[0],snakeY[0]);
        }
        else if(direction.equals("L"))
        {
            left.paintIcon(this,g,snakeX[0],snakeY[0]);
        }
        else if(direction.equals("U"))
        {
            up.paintIcon(this,g,snakeX[0],snakeY[0]);
        }
        else if(direction.equals("D"))
        {
            down.paintIcon(this,g,snakeX[0],snakeY[0]);
        }

        //画蛇身
        for(int i=1;i<len;i++)
        {
            body.paintIcon(this,g,snakeX[i],snakeY[i]);
        }


        //画提示语
        if(!isStarted)
        {
            g.setColor(Color.white);
            g.setFont(new Font("arial",Font.BOLD,30));
            g.drawString("Press Space to Start/Pause",300,300);
        }
        if(isGameOver)
        {
            g.setColor(Color.white);
            g.setFont(new Font("arial",Font.BOLD,30));
            g.drawString("Game Over!",300,300);
        }

        //画食物
        food.paintIcon(this,g,foodX,foodY);

        //计算分数和长度
        g.setColor(Color.white);
        g.setFont(new Font("arial",Font.PLAIN,15));

        g.drawString("Score:"+score+"   Length:"+len,750,30);
    }

    @Override
    public void keyTyped(KeyEvent e) {

    }

    @Override
    public void keyPressed(KeyEvent e) {
        int keyCode=e.getKeyCode();
        if(keyCode==KeyEvent.VK_SPACE)
        {
            if(isGameOver)
            {
                initSnake();
            }
            else{
                isStarted=!isStarted;
            }

            repaint();
        }
        else if(keyCode==KeyEvent.VK_UP && !direction.equals("D"))
        {
            direction="U";
        }
        else if(keyCode==KeyEvent.VK_DOWN && !direction.equals("U"))
        {
            direction="D";
        }
        else if(keyCode==KeyEvent.VK_RIGHT && !direction.equals("L"))
        {
            direction="R";
        }
        else if(keyCode==KeyEvent.VK_LEFT && !direction.equals("R"))
        {
            direction="L";
        }


    }

    @Override
    public void keyReleased(KeyEvent e) {

    }

    @Override
    public void actionPerformed(ActionEvent e) {
        /*
        1.重新定一个闹钟
        2.蛇移动
        3。重画
         */
        timer.start();
        if (isStarted  && !isGameOver)
        {
            //移动身体
            for (int i=len;i>0;i--)
            {
                snakeX[i]=snakeX[i-1];
                snakeY[i]=snakeY[i-1];

            }
            //头移动
            if(direction.equals("R"))
            {
                snakeX[0]=snakeX[0]+25;
                if(snakeX[0]>850)
                {
                    //snakeX[0]=25;
                    isGameOver=true;
                }
            }
            else if(direction.equals("L"))
            {
                snakeX[0]=snakeX[0]-25;
                if(snakeX[0]<25)
                {
                    //snakeX[0]=850;
                    isGameOver=true;
                }
            }
            else if(direction.equals("U"))
            {
                snakeY[0]=snakeY[0]-25;
                if(snakeY[0]<75)
                {
                    //snakeY[0]=650;
                    isGameOver=true;
                }
            }
            else if(direction.equals("D"))
            {
                snakeY[0]=snakeY[0]+25;
                if(snakeY[0]>650)
                {
                    //snakeY[0]=75;
                    isGameOver=true;
                }
            }


            //吃食物的逻辑
            if(snakeX[0]==foodX&&snakeY[0]==foodY)
            {
                len++;
                score++;
                foodX=random.nextInt(34)*25+25;
                foodY=random.nextInt(24)*25+75;
            }


            for (int i=1;i<len;i++)//不能从头开始循环
            {
                if(snakeX[0]==snakeX[i] && snakeY[0]==snakeY[i])
                {
                    isGameOver=true;
                }
            }
        }
        repaint();//这一步特别重要
    }
}

Panel类

你可能感兴趣的:(学习笔记)