利用java 編寫簡易貪吃蛇遊戲

貪吃蛇源碼

程序共包含以下兩個文件:

 

文件:ShellWin.java

 

import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.event.*;
import javax.swing.Box;
import javax.swing.JButton;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
@SuppressWarnings("serial")
public class ShellWin extends JPanel implements Runnable{  //實現了鍵盤與鼠標兩個接口
    int direction;
    int length;
    int score=0;
    boolean control=false;
    int[] xs=new int[400];               //蛇的個數,xy,座標控制
    int[] ys=new int[400];
    int douX;int douY;
    Thread thread;
    public ShellWin()
    {
         setPreferredSize(new Dimension(600, 600));
        setLocation(100, 100);
        setBackground(Color.GREEN);
       thread=new Thread(this);                    //爲這個類添加一個線程
        create_snake();
        create_bean();
    }
    public void paintComponent(Graphics g)            //繪製
    {
        super.paintComponent(g);                 //Jpanel內的方法,起到清屏的作用
        g.drawString("分數:"+score, 50, 50);
        g.drawRect(0,0, 600, 600);
        show_snake(g);
        show_bean(g);
    }
    //能不能吃豆,能不能死
    void die()
    {
        JOptionPane.showMessageDialog(null, "game over", "遊戲結束", JOptionPane.ERROR_MESSAGE);
    }
    void eat()
    {
        length++;
        this.score+=100;
        create_bean();
    }
    boolean out_bounds()
    {
        if(xs[0]==-10||xs[0]==600||ys[0]==-10||ys[0]==600)         //出界即死
        {
            return true;
        }
        return false;
    }
    void change_direction(int new_direction)                      //控制方向
    {
      if(direction%2!=new_direction%2||direction==0)               //    2 4 3 1 上下左右
      {
          direction=new_direction;
      }
    }
    boolean eat_self()
    {
        for (int i = 1; i < length; i++) {
            if(xs[0]==xs[i]&&ys[0]==ys[i])
            {
                return true;
            }
        }
        return false;
    }
    boolean can_eat()
    {
        if(xs[0]==douX&&ys[0]==douY)
        {
            return true;
        }
        return false;
    }
    void create_snake()
    {
        direction=0;length=5;
        for (int i = 0; i < length; i++) {
            xs[i]=400;
            ys[i]=400+10*i;
        }
    }
    void create_bean()
    {
       douX=10*(int)(Math.random()*60);
       douY=10*(int)(Math.random()*60);
       for (int i = 0; i < length; i++) {
           if(xs[i]==douX&&ys[i]==douY)
           {
               create_bean();
               return;
           }
       }
    }
    void show_bean(Graphics g)                   //仍然得要寫入到paintComponent方法內
    {
        g.setColor(Color.RED);
        g.fillOval(douX, douY, 10, 10);
    }
    void crawl()
    {
          if(direction!=0)
        for (int i = length-1; i >0; i--) {   //    2 4 3 1 上下左右
            xs[i]=xs[i-1];             //後一個替代前一個   4=3;
            ys[i]=ys[i-1];
        }
        switch (direction) {
        case 1:
            xs[0]=xs[0]+10;
            break;
        case 2:
            ys[0]=ys[0]-10;
            break;
        case 3:
            xs[0]=xs[0]-10;
            break;
        case 4:
            ys[0]=ys[0]+10;
            break;
        default:
            break;
        }
    }
    void show_snake(Graphics g)
    {
        for (int i = 0; i < length; i++) {
            g.setColor(Color.BLUE);
            if(i==0){
                g.drawOval(xs[i], ys[i], 10, 10);
                continue;
            }
            g.drawRect(xs[i], ys[i], 10, 10);           //蛇的長度與大小
        }
    }
    @Override
    public void run() {
        while(control)
        {
            try {               
                if(can_eat())
                {
                   eat();
                }
                if(out_bounds())
                {
                    die();
                    return;
                }
                if(eat_self())
                {
                    die();
                    return;
                }
                crawl();
                Thread.sleep(200);
                repaint();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
class panel extends JPanel implements ActionListener,KeyListener{
        JButton startGame=new JButton("開始");
        JButton stopGame=new JButton("停止");
        Box box1;                  //盒式佈局
        panel(){
            setLayout(new FlowLayout());
               box1=Box.createHorizontalBox();
               box1.add(startGame);
               box1.add(Box.createHorizontalStrut(2));
               box1.add(stopGame);
               setSize(800,800);
              setBackground(Color.black);
            addKeyListener(this);                  //爲ShellWin 對象註冊一個偵聽器
            startGame.addActionListener(this);     //爲開始按鈕添加偵聽器,this指代的是Actionistener這個類所創建的對象
            stopGame.addActionListener(this);     //爲結束按鈕添加偵聽器,this指代的是Actionistener這個類所創建的對象
        }
        @Override
        public void actionPerformed(ActionEvent e) {
            if(e.getSource()==startGame)
            {
                this.requestFocus();        //將光標添加到該控件中
                control=true;
                thread.start();               //線程開始
                //***********************************
              this.repaint(100,100, 600, 600);          
            }
            if(e.getSource()==stopGame)
            {
                this.requestFocus();        //將光標添加到該控件中
                control=false;             //線程結束          
            }
        }
        @Override
        public void keyPressed(KeyEvent e) {
            switch (e.getKeyCode()) {
            case KeyEvent.VK_UP:
                change_direction(2);
                break;
            case KeyEvent.VK_DOWN:
                if(direction!=0)
                change_direction(4);
                break;
            case KeyEvent.VK_LEFT:
                change_direction(3);
                break;
            case KeyEvent.VK_RIGHT:
                change_direction(1);
                break;
            default:
                break;
            }
        }
        @Override
        public void keyReleased(KeyEvent e) {
            // TODO Auto-generated method stub
             
        }
        @Override
        public void keyTyped(KeyEvent e) {
            // TODO Auto-generated method stub
        }
        
    }
}

文件:ShellMain.java

import javax.swing.Box;
import javax.swing.JFrame;
@SuppressWarnings("serial")
public class ShellMain extends JFrame {
    ShellWin win=new ShellWin();
      ShellWin.panel pan=win.new panel();
      Box box,box2;
    ShellMain(){
         box2=Box.createHorizontalBox();
         box2.add(win);
          box=Box.createVerticalBox();
          box.add(pan.box1);
          box.add(Box.createVerticalStrut(8));
          box.add(box2);
       pan.add(box);
        setTitle("貪吃蛇");
       setSize(800,800);
       setVisible(true);
       setLayout(null);
       add(pan);
       setLocation(0,0);
       setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
    public static void main(String[] args) {
          new ShellMain();
    }
}

 


 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章