📄 tanksprite.java
字号:
/**
* Copyright_2006, Liao Xuefeng
* Created on 2006-2-7
*/
package com.javaeedev.j2megame.tank;
import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Image;
import javax.microedition.lcdui.game.Sprite;
public abstract class TankSprite extends Sprite {
protected final GameMainCanvas canvas;
// position in map:
protected int x;
protected int y;
// target position in map:
protected int target_x;
protected int target_y;
// direction defined as Canvas.UP, Canvas.DOWN, Canvas.LEFT, Canvas.RIGHT:
protected int direction;
private int lock = 0; // when lock==0, can fire
public TankSprite(GameMainCanvas canvas, int x, int y, Image image, int direction) {
super(image, 16, 16);
this.canvas = canvas;
this.target_x = this.x = x;
this.target_y = this.y = y;
updateKeyPosition();
switch(direction) {
case Canvas.DOWN:
turnDown();
break;
case Canvas.LEFT:
turnLeft();
break;
case Canvas.RIGHT:
turnRight();
break;
default:
turnUp();
}
}
public Bullet fire() {
if(lock>0) // cannot fire!
return null;
lock = 2;
int fire_x = x*GameMap.TILE_WIDTH;
int fire_y = y*GameMap.TILE_WIDTH;
switch(direction) {
case Canvas.DOWN:
fire_y += GameMap.TILE_WIDTH*2;
case Canvas.UP:
fire_x += GameMap.TILE_WIDTH;
break;
case Canvas.RIGHT:
fire_x += GameMap.TILE_WIDTH*2;
case Canvas.LEFT:
fire_y += GameMap.TILE_WIDTH;
break;
}
return new Bullet(direction, fire_x, fire_y);
}
/**
* stay in the same position.
*/
public void stay() {
if(lock>0)
lock--;
x = target_x;
y = target_y;
updateKeyPosition();
}
public boolean moveUp() {
turnUp();
if(canvas.getGameMap().canMoveUp(x, y, 2, 2)) {
target_y = y-1;
updateKeyPosition();
return true;
}
return false;
}
public boolean moveDown() {
turnDown();
if(canvas.getGameMap().canMoveDown(x, y, 2, 2)) {
target_y = y+1;
updateKeyPosition();
return true;
}
return false;
}
public boolean moveLeft() {
turnLeft();
if(canvas.getGameMap().canMoveLeft(x, y, 2, 2)) {
target_x = x-1;
updateKeyPosition();
return true;
}
return false;
}
public boolean moveRight() {
turnRight();
if(canvas.getGameMap().canMoveRight(x, y, 2, 2)) {
target_x = x+1;
updateKeyPosition();
return true;
}
return false;
}
public void moveStep(int frameId) {
// frameId is from 1 to MAX_STEP_FRAMES:
if(target_x==x && target_y==y)
return; // no need move!
updatePosition(frameId);
}
public int getMapX() {
return x;
}
public int getMapY() {
return y;
}
private void updateKeyPosition() {
setPosition(x*GameMap.TILE_WIDTH, y*GameMap.TILE_WIDTH);
}
private void updatePosition(int frameId) {
int curr_x = x*GameMap.TILE_WIDTH;
int curr_y = y*GameMap.TILE_WIDTH;
int distance = GameMainCanvas.STEP_WIDTH * frameId;
switch(direction) {
case Canvas.UP:
curr_y -= distance;
break;
case Canvas.DOWN:
curr_y += distance;
break;
case Canvas.LEFT:
curr_x -= distance;
break;
case Canvas.RIGHT:
curr_x += distance;
}
setPosition(curr_x, curr_y);
}
public void turnUp() {
if(direction!=Canvas.UP) {
direction = Canvas.UP;
setFrame(0);
}
}
public void turnDown() {
if(direction!=Canvas.DOWN) {
direction = Canvas.DOWN;
setFrame(1);
}
}
public void turnLeft() {
if(direction!=Canvas.LEFT) {
direction = Canvas.LEFT;
setFrame(2);
}
}
public void turnRight() {
if(direction!=Canvas.RIGHT) {
direction = Canvas.RIGHT;
setFrame(3);
}
}
public boolean moveTo(int direction) {
switch(direction) {
case Canvas.UP:
return moveUp();
case Canvas.DOWN:
return moveDown();
case Canvas.LEFT:
return moveLeft();
case Canvas.RIGHT:
return moveRight();
}
// for other, such as 0, just return false:
return false;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -