1. 程式人生 > 程式設計 >java編寫簡易貪吃蛇遊戲

java編寫簡易貪吃蛇遊戲

本文例項為大家分享了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,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);
    continue;
   }
   g.drawRect(xs[i],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);   
   }
   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();
 }
}

更多精彩遊戲,請參考專題《java經典小遊戲》

更多有趣的經典小遊戲實現專題,分享給大家:

C++經典小遊戲彙總

python經典小遊戲彙總

python俄羅斯方塊遊戲集合

JavaScript經典遊戲 玩不停

javascript經典小遊戲彙總

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支援我們。