贪吃蛇 垃圾版 已经完成~~~耶~~~拿出来秀秀


import java.awt.Button;
import java.awt.Canvas;
import java.awt.Color;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.JOptionPane;


public class Snake implements KeyListener,Runnable {
        /**蛇最多只能有25个节点*/
        SnakeNode snakenods[] = new SnakeNode[25];
        /**蛇的每个节点都要有一个坐标*/
        Point pSnakeNodePoint[] = new Point[15];
        /**由于不知道怎样实现动态改变数组下标,只好用一个值来记录实际蛇的节点个数,即长度*/
        int itSnakeLength;
        /**特意的设置两个值来跟踪蛇的头和尾*/
        int itHead,itTail;
        /**蛇的运动方向*/
        int itDirection;
        /**只有一个线程*/
        Thread tdSnake;
        /**跟踪头尾点的坐标*/
        Point pHead,pTail;
        /**窗口必需为类成员*/
        SnakeFrame mf;
        /**蛇的目标--食物*/
        SnakeNode snFood;
        /**蛇运动的速度

  • 单位是毫秒,值越大,蛇的运动速度就越慢
  • */
            int itSnakeSpeed;
            /**游戏的分数显示*/
            int itGrades;
            /**在按钮上显示游戏分数和等级*/
            Button btShowGrades;
            public static void main(String args[]){       
                    new Snake();
            }
            Snake(){               
                    Init();
            }
            /**初始化界面
  • 之所以不把它写到构造方法是因为在蛇死亡的时候要进行重新开局
  • */
            public void Init(){
                    mf = new SnakeFrame(30,30,500,500);
                    mf.addKeyListener(this);
                    mf.setTitle("计科05级5班JAVA课程 课本例子的升级 教材245页 小球运动");
                    mf.setLayout(null);       
                    DrawWall();
                   
                    itSnakeSpeed = 500;
                    itGrades = 0;
                    btShowGrades = new Button("游戏分数"+String.valueOf(itGrades));
                    mf.add(btShowGrades);
                    btShowGrades.setBounds(400,400,100,20);

                    snakenods[0] = new SnakeNode(0);
                    mf.add(snakenods[0]);
                    snakenods[0].setLocation(40,40);
                    snakenods[0].setBackground(Color.RED);
                    itHead = 0;
                   
                    snakenods[1] = new SnakeNode(1);
                    snakenods[1].setBackground(Color.BLUE);
                    mf.add(snakenods[1]);
                    snakenods[1].setLocation(60,40);
                   
                    snakenods[2] = new SnakeNode(2);
                    snakenods[2].setBackground(Color.CYAN);
                    mf.add(snakenods[2]);
                    snakenods[2].setLocation(80,40);
                   
                    itTail = 2;
           
                    itDirection = 1;
                   
                    itSnakeLength = 3;
                   
                    snFood = new SnakeNode(90);
                    mf.add(snFood);
                    snFood.setLocation((int) Math.round(Math.random()*300),(int) Math.round(Math.random()*300));
                   
                    tdSnake = new Thread(this);
                    tdSnake.start();
            }
            public void keyPressed(KeyEvent e){
                    if(e.getKeyCode()==KeyEvent.VK_LEFT){
                            itDirection = KeyEvent.VK_LEFT;
                    }
                    if(e.getKeyCode()==KeyEvent.VK_RIGHT){
                            itDirection = KeyEvent.VK_RIGHT;
                    }
                    if(e.getKeyCode()==KeyEvent.VK_DOWN){
                            itDirection = KeyEvent.VK_DOWN;
                    }
                    if(e.getKeyCode()==KeyEvent.VK_UP){
                            itDirection = KeyEvent.VK_UP;
                    }
                    if(e.getKeyCode()==KeyEvent.VK_SPACE){
                            EatFood();
                    }
            }
            public void keyTyped(KeyEvent e){}
            public void keyReleased(KeyEvent e){}
            public void run(){
                    while(true){
                            switch(itDirection){
                                    case KeyEvent.VK_UP:{
                                            snakenods[itTail].setLocation(snakenods[itHead].getBounds().x,snakenods[itHead].getBounds().y-20);
                                            isSnakeDied();
                                            break;
                                    }
                                    case KeyEvent.VK_DOWN:{
                                            snakenods[itTail].setLocation(snakenods[itHead].getBounds().x,snakenods[itHead].getBounds().y+20);
                                            isSnakeDied();
                                            break;
                                    }
                                    case KeyEvent.VK_LEFT:{
                                            snakenods[itTail].setLocation(snakenods[itHead].getBounds().x-20,snakenods[itHead].getBounds().y);
                                            isSnakeDied();
                                            break;
                                    }
                                    default:{
                                            snakenods[itTail].setLocation(snakenods[itHead].getBounds().x+20,snakenods[itHead].getBounds().y);
                                            isSnakeDied();
                                            break;
                                    }
                            }
                           
                            EatFood();                       
                            itHead = itTail;
                            if(itTail == 0)itTail = itSnakeLength-1;
                            else {itTail = itTail-1;}
                            try {
                                    tdSnake.sleep(itSnakeSpeed);
                            } catch (InterruptedException e) {
                                    e.printStackTrace();
                            }
                    }
            }
            /**画一堵墙*/
            public void DrawWall(){
                    SnakeNode Wall[][] = new SnakeNode[2][18];
                    for(int i=0;i                        Wall[0] = new SnakeNode(i);
                            mf.add(Wall[0]);
                            Wall[0].setLocation(i*20+3,350);
                    }
                    for(int i=0;i                        Wall[1] = new SnakeNode(i);
                            mf.add(Wall[1]);
                            Wall[1].setLocation(350,i*20);
                    }
            }
            /**蛇吃到食物的时候,长一个节点*/
            public void EatFood(){
                    if(Math.abs(snakenods[itHead].getBounds().x-snFood.getBounds().x)<20&&Math.abs(snakenods[itHead].getBounds().y-snFood.getBounds().y)<20){
                            snakenods[itSnakeLength] = new SnakeNode(itSnakeLength);
                            mf.add(snakenods[itSnakeLength]);               
                            itSnakeLength ++;                               
                            snFood.setLocation((int) Math.round(Math.random()*300+20),(int) Math.round(Math.random()*300+20));
                           
                            itGrades ++;
                            btShowGrades.setLabel("游戏分数"+itGrades);
                            if(itGrades%2==0){
                                    itSnakeSpeed -= 100;
                            }
                            if(itGrades>=12){
                                    JOptionPane.showMessageDialog(null, "恭喜你!游戏已经赢了!!!", "胜利!", JOptionPane.INFORMATION_MESSAGE);
                                    System.exit(0);
                            }
                    }
            }
            /**判断蛇的行为是否是

  • 咬自己
  • 撞墙
  • */
            public void isSnakeDied(){
                    int itSnakteHeadID = 0;
                    for(int i=0;i                        if(snakenods[itHead].getBounds().x == snakenods.getBounds().x&&snakenods[itHead].getBounds().y == snakenods.getBounds().y){
                                    itSnakteHeadID = i;
                            }
                    }
                    for(int i=0;i                        if(i!=itSnakteHeadID){
                                    if(snakenods[itHead].getBounds().x == snakenods.getBounds().x&&snakenods[itHead].getBounds().y == snakenods.getBounds().y){
                                            JOptionPane.showMessageDialog(null, "蛇已死亡/n死亡原因:自残!", "蛇已死亡", JOptionPane.INFORMATION_MESSAGE);
                                            System.exit(0);
                                    }
                            }
                    }
                    if(snakenods[itHead].getBounds().x>300||snakenods[itHead].getBounds().y>300||snakenods[itHead].getBounds().x<0||snakenods[itHead].getBounds().y<0){
                            JOptionPane.showMessageDialog(null, "蛇已死亡/n死亡原因:撞墙!", "蛇已死亡", JOptionPane.INFORMATION_MESSAGE);
                            System.exit(0);
                    }
            }
    }

    /**蛇的节点 就是画一个正方形*/
    class SnakeNode extends Canvas{
            /** 长方形的 高

  • 默认的值是 10
  • */
            public int sqHeight = 10;
            /**长方形的 宽
  • 默认的值是 10
  • */
            public int sqWidth = 10;
            public Color c = Color.RED;
            public int iStr = 2;
            SnakeNode(int i){
                    iStr = i;
                    setSize(this.sqWidth*2,this.sqHeight*2);
            }
            public void InitSize(){
                    setSize(this.sqWidth*2,this.sqHeight*2);
            }
            public void paint(Graphics g){
                    g.drawString(String.valueOf(iStr), 10, 10);
                    g.fillRect(0,0,sqWidth,sqHeight);
            }
            SnakeNode(int w,int h){
                    this.sqHeight = h;
                    this.sqWidth = w;
                    setSize(this.sqWidth*2,this.sqHeight*2);
            }
    }

    /**
    * 产生一个窗口
    * */
    class SnakeFrame extends Frame
    {
            public SnakeFrame(int xStart,int yStart,int width,int height)
            {
                    setVisible(true);//设置窗体可见
                    validate();//窗体刷新
                    setBounds(xStart,yStart,width,height);//设定窗体在屏幕的初始位置和 横宽
                    addWindowListener(new WindowAdapter()
                    {
                            public void windowClosing(WindowEvent e)
                            {
                                    System.exit(0);//点击关闭,则关闭
                            }
                    });
            }

    你可能感兴趣的:(贪吃蛇 垃圾版 已经完成~~~耶~~~拿出来秀秀)