📄 tanksprite.java
字号:
/**
* <p>Title: Transpanzer</p>
* <p>Description:
* You cannot remove this copyright and notice.
* You cannot use this file any part without the express permission of the author.
* All Rights Reserved</p>
* @author Jjyo
* @version 1.0.0
*/
import java.io.DataInputStream;
import java.io.DataOutputStream;
import javax.microedition.lcdui.Canvas;
public class TankSprite extends StaticTankSprite {
public Maps map;
private GameManage gm;
// pubilc
public static int dx; //水平速度
public static int dy; //纵向速度
public static int bgdx; //背景X 编移
public static int bgdy; //背景Y 编移
public static int attack; //攻击力
public static int keyAction; //按键
public static int becomeCnt; //车行走时的状态
public static int tankPow; //Tank生命
public static int tanePow; //Tane生命
public static int tankHov; //燃料
public static int kaneGun; //枪
public static int tankMissile; //导弹
public static int tankHball; //追踪弹
public static int tankBolt; //闪光弹
public static int oddFacePow=300;
public int wheelY; //轮子的位置
public int gunType;
private boolean isTurn;
private boolean isUp;
public boolean isDown;
public boolean isWater;
public static boolean isDead;
public static boolean isBoss;
public static boolean isShowMap;
public static boolean isChangeScreen;
public static boolean isBossOver;
public static boolean haveData;
// private
private boolean isJumpOver;
public static boolean HYPER_BEAM;
public static boolean CRUSHER_BEAM;
public static boolean HOVER_BEAM;
public static boolean KEY_BEAM;
public static boolean DIVE_BEAM;
public static boolean WALL1_BEAM;
public static boolean WALL2_BEAM;
private int protectInterval=10; //无敌时间
private int protectCnt; //无敌倒计时
private boolean isProtected; //无敌
private boolean isLadder;
private static int value;
// final
private static final int MOVE_SPEED = 6; //移动的速度
// main sprite's state
public static char spriteState; //精灵状态
public static final char TANK = 0x01; //坦克
public static final char TANE = 0x02; //小人
public static final char KANE = 0x04; //小人迷宫
public static char direction;
public static final char left=0x01;
public static final char right=0x02;
public static final char up=0x04;
public static final char down=0x08;
public static int bulletType;
public static final int BULLET_GUN00=0;
public static final int BULLET_GUN01=1;
public static final int BULLET_GUN02=2;
public static final int BULLET_GUN03=3;
public static final int BULLET_GUN04=4;
public static final int BULLET_GUN05=5;
public static final int BULLET_GUN06=6;
public static final int BULLET_GUN07=7;
public static final int BULLET_GUN08=8;
public static final int BULLET_BOMB=9;
public static final int BULLET_TANE=10;
public static final int BULLET_DEFANUL=11;
public static final int BULLET_HYPER=12;
public static final int BULLET_CRUCHER=13;
private int index;
private static final int frame[]={0,1,2,1,2};
//构造函数,传入参数是游戏管理器和带有属性的地图
public TankSprite(GameManage gm, Maps map) {
System.out.println("TankSprite successful...");
this.gm=gm;
this.map = map;
isTurn=false;
spriteState = TANK;
frameCnt=5;
y=100;
x=80;
leftFrameCnt=1;
rightFrameCnt=2;
tankPow=16;
tanePow=16;
}
/**
* 初始化Tank角色
* @param staticTankSprite
*/
public void init(StaticTankSprite staticTankSprite){
this.x=staticTankSprite.x;
this.y=staticTankSprite.y;
this.leftFrameCnt=staticTankSprite.leftFrameCnt;
this.rightFrameCnt=staticTankSprite.rightFrameCnt;
isHidden=false;
if(staticTankSprite.isLeft)
frameCnt=2;
else
frameCnt=5;
this.wheelRise=staticTankSprite.wheelRise;
}
//重写基类的action
public void action() {
/** --------------------主角的三种不同的状态-----------------------------*/
attack=0;
switch(spriteState){
case TANK:
pow=tankPow;
bulletType=BULLET_DEFANUL;
if(HYPER_BEAM){
bulletType=BULLET_HYPER;
if(CRUSHER_BEAM){
bulletType=BULLET_CRUCHER;
}
}
dy += 2; //重力的加速度
if(isWater&&dy>0){
dy=1;
}
//纵向的地图碰撞检测
checkTileCollisionVertical();
//按下OK键 发射子弹
if ((keyAction & KeyMask.MASK_KEY_FIRE_FLAG) != 0) {
fire(); //发射子弹
keyAction &= ~KeyMask.MASK_KEY_FIRE_FLAG;
}
//按下左键,dx-
if ((keyAction & KeyMask.MASK_KEY_LEFT) != 0) {
if(isLeft){
frameCnt=2;
if(isUp)
frameCnt=0;
if (--dx < -MOVE_SPEED) {//横向加速度
dx = -MOVE_SPEED;
}
if(isWater){
dx = -MOVE_SPEED/3;
}
}else{
isTurn=true;
}
if (checkTileCollisionHorizon()) {
dx = 0;
}
isLeft = true;
if(!isUp)
wheelRise=0;
//按下右键,向右移动,dx+
} else if ((keyAction & KeyMask.MASK_KEY_RIGHT) != 0) {
if(!isLeft){
frameCnt=5;
if(isUp)
frameCnt=7;
if (++dx > MOVE_SPEED) {//横向加速度
dx = MOVE_SPEED;
}
if(isWater){
dx = MOVE_SPEED/3;
}
}else{
isTurn=true;
}
if (checkTileCollisionHorizon()) { //检测与精灵的水平碰撞判断
dx = 0;
}
isLeft = false;
if(!isUp)
wheelRise=0;
}else if((keyAction & KeyMask.MASK_KEY_UP)!=0){ //向上键判断
isUp=true;
if(isLeft){
if(frameCnt>0){
--frameCnt;
wheelRise+=1;
}
}else{
if(frameCnt<7){
frameCnt++;
wheelRise+=1;
}
}
if (checkTileCollisionHorizon()) { //检测与精灵的水平碰撞判断
dx = 0;
}
}else { //其余状态判断
if (dx > 0) { //横向加速度为正
if (checkTileCollisionHorizon()) {
dx = 0;
} else {
--dx;
}
} else if (dx < 0) { //横向加速度为负
if (checkTileCollisionHorizon()) {
dx = 0;
} else {
++dx;
}
}
}
//掉头状态
if(isTurn&&!isUp){
if(isLeft){
frameCnt--;
if(frameCnt<2){
isTurn=false;
frameCnt=2;
}
}else{
frameCnt++;
if(frameCnt>=5){
isTurn=false;
frameCnt=5;
}
}
}
//向上跳时轮子的状态
if(dy==0){
wheelY=becomeCnt;
if(dx==0)
becomeCnt=0;
}else if(dy>0){
if(wheelY<0)
wheelY++;
}else{
if(wheelY>=-2)
wheelY--;
}
//车轮的状态 向左
if(dx<0&&isLeft){
leftFrameCnt--;
if(leftFrameCnt<0)
leftFrameCnt=3;
rightFrameCnt--;
if(rightFrameCnt<0)
rightFrameCnt=3;
if(++timeCnt%3==0){
becomeCnt=1;
}else{
becomeCnt=0;
}
}
//车轮走动的状态 向右
if(dx>0&&!isLeft){
leftFrameCnt++;
if(leftFrameCnt>3)
leftFrameCnt=0;
rightFrameCnt++;
if(rightFrameCnt>3)
rightFrameCnt=0;
if(++timeCnt%3==0){
becomeCnt=1;
}else{
becomeCnt=0;
}
}
break;
//TODO 2 TANE 人物装态
/**-----------------------------------------------------------------*/
case TANE:
bulletType=BULLET_TANE;
pow=tanePow;
dy += 2; //纵向的加速度
if(isWater&&dy>0){
dy=1;
}
if(value==0x08){
dy=0;
}else{
isLadder=false;
}
checkTileCollisionVertical(); //纵向的地图碰撞检测
//按下OK键 发射子弹
if ((keyAction & KeyMask.MASK_KEY_FIRE_FLAG) != 0) {
if(!isLadder)
fire(); //发射子弹
keyAction &= ~KeyMask.MASK_KEY_FIRE_FLAG;
}
//按下左键,dx-
if ((keyAction & KeyMask.MASK_KEY_LEFT) != 0) { //左键判断,向左移动
if(isDown||isWater){
dx=-MOVE_SPEED/3;
}
else{ //横向加速度
dx = -(MOVE_SPEED/2)-1;
}
if (checkTileCollisionHorizon()||isLadder) {
dx = 0;
}
isLeft = true;
//按下右键,dx+
} else if ((keyAction & KeyMask.MASK_KEY_RIGHT) != 0) { //右键判断,向右移动
if(isDown||isWater){
dx=MOVE_SPEED/3;
}
else{ //横向加速度
dx = MOVE_SPEED/2+1;
}
if (checkTileCollisionHorizon()||isLadder) { //检测与精灵的水平碰撞判断
dx = 0;
}
isLeft = false;
}else { //其余状态判断
if (dx > 0) { //横向加速度为正
if (checkTileCollisionHorizon()) {
dx = 0;
} else {
--dx;
}
} else if (dx < 0) { //横向加速度为负
if (checkTileCollisionHorizon()) {
dx = 0;
} else {
++dx;
}
}
}
if((keyAction & KeyMask.MASK_KEY_UP)!=0){ //向上键判断
if(isLadder){
dy=-(MOVE_SPEED-4);
}else{
if(isWater)
dy=-MOVE_SPEED/3;
isDown=false;
}
checkTileCollisionVertical();
}else if((keyAction & KeyMask.MASK_KEY_DOWN)!=0){ //向下判断
if(isLadder){
dy=MOVE_SPEED-4;
}else{
if(isWater){
dy=MOVE_SPEED/2;
}else{
isDown=true;
}
}
if(checkTileCollisionVertical()){
if(isLadder){
isLadder=false;
}
}
}
//检测角色下落的落差,高度大于常值可-pow
if(dy-11>0){
isProtected=true;
loseLife(dy-11);
}
//不动状态
if(dx==0&&dy==0){
if(isLeft){
frameCnt=0;
if(isDown)
frameCnt=12;
}else{
frameCnt=4;
if(isDown)
frameCnt=14;
}
if(isLadder){
frameCnt=16;
}
//向左走状态
}else if(dx<0&&dy==0){
if(isDown){
nextFrame(1,12);
}else{
nextFrame(3,1);
}
//向右走状态
}else if(dx>0&&dy==0){
if(isDown){
nextFrame(1,14);
}else{
nextFrame(3,5);
}
//在水中的动作
}else if(isWater){
if(isLeft){
if(dy==0){
frameCnt=8;
if(isDown)
nextFrame(1,12);
else
nextFrame(3,1);
}else{
nextFrame(1,8);
}
}else{
if(dy==0){
frameCnt=10;
if(isDown)
nextFrame(1,14);
else
nextFrame(3,5);
}else{
nextFrame(1,10);
}
}
}
//在梯子上的动作
if(isLadder){
if(dy==0){
frameCnt=16;
}else{
nextFrame(1,16);
}
}
break;
//TODO 3 KANE
/**------------------------------------------------------*/
case KANE:
pow=tanePow;
bulletType=kaneGun;
//按下OK键 发射子弹
if ((keyAction & KeyMask.MASK_KEY_FIRE_FLAG) != 0) {
fire(); //发射子弹
keyAction &= ~KeyMask.MASK_KEY_FIRE_FLAG;
}
//按下0键 放炸弹
if((keyAction&KeyMask.MASK_KEY_JUMP_FLAG)!=0){
bulletType=BULLET_BOMB;
fire();
keyAction &= ~KeyMask.MASK_KEY_JUMP_FLAG;
}
//按下左键,dx-
if ((keyAction & KeyMask.MASK_KEY_LEFT) != 0) { //左键判断,向左移动
direction=left;
dx = -MOVE_SPEED/2-1;
if (checkTileCollisionHorizon()) {
dx = 0;
}
//按下右键,dx+
} else if ((keyAction & KeyMask.MASK_KEY_RIGHT) != 0) { //右键判断,向右移动
direction=right; //横向加速度
dx = MOVE_SPEED/2+1;
if (checkTileCollisionHorizon()) { //检测与精灵的水平碰撞判断
dx = 0;
}
}else { //其余状态判断
if (dx > 0) { //横向加速度为正
if (checkTileCollisionHorizon()) {
dx = 0;
} else {
--dx;
}
} else if (dx < 0) { //横向加速度为负
if (checkTileCollisionHorizon()) {
dx = 0;
} else {
++dx;
}
}
}
//向上键判断
if((keyAction & KeyMask.MASK_KEY_UP)!=0){
direction=up;
dy = -MOVE_SPEED/2-1;
if(checkTileCollisionVertical()){ //纵向的地图碰撞检测
dy=0;
}
//按下下键
}else if((keyAction & KeyMask.MASK_KEY_DOWN)!=0){
direction=down;
dy = MOVE_SPEED/2+1;
if(checkTileCollisionVertical()){ //纵向的地图碰撞检测
dy=0;
}
}else {
if(dy>0){
if (checkTileCollisionVertical()) {
dy = 0;
} else {
--dy;
}
}else if(dy<0){
if (checkTileCollisionVertical()) {
dy = 0;
} else {
++dy;
}
}
}
//方向
switch(direction){
case left:
if(dx==0)
frameCnt=1;
if(dx<0){
nextFrame(3,0);
}
break;
case right:
if(dx==0)
frameCnt=4;
if(dx>0){
nextFrame(3,3);
}
break;
case up:
if(dy==0)
frameCnt=7;
if(dy<0){
nextFrame(3,6);
}
break;
case down:
if(dy==0)
frameCnt=10;
if(dy>0){
nextFrame(3,9);
}
break;
}
break;
}
/** -------------------------------------------------------------*/
//检测所在地图块的属性 触发事件
checkTileValue();
/** 与敌人碰撞后,无敌1秒*/
if(isProtected){
if(protectCnt++==protectInterval){
protectCnt=0;
isProtected=false;
}
if(protectCnt%2==0)
isHidden=true;
}else{
//与敌人和敌人的子弹碰撞检测
if(checkEnemyCollision()||checkBulletDamage()){
isHidden=true;
isProtected=true;
loseLife(1);
if(checkTileCollisionHorizon()){
dx=0;
}else{
dx=-2;
}
}
}
if(tankPow<0||tanePow<0){
isHidden=true;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -