learning about colision detection

This commit is contained in:
2020-07-05 11:32:35 +02:00
parent 36e46e2528
commit 12c82ec224
11 changed files with 20 additions and 14 deletions

BIN
DemoProjects/res/laser.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.6 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.3 KiB

View File

@@ -3,6 +3,7 @@ package sprites.shootingMissles;
public class Missile extends Sprite {
private final int MISSILE_SPEED = 2;
private final int FRAME_WIDTH = 400;
public Missile(int x, int y) {
super(x, y);
@@ -11,15 +12,14 @@ public class Missile extends Sprite {
}
private void initMissile() {
loadImage("/home/bitecoding/Pictures/laser.png",35,35);
getImageDimensions();
loadImage("DemoProjects/res/laser.png",100,10);
}
public void move() {
y -= MISSILE_SPEED;
x += MISSILE_SPEED;
if (y < 0) {
if (x > FRAME_WIDTH) {
visible = false;
}
}

View File

@@ -20,8 +20,7 @@ public class SpaceShip extends Sprite {
missiles = new ArrayList<>();
loadImage("/home/bitecoding/Pictures/spaceship.png",50,50);
getImageDimensions();
loadImage("DemoProjects/res/spaceship.png",50,50);
}
public void move() {
@@ -59,7 +58,7 @@ public class SpaceShip extends Sprite {
}
public void fire() {
missiles.add(new Missile(x + width, y + height / 2));
missiles.add(new Missile(x - width/2, y + height/2 - 5));
}
public void keyReleased(KeyEvent e) {

View File

@@ -1,6 +1,10 @@
package sprites.shootingMissles;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
public class Sprite {
@@ -10,24 +14,21 @@ public class Sprite {
protected int width;
protected int height;
protected boolean visible;
protected String path;
protected Image image;
public Sprite(int x, int y) {
this.x = x;
this.y = y;
visible = true;
}
protected void loadImage(String imageName, int width, int height) {
path = imageName;
ImageIcon ii = new ImageIcon(imageName);
image = ii.getImage().getScaledInstance(width, height, Image.SCALE_DEFAULT);
}
protected void getImageDimensions() {
width = image.getWidth(null);
height = image.getHeight(null);
this.width = width;
this.height = height;
}
public Image getImage() {