-
Notifications
You must be signed in to change notification settings - Fork 0
/
Level2.java
95 lines (75 loc) · 2.62 KB
/
Level2.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
/* Level2 class introduces special feature of "rock climbing". Player gains the ability to scale tall walls for this level.
* Main message: Pink block is undeterred by challenges to get money.
*/
import java.awt.*;
import java.awt.event.*;
public class Level2 extends Level {
private static final int SPEED = 3; // velocity of player when moving horizontally
private Character m;
// Constructor method, initializes all characters and blocks
public Level2(Panel panel) {
this.panel = panel;
hasWon = false;
int panelW = Panel.W;
int panelH = Panel.H;
// Initializes charcters
c = new Character(50, panelH - 150, CustomColor.PINK);
m = new Money(panelW - 70, panelH - 150, panel);
// Create blocks for the floor
createRectOfBlocks(6, 15, panelW / 2 + 175, 100);
createRectOfBlocks(6, 12, panelW / 2 - 325, 200);
createRectOfBlocks(70, 5, 0, panelH - 120);
}
// Resets all character locations back to initial coordinates
void resetLevel() {
int panelW = Panel.W;
int panelH = Panel.H;
c = new Character(50, panelH - 150, CustomColor.PINK);
m = new Money(panelW - 70, panelH - 150, panel);
}
// Draws all blocks and characters onto the panel
@Override
public void draw(Graphics g) {
// Draw font
g.setColor(Color.BLACK);
g.setFont(new Font("Monospaced", Font.ITALIC, 20));
g.drawString("Climb any challenge...", 350, 100);
// Draw the characters
m.draw(g);
c.draw(g);
// Draw the floor blocks
for (Block b : blocks) {
b.draw(g);
}
}
// Allows player to double jump (rock climb) if they press the right/left and up
// arrow keys
@Override
public void keyPressed(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_LEFT) {
c.xVelocity = SPEED * -1;
}
else if (e.getKeyCode() == KeyEvent.VK_RIGHT) {
c.xVelocity = SPEED;
}
else if (e.getKeyCode() == KeyEvent.VK_UP) {
if (!c.isFalling) {
c.yVelocity = -9.8;
c.isFalling = true;
}
else if (c.isFalling && c.willIntersectX(blocks)) {
c.yVelocity = -9.8;
c.isFalling = true;
}
}
}
// Specifies end level conditions
// If the player touches the money block, start Level 3
@Override
protected void checkWin() {
if (c.intersects(m) && !hasWon) {
panel.nextLevel(new Level3(panel));
hasWon = true;
}
}
}