Javaは簡単なヘビ食いを実現

8286 ワード

この2,3日学んで1つの簡単な食いしん坊の蛇をして、ただ練習のために使って、だから多くの地方はすべて完璧ではありません
実装方法はチェーンテーブルで、コードに詳細なコメントがあります.
アクティブエリアYard:
import java.awt.Color;
import java.awt.Font;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;


public class Yard extends Frame {
	//     
	private static final int ROWS = 30;//   
	private static final int COLS = 30;//   
	private static final int BLOCK_SIZE = 15;//     
	
	private Font fontGameOver = new Font("  ", Font.BOLD, 50);//  
	private int score = 0;//   

	Image offScreenImage = null;//       ,    

	Snake snake = new Snake(this);//      
	Egg e = new Egg();//        ;

	PaintThread paintThread = new PaintThread();
	private boolean gameOver=false;//        

	public static int getROWS() {
		return ROWS;
	}

	public static int getCOLS() {
		return COLS;
	}

	public static int getBLOCK_XIZE() {
		return BLOCK_SIZE;
	}

	public int getScore() {
		return score;
	}

	public void launch() {//     
		this.setLocation(300, 300);//       
		this.setSize(COLS * BLOCK_SIZE, ROWS * BLOCK_SIZE);//     
		//       
		this.addWindowListener(new WindowAdapter() {
			@Override
			public void windowClosing(WindowEvent e) {
				System.exit(0);
			}
		});
		this.setVisible(true);//       

		this.addKeyListener(new KeyMonitor());//     
		new Thread(paintThread).start();
		///     
	}

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		new Yard().launch();
	}

	public void setScore(int score) {//     
		this.score = score;
	}

	@Override
	public void paint(Graphics g) {//  

		//       
		Color c = g.getColor();
		g.setColor(Color.GRAY);
		g.fillRect(0, 0, COLS * BLOCK_SIZE, ROWS * BLOCK_SIZE);

		//       
		g.setColor(Color.DARK_GRAY);

		//   
		for (int i = 1; i < ROWS; i++) {
			g.drawLine(0, i * BLOCK_SIZE, COLS * BLOCK_SIZE, BLOCK_SIZE * i);
		}
		for (int i = 1; i < ROWS; i++) {
			g.drawLine(i * BLOCK_SIZE, 0, BLOCK_SIZE * i, ROWS * BLOCK_SIZE);
		}

		g.setColor(Color.YELLOW);
		g.drawString("score:" + score, 10, 60);//    

		if(gameOver) {
			g.setFont(fontGameOver);
			g.drawString("    ", 120, 180);
			
			paintThread.pause();
		}
		
		g.setColor(c);

		snake.eat(e);
		snake.draw(g);//   
		e.draw(g);//   
	}

	@Override
	public void update(Graphics g) {
		if (offScreenImage == null) {
			offScreenImage = this.createImage(COLS * BLOCK_SIZE, ROWS * BLOCK_SIZE);
		}
		Graphics gOff = offScreenImage.getGraphics();
		paint(gOff);
		g.drawImage(offScreenImage, 0, 0, null);
	}

	public void stop() {
		gameOver=true;//    
	}

	private class PaintThread implements Runnable {
		private boolean running = true;
		private boolean pause = false;
		public void run() {
			while(running) {
				if(pause) continue; 
				else repaint();
				
				try {
					Thread.sleep(200);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}
		}
		
		public void pause() {
			this.pause = true;
		}
		
		public void reStart() {
			this.pause = false;
			snake = new Snake(Yard.this);
			gameOver = false;
		}
		
		public void gameOver() {
			running = false;
		}
		
	}

	private class KeyMonitor extends KeyAdapter {
		//     
		@Override
		public void keyPressed(KeyEvent e) {
			 int key = e.getKeyCode();
			 if(key == KeyEvent.VK_F2) {
			 paintThread.reStart();
			 }
			snake.keyPressed(e);
		}
		
	}
}

ヘビSnake
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Rectangle;
import java.awt.event.KeyEvent;


public class Snake {

	private Node head = null;//    
	private Node tail = null;//    
	private int size = 0;//     

	private Node node = new Node(20, 30, Direction.LEFT);//     ( )
	private Yard y;//      

	public Snake(Yard y) {
		head = node;
		tail = node;
		size = 1;
		this.y=y;
	}

	public void addToTail() {//        
		Node node = null;
		switch (tail.dir) {
		case LEFT:
			node = new Node(tail.row, tail.col + 1, tail.dir);//       
			break;
		case RIGHT:
			node = new Node(tail.row, tail.col - 1, tail.dir);
			break;
		case UP:
			node = new Node(tail.row + 1, tail.col, tail.dir);
			break;
		case DOWN:
			node = new Node(tail.row - 1, tail.col, tail.dir);
			break;
		}
		//      
		tail.next = node;
		node.prev = tail;
		tail = node;
		size++;
	}

	public void addToHead() {//       
		Node node = null;
		switch (head.dir) {
		case LEFT:
			node = new Node(head.row, head.col - 1, head.dir);
			break;
		case UP:
			node = new Node(head.row - 1, head.col, head.dir);
			break;
		case RIGHT:
			node = new Node(head.row, head.col + 1, head.dir);
			break;
		case DOWN:
			node = new Node(head.row + 1, head.col, head.dir);
			break;
		}
		node.next = head;
		head.prev = node;
		head = node;
		size++;
	}

	private void deleteFromTail() {
		if (size == 0)
			return;
		tail = tail.prev;
		tail.next = null;
	}

	private void checkDead() {
		//       
		if(head.row < 2 || head.col < 0 || head.row > Yard.getROWS()|| head.col > Yard.getCOLS())  {
			y.stop();
		}
		
		for(Node n = head.next; n != null; n = n.next) {
			if(head.row == n.row && head.col == n.col) {
				y.stop();
			}
		}
	}
	
	private void move() {//    
		addToHead();//          
		deleteFromTail();
		checkDead();
	}

	public void draw(Graphics g) {//    
		if (size <= 0)
			return;
		move();
		for (Node n = head; n != null; n = n.next) {
			n.draw(g);
		}
	}

	private class Node {
		int w = Yard.getBLOCK_XIZE();//     
		int h = Yard.getBLOCK_XIZE();//     
		int row, col;//     
		Direction dir = Direction.LEFT;//       
		Node next = null;//    
		Node prev = null;//    

		Node(int row, int col, Direction dir) {
			this.row = row;
			this.col = col;
			this.dir = dir;
		}

		void draw(Graphics g) {//     
			Color c = g.getColor();
			g.setColor(Color.black);
			g.fillRect(Yard.getBLOCK_XIZE() * col, Yard.getBLOCK_XIZE() * row, w, h);
			g.setColor(c);
		}
	}

	public void eat(Egg e) {
		if(this.getRect().intersects(e.getRect())) {
			e.reAppear();
			this.addToHead();
			y.setScore(y.getScore() + 5);
		}
	}

	private Rectangle getRect() {
		return new Rectangle(Yard.getBLOCK_XIZE() * head.col, Yard.getBLOCK_XIZE() * head.row, head.w, head.h);
	}

	public void keyPressed(KeyEvent e) {
		int key = e.getKeyCode();
		switch (key) {
		case KeyEvent.VK_LEFT:
			if (head.dir != Direction.RIGHT)
				head.dir = Direction.LEFT;
			break;
		case KeyEvent.VK_RIGHT:
			if (head.dir != Direction.LEFT)
				head.dir = Direction.RIGHT;
			break;
		case KeyEvent.VK_UP:
			if (head.dir != Direction.DOWN)
				head.dir = Direction.UP;
			break;
		case KeyEvent.VK_DOWN:
			if (head.dir != Direction.UP)
				head.dir = Direction.DOWN;
			break;
		}
	}
}

対象物体Egg:
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Rectangle;
import java.util.Random;

public class Egg {
	int row,col;
	int w=Yard.getBLOCK_XIZE();
	int h=Yard.getBLOCK_XIZE();
	private static Random r=new Random();//        
	private Color color=Color.green;//     
	
	public int getRow() {
		return row;
	}

	public void setRow(int row) {
		this.row = row;
	}

	public int getCol() {
		return col;
	}

	public void setCol(int col) {
		this.col = col;
	}

	
	public Egg(int row, int col) {
		this.row = row;
		this.col = col;
	}
	
	public Egg(){
		this(r.nextInt(Yard.getROWS()-2)+2, r.nextInt(Yard.getCOLS()));
	}
	
	public void reAppear(){//        
		this.row = r.nextInt(Yard.getROWS()-2) + 2;
		this.col = r.nextInt(Yard.getCOLS());
	}
	
	public Rectangle getRect() {
		return new Rectangle(Yard.getBLOCK_XIZE() * col, Yard.getBLOCK_XIZE() * row, w, h);
	}
	
	void draw(Graphics g) {//    
		Color c = g.getColor();
		g.setColor(color);
		g.fillOval(Yard.getBLOCK_XIZE() * col, Yard.getBLOCK_XIZE() * row, w, h);
		g.setColor(c);
		if(color==Color.green){
			color=color.red;
		}else{
			color=color.green;
		}
	}
}

方向direction
public enum Direction {
	LEFT,RIGHT,UP,DOWN
}