⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 snake.java

📁 用Java编写的小游戏
💻 JAVA
字号:
package com.cienet.levi;
/**
 * Copyright cienet.levi
 * 
 * Snake.Snake
 * 
 * @author cienet
 * @create 2008/01/11 13:34:10 - ver1.0
 */
import java.util.Random;
import java.util.Vector;

import javax.microedition.lcdui.Graphics;

public class Snake {
	//const of snake body length
	private final int INIT_BODY_LENGTH = 8;
	//const of snake head color
	private final int HEAD_COLOR = 0xff0000;
	//const of snake body color
	private final int BODY_COLOR = 0xffffff;
	
	//random num generator
	public static Random random = new Random();
	//snakecanvas reference
	public SnakeCanvas snakeCanvas;
	//current direction of snake
	public int currentDirection;

	//head pixel 
	private Pixel head;
	//rail pixel
	private Pixel lastRail;
	
	private Vector body;

	/**
	 * Constructor of Snake
	 * In this method, it just do following things:
	 * 1. caculate head's location
	 * 2. init each body's location based on INIT_BODY_LENGTH
	 * 3. init lastRail
	 * 4. init currentDirection
	 * @param sc SnakeCanvas object
	 */
	public Snake(SnakeCanvas sc) {
		snakeCanvas = sc;
		Logger.log(sc.getRelativeWidth(), sc.getRelativeHeight(), "relative");
		
		head = new Pixel(sc.getRelativeWidth() / 2, sc.getRelativeHeight() / 2);
		body = new Vector(INIT_BODY_LENGTH * 2);
		Pixel last, p;
		last = p= head;
		for(int i = 0;i < INIT_BODY_LENGTH;i++){
			if(last.coordinateX >= 0)
				p = new Pixel(last.coordinateX - 1, last.coordinateY);
			else if(last.coordinateY >= 0)
				p = new Pixel(last.coordinateX, last.coordinateY - 1);
			else{
//				error;
			}
			body.addElement(p);
			last = p;
		}
		lastRail = new Pixel(last.coordinateX, last.coordinateY);
		currentDirection = KeyCode.RIGHT;
	}

	/**
	 * Check if the game is over
	 * If snake touch the wall or bite itself, the game is over
	 * @return if snake touch the wall or bite itself, return true else return false
	 */
	public boolean isOver() {
		if(isTouchWall() || isBiteSelf()){
			return true; //over
		}
		return false;
	}
	
	/**
	 * check if snake touch the wall
	 * If snake's head location is over walls round, that means touch the wall
	 * @return If snake's head location is over walls round, return true else false 
	 */
	public boolean isTouchWall(){
		if(head.coordinateX < 0 || head.coordinateX > snakeCanvas.getRelativeWidth() - 1 
				|| head.coordinateY < 0 || head.coordinateY > snakeCanvas.getRelativeHeight() - 1)
			return true;
		return false;
	}
	
	/**
	 * if head touch body, that means bite itself
	 * @return if head's coordinate equals to and body's coordinate, return true else false
	 */
	public boolean isBiteSelf() {
		if(head == null)
			return false;
	
		for(int i = 0;i < body.size();i++){
			Pixel p = (Pixel)body.elementAt(i);
			if(p.coordinateX == head.coordinateX && 
					p.coordinateY == head.coordinateY){
				
				Logger.log(p.coordinateX, p.coordinateY, "isbiteSelf.true");
				return true;
			}
		}
		return false;
	}
	
	/**
	 * increase pixel append to the lastRail
	 */
	public void increaseLengthAfterRail(){
		Logger.log(head.coordinateX, head.coordinateY, "increaseLengthAfterRail.head");
		Pixel newRail = lastRail;
		body.addElement(newRail);
		lastRail = new Pixel(newRail.coordinateX, newRail.coordinateY);
	}
	
	/**
	 * if snake's head or body touch the food, that means touch food
	 * @param foodX food's coordinateX
	 * @param foodY food's corrdinateY
	 * @return if snake's head or body touch the food, return true else false 
	 */
	public boolean isTouchFood(int foodX, int foodY){
		if(head == null)
			return false;
		
		if(head.coordinateX == foodX 
				&& head.coordinateY == foodY)
			return true;
		
		for(int i = 0;i < body.size();i++){
			Pixel p = (Pixel)body.elementAt(i);
			if(p.coordinateX == foodX && 
					p.coordinateY == foodY){
				return true;
			}
		}
		return false;
		
	}
	
	/**
	 * if snake's head touch the food, that means snake eat the food
	 * @param food food in current screen
	 * @return if snake's head touch the food, return true else false
	 */
	public boolean isEatFood(Food food){
		
		if(food.coordinateX == head.coordinateX &&
				food.coordinateY == head.coordinateY){
			snakeCanvas.score++;
			Logger.log("******************start eat food**********************");
			increaseLengthAfterRail();
			return true;
		}
		return false;
	}
	
//	/**
//	 * Find the previous of the rail, that is, find the last two of body
//	 * @return the last two of body
//	 */
//	public Pixel findRailPrevious(){
//		if(head == null)
//			return null;
//		Pixel p, q;
//		p = q = head;
//		while(p.next != null){
//			q = p;
//			p = p.next;//find the last one
//		}
//		return q;
//	}

	/**
	 * Snake walk
	 * direct to current direction, move one pixel
	 */
	public void crawling() {
		Logger.log(lastRail.coordinateX , lastRail.coordinateY, "crawing.lastRail");
		
		Pixel rail = (Pixel)body.elementAt(body.size() - 1);
		lastRail.coordinateX = rail.coordinateX;
		lastRail.coordinateY = rail.coordinateY;
		
		Logger.log(rail.coordinateX, rail.coordinateY, "crawing.rail");
		Logger.log(currentDirection);
		
		body.insertElementAt(head, 0);
		Pixel temp = new Pixel();
		switch(currentDirection){
			case KeyCode.UP:
				temp.coordinateX = head.coordinateX;
				temp.coordinateY = head.coordinateY - 1;
				break;
			case KeyCode.DOWN:
				temp.coordinateX = head.coordinateX;
				temp.coordinateY = head.coordinateY + 1;
				break;
			case KeyCode.LEFT:
				temp.coordinateX = head.coordinateX - 1;
				temp.coordinateY = head.coordinateY;
				break;
			case KeyCode.RIGHT:
				temp.coordinateX = head.coordinateX + 1;
				temp.coordinateY = head.coordinateY;
				break;
		}
		head = temp;
		body.removeElement(rail);
		Logger.log(head.coordinateX, head.coordinateY, "crawing.head");
	}

	/**
	 * Snake turn
	 * if direction is set the right, change current direction to direction
	 * @param direction direction want snake turn
	 */
	public void turn(int direction) {
		if(direction == currentDirection)
			return;
		switch(direction){
			case KeyCode.UP:{
				if(currentDirection != KeyCode.DOWN){
					currentDirection = direction;
				}
				break;
			}
			case KeyCode.DOWN:{
				if(currentDirection != KeyCode.UP){
					currentDirection = direction;
				}
				break;
			}
			case KeyCode.LEFT:{
				if(currentDirection != KeyCode.RIGHT){
					currentDirection = direction;
				}
				break;
			}
			case KeyCode.RIGHT:{
				if(currentDirection != KeyCode.LEFT){
					currentDirection = direction;
				}
				break;
			}
		}
			
	}

	/**
	 * draw snake from head to rail
	 * @param g Graphics which is the tool of paint
	 */
	public void drawSnake(Graphics g){
		if(head == null)
			return ;
		//draw head
		g.setColor(HEAD_COLOR);
		g.fillRect(head.coordinateX * SnakeCanvas.pixelUnit, head.coordinateY * SnakeCanvas.pixelUnit, 
				SnakeCanvas.pixelUnit, SnakeCanvas.pixelUnit);
		
		//draw body
		g.setColor(BODY_COLOR);
		for(int i = 0;i < body.size();i++){
			Pixel p = (Pixel)body.elementAt(i);
			g.fillRect(p.coordinateX * SnakeCanvas.pixelUnit, p.coordinateY * SnakeCanvas.pixelUnit,
					SnakeCanvas.pixelUnit, SnakeCanvas.pixelUnit);
		}
	}
	
	/**
	 * return the snake coordinate string
	 */
	public String toString(){
		StringBuffer sb = new StringBuffer();
		sb.append("snake: ");
		sb.append("(" + head.coordinateX + "," + head.coordinateY + ") ");
		for(int i = 0;i < body.size();i++){
			Pixel p = (Pixel)body.elementAt(i);
			sb.append("(" + p.coordinateX + "," + p.coordinateY + ") ");
		}
		return sb.toString();
	}
	
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -