溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

java怎么編寫(xiě)簡(jiǎn)易貪吃蛇游戲

發(fā)布時(shí)間:2020-07-23 15:24:05 來(lái)源:億速云 閱讀:179 作者:小豬 欄目:編程語(yǔ)言

小編這次要給大家分享的是java怎么編寫(xiě)簡(jiǎn)易貪吃蛇游戲,文章內(nèi)容豐富,感興趣的小伙伴可以來(lái)了解一下,希望大家閱讀完這篇文章之后能夠有所收獲。

程序共包含以下兩個(gè)文件:

文件: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{ //實(shí)現(xiàn)了鍵盤(pán)與鼠標(biāo)兩個(gè)接口
 int direction;
 int length;
 int score=0;
 boolean control=false;
 int[] xs=new int[400];    //蛇的個(gè)數(shù),xy,坐標(biāo)控制
 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);     //為這個(gè)類(lèi)添加一個(gè)線程
  create_snake();
  create_bean();
 }
 public void paintComponent(Graphics g)   //繪制
 {
  super.paintComponent(g);     //Jpanel內(nèi)的方法,起到清屏的作用
  g.drawString("分?jǐn)?shù):"+score, 50, 50);
  g.drawRect(0,0, 600, 600);
  show_snake(g);
  show_bean(g);
 }
 //能不能吃豆,能不能死
 void die()
 {
  JOptionPane.showMessageDialog(null, "game over", "游戲結(jié)束", 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)     //仍然得要寫(xiě)入到paintComponent方法內(nèi)
 {
  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];    //后一個(gè)替代前一個(gè) 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);   //蛇的長(zhǎng)度與大小
  }
 }
 @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("開(kāi)始");
  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 對(duì)象注冊(cè)一個(gè)偵聽(tīng)器
   startGame.addActionListener(this);  //為開(kāi)始按鈕添加偵聽(tīng)器,this指代的是Actionistener這個(gè)類(lèi)所創(chuàng)建的對(duì)象
   stopGame.addActionListener(this);  //為結(jié)束按鈕添加偵聽(tīng)器,this指代的是Actionistener這個(gè)類(lèi)所創(chuàng)建的對(duì)象
  }
  @Override
  public void actionPerformed(ActionEvent e) {
   if(e.getSource()==startGame)
   {
    this.requestFocus();  //將光標(biāo)添加到該控件中
    control=true;
    thread.start();    //線程開(kāi)始
    //***********************************
    this.repaint(100,100, 600, 600);   
   }
   if(e.getSource()==stopGame)
   {
    this.requestFocus();  //將光標(biāo)添加到該控件中
    control=false;    //線程結(jié)束   
   }
  }
  @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();
 }
}

看完這篇關(guān)于java怎么編寫(xiě)簡(jiǎn)易貪吃蛇游戲的文章,如果覺(jué)得文章內(nèi)容寫(xiě)得不錯(cuò)的話,可以把它分享出去給更多人看到。

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI