利用J2ME制作的手机游戏----贪吃蛇

在此附上游戏画布的的代码及完成后的产品

package com.xiang.game.graph;


import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Gauge;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.TextField;

import com.xiang.game.api.object.Crawler;
import com.xiang.game.api.object.SingleNode;
import com.xiang.game.api.object.Snake;
import com.xiang.game.api.object.SnakeInit;
import com.xiang.game.util.GameConstant;
import com.xiangqi.control.Navigator;

public class MyCanvas extends Canvas implements CommandListener {
	private Snake snake;
	private Crawler crawler;
	private static Displayable instance;
	//0表示还在游戏,1表示游戏失败结束,2表示游戏成功结束
	private int gameover = 0; 

	TextField url = null;

	Gauge volume = null;
	
	private String gameoveralert = "";
	
	synchronized public static Displayable getInstance() {

		if (instance == null) {
			instance = new MyCanvas();
		}

		return instance;

	}

	private MyCanvas() {
		if(this.getWidth()/10 > this.getHeight()/10)
			GameConstant.mapSize =  this.getHeight()/10;
		else
			GameConstant.mapSize =  this.getWidth()/10;
		setTitle("游戏");
		addCommand(new Command("规则介绍", Command.HELP, 1));
		addCommand(new Command("返回", Command.BACK, 1));
		setCommandListener(this);
		snake = SnakeInit.init();
		crawler = new Crawler(snake);
		//启动多线程
		drawSnakeByThread();
	}

	public void commandAction(Command c, Displayable d) {

		Navigator.flow(c.getLabel());

	}
	protected void keyPressed(int keyCode) {
		if(gameover!=1){
		if(getGameAction(keyCode) == Canvas.UP || getGameAction(keyCode) == Canvas.DOWN
				|| getGameAction(keyCode) == Canvas.LEFT || getGameAction(keyCode) == Canvas.RIGHT)
			if(!SnakeInit.ifGoingTouchTheSide(getGameAction(keyCode), snake)){
				//判断蛇不能往身体的反方向行走,true为反方向行走
				snake = SnakeInit.eatOrGo(snake, crawler, getGameAction(keyCode));
			}else{
				//碰到边界游戏结束
				gameover = 1;
			}
		repaint();
		}
	}

	protected void paint(Graphics g) {
		if(gameover == 0){
		g.setColor(255, 255, 255);
		g.fillRect(0,0,this.getWidth(),this.getHeight());
		g.setColor(0, 0, 0);
		for (int i = 0; i < GameConstant.mapSize + 1; i++) {
			// 绘制X线
			g.drawLine(0, i * GameConstant.singleSize, GameConstant.mapSize * GameConstant.singleSize, i
					* GameConstant.singleSize);
			// 绘制Y线
			g.drawLine(i * GameConstant.singleSize, 0, i * GameConstant.singleSize, GameConstant.mapSize
					* GameConstant.singleSize);
		}
		g.setColor(140, 101, 240);
		
		SingleNode[] snakeTemplate = snake.getSnakeTemplate();
		for (int i = 0; i < snakeTemplate.length; i++) {
			SingleNode singleNode = snakeTemplate[i];
			g.fillRect(singleNode.getxCoordinate() * 10 + 1, singleNode.getyCoordinate() * 10 + 1, 10 - 1, 10 - 1);
		}
	
		g.fillRect(crawler.getxCoordinate() * 10 + 1, crawler.getyCoordinate() * 10 + 1, 10 - 1, 10 - 1);
		}else {
			g.drawString(gameoveralert, 50, 50, 0);
		}
	}
	
	public void drawSnakeByThread(){
		Thread backgroundThread = new Thread(
				new Runnable() {
					public void run() {
						while(gameover == 0){
							if(!SnakeInit.ifGoingTouchTheSide(SnakeInit.getDirectionByNode(snake), snake)){
								//判断蛇不能往身体的反方向行走,true为反方向行走
//								snake = SnakeInit.eatOrGo(snake, crawler, SnakeInit.getDirectionByNode(snake));
								int direction = SnakeInit.getDirectionByNode(snake);
								if(!SnakeInit.getIfGostraight(direction, snake)){
									if(SnakeInit.ifTouchTheCrawler(snake, crawler, direction)){
										snake = SnakeInit.eat(direction,snake);
										crawler = new Crawler(snake);
									}else {
										snake = SnakeInit.move(direction, snake);
									}
								}else{
									if(SnakeInit.ifTouchTheCrawler(snake, crawler, direction)){
										snake = SnakeInit.eat(direction,snake);
									}else{
										//如果为反方向,则直接保持前进方向,不做任何操作,由多线程操作移动
//										snake = SnakeInit.goStraight(snake);
									}
								}
								repaint();
								try {
									Thread.sleep(200);
								} catch (InterruptedException e) {
									e.printStackTrace();
								}
								if(snake.getSnakeTemplate().length == GameConstant.maxLength) gameover = 2;
							}else{
								//碰到边界游戏结束
								gameover = 1;
							}
						}
						
						if(gameover  == 1){
							gameoveralert = "游戏失败,下次再努力!";
						}else if(gameover == 2){
							gameoveralert = "恭喜你,成功过关!";
						}
						repaint();
							
					}
				}		
				);
			backgroundThread.start();
	}

}

 不懂得请详询  QQ:526151410;成品和源代码在附件里,源代码加密,请详询

你可能感兴趣的:(j2me)