W celu poduczenia się java próbuje zrobić sobie grę z kursu space invaders. Nie trzymam się tego kursu w 100%, staram się na jego podstawie zrobić grę Tank. Mój problem polega na tym że "sciana" w którą zostanie wystrzelony pocisk nie reaguje, lub reaguje dopiero po pewnym czasie. Poniżej kod programu:
KlasaGłowna"
public class Wojna extends Canvas implements Stage, KeyListener {
private static final long serialVersionUID = 7037479338593065179L;
public long usedTime;
public BufferStrategy strategia;
private SpriteCache spriteCache;
private ArrayList<Actor> actors;
private ArrayList<Blok> blok;
private Player player;
private Map map = new Map(this);
public Wojna() {
spriteCache = new SpriteCache();
JFrame okno = new JFrame(".: Gra:.");
JPanel panel = (JPanel) okno.getContentPane();
setBounds(0, 0, Stage.W, Stage.H);
panel.setPreferredSize(new Dimension(Stage.W, Stage.H));
panel.setLayout(null);
panel.add(this);
okno.setBounds(0, 0, Stage.W, Stage.H);
okno.setVisible(true);
okno.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
okno.setResizable(false);
createBufferStrategy(2);
strategia = getBufferStrategy();
requestFocus();
addKeyListener(this);
}
public void keyPressed(KeyEvent e) {
player.keyPressed(e);
}
public void keyReleased(KeyEvent e) {
player.keyReleased(e);
}
public void keyTyped(KeyEvent e) {
}
public void initWorld() {
actors = new ArrayList<Actor>();
blok = new ArrayList<Blok>();
map.rysuj();
for (int i = 0; i < 6; i++) {
Monster m = new Monster(this);
m.setX((int) (Math.random() * Stage.W - 60));
m.setY(i * 20);
m.setVx((int) (Math.random() * 3) + 1);
actors.add(m);
}
player = new Player(this);
player.setX(Stage.W / 2);
player.setY(Stage.H - 2 * player.getHeight());
}
public void paintWorld() {
Graphics2D g = (Graphics2D) strategia.getDrawGraphics();
g.setColor(Color.BLACK);
g.fillRect(0, 0, getWidth(), getHeight());
for (int i = 0; i < blok.size(); i++) {
Blok b = (Blok) blok.get(i);
b.paint(g);
}
for (int i = 0; i < actors.size(); i++) {
Actor m = (Actor) actors.get(i);
m.paint(g);
}
player.paint(g);
g.setColor(Color.RED);
if (usedTime > 0)
g.drawString(String.valueOf(1000 / usedTime) + "fps", 0,
Stage.H - 50);
else
g.drawString("--- fps", 5, Stage.H - 50);
;
strategia.show();
}
public void addActor(Actor a) {
actors.add(a);
}
public void updateWorld() {
int i = 0;
while (i < actors.size()) {
Actor m = (Actor) actors.get(i);
if (m.isMarkedForRemoval()) {
actors.remove(i);
} else {
m.act();
i++;
}
}
while (i < blok.size()) {
Blok b = (Blok) blok.get(i);
if (b.isMarkedForRemoval()) {
blok.remove(i);
} else {
i++;
}
}
player.act();
}
public SpriteCache getSpriteCache() {
return spriteCache;
}
public void checkCollisions() {
Rectangle playerBounds = player.getBounds();
for (int i = 0; i < actors.size(); i++) {
Actor a1 = (Actor) actors.get(i);
Rectangle r1 = a1.getBounds();
if (r1.intersects(playerBounds)) {
player.collision(a1);
a1.collision(player);
}
for (int j = i + 1; j < actors.size(); j++) {
Actor a2 = (Actor) actors.get(j);
Rectangle r2 = a2.getBounds();
if (r1.intersects(r2)) {
a1.collision(a2);
a2.collision(a1);
}
for (int k = 0; k < blok.size(); k++) {
Blok b1 = (Blok) blok.get(k);
Rectangle r3 = b1.getBounds();
if (r1.intersects(r3)) {
a1.collision(b1);
b1.collision(a1);
}
}
}
}
}
public void game() {
usedTime = 1000;
initWorld();
while (isVisible()) {
long startTime = System.currentTimeMillis();
updateWorld();
checkCollisions();
paintWorld();
usedTime = System.currentTimeMillis() - startTime;
do {
Thread.yield();
} while (System.currentTimeMillis() - startTime < 15);
}
}
public static void main(String[] args) {
Wojna inv = new Wojna();
inv.game();
}
public void addBlok(Blok b) {
blok.add(b);
}
}
KlasaBlok
<code= java>package Stage;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.image.BufferedImage;
import Actor.Actor;
import Core.SpriteCache;
import Core.Stage;
public class Blok {
protected int x, y;
protected int w, h;
protected String spriteName;
protected Stage stage;
protected SpriteCache spriteCache;
public Blok(Stage stage) {
this.stage = stage;
spriteCache = stage.getSpriteCache();
}
public Rectangle getBounds() {
return new Rectangle(x, y, w, h);
}
public void collision(Blok a) {
}
protected boolean markedForRemoval;
public void remove() {
markedForRemoval = true;
}
public boolean isMarkedForRemoval() {
return markedForRemoval;
}
public void paint(Graphics2D g) {
g.drawImage(spriteCache.getSprite(spriteName, 0), x, y,
stage);
}
public int getX() {
return x;
}
public void setX(int i) {
x = i;
}
public int getY() {
return y;
}
public void setY(int i) {
y = i;
}
public void setSpriteName(String name, int sP) {
spriteName = name;
h = 0;
w = 0;
for (int i = 0; i < sP; i++) {
BufferedImage image = spriteCache.getSprite(spriteName, i);
h = Math.max(h, image.getHeight());
w = Math.max(w, image.getWidth());
}
}
public int getHeight() {
return h;
}
public int getWidth() {
return w;
}
public void setHeight(int i) {
h = i;
}
public void setWidth(int i) {
w = i;
}
public void act() {
}
public void collision(Actor a) {
}
}
KlasaActor:
```java
public class Actor {
protected int x, y;
protected int w, h;
protected String spriteName;
protected Stage stage;
protected SpriteCache spriteCache;
protected int currentFrame;
protected String[] spriteNames;
protected int frameSpeed;
protected int t;
int sP;
public Actor(Stage stage) {
this.stage = stage;
spriteCache = stage.getSpriteCache();
frameSpeed = 1;
t = 0;
}
public Rectangle getBounds() {
return new Rectangle(x, y, w, h);
}
public void collision(Actor a) {
}
protected boolean markedForRemoval;
public void remove() {
markedForRemoval = true;
}
public boolean isMarkedForRemoval() {
return markedForRemoval;
}
public int getFrameSpeed() {
return frameSpeed;
}
public void setFrameSpeed(int i) {
frameSpeed = i;
}
public void paint(Graphics2D g) {
g.drawImage(spriteCache.getSprite(spriteName, currentFrame, w,h), x, y,
stage);
}
public int getX() {
return x;
}
public void setX(int i) {
x = i;
}
public int getY() {
return y;
}
public void setY(int i) {
y = i;
}
public void setSpriteName(String name, int sP, int w,int h) {
this.sP = sP;
spriteName = name;
this.h = h;
this.w = w;
for (int i = 0; i < sP; i++) {
BufferedImage image = spriteCache.getSprite(spriteName, i,w,h);
h = Math.max(h, image.getHeight());
w = Math.max(w, image.getWidth());
}
}
public int getHeight() {
return h;
}
public int getWidth() {
return w;
}
public void setHeight(int i) {
h = i;
}
public void setWidth(int i) {
w = i;
}
public void act() {
t++;
if (t % frameSpeed == 0) {
t = 0;
currentFrame = (currentFrame + 1) % sP;
}
}
public void collision(Blok b) {
// TODO Auto-generated method stub
}
}
KlasaPocisk
public class Bullet extends Actor {
protected static final int BULLET_SPEED = 10;
public Bullet(Stage stage) {
super(stage);
setSpriteName("r.jpg",1,10,30);
}
public void act() {
super.act();
y -= BULLET_SPEED;
if (y < 0)
remove();
}
public void collision(Actor a) {
if (a instanceof Monster)
remove();
}
public void collision(Blok b) {
if (b instanceof Beton)
remove();
}
}