我是新手程序员,我用Java制作了我的第一个游戏,我刚刚实现了目标AI,现在每当我运行它时,总是滞后于开始,我想解释为什么请或者更好地做到这一点,谢谢你提前。
代码:(对于最可能造成滞后的因素)
public class Handler {
//Use Linked Lists
private Animator a;
private boolean renderMini;
private int x, y;
public LinkedList<GameObject> object = new LinkedList<GameObject>();
public LinkedList<EntityObject> entity = new LinkedList<EntityObject>();
public LinkedList<Faction> faction = new LinkedList<Faction>();
public Handler(){
a = new Animator();
this.renderMini = false;
}
public void render(Graphics g){
///if(GameMain.numFrames > 5){
if(renderMini){
a.AnimateMini(g, x, y, 32, 32);
}
for(int i = 0; i < entity.size(); i++){
EntityObject tempObject = entity.get(i);
tempObject.render(g);
}
for(int i = 0; i < object.size(); i++){
GameObject tempObject = object.get(i);
tempObject.render(g);
}
//}
}
public void tick(){
//if(GameMain.numFrames > 5){
for(int i = 0; i < entity.size(); i++){
EntityObject tempObject = entity.get(i);
tempObject.tick();
}
for(int i = 0; i < object.size(); i++){
GameObject tempObject = object.get(i);
tempObject.tick();
}
for(int i = 0; i < faction.size(); i++){
Faction tempObject = faction.get(i);
tempObject.tick();
}
//}
}
public void addEntity(EntityObject o){
this.entity.add(o);
}
public void removeEntity(EntityObject o){
this.entity.remove(o);
}
public void addObject(GameObject o){
this.object.add(o);
}
public void removeObject(GameObject o){
if(o instanceof NpcLaser){
x = o.getX();
y = o.getY();
renderMini = true;
}
if(o instanceof PlayerLaser){
x = o.getX();
y = o.getY();
renderMini = true;
}
this.object.remove(o);
}
public void addFaction(Faction f){
this.faction.add(f);
}
public void removeFaction(Faction f){
this.faction.remove(f);
}
}
public class StandardShip extends EntityObject{
private Handler h;
private Random r;
private Animator a;
private Faction ef;
private EntityObject object;
private int desX, desY;
private int lx, ly, targetX, targetY; //target;
private boolean animateReady;
public static int isDead, ran, ra, rn;
public static boolean thisDeath;
public static boolean choAttack, choDefense, choSpeed, choShealth, choCommanding;
public StandardShip(int x, int y, int width, int height, Handler h, Faction f) {
super(x, y, width, height, h);
r = new Random();
a = new Animator();
this.setDeath(false);
this.x = x;
this.y = y;
this.width = width;
this.height = height;
this.h = h;
/*this.isDead = 0;
this.thisDeath = false;
this.p = p;*/
this.health = 100;
this.animateReady = false;
//this.target = 1;
this.killLim = r.nextInt(4) + 1;
EntityObject.f = f;
EntityObject.f.addMember(this);
this.attackLim = 20;
this.defenseLim = 20;
this.speedLim = 20;
this.shealthLim = 20;
this.commandingLim = 20;
this.currency = 0;
this.attack = r.nextInt(attackLim);
this.defense = r.nextInt(defenseLim) + 1;
this.speed = r.nextInt(speedLim) + 2;
this.shealth = r.nextInt(shealthLim);
this.commanding = r.nextInt(commandingLim);
this.velX = 1;
this.velY = 1;
this.SearchTime = 1000;
this.desX = r.nextInt(Window.screensize.width + 1000);
this.desY = r.nextInt(Window.screensize.height + 1000);
//this.vectW = 5;
rn = r.nextInt(5);
if(rn == 0){
if(attack < 15){
attack = r.nextInt(attackLim) + 15;
}
}
if(rn == 1){
if(defense < 15){
defense = r.nextInt(defenseLim) + 15;
}
}
if(rn == 2){
if(speed < 15){
speed = r.nextInt(speedLim) + 15;
}
}
if(rn == 3){
if(shealth < 15){
shealth = r.nextInt(shealthLim) + 15;
}
}
if(rn == 4){
if(commanding < 15){
commanding = r.nextInt(commandingLim) + 15;
}
}
if(choAttack){
if(attack < 15){
attack = r.nextInt(attackLim) + 15;
}
}
if(choDefense){
if(defense < 15){
defense = r.nextInt(defenseLim) + 15;
}
}
if(choSpeed){
if(speed < 15){
speed = r.nextInt(speedLim) + 15;
}
}
if(choShealth){
if(shealth < 15){
shealth = r.nextInt(shealthLim) + 15;
}
}
if(choCommanding){
if(commanding < 15){
commanding = r.nextInt(commandingLim) + 15;
}
}
}
public void tick() {
x = GameMain.clamp(0, Window.screensize.width + 1000, x);
y = GameMain.clamp(0, Window.screensize.height + 1000, y);
x += velX;
y += velY;
EnemySize = f.isEnemy.size();
if(coolDown > 0){
coolDown--;
coolDown = GameMain.clamp(0, 1000, coolDown);
}
if(this.target == null){
for(int i = 0; i < EnemySize; i++){
ef = EntityObject.f.isEnemy.get(i);
memberSize = ef.members.size();
}
}
for(int i = 0; i < ef.members.size(); i++){
object = ef.members.get(i);
if(object.getX() >= this.x && object.getX() <= this.x + 300){
this.setTarget(object);
System.out.println("TargetSS");
i = ef.members.size();
}
if(object.getY() >= this.y && object.getY() <= this.y + 300){
this.setTarget(object);
System.out.println("TargetSS");
i = ef.members.size();
}
}
if(this.target != null && coolDown <= 0){
targetX = this.target.getX();
targetY = this.target.getY();
attack(targetX, targetY);
coolDown = 500;
}
if(this.target == null){
wander();
}
/*rand = r.nextInt(100);
if(kills == killLim){
level++;
levelUp();
kills = 0;
killLim += killLim/2;
}
x += velX;
y += velY;
if(y < vectY && velY == -1){velY *= -1;}
if(y > vectY && velY == 1){velY *= -1;}
if(x < vectX && velX == -1){velX *= -1;}
if(x > vectX && velX == 1){velX *= -1;}
if(x == vectX){
if(r.nextInt(2) == 0){
vectX = r.nextInt(Window.screensize.width);
}else{
velX = 0;
}
}
if(y == vectY){
if(r.nextInt(2) == 0){
vectY = r.nextInt(Window.screensize.height);
}else{
velY = 0;
}
}*/
if(this.isDamage > 0 && this.isDamage > defense){
health -= isDamage - (defense / 2);
isDamage = 0;
//System.out.println("HELLO");
}
}
public void wander(){
if(y < desY && velY == -1){velY *= -1;}
if(y > desY && velY == 1){velY *= -1;}
if(x < desX && velX == -1){velX *= -1;}
if(x > desX && velX == 1){velX *= -1;}
System.out.println("desY: "+desY+" desX: "
+desX+" Y: "+y+" X: "+x+" velX: "+velX+" velY: "+velY);
if(x == desX){
//if(r.nextInt(2) == 0){
desX = r.nextInt(Window.screensize.width + 1000);
//}else{
velX = 0;
//}
}
if(y == desY){
//if(r.nextInt(2) == 0){
desY = r.nextInt(Window.screensize.height + 1000);
//}else{
velY = 0;
//}
}
}
public void levelUp(){
this.health += r.nextInt(300) + 100;
this.attack += r.nextInt(attackLim);
this.defense += r.nextInt(defenseLim) + 1;
this.speed += r.nextInt(speedLim) + 2;
this.shealth += r.nextInt(shealthLim);
this.commanding += r.nextInt(commandingLim);
if(choAttack){
attack += (r.nextInt(attackLim) + 15)/2;
}
if(choDefense){
defense += (r.nextInt(defenseLim) + 15)/2;
}
if(choSpeed){
speed += (r.nextInt(speedLim) + 15)/2;
}
if(choShealth){
shealth += (r.nextInt(shealthLim) + 15)/2;
}
if(choCommanding){
commanding += (r.nextInt(commandingLim) + 15)/2;
}
}
public void Collision(Graphics g){
/*for(int i = 0; i < h.object.size(); i++){
GameObject tempObject = h.object.get(i);
if(tempObject.getId() == ID.Money){
Money m = (Money) tempObject;
if(getBounds().intersects(tempObject.getBounds())){
currency += m.getCashValue();
h.removeObject(tempObject);
}
}
if(tempObject.getId() == ID.ShipPart){
ShipPart s = (ShipPart) tempObject;
if(getBounds().intersects(tempObject.getBounds())){
numShipParts++;
h.removeObject(tempObject);
}
}
if(tempObject.getId() == ID.Meteor){
Meteor m = (Meteor) tempObject;
if(getBounds().intersects(tempObject.getBounds())){
Rectangle OverLap = getBounds().intersection(tempObject.getBounds());
if(OverLap.height >= OverLap.width){
if(m.isDoesDamage() && !m.isExplodes() && !m.isOnFire()){
this.health -= m.getDamage();
velX *= -2;
}
if(m.isDoesDamage() && m.isExplodes() || m.isDoesDamage() && m.isOnFire()){
if(m.isExplodes()){
a.AnimateExplosion(g, tempObject.getX(), tempObject.getY(), 32, 32);
this.health -= m.getDamage();
if(a.isFin[0]){
h.removeObject(tempObject);
a.isFin[0] = false;
}
}
if(m.isOnFire()){
this.health -= m.getDamage();
velX *= -2;
}
}else{
velX *= -2;
}
}
if(OverLap.width >= OverLap.height){
if(m.isDoesDamage() && !m.isExplodes() && !m.isOnFire()){
this.health -= m.getDamage();
velY *= -2;
}
if(m.isDoesDamage() && m.isExplodes() || m.isDoesDamage() && m.isOnFire()){
if(m.isExplodes()){
this.health -= m.getDamage();
m.setExplodeNow(true);
}
if(m.isOnFire()){
this.health -= m.getDamage();
velY *= -2;
}
}else{
velY *= -2;
}
}
}
}
if(tempObject.getId() == ID.Player){
p = (Player) tempObject;
if(getBounds().intersects(tempObject.getBounds())){
Rectangle OverLap = getBounds().intersection(tempObject.getBounds());
health -= r.nextInt(10) + 10;
p.setDamage(r.nextInt(10) + 10);
if(OverLap.height >= OverLap.width){
tempObject.setVelX(0);
}
if(OverLap.width >= OverLap.height){
tempObject.setVelY(0);
}
}
}
}*/
}
public void attack(int targetX, int targetY){
System.out.println("Hello");
h.addObject(new NpcLaser(x + 60, y + 60, 5, 9, h, f, targetX, targetY, this.attack));
}
public void render(Graphics g) {
a.AnimateEnemy(g, this.x, this.y, this.width, this.height, f);
if(animateReady){
a.AnimateMini(g, lx, ly, ran, ran);
}
if(health <= 0){
setDeath(true);
StandardShip.thisDeath = true;
a.AnimateExplosion(g, x, y, width, height);
if(Animator.isFin[0]){
ra = r.nextInt(10);
isDead++;
if(ra <= 7){
h.addObject(new Money(x, y, 32, 32, ID.Money, h, currency));
}else{
for(int i = 0; i < numShipParts; i++){
h.addObject(new ShipPart(x, y, 32, 32, ID.ShipPart, h));
}
}
h.removeEntity(this);
Animator.isFin[0] = false;
}
}
g.setColor(Color.red);
}
public Rectangle getBounds() {
return new Rectangle(x + 40, y + 37, 50, 50);
}
}
public class NpcLaser extends GameObject{
private Handler h;
private Faction f, ef;
private int laserAim;
private int currentTarget;
private int vectorX, vectorY;
private int damage;
public NpcLaser(int x, int y, int width, int height, Handler h, Faction f, int targetX, int targetY, int attack) {
super(x, y, width, height, h);
this.x = x;
this.y = y;
this.width = width;
this.height = height;
this.h = h;
this.f = f;
this.vectorX = targetX;
this.vectorY = targetY;
this.damage = attack;
new Animator();
aimAI();
}
public void tick() {
System.out.println("Hello");
//Collision(g);
x += velX;
y += velY;
if(velX == 0 && velY == 0){
h.object.remove();
}
for(int i = 0; i < f.isEnemy.size(); i++){
ef = f.isEnemy.get(i);
}
for(int u = 0; u < ef.members.size(); u++){
EntityObject eo = ef.members.get(u);
if(getBounds().intersects(eo.getBounds())){
eo.setDamage(damage);
h.removeObject(this);
}
}
if(x > Window.screensize.getWidth() + 1200 || x < 0 - 1200){
h.removeObject(this);
}
if(y > Window.screensize.getHeight() + 1200 || y < 0 - 1200){
h.removeObject(this);
}
}
public void aimAI(){
if(x < vectorX){velX = 10;}
if(x > vectorX){velX = -10;}
if(y < vectorY){velY = 10;}
if(y > vectorY){velY = -10;}
if(x <= vectorX + 60 && x >= vectorX - 60){velX = 0;}
if(y <= vectorY + 60 && y >= vectorY - 60){velY = 0;}
}
public void render(Graphics g) {
/*if(p.isRemoveShot()){
h.removeObject(this);
p.setRemoveShot(false);
}*/
if(velX == 10 && velY == 10 || velX == -10 && velY == -10){
laserAim = 3;
}
if(velX == 10 && velY == -10 || velX == -10 && velY == 10){
laserAim = 2;
}
if(velX == 10 && velY == 0 || velX == -10 && velY == 0){
laserAim = 1;
}
if(velX == 0 && velY == 10 || velX == 0 && velY == -10){
laserAim = 0;
}
if(laserAim == 0){
g.drawImage(Assets.playerLaser, x, y, width, height, null);
}
if(laserAim == 1){
g.drawImage(Assets.playerLaser1, x, y, width, height, null);
}
if(laserAim == 2){
g.drawImage(Assets.playerLaser2, x, y, width, height, null);
}
if(laserAim == 3){
g.drawImage(Assets.playerLaser3, x, y, width, height, null);
}
}
public int getDamage() {
return damage;
}
public void setDamage(int damage) {
this.damage = damage;
}
public int getCurrentTarget() {
return currentTarget;
}
public void setCurrentTarget(int currentTarget) {
this.currentTarget = currentTarget;
}
public Rectangle getBounds() {
return new Rectangle(x, y, width, height);
}
}
答案 0 :(得分:5)
for(int i = 0; i < entity.size(); i++){
EntityObject tempObject = entity.get(i);
tempObject.render(g);
}
您的代码包含几个类似于此的循环,每个循环都需要二次时间,因为它们会在LinkedList
上运行,O(n)
需要get
时间。您几乎肯定会使用ArrayList
,或者至少使用for-each循环,例如。
for (EntityObject tempObject : entity) {
tempObject.render(g);
}