java實現簡單貪吃蛇小遊戲

本文實例為大傢分享瞭java實現貪吃蛇小遊戲,供大傢參考,具體內容如下

有兩個類,放在同一文件下下即可,背景圖片,標題,蛇尾可自行更改

import java.awt.*;
import javax.swing.*;

import java.awt.event.*;
public class MyJFrame extends JFrame{

 public MyJFrame() {
  
  int x= Toolkit.getDefaultToolkit().getScreenSize().width;
  int y = Toolkit.getDefaultToolkit().getScreenSize().height;
  setLocation((x-800)/2, (y-800)/2);
  setSize(800,800);
  setVisible(true);
  setDefaultCloseOperation(EXIT_ON_CLOSE);
  setTitle("貪吃蛇");
  
  setLayout(null);
  
  getLayeredPane().setLayout(null);
  JLabel background = new JLabel(new ImageIcon("C:\\Users\\HP\\Desktop\\javaa\\background3.jpg"));//註意此處路徑
  JLabel title = new JLabel(new ImageIcon("C:\\Users\\HP\\Desktop\\javaa\\title1.jpg"));//註意此處路徑
  background.setBounds(20,80,750,650);
  title.setBounds(0,0,800,75);
  getLayeredPane().add(background,new Integer(Integer.MIN_VALUE));
  getLayeredPane().add(title,new Integer(Integer.MIN_VALUE+1));
  
  ((JPanel)getContentPane()).setOpaque(false);
  
  MyPanel mypanel = new MyPanel();
  mypanel.setBounds(0,0,getWidth(),getHeight());
  add(mypanel);
 
 }
 
 public static void main(String[] args) {
  new MyJFrame();
 }
}
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.util.Random;

public class MyPanel extends JPanel implements ActionListener{
 int lenght ;
 int score1;
 Boolean jieshux = false;

 JLabel scorel = new JLabel("分數:"+score1);
 JLabel lenghl = new JLabel("長度:"+lenght);
 
 JLabel shibai = new JLabel("按下空格開始遊戲!");
 JLabel jieshu = new JLabel("遊戲失敗!按空格重新開始!");
 
 String dir = "R";
 Boolean isstart = false;
 int[][] snake = new int[300][2];
 
 Random r = new Random();
 int x,y;
 
 //初始化
 public void initialize() {
  snake[0][0]=70;
  snake[0][1]=80;
  snake[1][0]=45;
  snake[1][1]=80;
  snake[2][0]=20;
  snake[2][1]=80;
  isstart = true;
  jieshux = false;
  lenght = 3;
  score1 = 0;
  dir = "R";
  repaint();
 }
 
 public MyPanel() {
  
  setLayout(null);
  scorel.setBounds(700, 20, 50, 30);
  lenghl.setBounds(700, 40, 50, 30);
  shibai.setBounds(300,400,200,60);
  shibai.setFont(new Font("黑體",Font.BOLD,20));
  jieshu.setFont(new Font("黑體",Font.BOLD,20));
  jieshu.setVisible(false);
  jieshu.setBounds(250,400,300,60);
  
  add(scorel);
  add(lenghl);
  add(shibai);
  add(jieshu);
  
  x = r.nextInt(29)*25+20;
  y = r.nextInt(25)*25+80;
  
  //this.setFocusable(true); //焦點獲取
  setOpaque(false);
  
  initialize();
  
  Timer t = new Timer(200,this);
  this.addKeyListener(new KeyAdapter() {
   public void keyPressed(KeyEvent e) {
    if(e.getKeyCode()==KeyEvent.VK_SPACE && isstart==false) {
     isstart=true;
    }else if(e.getKeyCode()==KeyEvent.VK_SPACE && isstart==true &&jieshux == false){
     isstart = false;
    }else if(e.getKeyCode()==KeyEvent.VK_W && isstart==true && dir!="D"&&jieshux == false) {
     dir="U";
    }else if(e.getKeyCode()==KeyEvent.VK_A && isstart==true && dir!="R"&&jieshux == false) {
     dir="L";
    }else if(e.getKeyCode()==KeyEvent.VK_D && isstart==true && dir!="L"&&jieshux == false) {
     dir="R";
    }else if(e.getKeyCode()==KeyEvent.VK_S && isstart==true && dir!="U"&&jieshux == false) {
     dir="D";
    }else if(e.getKeyCode()==KeyEvent.VK_SPACE && jieshux==true) {
     initialize();
    }
   }
  });
  t.start();

 }
 
 @Override
 public void actionPerformed(ActionEvent e) {
  this.requestFocus(true);

   if(isstart&&dir.equals("R")&&!jieshux) {
    for(int i=0;i<lenght-1;i++) {
     if(snake[i][0]>=745) {
      //snake[i][0]=20;
      jieshux = !jieshux;
      //isstart = !isstart;
      break;
     }else {
      snake[lenght-i-1][0]=snake[lenght-i-2][0];
      snake[lenght-i-1][1]=snake[lenght-i-2][1];
     }
    }
    if(!jieshux) snake[0][0]=snake[1][0]+25;
   } else if(isstart&&dir.equals("D")&&!jieshux) {
    for(int i=0;i<lenght-1;i++) {
     if(snake[i][1]>=705) {
      //snake[i][1]=80;
      jieshux = !jieshux;
      //isstart = !isstart;
      break;
     }else {
      snake[lenght-i-1][0]=snake[lenght-i-2][0];
      snake[lenght-i-1][1]=snake[lenght-i-2][1];
     }
    }
    if(!jieshux) snake[0][1]=snake[1][1]+25;
   }else if(isstart&&dir.equals("U")&&!jieshux) {
    for(int i=0;i<lenght-1;i++) {
     if(snake[i][1]<=80) {
      //snake[i][1]=705;
      jieshux = !jieshux;
      //isstart = !isstart;
      break;
     }else {
      snake[lenght-i-1][0]=snake[lenght-i-2][0];
      snake[lenght-i-1][1]=snake[lenght-i-2][1];
     }
    }
    if(!jieshux) snake[0][1]=snake[1][1]-25;
   }else if(isstart&&dir.equals("L")&&!jieshux) {
    for(int i=0;i<lenght-1;i++) {
     if(snake[i][0]<=20) {
      jieshux = !jieshux;
      //isstart = !isstart;
      break;
     }else {
      snake[lenght-i-1][0]=snake[lenght-i-2][0];
      snake[lenght-i-1][1]=snake[lenght-i-2][1];
     }
    }
    if(!jieshux) snake[0][0]=snake[1][0]-25;
   }

   if(snake[0][1]==y&&snake[0][0]==x) {
    x = r.nextInt(29)*25+20;
    y = r.nextInt(25)*25+80;
    
    score1+=10;
    lenght++;
    snake[lenght-1][1]=snake[lenght-2][1];
    snake[lenght-1][0]=snake[lenght-2][0];

    
    add(scorel);
    add(lenghl);
   }
   
   if(isstart==false) {
    shibai.setVisible(true);
   }else {
    shibai.setVisible(false);
    if(jieshux == false) {
     jieshu.setVisible(false);
    }else {
     jieshu.setVisible(true);
    }
   }
   
   for(int i = 1;i<lenght;i++) {
    if(snake[0][0]==snake[i][0]&&snake[0][1]==snake[i][1]) {
     jieshux=true;
    }
   }
   
   if(isstart&&!jieshux)repaint();
 }
 
 public void paint(Graphics g) {
  super.paint(g);
  //邊界為(20,80),(770,730)
  scorel.setText("分數:"+score1);
  lenghl.setText("長度:"+lenght);
  
  g.drawLine(18, 78, 18, 732);
  g.drawLine(18, 78, 772, 78);
  g.drawLine(18, 732, 772, 732);
  g.drawLine(772, 78, 772, 732);
  //豎線
  for(int i=20;i<=770;i+=25) {
   g.drawLine(i,80,i,730);
   //g.drawLine(0,i,600,i);
  }
  
  //橫線
  for(int i=80;i<=750;i+=25) {
   //g.drawLine(i,50,i,780);
   g.drawLine(20,i,770,i);
  }
  
  for(int i = 0;i<lenght;i++) {
   if(i>0) {
    g.drawImage(new ImageIcon("C:\\Users\\HP\\Desktop\\javaa\\body.png").getImage(), snake[i][0],snake[i][1],this);//註意此處路徑
   }else {
    g.fillOval(snake[i][0], snake[i][1], 25, 25);
   }
  }
  
  g.setColor(Color.red);
  g.fillOval(x, y, 25, 25);
  
 }

}

以上就是本文的全部內容,希望對大傢的學習有所幫助,也希望大傢多多支持WalkonNet。

推薦閱讀: