📄 pigworld.java
字号:
import javax.microedition.lcdui.*;
import java.io.IOException;
import java.io.InputStream;
public class PigWorld {
public final static int MAP_W = 10;
public final static int MAP_H = 240;
public static final int TILE_H = 22;
private int scrX, scrY;
protected int viewX, viewY;
private int addX, addY;
private Image mapG;
private SpriteClip mapSprite;
private byte tileMap[][];
private int NpcPos[][];
private boolean bossLive;
private int startTileX, startTileY, endTileX, endTileY;
private int iRand;
private long NPCTime, huoTime;
private FlyPig flyPig;
private ActorPool npcPool;
private ActorPool bulletPool;
public PigWorld(int scrXArg, int scrYArg) {
try {
mapG = Image.createImage("/images/changjing2.png");
} catch (IOException e) {
e.printStackTrace();
}
mapSprite = new SpriteClip();
mapSprite.init(mapG, 9);
for (int i = 0; i < 9; i++) {
mapSprite.setArray(i, 22, 22, i * 22, 0, 0, 0);
}
tileMap = new byte[MAP_H][MAP_W];
readString();
scrX = scrXArg;
scrY = scrYArg;
NpcPos = new int[MAP_H][MAP_W];
for (int m = 0; m < MAP_H; m++) {
for (int n = 0; n < MAP_W; n++) {
NpcPos[m][n] = tileMap[m][n];
}
}
Npcs npcArray[] = new Npcs[20];
for (int i = 0; i < npcArray.length; i++) {
npcArray[i] = new Npcs(this);
}
npcPool = new ActorPool(npcArray);
Bullet bulletArray[] = new Bullet[10];
for (int i = 0; i < bulletArray.length; i++) {
bulletArray[i] = new Bullet(this);
}
bulletPool = new ActorPool(bulletArray);
}
public void render(Graphics g) {
g.setColor(0, 0, 0);
g.drawRect(scrX, scrY, PigCanvas.SCRW + 1, PigCanvas.SCRH + 1);
addX = scrX - viewX;
addY = scrY - viewY;
startTileX = (viewX / TILE_H) - 1;
startTileY = (viewY / TILE_H) - 1;
endTileX = ((Math.abs(viewX) + PigCanvas.SCRW) / TILE_H) + 1;
endTileY = ((Math.abs(viewY) + PigCanvas.SCRH) / TILE_H) + 1;
if (endTileX > MAP_W) {
endTileX = MAP_W;
}
if (endTileY > MAP_H) {
endTileY = MAP_H;
}
if (startTileX < 0) {
startTileX = 0;
}
if (startTileY < 0) {
startTileY = 0;
}
int tileType = 0;
int xpos = 0;
int ypos = 0;
for (int tileY = startTileY; tileY < endTileY; tileY++) {
for (int tileX = startTileX; tileX < endTileX; tileX++) {
if (tileY >= 0 && tileX >= 0) {
tileType = tileMap[tileY][tileX];
mapSprite.setState(tileType, 200);
xpos = (tileX * TILE_H) + addX;
ypos = (tileY * TILE_H) + addY;
if (xpos > scrX - TILE_H && xpos < scrX + PigCanvas.SCRW &&
ypos > scrY - TILE_H && ypos < scrY + PigCanvas.SCRH) {
mapSprite.drawClip(g, xpos, ypos);
}
}
}
}
flyPig.render(g, addX, addY);
Actor a = npcPool.getFirstUsed();
while (a != null) {
if (Actor.pointInRect(a.x, a.y, viewX, viewY, PigCanvas.SCRW,
PigCanvas.SCRH) ||
Actor.pointInRect(a.x + a.imageW, a.y + a.imageH,
viewX, viewY, PigCanvas.SCRW,
PigCanvas.SCRH)) {
a.render(g, addX, addY);
}
a = a.getNextLink();
}
a = bulletPool.getFirstUsed();
while (a != null) {
if (Actor.pointInRect(a.x, a.y, viewX, viewY,
PigCanvas.SCRW, PigCanvas.SCRH)
|| Actor.pointInRect(a.x + a.imageW, a.y + a.imageH,
viewX, viewY, PigCanvas.SCRW,
PigCanvas.SCRH)) {
a.render(g, addX, addY);
}
a = a.getNextLink();
}
}
public void cycle() {
flyPig.cycle();
// if (startTileY / 20 == 0) {
if (!bossLive) {
getNpcFromPool().init(Actor.BOSS, 2, viewX + (PigCanvas.SCRW / 2),
viewY + 10, Actor.LEFT, 3, 0);
bossLive = true;
}
// }
int tileType = 0;
int xpos = 0;
int ypos = 0;
if (startTileY < 245) {
for (int tileY = startTileY; tileY < endTileY; tileY++) {
for (int tileX = startTileX; tileX < endTileX; tileX++) {
if (tileY >= 0 && tileX >= 0) {
tileType = NpcPos[tileY][tileX];
xpos = (tileX * TILE_H);
ypos = (tileY * TILE_H);
if (tileType == 1) {
if (xpos > viewX - TILE_H &&
xpos < viewX + PigCanvas.SCRW &&
ypos > viewY - TILE_H &&
ypos < viewY + PigCanvas.SCRH - 80) {
int iRandChoose = Actor.getRand(1, 2);
if (iRandChoose == 1) {
iRand = Actor.getRand(1, 9);
addNPC(xpos, ypos, 0, 0);
}
NpcPos[tileY][tileX] = 0;
}
}
}
}
}
}
if (System.currentTimeMillis() - huoTime > 2000) {
huoTime = System.currentTimeMillis();
int startX = Actor.getRand(viewX, viewX + 150);
getBulletFromPool().init(Actor.BULLET, 1, startX, viewY - 20,
Actor.DOWN, 0, 5);
}
if (System.currentTimeMillis() - NPCTime > 4000) {
NPCTime = System.currentTimeMillis();
int x = Actor.getRand(viewX, viewX + 140);
iRand = Actor.getRand(1, 18);
addNPC(x, viewY - 30, 0, 2);
}
Actor a = npcPool.getFirstUsed();
while (a != null) {
a.cycle();
a = a.getNextLink();
}
a = bulletPool.getFirstUsed();
while (a != null) {
a.cycle();
a = a.getNextLink();
}
}
public void addNPC(int startX, int startY, int speedX, int speedY) {
int iType = 0;
int iState = 0;
int offsetY = 0;
switch (iRand) {
case 1:
case 2:
case 3:
iType = Actor.NPC;
iState = 0;
break;
case 4:
case 5:
case 6:
iType = Actor.NPC;
iState = 2;
offsetY = -1;
break;
case 7:
case 8:
case 9:
iType = Actor.NPC;
iState = 3;
offsetY = 15;
break;
case 10:
case 11:
case 12:
case 13:
case 14:
case 15:
case 16:
case 17:
case 18:
iType = Actor.daoju;
break;
}
getNpcFromPool().init(iType, iState, startX, startY + offsetY,
Actor.DOWN, speedX, speedY);
}
public final void setView(int viewXArg, int viewYArg) {
viewX = viewXArg;
viewY = viewYArg;
}
public final int getTileAtX(int x) {
return x / TILE_H;
}
public final int getTileAtY(int y) {
return y / TILE_H;
}
public final boolean checkCollision(Actor actor, int x, int y, int w,
int h) {
if (actor.type == Actor.NPC) {
Actor a = npcPool.getFirstUsed();
while (a != null) {
if (actor != a && a.type == Actor.daoju &&
actor.collideWith(a)) {
actor.onCollision(a);
return true;
}
a = a.getNextLink();
}
}
if (actor.type == Actor.BULLET) {
if (actor.collideWith(flyPig)) {
actor.onCollision(flyPig);
return true;
}
Actor a = npcPool.getFirstUsed();
while (a != null) {
if (a.type == Actor.BOSS) {
if (actor.collideWith(a)) {
actor.onCollision(a);
return true;
}
}
a = a.getNextLink();
}
}
if (actor.type == Actor.daoju) {
if (actor.state != 9) {
if (actor.collideWith(flyPig)) {
actor.onCollision(flyPig);
return true;
}
}
}
return false;
}
public final void setPlayer(FlyPig a) {
flyPig = a;
}
public void readString() {
try {
InputStream is = null;
is = this.getClass().getResourceAsStream("/maps/map1.txt");
byte[] buffer = new byte[2];
int bytesRead = 0;
String str1 = new String(buffer, 0, 2);
for (int i = 0; i < MAP_H; i++) {
for (int j = 0; j < MAP_W; j++) {
bytesRead = is.read(buffer);
if (bytesRead > 0) {
str1 = new String(buffer, 0, 2).trim();
byte c = Byte.parseByte(str1);
tileMap[i][j] = c;
System.out.println("data:" + tileMap[i][j]);
}
}
}
} catch (Exception e) {
System.out.println("Can't load map.txt");
}
}
public final Npcs getNpcFromPool() {
return (Npcs) npcPool.getNextFree();
}
public final void releaseNpc(Npcs s) {
npcPool.release(s);
}
public final Bullet getBulletFromPool() {
return (Bullet) bulletPool.getNextFree();
}
public final void releaseBullet(Bullet b) {
bulletPool.release(b);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -