-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSpaceInvaders.java
More file actions
61 lines (52 loc) · 1.53 KB
/
SpaceInvaders.java
File metadata and controls
61 lines (52 loc) · 1.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
// Noor Ansari
import java.util.Random;
public class SpaceInvaders {
private int playerX = 4;
private int enemyX = randomX();
private int enemyY = 0;
private int bulletX = 0, bulletY = 0;
private boolean bulletActive = false;
private boolean gameOver = false;
private static int randomX() {
return new Random().nextInt(5) + 1;
}
public int getPlayerX() { return playerX; }
public int getEnemyX() { return enemyX; }
public int getEnemyY() { return enemyY; }
public int getBulletX() { return bulletX; }
public int getBulletY() { return bulletY; }
public boolean isBulletActive() { return bulletActive; }
public boolean isGameOver() { return gameOver; }
public void movePlayerLeft() {
if (playerX > 0) playerX--;
}
public void movePlayerRight() {
if (playerX < 7) playerX++;
}
public void fireBullet() {
if (!bulletActive) {
bulletX = playerX;
bulletY = 6;
bulletActive = true;
}
}
public void updateBullet() {
if (bulletActive) {
bulletY--;
if (bulletY < 0) {
bulletActive = false;
} else if (bulletX == enemyX && bulletY == enemyY) {
// hit
enemyX = randomX();
enemyY = 0;
bulletActive = false;
}
}
}
public void updateEnemy() {
enemyY++;
if (enemyY == 7 && enemyX == playerX) {
gameOver = true;
}
}
}