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

📄 scanvas.java.bak

📁 JackyColorLinez源码上传。 我是第一个上传的。
💻 BAK
📖 第 1 页 / 共 2 页
字号:
import javax.microedition.lcdui.*;
import javax.microedition.lcdui.game.*;
import javax.microedition.lcdui.Image;
import java.util.*;
import java.io.*;
/**
* <p>Title: Jacky First J2ME Program</p>
*
* <p>Description: 2005-12-19</p>
*
* <p>Copyright: Copyright (c) 2005</p>
*
* <p>Company: Hong</p>
*
* @author Jacky
* @version 1.0
*/

public class SCanvas extends Canvas {
	private Display display;
	private TiledLayer tile;//棋盘
	private TiledLayer balls;//棋盘上的球
	private int upsize;//标题图片距顶端的距离
	private int themenu;//当前高亮显示的菜单项
	private String[] menus;//菜单选项
	private int menuy;//菜单开始的y坐标
	private int bdColumn;//棋盘的列数
	private int bdRow;//棋盘的行数
	private int killBall;//小球消失的最低限度数目
	private int startBall;//开始时显示的小球数量
	private int ballNumber;//彩球的总的颜色数
	private int mx,my;//小球的x,y坐标偏移量(单位:像素)

	//定义常量
	private int GameStart;//游戏开始
	private int GamePause;//游戏暂停
	private int GameOver;//游戏结束
	private int GameStatus;//游戏当前状态
	private int NewGame;//新游戏
	private Random bdRand;//棋盘的随机数
	private Random ballRand;//小球颜色随机数
	private ColorBall cur;//定义一个移动光标
	private Sprite flashcur;//一个选中后出现的光标
	private int[] bp;//小球的索引数组
	private boolean isFailed;//是否失败了
	private int score;//该局的得分
	private int thecol,therow,thecolor;//定义临时的行、列及颜色存储数
	private boolean isFlash;//是否开始显示闪烁光标
	private int[] flashIndex;//闪烁光标的动画索引


	//定义一个移动精灵
	class ColorBall{
		public Sprite ball;//小球精灵
		public int color;//小球的颜色
		public int x,y;//小球的坐标

		//构造函数
		public ColorBall(){
			color=0;
			x=0;
			y=0;
		}
		public ColorBall(int cl,int bx,int by){
			color=cl;
			x=bx;
			y=by;
		}

		//设置小球的颜色
		void setColor(int cl){
			color=cl;
		}

		//设置小球的位置
		void setPosition(int bx,int by){
			x=bx;
			y=by;
		}

		//更新小球
		void refresh(){
			repaint();
		}

		//绘制小球
		void paint(Graphics g){
			if(color==0)
				return;
			try{
				ball=new Sprite(Image.createImage("/"+color+".png"),18,18);
			}catch(IOException e){
				System.err.println("加载图片失败!");
			}
			ball.setPosition(x,y);
			ball.paint(g);
		}

		void paint(Graphics g,String img,int width,int height){
			try{
				ball=new Sprite(Image.createImage(img),width,height);
			}catch(IOException e){
				System.err.println("加载图片失败!");
			}
			ball.setPosition(x,y);
			ball.paint(g);
		}

		//释入资源
		void clear(){
			if(ball!=null){
				ball=null;
			}
		}
	}

	public SCanvas(Display d) {
		super();
		//初始化参数
		display			=d;
		upsize			=20;
		themenu			=0;
		bdColumn		=9;
		bdRow			=9;
		GamePause		=0;
		GameStart		=1;
		GameOver		=2;
		GameStatus		=0;
		menuy			=20;
		killBall		=5;
		startBall		=5;
		mx				=0;
		my				=10;
		ballNumber		=7;
		bdRand			=new Random();
		ballRand		=new Random();
		bp				=new int[81];
		isFailed		=false;
		score			=0;
		thecol			=0;//临时存储的行索引
		therow			=0;//临时存储的列索引
		thecolor		=0;//临时存储的颜色索引
		isFlash			=false;
		flashIndex		=new int[34];
		for(int i=0;i<flashIndex.length;i+=2){
			if(i%2==0){
				flashIndex[i]=1;
			}else{
				flashIndex[i]=2;
			}
		}

		int[] layerMap={
			1,1,1,1,1,1,1,1,1,
			1,1,1,1,1,1,1,1,1,
			1,1,1,1,1,1,1,1,1,
			1,1,1,1,1,1,1,1,1,
			1,1,1,1,1,1,1,1,1,
			1,1,1,1,1,1,1,1,1,
			1,1,1,1,1,1,1,1,1,
			1,1,1,1,1,1,1,1,1,
			1,1,1,1,1,1,1,1,1
		};
		for(int i=0;i<bp.length;i++){
			bp[i]=0;//初始化当前显示为空
			//bp[i]=Math.abs(ballRand.nextInt()%8);
		}

		try{
			tile	=new TiledLayer(bdColumn,bdRow,Image.createImage("/rect.jpg"),18,18);//棋盘
			balls	=new TiledLayer(bdColumn,bdRow,Image.createImage("/ball.png"),18,18);//显示的小球
			flashcur=new Sprite(Image.createImage("/flashcursor.png"),18,18);//创建闪烁的光标
		}
		catch(IOException e){
			System.err.println("加载图片失败!");
		}
		//设置棋盘
		for(int i=0;i<layerMap.length;i++){
			int column=i%bdColumn;
			int row=(i-column)/bdRow;
			tile.setCell(column,row,layerMap[i]);
		}
		for(int i=0;i<bp.length;i++){
			int column=i%bdColumn;
			int row=(i-column)/bdRow;
			balls.setCell(column,row,bp[i]);
		}
		tile.setPosition(0,0);
		balls.setPosition(0,0);

		menus=new String[4];
		menus[0]="开始游戏";
		menus[1]="游戏排行";
		menus[2]="制作小组";
		menus[3]="退出游戏";

		//建立光标
		cur=new ColorBall(0,72,72);
		flashcur.setPosition(72,72);
		flashcur.setFrameSequence(flashIndex);//设置动画索引序列
		flashcur.setVisible(false);//设为不可见
	}

	//开始
	void start(){
		display.setCurrent(this);
		repaint();
	}

	void placeaball(){
		boolean hasZero=false;
		while(true){
			//System.out.println("-------");
			for(int i=0;i<bp.length;i++){
				int column=i%bdColumn;
				int row=(i-column)/bdRow;
				if(balls.getCell(column,row)==0){
					hasZero=true;
				}
			}
			if(hasZero==false){
				isFailed=true;
				return;
			}
			int col=Math.abs(bdRand.nextInt()%9);
			int row=Math.abs(bdRand.nextInt()%9);
			if(balls.getCell(col,row)==0){
				//System.out.println("rol:"+col+"--row:"+row);
				int cl=Math.abs(ballRand.nextInt()%7)+1;
				//存储当前Cell的行列及颜色
				thecol=col;
				therow=row;
				thecolor=cl;
				balls.setCell(col,row,cl);
				return;
			}
		}
	}

	public void paint(Graphics g){
		//测试随机数
		//Random rd=new Random();
		//System.out.println(Math.abs(rd.nextInt()%81));
		//清理画布
		g.setColor(0,0,0);
		g.fillRect(0,0,getWidth(),getHeight());

		//加载棋盘
		tile.paint(g);

		switch(GameStatus){
			case 0:
				int y=20;
				//绘制游戏菜单
				for(int i=0;i<menus.length;i++){
					if(i==themenu){
						g.setColor(0,0,255);
					}else{
						g.setColor(255,255,255);
					}
					int width=Font.getDefaultFont().getHeight()*4;
					g.drawString(menus[i],getWidth()/2-width/2,y,Graphics.TOP|Graphics.LEFT);
					y+=Font.getDefaultFont().getHeight();
				}
				break;
			case 1:
				//游戏开始
				balls.paint(g);
				if(isFlash)
					flashcur.paint(g);
				cur.paint(g,"/cursor.png",18,18);
				g.setColor(255,255,255);
				int height=tile.getHeight();
				g.drawString("Score:"+score,0,height,Graphics.TOP|Graphics.LEFT);
				break;
			case 2:
				//游戏结束
				g.setColor(255,255,255);
				String gameover="是否要结束游戏?";
				g.drawString(gameover,(getWidth()/2-Font.getDefaultFont().getHeight()*gameover.length()/2),getHeight()/2,Graphics.TOP|Graphics.LEFT);
				break;
			default:
				break;
		}
	}
	
	//小球放下后按规则消掉5个或以上的小球
	int killball(int col,int row,int thecolor){
		int lcol,rcol,urow,drow,lucol,lurow,rdcol,rdrow,rucol,rurow,ldcol,ldrow;
		int hballnum,vballnum,pballnum,nballnum,killedballnum,kb,i,j;
		//横
		lcol=col;
		rcol=col;
		//竖
		urow=row;
		drow=row;
		//捺
		lucol=col;
		lurow=row;
		rdcol=col;
		rdrow=row;
		//撇
		rucol=col;
		rurow=row;
		ldcol=col;
		ldrow=row;
		//各方向小球数
		hballnum=0;
		vballnum=0;
		pballnum=0;
		nballnum=0;
		killedballnum=0;
		kb=0;
		i=0;
		j=0;
		
		//横:先水平左后水平右
		for(i=lcol;i>=0;i--){
			if(balls.getCell(i,row)!=thecolor){
				lcol=i+1;
				break;
			}else if(i==0&&balls.getCell(i,row)==thecolor){
				lcol=0;
				break;
			}
		}
		for(i=rcol;i<=8;i++){
			if(balls.getCell(i,row)!=thecolor){
				rcol=i-1;
				break;
			}else if(i==8&&balls.getCell(i,row)==thecolor){
				rcol=8;
				break;
			}
		}
		//竖:先垂直上后垂直下
		for(i=urow;i>=0;i--){
			if(balls.getCell(col,i)!=thecolor){
				urow=i+1;
				break;
			}else if(i==0&&balls.getCell(col,i)==thecolor){
				urow=0;
				break;
			}
		}
		for(i=drow;i<=8;i++){
			if(balls.getCell(col,i)!=thecolor){
				drow=i-1;

⌨️ 快捷键说明

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