📄 westgamecanvas.java~52~
字号:
*/
private void drawLayer(Graphics g, int id, int xDraw, int yDraw)
{
drawId(g, id, xDraw, yDraw);
drawId(g, id, xDraw + screenWidth, yDraw);
if (yFar != 0)
{
mapId++;
drawId(g, id, xDraw, yDraw - screenHeight);
drawId(g, id, xDraw + screenWidth, yDraw - screenHeight);
mapId--;
}
}
/**
* 绘制一张图片---------------------------1
*
* @param g
* @param id
* 0:远景 1:中景 2:近景
* @param xImg
* 绘图的x坐标
* @param yImg
* 绘图的y坐标
*
*/
private void drawId(Graphics g, int id, int xImg, int yImg)
{
if (mapId > 0)
{
// switch(gameLevel)
// {
// case 1:
// break;
// case 2:
// switch (id)
// {
// case 0:
// g.setColor(65, 144, 218);
// g.fillRect(0, yImg, screenWidth, screenHeight);
// return;
// case 1:
// return;
// }
// break;
// case 3:
// break;
// case 4:
// if(id==0)
// {
// g.setColor(86, 163, 226);
// g.fillRect(0, yImg, screenWidth, screenHeight);
// }
// break;
// }
// }
// }
if (( gameLevel == 1 || gameLevel == 4 ) && id == 0)
{
g.setColor(86, 163, 226);
g.fillRect(0, yImg, screenWidth, screenHeight);
return;
}
if (gameLevel == 2)
{
switch (id)
{
case 0:
g.setColor(65, 144, 218);
g.fillRect(0, yImg, screenWidth, screenHeight);
return;
case 1:
return;
}
}
if (gameLevel == 3)
g.drawImage(GameDataSimple.imgBack[id], xImg, yImg, 0);
else
g.drawImage(GameDataSimple.imgBack[id + 3], xImg, yImg, 0);
}
else
{
g.drawImage(GameDataSimple.imgBack[id], xImg, yImg, 0);
}
}
//----------------------------------------------------------------------back和map>
//-----------------------------------------------------------------------<surface
private void drawSurface(Graphics g)
{
g
.setClip(0, 0, WestGameCanvas.screenWidth,
WestGameCanvas.screenHeight);
//头像
g.drawImage(GameDataSimple.imgSurface[0], 3, 3, 0);
//画血
g.setColor(0xff0000);
int lif_w = 72 * WestGameCanvas.hp / 100;
g.fillRect(29, 8, lif_w, 3);
g.drawImage(GameDataSimple.imgSurface[1], 25, 7, 0);//画血槽
//分数
drawNum(score, 4, 110, 0, GameDataSimple.imgSurface[3], 15, 15, g);
//命数
drawNum(life, 1, 32, 14, GameDataSimple.imgSurface[2], 7, 7, g);
}
private void drawNum(int num, int nBit, int xStart, int yStart, Image img,
int srcFrameWidth, int srcFrameHeight, Graphics g)
{
//要绘制的字符串
String str = Integer.toString(num);
//补0的位数
int addZeroBite = nBit - str.length();
for (int j = 0; j < nBit; j++)
{
int useStartX = xStart;
//要绘制的字符
char currentDrawChar = j < addZeroBite ? '0' : str.charAt(j
- addZeroBite);
for (int i = 48; i < 58; i++)
{
if (currentDrawChar == i)
{
g.setClip(xStart, yStart, srcFrameWidth, srcFrameHeight);
g.drawImage(img, useStartX, yStart, 0);
break;
}
useStartX -= srcFrameWidth;
}
xStart += srcFrameWidth;
}
}
//-----------------------------------------------------------------------surface>
//-----------------------------------------------------------------------<sound
/* ************************************************* */
//TODO 播放声音方法******************************** */
/* *********************************************** */
/**
* 打开声音
*
* @param idx
* 声音文件序号
* @return 声音对象
*/
private Player soundOpen(int idx)
{
//TODO 打开声音
try
{
return Manager.createPlayer(getClass().getResourceAsStream(
"/" + idx), "audio/midi");
}
catch (IOException e)
{
//Tools.txtOut("sound IOException");
}
catch (MediaException e)
{
//Tools.txtOut("sound MediaException");
}
return null;
}
/** 播放声音 */
private void soundPlay()
{
int idx;
switch (0xf000000 & markNum)
{
case 0:
case 0x8000000:
idx = SOUND_MENU_IDX;
break;
case 0x2000000:
switch (gameState)
{
case 0://正常游戏
idx = SOUND_GAME_IDX;
break;
case 2://胜利
idx = SOUND_WIN_IDX;
break;
case 4://死了一条命
case 8://失败
idx = SOUND_FAIL_IDX;
break;
default:
return;
}
break;
default:
return;
}
if (this.soundIdx != idx)
{
this.soundStop();
this.soundIdx = idx;
}
try
{
this.sounds[idx].realize();
this.sounds[idx].setLoopCount(-1);
VolumeControl soundVolumeControl = (VolumeControl) this.sounds[idx]
.getControl("VolumeControl");
soundVolumeControl.setLevel(60);
this.sounds[idx].start();
this.soundPlaying = true;
}
catch (MediaException e)
{
}
}
/** 停止声音 */
private final void soundStop()
{
try
{
this.soundPlaying = false;
if (this.sounds[this.soundIdx] != null)
this.sounds[this.soundIdx].stop();
}
catch (MediaException e)
{
}
}
/**
* 关闭声音
*
* @param idx
* 声音数组的索引,退出游戏时为声音数组最大索引号,关卡切换时为0
*/
private final void soundClose(int idx)
{
for (; idx >= 0; idx--)
{
if (this.sounds[idx] == null)
continue;
this.sounds[idx].close();
this.sounds[idx] = null;
}
}
/* ************************************************* */
//TODO 声音播放临时变量***************************** */
/* *********************************************** */
/** 声音索引 */
private static final byte SOUND_GAME_IDX = 0;
/** 声音索引 */
private static final byte SOUND_FAIL_IDX = 1;
/** 声音索引 */
private static final byte SOUND_WIN_IDX = 2;
/** 声音索引 */
private static final byte SOUND_MENU_IDX = 3;
/** 声音文件号 */
private static final byte SOUND_FAIL_NUM = 4;
/** 声音文件号 */
private static final byte SOUND_WIN_NUM = 5;
/** 声音文件号 */
private static final byte SOUND_MENU_NUM = 6;
/** 播放对象数组 */
private Player sounds[] = new Player[4];
/** 声音播放标志 */
private boolean soundPlaying = false;
/** 声音索引 */
private int soundIdx = SOUND_MENU_IDX;
/** 系统切换出去标志 */
private boolean gameIsOut;
//-----------------------------------------------------------------------sound>
//-----------------------------------------------------------------------<rmd
/* ************************************************* */
//TODO 数据库处理方法****************************** */
/* *********************************************** */
/**
* 开启纪录仓库
*
* @param recordStoreName
* @param createIfNecessary
* true: 开启纪录仓库; false: 开启已经存在的纪录仓库
* @return RecordStore 纪录仓库对象
*/
private RecordStore rsOpen(String recordStoreName, boolean createIfNecessary)
{
try
{
return RecordStore.openRecordStore(recordStoreName,
createIfNecessary);
}
catch (RecordStoreFullException e)
{
}
catch (RecordStoreNotFoundException e)
{
}
catch (RecordStoreException e)
{
}
return null;
}
/**
* 关闭数据库
*
* @param rs
* RecordStore 数据库对象
*/
private void rsClose(RecordStore rs)
{
try
{
rs.closeRecordStore();
}
catch (RecordStoreNotOpenException e)
{
}
catch (RecordStoreException e)
{
}
}
/**
* 向数据库中加入记录
*
* @param rs
* RecordStore 数据库对象
* @param data
* byte[] 1维数据数组
*/
private void rsAddRecord(RecordStore rs, byte[] data)
{
try
{
rs.addRecord(data, 0, data.length);
}
catch (RecordStoreNotOpenException e)
{
}
catch (RecordStoreFullException e)
{
}
catch (RecordStoreException e)
{
}
}
/**
* 取得记录
*
* @param rs
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -