learning about colision detection
This commit is contained in:
6
.idea/inspectionProfiles/Project_Default.xml
generated
Normal file
6
.idea/inspectionProfiles/Project_Default.xml
generated
Normal file
@@ -0,0 +1,6 @@
|
||||
<component name="InspectionProjectProfileManager">
|
||||
<profile version="1.0">
|
||||
<option name="myName" value="Project Default" />
|
||||
<inspection_tool class="SuspiciousListRemoveInLoop" enabled="false" level="WARNING" enabled_by_default="false" />
|
||||
</profile>
|
||||
</component>
|
||||
BIN
DemoProjects/res/laser.png
Normal file
BIN
DemoProjects/res/laser.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 1.6 KiB |
BIN
DemoProjects/res/spaceship.png
Normal file
BIN
DemoProjects/res/spaceship.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 1.3 KiB |
@@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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) {
|
||||
|
||||
@@ -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() {
|
||||
|
||||
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Reference in New Issue
Block a user