Game-1,2,3: Added comments
This commit is contained in:
Binary file not shown.
|
Before Width: | Height: | Size: 9.7 KiB After Width: | Height: | Size: 269 B |
Binary file not shown.
|
Before Width: | Height: | Size: 12 KiB After Width: | Height: | Size: 738 B |
@@ -2,6 +2,7 @@
|
||||
|
||||
package Game;
|
||||
|
||||
//Inherit some methods shared with class SpaceShip
|
||||
public class Missile extends Sprite {
|
||||
|
||||
private final int BOARD_WIDTH = 800;
|
||||
|
||||
@@ -6,6 +6,7 @@ import java.awt.event.KeyEvent;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
// Inherit some methods shared with class Missile
|
||||
public class SpaceShip extends Sprite {
|
||||
|
||||
private int dx;
|
||||
@@ -19,7 +20,6 @@ public class SpaceShip extends Sprite {
|
||||
}
|
||||
|
||||
private void initSpaceShip() {
|
||||
|
||||
missiles = new ArrayList<>();
|
||||
|
||||
loadImage("src/resources/spaceship.png");
|
||||
@@ -36,7 +36,6 @@ public class SpaceShip extends Sprite {
|
||||
}
|
||||
|
||||
public void keyPressed(KeyEvent e) {
|
||||
|
||||
int key = e.getKeyCode();
|
||||
|
||||
if (key == KeyEvent.VK_SPACE) {
|
||||
@@ -65,7 +64,6 @@ public class SpaceShip extends Sprite {
|
||||
}
|
||||
|
||||
public void keyReleased(KeyEvent e) {
|
||||
|
||||
int key = e.getKeyCode();
|
||||
|
||||
if (key == KeyEvent.VK_LEFT) {
|
||||
|
||||
@@ -1,5 +1,11 @@
|
||||
// Origin: https://zetcode.com/javagames/movingsprites/
|
||||
|
||||
// SpaceShip and Missile have some methods in common.
|
||||
// We can just as well have them inherit those methods
|
||||
|
||||
// The protected modifier specifies that the member can only be accessed within its own package
|
||||
// The private modifier specifies that the member can only be accessed within its own class.
|
||||
|
||||
package Game;
|
||||
|
||||
import java.awt.Image;
|
||||
@@ -14,21 +20,19 @@ public class Sprite {
|
||||
protected boolean visible;
|
||||
protected Image image;
|
||||
|
||||
// Set the initial position
|
||||
public Sprite(int x, int y) {
|
||||
|
||||
this.x = x;
|
||||
this.y = y;
|
||||
visible = true;
|
||||
}
|
||||
|
||||
protected void loadImage(String imageName) {
|
||||
|
||||
ImageIcon ii = new ImageIcon(imageName);
|
||||
image = ii.getImage();
|
||||
}
|
||||
|
||||
protected void getImageDimensions() {
|
||||
|
||||
width = image.getWidth(null);
|
||||
height = image.getHeight(null);
|
||||
}
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
|
||||
package Game;
|
||||
|
||||
//Inherit some methods shared with class SpaceShip
|
||||
public class Missile extends Sprite {
|
||||
|
||||
private final int BOARD_WIDTH = 800;
|
||||
|
||||
@@ -6,6 +6,7 @@ import java.awt.event.KeyEvent;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
//Inherit some methods shared with class Missile
|
||||
public class SpaceShip extends Sprite {
|
||||
|
||||
private int dx;
|
||||
@@ -19,7 +20,6 @@ public class SpaceShip extends Sprite {
|
||||
}
|
||||
|
||||
private void initSpaceShip() {
|
||||
|
||||
missiles = new ArrayList<>();
|
||||
|
||||
loadImage("src/resources/spaceship.png");
|
||||
@@ -36,7 +36,6 @@ public class SpaceShip extends Sprite {
|
||||
}
|
||||
|
||||
public void keyPressed(KeyEvent e) {
|
||||
|
||||
int key = e.getKeyCode();
|
||||
|
||||
if (key == KeyEvent.VK_SPACE) {
|
||||
@@ -65,7 +64,6 @@ public class SpaceShip extends Sprite {
|
||||
}
|
||||
|
||||
public void keyReleased(KeyEvent e) {
|
||||
|
||||
int key = e.getKeyCode();
|
||||
|
||||
if (key == KeyEvent.VK_LEFT) {
|
||||
|
||||
@@ -1,5 +1,11 @@
|
||||
// Origin: https://zetcode.com/javagames/movingsprites/
|
||||
|
||||
// SpaceShip and Missile have some methods in common.
|
||||
// We can just as well have them inherit those methods
|
||||
|
||||
// The protected modifier specifies that the member can only be accessed within its own package
|
||||
// The private modifier specifies that the member can only be accessed within its own class.
|
||||
|
||||
package Game;
|
||||
|
||||
import java.awt.Image;
|
||||
|
||||
Reference in New Issue
Block a user