📄 foxrole.java
字号:
import com.nttdocomo.ui.*;
import com.nttdocomo.ui.AudioPresenter;
public class FoxRole {
static int reduceLife = 4; //当前费血量
int x = 0, y = 0; //狐狸的XY轴
int direct = 0; //狐狸运动的方向0是左,1是右
int life = 12; //狐狸的总生命值
static int number = 0; //玩家下降的层数
Image imgFox;
Image imgSweat;
public boolean keyTurn = false; //连续按键开关
public boolean bMove=true;
FoxCanvas canvas;
int speed;
private int fallIndext = 0;
public static AudioPresenter[] sndPlayers = new AudioPresenter[5];
MediaSound[] msList = new MediaSound[5];
public static int iSoundKind = 0;
public FoxRole(FoxCanvas canvas) {
this.canvas = canvas;
speed = canvas.boardSpeed;
imgFox = canvas.createImage("frost");
imgSweat = canvas.createImage("sweat");
for (int i = 0; i < 5; i++) {
try {
sndPlayers[i] = AudioPresenter.getAudioPresenter();
msList[i] = MediaManager.getSound("resource:///mid/" + i + ".mid");
msList[i].use();
sndPlayers[i].setSound(msList[i]);
}
catch (Exception e) {
e.printStackTrace();
}
}
switch (canvas.levelIndex) {
case 0:
offset = 0;
break;
case 1:
offset = 1;
break;
case 2:
offset = 3;
break;
}
}
private int offset = 0;
private void initPlayer(int i) {
try {
sndPlayers[i] = AudioPresenter.getAudioPresenter();
msList[i] = MediaManager.getSound("resource:///mid/" + i + ".mid");
msList[i].use();
sndPlayers[i].setSound(msList[i]);
}
catch (Exception e) {
e.printStackTrace();
}
}
//销毁音乐资源
private void disposeSound(int index) {
try {
if (sndPlayers[index] != null) {
sndPlayers[index].stop();
sndPlayers[index] = null;
}
if (msList[index] != null) {
msList[index].dispose();
msList[index] = null;
}
}
catch (ArrayIndexOutOfBoundsException aie) {
}
}
int sweatImageIndex = 0;
//绘制人物
boolean isJump = false; //狐狸是否处于跳的状态
int motion[] = {
4, 5, 0, 1, 5, 3, 6, 8, 7, 9};
int jump[] = {7, 7};
int imagesIndex = 9; //狐狸一系列动作的索引
int jumpIndex; //跳跃动画的索引
boolean isShow = true;
boolean isFallDown=false;
public void drawFox(Graphics g, int[][] point) { //绘制狐狸方法
if (!hit(point)) { //狐狸没有和板子相撞
isShow = true;
currenHitType = -1; // 没有碰撞之前,把碰撞的板子的类型初始化为什么板子也不是
y += 3; //狐狸下降的速度
if (hit(point)) { //狐狸下降之后,如果相撞了
if (y > 20) { //碰到下面的板子
isFallDown = false;
hitEffect(currenHitType);
y -= speed; //和某个板子相撞之后,站在板子上,和板子一起上升
}
else { //撞到上面的锯齿,费血
if (canvas.bSound) {
playmusic(1);
}
isShow = isShow ? false : true;
life -= reduceLife;
isFallDown = true;
y += 8;
}
}
else {
isFallDown = true;
}
}
else { //狐狸和板子相撞
if (y > 20) { //站在板子上
isFallDown = false;
hitEffect(currenHitType);
y -= speed; //和某个板子相撞之后,站在板子上,和板子一起上升
}
else { //撞到上面的锯齿,费血
if (canvas.bSound) {
playmusic(1);
}
isShow = isShow ? false : true;
life -= reduceLife;
isFallDown = true;
y += 8;
}
}
if (keyTurn) { //如果一直按着键
if (direct == 0) { //向左
x = x - 5 > 2 ? x - 5 : 2;
}
if (direct == 1) { //向右
x = x + 5 < 211 ? x + 5 : 211;
}
}
else {
imagesIndex = 9; //不安任何按键的时候,狐狸是直立的动作
}
if (isShow) {
if (keyTurn) {
if (direct == 1) { //画向右走的动画
drawClip(g, imgFox, x, y + 2,
24 * (isJump ? jump[jumpIndex] :
motion[imagesIndex]), 0, 24, 26);
if (imagesIndex != 9 && currenHitType == 5) {
drawClip(g, imgSweat, x - 3, y, 12 * sweatImageIndex,
0, 12, 9);
}
}
else { //向左走的动画
drawClipTrun(g, x, y + 2, imgFox,
24 * (isJump ? (jump[jumpIndex]) :
(motion[imagesIndex])), 0, 24, 26);
if (imagesIndex != 9 && currenHitType == 6) {
drawClipTrun(g, x + 8, y, imgSweat, 12 * (sweatImageIndex), 0, 12,
9);
}
}
}
else {
if (isFallDown) {
switch (fallIndext) {
case 1:
case 0:
drawClip(g, imgFox, x, y + 2, 264, 0, 24, 26);
break;
case 3:
case 2:
drawClip(g, imgFox, x, y + 2, 289, 0, 24, 26);
break;
}
fallIndext = fallIndext >= 3 ? 0 : fallIndext + 1;
}
else {
drawClip(g, imgFox, x, y + 2, 24 * 9, 0, 24, 26);
}
}
}
if (keyTurn) {
imagesIndex = ++imagesIndex >= 4 ? 0 : imagesIndex + 1;
}
sweatImageIndex = ++sweatImageIndex == 3 ? 0 : sweatImageIndex + 1;
if (isJump) {
jumpIndex++;
if (jumpIndex == jump.length) {
jumpIndex = 0;
isJump = false;
}
}
if ( y > 210) {
canvas.gameState = canvas.GAME_OVER_DIE;
}
}
public void playmusic(int k) {
if (canvas.bSound) {
if (iSoundKind != k) {
try{
sndPlayers[iSoundKind].stop();
}catch(Exception e){};
}
if (msList[k] == null || sndPlayers[k] == null) {
initPlayer(k);
}
sndPlayers[k].play();
iSoundKind = k;
}
}
//判断1和2物体与没有相碰
int currenHitType; //
int currentCanRunMotionIndex;
boolean hit(int[][] point) { //相撞函数
int x1 = keyTurn ? x + 16 : x + 12;
int x2 = keyTurn ? x + 0 : x + 6;
int x3 = keyTurn ? x + 22 : x + 18;
int x4 = keyTurn ? x + 0 : x + 4;
for (int i = 0; i < point.length; i++) {
if (direct == 1) { //狐狸向右走是
if (x1 >= point[i][0] && x2 <= point[i][0] + 46 &&
y >= point[i][1] - 24 && y <= point[i][1] - 16) {
currentCanRunMotionIndex = i;
currenHitType = point[i][2];
if (currenHitType == 2) {
isJump = true;
}
return true;
}
}
else { //狐狸向左走是
if (x3 >= point[i][0] && x4 <= point[i][0] + 46 &&
y >= point[i][1] - 24 && y <= point[i][1] - 16) {
currentCanRunMotionIndex = i;
currenHitType = point[i][2];
if (currenHitType == 2) {
isJump = true;
}
return true;
}
}
}
return false;
}
public void hitEffect(int type) { //1.普通木板;2.弹簧;3草板;4钉板;5左滚板;6右滚板
switch (type) {
case 1: //普通木板
if (canvas.boards[currentCanRunMotionIndex][4] == 0) { //如果没有被踩过的话
if (canvas.bSound) {
playmusic(0);
}
if (life < 12) {
life += 1; //加血
}
canvas.boards[currentCanRunMotionIndex][4] = 1; //状态设为被踩过的
}
y = canvas.boards[currentCanRunMotionIndex][1] - 22 + offset;
break;
case 2: //弹
if (canvas.bSound) {
playmusic(3);
}
if (life < 12 &&canvas.boards[currentCanRunMotionIndex][4] == 0) {
life += 1;
}
if (y - 20 > 16) {
y -= 13;
}
else {
y += 16;
life -= reduceLife;
}
canvas.boards[currentCanRunMotionIndex][4] = 1;
break;
case 3: //草板
canvas.isStart = true;
if (canvas.boards[currentCanRunMotionIndex][4] == 0) {
y = canvas.boards[currentCanRunMotionIndex][1] - 23 + offset;
if (canvas.bSound) {
playmusic(2);
}
if (life < 12) {
life += 1;
}
canvas.boards[currentCanRunMotionIndex][4] = 1;
}
if (canvas.boards[currentCanRunMotionIndex][3] == 3) {
canvas.isStart = false;
canvas.timer = 0;
y += 4;
}
break;
case 4: //钉板
isShow = isShow ? false : true;
y = canvas.boards[currentCanRunMotionIndex][1] - 22 + offset;
if (canvas.boards[currentCanRunMotionIndex][4] == 0) {
if (canvas.bSound) {
playmusic(1);
}
if (life > 0) {
life -= reduceLife - 1;
}
canvas.boards[currentCanRunMotionIndex][4] = 1;
}
break;
case 5: //滚板左
if (canvas.boards[currentCanRunMotionIndex][4] == 0) {
if (canvas.bSound) {
playmusic(4);
}
if (life < 12) {
life += 1;
}
canvas.boards[currentCanRunMotionIndex][4] = 1;
}
y = canvas.boards[currentCanRunMotionIndex][1] - 22 + offset;
x = x > 4 ? x - 2 : x;
break;
case 6: //滚板右
if (canvas.boards[currentCanRunMotionIndex][4] == 0) {
if (canvas.bSound) {
playmusic(4);
}
if (life < 12) {
life += 1;
}
canvas.boards[currentCanRunMotionIndex][4] = 1;
}
y = canvas.boards[currentCanRunMotionIndex][1] - 22 + offset;
x = x < 240 - 8 ? x + 2 : x;
break;
}
}
private final void drawClip(Graphics g, Image img, int x, int y, int clipX,
int clipY, int clipW, int clipH) {
g.setClip(x, y, clipW, clipH);
g.drawImage(img, x - clipX, y - clipY);
g.setClip(0, 0, 240, 260);
}
private final void drawClipTrun(Graphics g, int x, int y,
Image image, int clipX, int clipY,
int clipWidth,
int clipHeight) {
for (int i = 0; i < clipWidth; i++) {
g.setClip(x + i, y, 1, clipHeight);
g.drawImage(image, x + i - clipX - clipWidth + i + 1, y - clipY);
}
g.setClip(0, 0, 240, 260);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -