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

📄 mainpit.java

📁 一个J2ME SLG游戏范例
💻 JAVA
📖 第 1 页 / 共 4 页
字号:
			{
				c2 = 1;
			}
			if(infoact[atktarget[currentatk]][1] > infoact[currentact][1])
			{
				picnum = actpic[infoact[atktarget[currentatk]][0]][4] + (tally/10)%2;

				g.drawImage(picact[picnum],
				(infoact[atktarget[currentatk]][1] - x) * 16 - c1 + actorpos[picnum][0],
				(infoact[atktarget[currentatk]][2] - y) * 16 + actorpos[picnum][1],
				g.LEFT|g.TOP);

				picnum = actpic[infoact[currentact][0]][5] + (tally/10)%2;

				g.drawImage(picact[picnum],
				(infoact[currentact][1] - x) * 16 + c2 + actorpos[picnum][0],
				(infoact[currentact][2] - y) * 16 + actorpos[picnum][1],
				g.LEFT|g.TOP);
			}
			else if(infoact[atktarget[currentatk]][1] < infoact[currentact][1])
			{
				picnum = actpic[infoact[atktarget[currentatk]][0]][5] + (tally/10)%2;

				g.drawImage(picact[picnum],
				(infoact[atktarget[currentatk]][1] - x) * 16  + c1 + actorpos[picnum][0],
				(infoact[atktarget[currentatk]][2] - y) * 16 + actorpos[picnum][1],
				g.LEFT|g.TOP);

				picnum = actpic[infoact[currentact][0]][4] + (tally/10)%2;

				g.drawImage(picact[picnum],
				(infoact[currentact][1] - x) * 16  - c2 + actorpos[picnum][0],
				(infoact[currentact][2] - y) * 16 + actorpos[picnum][1],
				g.LEFT|g.TOP);
			}
			else if(infoact[atktarget[currentatk]][2] > infoact[currentact][2])
			{
				picnum = actpic[infoact[atktarget[currentatk]][0]][3] + (tally/10)%2;

				g.drawImage(picact[picnum],
				(infoact[atktarget[currentatk]][1] - x) * 16 + actorpos[picnum][0],
				(infoact[atktarget[currentatk]][2] - y) * 16  - c1 + actorpos[picnum][1],
				g.LEFT|g.TOP);

				picnum = actpic[infoact[currentact][0]][2] + (tally/10)%2;

				g.drawImage(picact[picnum],
				(infoact[currentact][1] - x) * 16 + actorpos[picnum][0],
				(infoact[currentact][2] - y) * 16  + c2 + actorpos[picnum][1],
				g.LEFT|g.TOP);
			}
			else
			{
				picnum = actpic[infoact[atktarget[currentatk]][0]][2] + (tally/10)%2;

				g.drawImage(picact[picnum],
				(infoact[atktarget[currentatk]][1] - x) * 16 + actorpos[picnum][0],
				(infoact[atktarget[currentatk]][2] - y) * 16 + c1 + actorpos[picnum][1],
				g.LEFT|g.TOP);

				picnum = actpic[infoact[currentact][0]][3] + (tally/10)%2;

				g.drawImage(picact[picnum],
				(infoact[currentact][1] - x) * 16 + actorpos[picnum][0],
				(infoact[currentact][2] - y) * 16 - c2 + actorpos[picnum][1],
				g.LEFT|g.TOP);
			}
			DrawPerson(g);
			DrawLifeShow(g, currentact, atktarget[currentatk]);
			repaint();
			try
			{
				Thread.sleep(100);
			}
			catch(Exception e)
			{}
		}
	}

	public void DrawMoveRange(Graphics g,int c)
	{
		for(int j = 0;j<= MAP_MAX_HEIGHT;j++)
		{
			for(int i = 0; i<= MAP_MAX_WIDTH;i++)
			{
				if(i - x > SCREEN_MAX_WIDTH||i - x < 0||j - y > SCREEN_MAX_HEIGHT||j - y < 0)
				{
					continue;
				}
				if(move[j][i] > -1)
				{
					g.setColor(c);
					g.drawRect((i - x) * TILE_SIZE,(j - y) * TILE_SIZE,TILE_SIZE,TILE_SIZE);
				}
			}
		}
	}

	public void DrawPerson(Graphics g)
	{
		int picnum;
		for(int i = 0; i < actnum; i++)//绘制人物
		{
			if(infoact[idact[i]][2] - y < 0||infoact[idact[i]][2] - y > 7||infoact[idact[i]][1] - x < 0||infoact[idact[i]][1] - x > 7||idact[idact[i]] == currentact)//屏幕显示外和获得焦点的人物不绘制
			{
				continue;
			}
			if(targetstate == 3&&idact[idact[i]] == atktarget[currentatk])
			{
				continue;
			}
			picnum = actpic[infoact[idact[i]][0]][0] + (tally/10)%2;
			g.drawImage(picact[picnum],
			(infoact[idact[i]][1] - x) * TILE_SIZE + actorpos[picnum][0],
			(infoact[idact[i]][2] - y) * TILE_SIZE + actorpos[picnum][1],
			g.LEFT|g.TOP);
		}
		if(targetstate != 3)
		{
			if(currentact >= 0)//有人获得焦点的情况下绘制
			{
				if(showmovearea)//绘制选中人物移动范围
				{
					DrawMoveRange(gb,0x000000ff);
				}

				//绘制人物
				picnum = actpic[currentactkind][currentactstate] + (tally/10)%2;

				g.drawImage(picact[picnum],
				currentactSX * TILE_SIZE + actorpos[picnum][0] + currentactmovex,
				currentactSY * TILE_SIZE + actorpos[picnum][1] + currentactmovey,
				g.LEFT|g.TOP);
			}
		}
	}

	public void DealCursor()//处理光标
	{
		switch(STATE)
		{
			case 1:
				DrawCursor(gb,(SCREEN_WIDTH - 72) / 2, ((SCREEN_HEIGHT - 22 * 5)/6 + 22) * MenuFocus, 56, 6);
			break;
			case 2:
				switch(CurrentScene)
				{
					case 1://地图移动
						for(int i = 0; i < actnum; i++)
						{
							if(targetstate == 0)
							{
								if(cursorSX == infoact[idact[i]][1] - x&&cursorSY == infoact[idact[i]][2] - y)
								{
									cursortally = 0;
									currentact = idact[i];
									targetSX = cursorSX;
						   			targetSY = cursorSY;
									chooseact(currentact);//选中目标
									DealPersonInfo();
									break;
								}
							}
							if(targetstate == 0)
							{
								currentact = -1;
							}
						}
						if(!moveaction)
						{
							DrawCursor(gb,cursorSX * TILE_SIZE,cursorSY * TILE_SIZE,0,0);
						}
					break;
					case 2://菜单选择
						if(cursorSX >=4)
						{
							DrawCursor(gb,16,TILE_SIZE + MenuFocus * TILE_SIZE,16,0);
						}
						else
						{
							DrawCursor(gb,80,TILE_SIZE + MenuFocus * TILE_SIZE,16,0);
						}
					break;
					case 3://攻击目标选择
						DrawCursor(gb,(infoact[idact[atktarget[currentatk]]][1] - x) * TILE_SIZE,(infoact[idact[atktarget[currentatk]]][2] - y) * TILE_SIZE,0,0);
					break;
				}
			break;
		}
	}
	//绘制光标(光标位置,光标框的大小)
	public void DrawCursor(Graphics g, int x, int y, int w, int h)
	{
		g.drawImage(piccursor[0], x + 0 + (cursortally/30)%2, y + 0 + (cursortally/30)%2, g.LEFT|g.TOP);
		g.drawImage(piccursor[1], x + 10 - (cursortally/30)%2 + w, y + 0 + (cursortally/30)%2, g.LEFT|g.TOP);
		g.drawImage(piccursor[2], x + 0 + (cursortally/30)%2, y + 10 - (cursortally/30)%2 + h, g.LEFT|g.TOP);
		g.drawImage(piccursor[3], x + 10 - (cursortally/30)%2 + w, y + 10 - (cursortally/30)%2 + h, g.LEFT|g.TOP);
	}

	public void DrawStaticPic(Graphics g)//绘制静态图形
	{
		DrawMap(g);//绘制显示地图
	}

	public void DrawMap(Graphics g)//绘制地图
	{
		for(int j = 0; j <= SCREEN_MAX_HEIGHT; j++)
		{
			for(int i = 0; i <= SCREEN_MAX_WIDTH; i++)
			{
				g.drawImage(picmap[map[y + j][x + i]], i * TILE_SIZE, j * TILE_SIZE, g.TOP|g.LEFT);
			}
		}
	}
	//滚屏
	public void RollScreen(Graphics g, int dir)
	{
		switch(dir)
		{
			case 2:
				gb.drawImage(static_buffer, 0, TILE_SIZE, gb.TOP|gb.LEFT);
				g.drawImage(back_buffer, 0, 0, g.TOP|g.LEFT);
				for(int i = 0; i <= SCREEN_MAX_WIDTH; i++)
				{
					g.drawImage(picmap[map[cursorMY-2][x + i]], i * TILE_SIZE, 0, g.TOP|g.LEFT);
				}
			break;
			case 8:
				gb.drawImage(static_buffer, 0, -TILE_SIZE, gb.TOP|gb.LEFT);
				g.drawImage(back_buffer, 0, 0, g.TOP|g.LEFT);
				for(int i = 0; i <= SCREEN_MAX_WIDTH; i++)
				{
					g.drawImage(picmap[map[cursorMY+2][x + i]], i * TILE_SIZE, TILE_SIZE * SCREEN_MAX_HEIGHT, g.TOP|g.LEFT);
				}
			break;
			case 4:
				gb.drawImage(static_buffer, TILE_SIZE, 0, gb.TOP|gb.LEFT);
				g.drawImage(back_buffer, 0, 0, g.TOP|g.LEFT);
				for(int j = 0; j <= SCREEN_MAX_HEIGHT; j++)
				{
					g.drawImage(picmap[map[y + j][cursorMX-2]], 0, j * TILE_SIZE, g.TOP|g.LEFT);
				}
			break;
			case 6:
				gb.drawImage(static_buffer, -TILE_SIZE, 0, gb.TOP|g.LEFT);
				g.drawImage(back_buffer, 0, 0, g.TOP|g.LEFT);
				for(int j = 0; j <= SCREEN_MAX_HEIGHT; j++)
				{
					g.drawImage(picmap[map[y + j][cursorMX+2]], SCREEN_MAX_WIDTH * TILE_SIZE, j * TILE_SIZE, g.TOP|g.LEFT);
				}
			break;
		}
	}

	//移动能力判断
	public boolean movejudge(int x,int y)
	{
		if(move[y][x] != -1)
		{
			return true;
		}
		return false;
	}

	public void chooseact(int act)
	{
		currentactkind = infoact[act][0];
		currentactMX = infoact[act][1];
		currentactMY = infoact[act][2];
		currentactmovearea = infoact[act][3];
		currentactatkmin = infoact[act][4];
		currentactatkmax = infoact[act][5];

		currentactSX = currentactMX - x;
		currentactSY = currentactMY - y;
		currentactmovex = 0;
		currentactmovey = 0;

	}

	//递规实现可移动范围的设定(地图限定数组,可移动数组(1为可以移动),当前人物位置,当前人物移动能力)
	void fmove(int mx,int my,int n)
	{
		if(mx < 0||my < 0||mx > SCREEN_WIDTH||my > SCREEN_HEIGHT)
		{
			return;
		}
		n = n - mappos[map[my][mx]][currentactkind];
		for(int k = 0; k < actnum; k++)//对于有敌人的位置是不可移动的
		{
			if(idact[k] == currentact)
			{
				continue;
			}
			if(infoact[idact[k]][2] == my&&infoact[idact[k]][1] == mx)
			{
				n = n - 100;
				break;
			}
		}
		if(n < 0)
		{
//			try{Thread.sleep(20);
//			}
//			catch(Exception e)
//			{}
			return;
		}
		else
		{
			if(move[my][mx] == -1)
			{
				move[my][mx] = (byte)n;
			}
			else if(move[my][mx] < n)
			{
				move[my][mx] = (byte)n;
			}

			if(mx+1 <= MAP_MAX_HEIGHT)
			{
				fmove(mx+1,my,n);
			}
			if(mx-1 >= 0)
			{
				fmove(mx-1,my,n);
			}
			if(my+1 <= MAP_MAX_WIDTH)
			{
				fmove(mx,my+1,n);
			}
			if(my-1 >= 0)
			{
				fmove(mx,my-1,n);
			}
		}

	}
	//寻路
	void smove(int x,int y)//寻路算法
	{
		if(y+1 <= MAP_MAX_HEIGHT)
		{
			if(move[y+1][x] == move[y][x] + mappos[map[y][x]][currentactkind])
			{
				smove(x,y+1);
			}
		}
		if(y-1 >=0)
		{
			if(move[y-1][x] == move[y][x] + mappos[map[y][x]][currentactkind])
			{
				smove(x,y-1);
			}
		}
		if(x+1 <= MAP_MAX_WIDTH)
		{
			if(move[y][x+1] == move[y][x] + mappos[map[y][x]][currentactkind])
			{
				smove(x+1,y);
			}
		}
		if(x-1 >= 0)
		{
			if(move[y][x-1] == move[y][x] + mappos[map[y][x]][currentactkind])
			{
				smove(x-1,y);
			}
		}
		move[y][x] = (byte)(move[y][x] + 10);
	}
	//初始化人物信息
	public void initializeinfo()
	{
		for(int ih = 0; ih < rednum; ih++)
		{
			for(int iw = 5; iw < 12; iw++)
			{

			}
		}
	}

	public void keyPressed(int keyCode)
	{
		if(!hasPressed)
		{
			switch(keyCode)
			{
					case UP:
						keyAction(2);
						hasPressed = true;
					break;
					case DOWN:
						keyAction(8);
						hasPressed = true;
					break;
					case LEFT:
						keyAction(4);
						hasPressed = true;
					break;
					case RIGHT:
						keyAction(6);
						hasPressed = true;
					break;
					case OK:
						keyAction(5);
						hasPressed = true;
					break;
					case LCOMMAND:
						keyAction(5);
						hasPressed = true;
					break;
					case RCOMMAND:
						keyAction(3);
						hasPressed = true;
					break;
			}
		}
	}

	public void keyReleased(int keyCode)
	{
		hasPressed = false;
		hangfire = 0;
		if(targetstate == 0)
		{
			if(cursorSX == currentactSX&&cursorSY == currentactSY)
			{
				currentactstate = 1;
			}
			else
			{
				currentactstate = 0;
			}
		}
	}

	public void keyAction(int kC)
  	{
	   	kCode = kC;
	   	switch(STATE)
	   	{
		   	case 0:
		   	break;
		   	case 1:
		   		switch(kC)
		   		{
		   			case 2:
		   			case 4:
		   				MenuFocus--;
		   				if(MenuFocus<0)
		   				{
		   					MenuFocus = 4;
		   				}
		   			break;
		   			case 8:
		   			case 6:
		   				MenuFocus++;
		   				if(MenuFocus>4)
		   				{
		   					MenuFocus = 0;
		   				}
		   			break;
		   			case 5:
		   				switch(MenuFocus)
		   				{
		   					case 0://开始新游戏
		   						STATE = 2;
		   						newGame();
		   					break;
		   					case 1:
		   					break;
		   					case 2:
		   					break;
		   					case 3:
		   					break;
		   					case 4:
		   						myMid.exit();
		   					break;
		   				}
		   			break;
		   		}
		   	break;
		   	case 2:

			   	switch(CurrentScene)
			   	{
			   		case 1://光标移动状态
			   			hangfire = 100;
			   			switch(kC)
			   			{
			   				case 5://按中键

⌨️ 快捷键说明

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