所以我得到了子弹类:
import java.awt.*;
public class Bullet extends GameObject
{
private Player player;
private int deltaX;
public Bullet(final Player player, final int deltaX, final int xPos, final int yPos, final int width, final int height, final String img) {
this.deltaX = deltaX;
this.player = player;
this.xPos = xPos;
this.yPos = yPos;
this.height = height;
this.width = width;
this.rect = new Rectangle(xPos, yPos, width, height);
this.img = getImage(img);
}
@Override
public void draw(Graphics g)
{
g.drawImage(img, xPos, yPos, width, height, null);
}
@Override
void update(final Shooter shooter, final int id)
{
if(rect.intersects(player.rect))
{
shooter.bullets.remove(this);
if(!(shooter.player1.getHull() == 0))
{
player.setHealth(player.getHealth() - 1);
if(!(getStamina() < 1))
if(shooter.player1.getStamina() > 10)
shooter.player1.setStamina(shooter.player1.getStamina() - 10);
else
shooter.player1.setStamina(shooter.player1.getStamina() - 1);
else
shooter.player1.setStamina(shooter.player1.getStamina() - 0);
}
else
{
player.setHealth(player.getHealth() - 2);
}
if(!(player.getHull() == 0))
player.setHull(player.getHull() - 2);
else
player.setHull(player.getHull() - 0);
}
else if (yPos < -100 || yPos > 2000)
{
shooter.bullets.remove(this);
}
else
{
if(deltaX == 1)
{
yPos++;
rect.y++;
}
else
{
yPos--;
rect.y--;
yPos--;
rect.y--;
}
}
}
public void setPlayer(Player player) {
this.player = player;
}
public Player getPlayer()
{
return player;
}
@Override
Image getImage(String img) {
return Toolkit.getDefaultToolkit().getImage(img);
}
public int getDeltaX() {
return deltaX;
}
public void setDeltaX(int deltaX) {
this.deltaX = deltaX;
}
}
这是我的流星课:
import java.awt.*;
public class Meteor extends GameObject
{
private Player player;
private int deltaX;
public Meteor(final Player player, final int deltaX, final int xPos, final int yPos, final int width, final int height, final String img) {
this.deltaX = deltaX;
this.player = player;
this.xPos = xPos;
this.yPos = yPos;
this.height = height;
this.width = width;
this.rect = new Rectangle(xPos, yPos, width, height);
this.img = getImage(img);
}
@Override
public void draw(Graphics g)
{
g.drawImage(img, xPos, yPos, width, height, null);
}
@Override
void update(final Shooter shooter, final int id)
{
if (yPos < -100 || yPos > 2000)
{
shooter.meteors.remove(this);
}
else
{
if(deltaX == 1)
{
yPos++;
rect.y++;
}
else
{
yPos++;
rect.y++;
}
}
if(rect.intersects(shooter.player1.rect))
{
System.out.println("Collision");
shooter.meteors.remove(this);
shooter.player1.setHealth(shooter.player1.getHealth() - 100);
}
}
public void setPlayer(Player player) {
this.player = player;
}
public Player getPlayer()
{
return player;
}
@Override
Image getImage(String img) {
return Toolkit.getDefaultToolkit().getImage(img);
}
public int getDeltaX() {
return deltaX;
}
public void setDeltaX(int deltaX) {
this.deltaX = deltaX;
}
}
现在在 Meteor 类中我想使用这个:
if(bullet.rect.intersect(shooter.player1.rect)
{..}
但这不起作用,因为我无法从中引用子弹类。有什么办法让它工作吗?
这是游戏对象类
import java.awt.*;
public abstract class GameObject
{
protected Rectangle rect;
protected int xPos;
protected int yPos;
protected int height;
protected int width;
protected Image img;
protected int health;
protected int stamina;
protected int hull;
abstract void draw(Graphics g);
abstract void update(final Shooter shooter, final int id);
abstract Image getImage(String img);
public int getHealth() {
return health;
}
public void setHealth(int health) {
this.health = health;
}
public int getStamina() {
return stamina;
}
public void setStamina(int stamina) {
this.stamina = stamina;
}
public Rectangle getRect() {
return rect;
}
public void setRect(Rectangle rect) {
this.rect = rect;
}
public int getHull() {
return hull;
}
public void setHull(int hull) {
this.hull = hull;
}
public int getxPos() {
return xPos;
}
public void setxPos(int xPos) {
this.xPos = xPos;
}
public int getyPos() {
return yPos;
}
public void setyPos(int yPos) {
this.yPos = yPos;
}
public Image getImg() {
return img;
}
public void setImg(Image img) {
this.img = img;
}
public int getHeight() {
return height;
}
public void setHeight(int height) {
this.height = height;
}
public int getWidth() {
return width;
}
public void setWidth(int width) {
this.width = width;
}
}