好的,所以我已经在课堂上和在线教程中学习了几个星期的 java。我做了一个简单的游戏,方块落在屏幕底部,而玩家控制一个小球,只在 x 轴上移动并试图避开它们。
我遇到的问题是方块开始下降得太快了。现在我将它们设置如下:
ix = 0;
iy = 1;
然后在我的 move() 方法中,我有以下内容:
hitbox.x += ix;
hitbox.y += iy;
在此示例中,ix 和 iy 都是整数。
我的第一个假设是将整数更改为浮点数,然后使用:
ix= 0;
iy = 0.5;
接着:
hitbox.x += ix;
hitbox.y += 0.5f;
但这只是将物体冻结在它们的轨道上。我相信这是因为绳索被视为整数,所以我想如果我修改了我的 getX() 和 getY() 方法,也许我可以以某种方式操纵它们以使用十进制数?但我不太确定该怎么做。对此问题的任何帮助/提示/解决方案将不胜感激。
这是一些令人兴奋的代码,让我知道是否还需要!
敌人经理:
public class EnemyManager {
private int amount;
private List<Enemy> enemies = new ArrayList<Enemy>();
private GameInJavaOne instance;
public EnemyManager(GameInJavaOne instance, int a){
this.amount = a;
this.instance = instance;
spawn();
}
private void spawn(){
Random random = new Random();
int ss = enemies.size();
// If the current amount of enemies is less than the desired amount, we spawn more enemies.
if(ss < amount){
for(int i = 0; i < amount - ss; i++){
enemies.add(new Enemy(instance, random.nextInt(778), random.nextInt(100)+1));
}
// If its greater than the desired number of enemies, remove them.
}else if (ss > 20){
for(int i = 0; i < ss - amount; i++){
enemies.remove(i);
}
}
}
public void draw(Graphics g){
update();
for(Enemy e : enemies) e.draw(g);
}
private void update() {
boolean re = false;
for(int i = 0; i < enemies.size(); i ++){
if(enemies.get(i).isDead()){
enemies.remove(i);
re = true;
}
}
if(re) spawn();
}
public boolean isColliding(Rectangle hitbox){
boolean c =false;
for(int i = 0; i < enemies.size(); i ++){
if(hitbox.intersects(enemies.get(i).getHitbox())) c = true;
}
return c;
}
}
实体:
public abstract class Entity {
protected int x, y, w, h;
protected boolean removed = false;
public Entity(int x, int y){
this.x = x;
this.y = y;
}
public void Draw(Graphics g){
}
public int getX() { return x; }
public int getY() { return y; }
public int getH() { return h; }
public int getW() { return w; }
}
和敌人类:
public class Enemy extends Entity{
private Rectangle hitbox;
private int ix, iy;
private boolean dead = false;
private GameInJavaOne instance;
public Enemy(GameInJavaOne instance, int x, int y){
super(x, y);
this.instance = instance;
hitbox = new Rectangle(x, y, 32, 32);
ix = 0;
iy = 1;
}
private void move(){
if(instance.getStage().isCollided(hitbox)){
iy =0;
dead = true;
}
hitbox.x += ix;
hitbox.y += iy;
}
public boolean isDead() {return dead;}
public Rectangle getHitbox() {return hitbox; }
public void draw(Graphics g){
move();
g.setColor(Color.MAGENTA);
g.fillRect(hitbox.x, hitbox.y, hitbox.height, hitbox.width);
}
}