-
Notifications
You must be signed in to change notification settings - Fork 1
/
AnimationApp.java
147 lines (121 loc) · 4.37 KB
/
AnimationApp.java
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
import java.util.Scanner;
import java.awt.Rectangle;
import java.util.ArrayList;
/**
* Original text-based version of the game.
*/
public class AnimationApp {
static ArrayList<Enemy> enemies = new ArrayList<Enemy>();
/**
* Method that controls how the enemy moves. The enemy is set to move in a
* specific pattern. The enemy will move to the right until it hits an x
* coordinate of 40. Afterwards, it will move down and then to the left,
* continuing this pattern until it collides with the player. The enemy's
* coordinates will print out after each user input.
*
* @param enemy
*/
public static void enemyMovement(Enemy enemy) {
if (enemy.getEnemyMovementRight() == true) {
if (enemy.getX_Coordinate() == (40)) {
// if the enemy is against the right wall, it will move down and move left
enemy.moveDown(5);
enemy.setEnemyMovementRight(false);
System.out.println("Enemy position : " + enemy.getX_Coordinate() + " " + enemy.getY_Coordinate());
}
else {
enemy.moveRight(5);
System.out.println("Enemy position : " + enemy.getX_Coordinate() + " " + enemy.getY_Coordinate());
}
}
else {
if (enemy.getX_Coordinate() == (-40)) {
// if the enemy is against the left wall, it will move down and move right
enemy.moveDown(5);
enemy.setEnemyMovementRight(true);
System.out.println("Enemy position : " + enemy.getX_Coordinate() + " " + enemy.getY_Coordinate());
} else {
enemy.moveLeft(5);
System.out.println("Enemy position : " + enemy.getX_Coordinate() + " " + enemy.getY_Coordinate());
}
}
}
/**
* Method that detects when the player and the enemy's rectangles have
* intersected, causing the game to end.
*
* @param player
* @param enemy
*/
public static boolean collisionDetection(Player player, Enemy enemy) {
boolean collision = false;
Rectangle r1 = player.getUnitHitBox();
Rectangle r2 = enemy.getUnitHitBox();
if (r1.intersects(r2))
collision = true;
return collision;
}
public static void main(String[] args) {
Player player1 = new Player(0, 0, 5, 5);
Enemy enemy1 = new Enemy(-40, 25, 5, 5);
Enemy enemy2 = new Enemy(-30, 25, 5, 5);
Enemy enemy3 = new Enemy(-20, 25, 5, 5);
Enemy enemy4 = new Enemy(-10, 25, 5, 5);
Enemy enemy5 = new Enemy(0, 25, 5, 5);
enemies.add(enemy1);
enemies.add(enemy2);
enemies.add(enemy3);
enemies.add(enemy4);
enemies.add(enemy5);
boolean enemy_movement_right = true;
Scanner keyboard = new Scanner(System.in);
System.out.println("Player Position: " + player1.getX_Coordinate() + ", " + player1.getY_Coordinate());
for (int i = 0; i < enemies.size(); i++)
System.out.println("Enemy " + i + " position : " + enemies.get(i).getX_Coordinate() + ", "
+ enemies.get(i).getY_Coordinate());
while (player1.getLive() == true) {
System.out.println("Press the 'a' key to move left. Press the 'd' key to move right.");
char keyEntered = keyboard.next().charAt(0);
if (keyEntered == 'a') {
if (player1.getX_Coordinate() == (-50)) {
// sets a limit to how far the player can move left
System.out.println("Sorry, you can't move any farther left.");
System.out.println(
"Player Position: " + player1.getX_Coordinate() + ", " + player1.getY_Coordinate());
}
else {
player1.moveLeft(5);
System.out.println(
"Player Position: " + player1.getX_Coordinate() + ", " + player1.getY_Coordinate());
}
}
if (keyEntered == 'd') {
if (player1.getX_Coordinate() == 50) {
// sets a limit to how far the player can move right.
System.out.println("Sorry, you can't move any farther right.");
System.out.println(
"Player Position: " + player1.getX_Coordinate() + ", " + player1.getY_Coordinate());
}
else {
player1.moveRight(5);
System.out.println(
"Player Position: " + player1.getX_Coordinate() + ", " + player1.getY_Coordinate());
}
}
for (int i = 0; i < enemies.size(); i++)
enemyMovement(enemies.get(i));
for (int i = 0; i < enemies.size(); i++)
if (collisionDetection(player1, enemies.get(i)) == true) {
// the game's end condition where the enemy and the player collide.
player1.setLive(false);
System.out.println("GAME OVER");
}
for (int i = 0; i < enemies.size(); i++) {
if ((enemies.get(i).getY_Coordinate() == 0)) {
player1.setLive(false);
System.out.println("GAME OVER");
}
}
}
}
}