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;
|
package Game;
|
||||||
|
|
||||||
|
//Inherit some methods shared with class SpaceShip
|
||||||
public class Missile extends Sprite {
|
public class Missile extends Sprite {
|
||||||
|
|
||||||
private final int BOARD_WIDTH = 800;
|
private final int BOARD_WIDTH = 800;
|
||||||
|
|||||||
@@ -6,6 +6,7 @@ import java.awt.event.KeyEvent;
|
|||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
|
// Inherit some methods shared with class Missile
|
||||||
public class SpaceShip extends Sprite {
|
public class SpaceShip extends Sprite {
|
||||||
|
|
||||||
private int dx;
|
private int dx;
|
||||||
@@ -19,7 +20,6 @@ public class SpaceShip extends Sprite {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private void initSpaceShip() {
|
private void initSpaceShip() {
|
||||||
|
|
||||||
missiles = new ArrayList<>();
|
missiles = new ArrayList<>();
|
||||||
|
|
||||||
loadImage("src/resources/spaceship.png");
|
loadImage("src/resources/spaceship.png");
|
||||||
@@ -36,7 +36,6 @@ public class SpaceShip extends Sprite {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public void keyPressed(KeyEvent e) {
|
public void keyPressed(KeyEvent e) {
|
||||||
|
|
||||||
int key = e.getKeyCode();
|
int key = e.getKeyCode();
|
||||||
|
|
||||||
if (key == KeyEvent.VK_SPACE) {
|
if (key == KeyEvent.VK_SPACE) {
|
||||||
@@ -65,7 +64,6 @@ public class SpaceShip extends Sprite {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public void keyReleased(KeyEvent e) {
|
public void keyReleased(KeyEvent e) {
|
||||||
|
|
||||||
int key = e.getKeyCode();
|
int key = e.getKeyCode();
|
||||||
|
|
||||||
if (key == KeyEvent.VK_LEFT) {
|
if (key == KeyEvent.VK_LEFT) {
|
||||||
|
|||||||
@@ -1,5 +1,11 @@
|
|||||||
// Origin: https://zetcode.com/javagames/movingsprites/
|
// 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;
|
package Game;
|
||||||
|
|
||||||
import java.awt.Image;
|
import java.awt.Image;
|
||||||
@@ -14,21 +20,19 @@ public class Sprite {
|
|||||||
protected boolean visible;
|
protected boolean visible;
|
||||||
protected Image image;
|
protected Image image;
|
||||||
|
|
||||||
|
// Set the initial position
|
||||||
public Sprite(int x, int y) {
|
public Sprite(int x, int y) {
|
||||||
|
|
||||||
this.x = x;
|
this.x = x;
|
||||||
this.y = y;
|
this.y = y;
|
||||||
visible = true;
|
visible = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
protected void loadImage(String imageName) {
|
protected void loadImage(String imageName) {
|
||||||
|
|
||||||
ImageIcon ii = new ImageIcon(imageName);
|
ImageIcon ii = new ImageIcon(imageName);
|
||||||
image = ii.getImage();
|
image = ii.getImage();
|
||||||
}
|
}
|
||||||
|
|
||||||
protected void getImageDimensions() {
|
protected void getImageDimensions() {
|
||||||
|
|
||||||
width = image.getWidth(null);
|
width = image.getWidth(null);
|
||||||
height = image.getHeight(null);
|
height = image.getHeight(null);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,6 +2,7 @@
|
|||||||
|
|
||||||
package Game;
|
package Game;
|
||||||
|
|
||||||
|
//Inherit some methods shared with class SpaceShip
|
||||||
public class Missile extends Sprite {
|
public class Missile extends Sprite {
|
||||||
|
|
||||||
private final int BOARD_WIDTH = 800;
|
private final int BOARD_WIDTH = 800;
|
||||||
|
|||||||
@@ -6,6 +6,7 @@ import java.awt.event.KeyEvent;
|
|||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
|
//Inherit some methods shared with class Missile
|
||||||
public class SpaceShip extends Sprite {
|
public class SpaceShip extends Sprite {
|
||||||
|
|
||||||
private int dx;
|
private int dx;
|
||||||
@@ -19,7 +20,6 @@ public class SpaceShip extends Sprite {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private void initSpaceShip() {
|
private void initSpaceShip() {
|
||||||
|
|
||||||
missiles = new ArrayList<>();
|
missiles = new ArrayList<>();
|
||||||
|
|
||||||
loadImage("src/resources/spaceship.png");
|
loadImage("src/resources/spaceship.png");
|
||||||
@@ -36,7 +36,6 @@ public class SpaceShip extends Sprite {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public void keyPressed(KeyEvent e) {
|
public void keyPressed(KeyEvent e) {
|
||||||
|
|
||||||
int key = e.getKeyCode();
|
int key = e.getKeyCode();
|
||||||
|
|
||||||
if (key == KeyEvent.VK_SPACE) {
|
if (key == KeyEvent.VK_SPACE) {
|
||||||
@@ -65,7 +64,6 @@ public class SpaceShip extends Sprite {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public void keyReleased(KeyEvent e) {
|
public void keyReleased(KeyEvent e) {
|
||||||
|
|
||||||
int key = e.getKeyCode();
|
int key = e.getKeyCode();
|
||||||
|
|
||||||
if (key == KeyEvent.VK_LEFT) {
|
if (key == KeyEvent.VK_LEFT) {
|
||||||
|
|||||||
@@ -1,5 +1,11 @@
|
|||||||
// Origin: https://zetcode.com/javagames/movingsprites/
|
// 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;
|
package Game;
|
||||||
|
|
||||||
import java.awt.Image;
|
import java.awt.Image;
|
||||||
|
|||||||
Reference in New Issue
Block a user