📄 tankcanvas.java
字号:
{
while ((c = is.read()) != - 1)
{
itemNum = c;
item = is.read();
for (i = 0; i < itemNum; i++)
{
tempInfo[j] = item;
tempInfo[52+j] = item;
tempInfo[j + 1] = item;
tempInfo[52+j + 1] = item;
j += 2;
if ((j % (52)) == 0)
j += 52;
}
}
is.close();
}
else
{
System.out.println("Could not find the map for level " + lev);
return false;
}
}
catch (java.io.IOException ex)
{
return false;
}
i = 0;
j = 0;
index = dataOffsetY * (52) + dataOffsetX;
for (i = 0; i < (52) * (52); i++)
{
if (i >= index && i < (index + mapWidth / tileW))
mapInfo[j++] = tempInfo[i];
else if (i == (index + mapWidth / tileW))
{
index += (52);
if (i == index)
mapInfo[j++] = tempInfo[i];
}
}
//add by johnson to store the former map
i = 0;
j = 0;
index = dataOffsetY * (52) + dataOffsetX;
for (i = 0; i < (52) * (52); i++)
{
if (i >= index && i < (index + mapWidth / tileW))
mapInfoFormer[j++] = tempInfo[i];
else if (i == (index + mapWidth / tileW))
{
index += (52);
if (i == index)
mapInfoFormer[j++] = tempInfo[i];
}
}
return true;
}
protected void paint(Graphics g)
{
switch (gameStatus)
{
case 1:
drawSplash(g);
break;
case 2:
drawAnmi(g);
break;
case 3:
drawMenu(g);
break;
case 4:
drawGame(g);
break;
case 5:
drawResult(g);
break;
case 6:
drawOption(g);
break;
case 7:
drawAbout(g);
break;
case 8:
default:
break;
}
}
private void drawSplash(Graphics g)
{
Image imgSplash = createImage("/splash.png");
if (imgSplash != null)
{
g.drawImage(imgSplash, CanvasWidth / 2, CanvasHeight / 2,
Graphics.VCENTER | Graphics.HCENTER);
}
SplashCnt++;
if (SplashCnt == 10)
{
gameStatus = 2;
gameSubStatus = 1;
SplashCnt = 0;
}
}
private void drawAnmi(Graphics g)
{
switch (gameSubStatus)
{
case 1:
g.setColor(0x000000);
g.fillRect(0, 0, CanvasWidth, CanvasHeight);
imgTankEnemy = createImage("/tank_enemy.png");
fadeColor = 0x0;
gameSubStatus++;
break;
case 2:
g.setColor(fadeColor, fadeColor, fadeColor);
g.setFont(font);
g.drawString("敌人开始进攻了......", CanvasWidth / 2, (CanvasHeight -
imgTankEnemy.getHeight() - fontHeight) / 2-3, g.TOP |
g.HCENTER);
if (fadeColor < 0xfc)
fadeColor += 4;
else
{
gameSubStatus++;
title_y = imgTankEnemy.getHeight() / 2;
title_h = 0;
}
break;
case 3:
g.setColor(0xffffff);
g.fillRect(0, (CanvasHeight + fontHeight) / 2-title_h,
CanvasWidth, title_h * 2);
if (title_h < (imgTankEnemy.getHeight() / 2))
{
title_h += 2;
}
else
{
gameSubStatus++;
title_x = - imgTankEnemy.getWidth();
}
break;
case 4:
g.setColor(0xffffff);
g.fillRect(0, ((CanvasHeight - imgTankEnemy.getHeight() +
fontHeight) / 2), CanvasWidth, imgTankEnemy.getHeight());
g.drawImage(imgTankEnemy, title_x, ((CanvasHeight -
imgTankEnemy.getHeight() + fontHeight) / 2), g.TOP | g.LEFT)
;
if (title_x < 0)
{
title_x += 1;
}
else
{
gameSubStatus++;
imgTankPlayer = createImage("/tank_player.png");
title_x = CanvasWidth + imgTankPlayer.getWidth();
}
break;
case 5:
g.setColor(0xffffff);
g.fillRect(0, ((CanvasHeight - imgTankEnemy.getHeight() +
fontHeight) / 2), CanvasWidth, imgTankPlayer.getHeight());
g.drawImage(imgTankEnemy, 0, ((CanvasHeight -
imgTankEnemy.getHeight() + fontHeight) / 2), g.TOP | g.LEFT)
;
g.drawImage(imgTankPlayer, title_x, ((CanvasHeight -
imgTankPlayer.getHeight() + fontHeight) / 2), g.TOP |
g.LEFT);
if (title_x >= (CanvasWidth - imgTankPlayer.getWidth()))
{
title_x -= 2;
}
else
{
gameSubStatus++;
g.setColor(0x00);
g.fillRect(0, 0, CanvasWidth, ((CanvasHeight -
imgTankPlayer.getHeight() + fontHeight) / 2-1));
}
break;
case 6:
g.setColor(fadeColor, fadeColor, fadeColor);
g.setFont(font);
g.drawString("我们决一死战吧!", CanvasWidth / 2, (CanvasHeight
- imgTankPlayer.getHeight() - fontHeight) / 2-3, g.TOP |
g.HCENTER);
if (fadeColor > 0)
fadeColor -= 4;
else
{
title_h = 0;
gameSubStatus++;
}
break;
case 7:
imgTankEnemy = null;
imgTankPlayer = null;
gameStatus = 3;
gameSubStatus = 1;
default:
break;
}
}
private void drawMenu(Graphics g)
{
switch (gameSubStatus)
{
case 1:
g.setColor(0x0);
g.fillRect(0, 0, CanvasWidth, CanvasHeight);
imgTitle = createImage("/title.png");
gameSubStatus++;
title_y = imgTitle.getHeight() / 2;
title_h = 0;
break;
case 2:
g.setColor(0x0);
g.fillRect(0, 0, CanvasWidth, CanvasHeight);
g.setColor(0xffffff);
g.fillRect(0, (CanvasHeight - imgTitle.getHeight()) / 6+title_y,
(CanvasWidth - imgTitle.getWidth()) / 2, title_h);
g.fillRect((CanvasWidth + imgTitle.getWidth()) / 2,
(CanvasHeight - imgTitle.getHeight()) / 6+title_y,
(CanvasWidth - imgTitle.getWidth()) / 2, title_h);
g.translate((CanvasWidth - imgTitle.getWidth()) / 2,
(CanvasHeight - imgTitle.getHeight()) / 6);
g.setClip(0, title_y, imgTitle.getWidth(), title_h);
g.drawImage(imgTitle, 0, 0, g.TOP | g.LEFT);
if (title_h < imgTitle.getHeight())
{
title_y -= 2;
title_h += 4;
}
else
{
gameSubStatus++;
}
break;
case 3:
g.setColor(0x0);
g.fillRect(0, 0, CanvasWidth, CanvasHeight);
g.setColor(0xffffff);
g.fillRect(0, (CanvasHeight - imgTitle.getHeight()) / 6+title_y,
(CanvasWidth - imgTitle.getWidth()) / 2, title_h);
g.fillRect((CanvasWidth + imgTitle.getWidth()) / 2,
(CanvasHeight - imgTitle.getHeight()) / 6+title_y,
(CanvasWidth - imgTitle.getWidth()) / 2, title_h);
g.drawImage(imgTitle, (CanvasWidth - imgTitle.getWidth()) / 2,
(CanvasHeight - imgTitle.getHeight()) / 6, g.TOP | g.LEFT);
g.setColor(0x00, 0x00, 0x00);
g.fillRect(CanvasWidth / 2-50, (CanvasHeight -
imgTitle.getHeight()) / 3+imgTitle.getHeight(), 100,
fontHeight * 3);
g.setFont(font);
switch (gameSelect)
{
case 1:
g.setColor(0xfe, 0x3f, 0x00);
g.drawString("开始游戏", CanvasWidth / 2, (CanvasHeight -
imgTitle.getHeight()) / 3+imgTitle.getHeight(), g.TOP |
g.HCENTER);
g.setColor(0x00, 0xfe, 0xfe);
g.drawString("选择关卡", CanvasWidth / 2, (CanvasHeight -
imgTitle.getHeight()) / 3+imgTitle.getHeight() +
fontHeight, g.TOP | g.HCENTER);
g.drawString("游戏说明", CanvasWidth / 2, (CanvasHeight -
imgTitle.getHeight()) / 3+imgTitle.getHeight() + 2 *
fontHeight, g.TOP | g.HCENTER);
g.drawString("退出程序", CanvasWidth / 2, (CanvasHeight -
imgTitle.getHeight()) / 3+imgTitle.getHeight() + 3 *
fontHeight, g.TOP | g.HCENTER);
break;
case 2:
g.setColor(0x00, 0xfe, 0xfe);
g.drawString("开始游戏", CanvasWidth / 2, (CanvasHeight -
imgTitle.getHeight()) / 3+imgTitle.getHeight(), g.TOP |
g.HCENTER);
g.setColor(0xfe, 0x3f, 0x00);
g.drawString("选择关卡", CanvasWidth / 2, (CanvasHeight -
imgTitle.getHeight()) / 3+imgTitle.getHeight() +
fontHeight, g.TOP | g.HCENTER);
g.setColor(0x00, 0xfe, 0xfe);
g.drawString("游戏说明", CanvasWidth / 2, (CanvasHeight -
imgTitle.getHeight()) / 3+imgTitle.getHeight() + 2 *
fontHeight, g.TOP | g.HCENTER);
g.drawString("退出程序", CanvasWidth / 2, (CanvasHeight -
imgTitle.getHeight()) / 3+imgTitle.getHeight() + 3 *
fontHeight, g.TOP | g.HCENTER);
break;
case 3:
g.setColor(0x00, 0xfe, 0xfe);
g.drawString("开始游戏", CanvasWidth / 2, (CanvasHeight -
imgTitle.getHeight()) / 3+imgTitle.getHeight(), g.TOP |
g.HCENTER);
g.drawString("选择关卡", CanvasWidth / 2, (CanvasHeight -
imgTitle.getHeight()) / 3+imgTitle.getHeight() +
fontHeight, g.TOP | g.HCENTER);
g.setColor(0xfe, 0x3f, 0x00);
g.drawString("游戏说明", CanvasWidth / 2, (CanvasHeight -
imgTitle.getHeight()) / 3+imgTitle.getHeight() + 2 *
fontHeight, g.TOP | g.HCENTER);
g.setColor(0x00, 0xfe, 0xfe);
g.drawString("退出程序", CanvasWidth / 2, (CanvasHeight -
imgTitle.getHeight()) / 3+imgTitle.getHeight() + 3 *
fontHeight, g.TOP | g.HCENTER);
break;
case 4:
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -