当前位置:编程学习 > JAVA >>

Java贪吃蛇游戏

package snake.game;


 
import java.awt.CardLayout;
import java.awt.Color;
import java.awt.Graphics;
 
import java.awt.Image;
import java.awt.Insets;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Timer;


import javax.imageio.ImageIO;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;


public class SnakeGame implements ActionListener,KeyListener,MouseListener{


private long initTime ;
SnakeGame.InterClassSnake innerSnake;
boolean signal=false;
private Timer  tim;
int douX=120,douY=120,direct;
private ArrayList<Point>  snake = new ArrayList<Point>();
JFrame jf = new JFrame("贪吃蛇游戏");
JPanel  mainPane = new JPanel();
//欢迎界面、开始界面、游戏界面
WelcomPanel  wp = new WelcomPanel();
StartPanel  sp = new StartPanel();
JPanel  gamePane= new JPanel();
 
BallDemo bd ;

JButton [] btns = {new JButton("下一步"),new JButton("上一步"),new JButton("开始")};
Color[] yanse = {Color.black,Color.blue,Color.cyan,Color.green};
CardLayout gld= new CardLayout();
{
snake.add(new Point(90,150));
snake.add(new Point(90,120));
snake.add(new Point(90,90));
snake.add(new Point(90,60));
snake.add(new Point(90,30));

wp.setLayout(null);
wp.setBounds(0, 0, 500, 450);
btns[0].setMargin(new Insets(0,0,0,0));
btns[0].setBounds(350, 350, 50, 30);
wp.add(btns[0]);


sp.setLayout(null);
sp.setBounds(0, 0, 500, 450);
btns[1].setMargin(new Insets(0,0,0,0));
btns[1].setBounds(300, 350, 50, 30);
btns[2].setMargin(new Insets(0,0,0,0));
btns[2].setBounds(350, 350, 50, 30);
sp.add(btns[1]);
sp.add(btns[2]);
 
btns[0].addActionListener(this);
btns[1].addActionListener(this);
btns[2].addActionListener(this);

gamePane.addMouseListener(this);
jf.addKeyListener(this);
mainPane.setLayout(gld);
mainPane.add(wp,"1");
mainPane.add(sp, "2");
mainPane.add(gamePane, "3");
bd=new BallDemo(500,450,gamePane);
mainPane.setSize(500,450);
mainPane.setVisible(true);
}
public SnakeGame(){
innerSnake =this. new InterClassSnake();
jf.add(mainPane);
jf.setSize(500, 450);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jf.setVisible(true);
}
/**
* @param args
*/
public static void main(String[] args) {
    
      
       
SnakeGame sg=new SnakeGame();


 
@Override
public void actionPerformed(ActionEvent e) {
if(e.getSource()==btns[0]){
gld.show(mainPane, "2");
}
if(e.getSource()==btns[1]){
gld.show(mainPane, "1");
}
if(e.getSource()== btns[2]){
gld.show(mainPane, "3");
initTime=System.currentTimeMillis();
tim = new Timer();
tim.scheduleAtFixedRate(new TimerCounts(initTime), 0,50000); //间隔大概10分钟提醒一次
innerSnake.setDaemon(true);
innerSnake.start();
}

}

    class InterClassSnake extends Thread {
   
        int tempDouX,tempDouY;
       
    public void run(){
    while(true){
   
    this.displaySnake(gamePane.getGraphics());
    this.displayDou(gamePane.getGraphics());
       if(signal){moveSnake();signal=false;gamePane.repaint();  }
           if(this.isEatDou()){
            snake.add(0, new Point(douX,douY));System.out.println("hello world 1");
            makeDou();          
           }
           if(this.isEatSelf() || this.isOverBounds()){System.out.println("hello world 2");
            JOptionPane.showMessageDialog(null, "你玩完了");
            System.exit(0);
           }
    }
    }
   /*造豆和造蛇、显示蛇、显示豆子函数*/
  public void makeDou(){
 douX=(int)((int)(Math.random()*10));
 douY=(int)((int)(Math.random()*8));
     if(douX>15)douX=douX-4;
     if(douY>14)douY=douY-4;
     douX=douX*30;douY=douY*30;
     for(Point x:snake){
     int tempX=x.x;
     int tempY=x.y;
     if(tempX==douX&&tempY==douY){  
    makeDou();
     }
       }
   }
   public void displaySnake(Graphics g){
  for(Point pos:snake){
  if(pos==snake.get(0)){
  g.setColor(Color.green); 
  g.fillRect(pos.x, pos.y, 30, 30); 
  } 
  else
      g.setColor(Color.red); 
      g.fillRect(pos.x, pos.y, 30, 30);
   }
   }
   public void displayDou(Graphics g){
       g.setColor(gamePane.getBackground());
       g.drawRect(tempDouX, tempDouY, 30, 30);
       g.setColor(Color.blue);
       g.fillRect(douX, douY, 30, 30);
       tempDouX=douX;tempDouY=douY;
   }
   //eat the bean ,return true if eat successfully,otherwise return false;
   public boolean  isEatDou(){
    if(douX==snake.get(0).x && douY==snake.get(0).y){
     return true;
    }
    return false;
   }
        public boolean isEatSelf(){
       int x=snake.get(0).x , y =snake.get(0).y;
        for(int i=snake.size()-1;i>1;i--){
      if(x==snake.get(i).x && y==snake.get(i).y)
        return true;
       }
     return false;
       }
       public boolean isOverBounds(){
   

补充:软件开发 , Java ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,