java写出蛇吃蛋小游戏

package snakeategg;

import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.Map;

/**
 * Created by Administrator on 2016/11/9.
 */
public class SnakeControl implements KeyListener {
    private SnakeModel model;
    private SnakeView view;
    private int district_maxX;
    private int district_maxY;
    public SnakeControl(){
        this.district_maxX=30;
        this.district_maxY=40;
        inits();
    }
   public SnakeControl(int maxX,int maxY){
       if((10

package snakeategg;

import javax.swing.*;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.Observable;
import java.util.Random;

/**
 * Created by Administrator on 2016/11/9.
 */
public class SnakeModel extends Observable implements Runnable{
    public static final int UP=2;
    public static final int DOWN=4;
    public static final int LEFT=1;
    public static final int RIGHT=3;
    private boolean [][]matrix;
    private LinkedList nodeArray=new LinkedList();
    private Node food;
    private int maxX;
    private int maxY;
    private int timeInterval=200;
    private int direction=UP;
    private boolean running=false;
    private double speedChangeRate=0.75;
    private boolean paused=false;
    private int score=0;
    private int countMove=0;
    public SnakeModel(int maxX,int maxY){
        this.maxX=maxX;
        this.maxY=maxY;
        reset();
    }
    public void reset(){
        direction=SnakeModel.UP;
        timeInterval=200;
        paused=false;
        score=0;
        countMove=0;
        matrix=new boolean[maxX][maxY];
        for(int i=0;i20?10:maxX/2;
        int x=maxX/2;
        int y=maxY/2;
        for(int i=0;i0?ScoreGet:10;
                    countMove=0;
                    food=creatFood();
                    matrix[food.x][food.y]=true;
                    return true;
                }else {
                    return false;
                }
            }else {
                nodeArray.addFirst(new Node(headX,headY));
                matrix[headX][headY]=true;
                head=(Node)nodeArray.removeLast();
                matrix[head.x][head.y]=false;
                countMove++;
                return true;
            }
        }
        return false;
    }
    private Node creatFood() {
        int x=0;
        int y=0;
        do{
            Random r=new Random();
            x=r.nextInt(maxX);
            y=r.nextInt(maxY);
        }while (matrix[x][y]);

            return new Node(x,y);
    }

    @Override
    public void run() {
        running=true;
        while (running){
            try {
                Thread.sleep(timeInterval);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            if(!paused){
                if(moveOn()){
                    setChanged();
                    notifyObservers();
                }else {
                    JOptionPane.showMessageDialog(null,"你失败了","GAME OVER",JOptionPane.INFORMATION_MESSAGE);
                    break;
                }
            }
        }if(!running){
            JOptionPane.showMessageDialog(null,"你停止了游戏","game over",JOptionPane.INFORMATION_MESSAGE);
        }
    }
    public void speedUp(){
        timeInterval*=speedChangeRate;
    }
    public void speedDown(){
        timeInterval/=speedChangeRate;
    }
    public void changePauserState(){
        paused=!paused;
    }
    public boolean isRunning(){
        return running;
    }
    public void SetRunning(boolean running){

        this.running=running;
    }
    public LinkedList getNodeArray(){
        return nodeArray;
    }
    public Node getFood(){
        return food;
    }
    public int getSore(){
        return score;
    }
    public String toString(){
         String result="";
        for(int i=0;i

package snakeategg;

import javafx.beans.Observable;

import javax.swing.*;
import java.awt.*;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.Observer;

/**
 * Created by Administrator on 2016/11/9.
 */
public class SnakeView extends JFrame implements Observer{
    public static final int nodeWidth=10;
    public static final int nodeHeight=10;
    private int canvasWidth;
    private int cancasHeight;
    private int startX=0;
    private int startY=0;
    JLabel labelScore;
    Canvas paitCanvas;
    public SnakeView(){
        this(30,40,0,0);
    }
    public SnakeView(int maxX,int maxY){
        this(maxX,maxY,0,0);
    }
    public SnakeView(int maxX,int maxY,int startX,int startY){
        this.canvasWidth=maxX*nodeWidth;
        this.cancasHeight=maxY*nodeHeight;
        this.startX=startX;
        this.startY=startY;
        init();
    }

    private void init() {
        this.setName("蛇吃蛋");
        this.setLocation(startX,startY);
        Container cp = this.getContentPane();
        labelScore=new JLabel("Score");
        cp.add(labelScore,BorderLayout.NORTH);
        paitCanvas=new Canvas();
        paitCanvas.setSize(canvasWidth+1,cancasHeight+1);
        cp.add(paitCanvas,BorderLayout.CENTER);
        JPanel panelBottom=new JPanel();
        panelBottom.setLayout(new BorderLayout());
        JPanel panel1=new JPanel();
        panel1.setLayout(new BorderLayout());
        JLabel lablehelp;
        lablehelp=new JLabel("pageup,pagedown for speed",JLabel.CENTER);
        panel1.add(lablehelp,BorderLayout.NORTH);
        lablehelp=new JLabel("enter ot r or s for start",JLabel.CENTER);
        panel1.add(lablehelp,BorderLayout.CENTER);
        panelBottom.add(panel1,BorderLayout.NORTH);
        lablehelp=new JLabel("q for stop",JLabel.CENTER);
        panelBottom.add(lablehelp);
        cp.add(panelBottom,BorderLayout.SOUTH);
        this.pack();
        this.setResizable(false);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setVisible(true);

    }

    @Override
    public void update(java.util.Observable o, Object arg) {
        SnakeModel model=(SnakeModel) o;
        Graphics g = paitCanvas.getGraphics();
        g.setColor(Color.white);
        g.fillRect(0,0,canvasWidth,cancasHeight);
        g.setColor(Color.black);
        LinkedList na=model.getNodeArray();
        Iterator it = na.iterator();
        while (it.hasNext()){
            SnakeModel.Node n=(SnakeModel.Node) it.next();
            drawNode(g,n);
        }
        g.setColor(Color.RED);
        SnakeModel.Node n = model.getFood();
        drawNode(g,n);
        this.updateScore(model.getSore());
    }

    private void updateScore(int sore) {
        String s="Score:"+sore;
        labelScore.setText(s);
    }

    private void drawNode(Graphics g, SnakeModel.Node n) {
        g.fillRect(n.x*nodeWidth,n.y*nodeHeight,nodeWidth-1,nodeHeight-1);

    }
}

package snakeategg;

/**
 * Created by Administrator on 2016/11/9.
 */
public class Test {
    public  static void main(String []args){
        SnakeControl control=new SnakeControl(30,30);
    }
}

你可能感兴趣的:(java小游戏)