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

📄 snakecanvas.java

📁 用Java编写的小游戏
💻 JAVA
字号:
package com.cienet.levi;
/**
 * Copyright cienet.levi
 * 
 * Snake.SnakeCanvas
 * 
 * @author cienet
 * @create 2008/01/11 13:34:10 - ver1.0
 */
import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Graphics;

public class SnakeCanvas extends Canvas implements Runnable {

	//background color
	public final int BACK_COLOR = 0x000000;
	//string color
	public final int STRING_COLOR = 0x0000ff;
	//snake object
	private Snake snake;
	//food object
	private Food food;

	//is destroy the whole game
	private boolean isDestroy = false;
	//is game over this time
	public boolean isGameOver = false;
	//score of this time
	public int score;
	//pixelUnix which means resolution
	public static int pixelUnit = 10;
	//game hard level
	public int level = 10;
	//side width
	private static final int SIDE_WIDTH = 4;
	
	/**
	 * Set resolution of game
	 * @param resolution resolution of game
	 */
	public void setResolution(int resolution){
		pixelUnit = resolution;
	}
	
	/**
	 * Set level of game
	 * @param level hard level of game
	 */
	public void setLevel(int level){
		this.level = level;
	}

	/**
	 * Relative width
	 * @return
	 */
	public int getRelativeWidth() {
		return getWidth() / pixelUnit;
	}

	/**
	 * Relative height
	 * @return
	 */
	public int getRelativeHeight() {
		return getHeight() / pixelUnit;
	}

	/**
	 * Constructor of SnakeCanvas
	 */
	public SnakeCanvas() {
		snake = new Snake(this);
		food = new Food(this, snake);
		repaint();
	}

	/**
	 * Run methods which running in a thread
	 * While not exit, continue run
	 */
	public void run() {
		while (!isDestroy) {
			while (!isGameOver) {
				repaint();
				if (snake.isOver() == true) {
					isGameOver = true;
				}
				snake.crawling();
				if (snake.isEatFood(food))
					food = new Food(this, snake);
				repaint();
				try {
					Thread.sleep(20 * level);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
			repaint();
			try{
				Thread.sleep(500);
			}
			catch(Exception e){
				e.printStackTrace();
			}
		}
	}

	/**
	 * Paint method which override Canvas
	 */
	public void paint(Graphics g) {

		g.setColor(BACK_COLOR);
		g.fillRect((getWidth() % pixelUnit) / 2, (getHeight() % pixelUnit) / 2,
				getWidth() - (getWidth() % pixelUnit), getHeight()
						- (getHeight() % pixelUnit));
		if (!isGameOver) {
			snake.drawSnake(g);
			food.drawFood(g);
		} else {
			showScore(g);
		}
	}

	/**
	 * keyPressed method which override Canvas
	 */
	protected void keyPressed(int keyCode) {
		// TODO Auto-generated method stub
		Logger.log(keyCode);
		if(keyCode == KeyCode.FUNCTION2){
			restart();
		}
		if(keyCode == KeyCode.FUNCTION1){
			destroy();
		}
		if (keyCode == KeyCode.UP || keyCode == KeyCode.DOWN
				|| keyCode == KeyCode.LEFT || keyCode == KeyCode.RIGHT) {
			snake.turn(keyCode);
		}

	}

	/**
	 * Show score when game is over
	 * @param g graphics object which means draw tool
	 */
	public void showScore(Graphics g) {
		g.setColor(STRING_COLOR);
		g.drawString("Your score is: " + score, SIDE_WIDTH, SIDE_WIDTH,
				Graphics.TOP | Graphics.LEFT);
	}
	
	/**
	 * Destroy method which destroy game
	 */
	public void destroy(){
		snake = null;
		food = null;
		isDestroy = true;
	}

	/**
	 * restart method which start a new game
	 */
	public void restart() {
		score = 0;
		isGameOver = false;
		snake = new Snake(this);
		food = new Food(this, snake);
	}

}

⌨️ 快捷键说明

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