-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGRectangle.java
82 lines (61 loc) · 1.16 KB
/
GRectangle.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
import java.awt.Rectangle;
import java.awt.*;
import java.awt.event.*;
public class GRectangle extends Rectangle {
// public int x, y, WIDTH, HEIGHT;
int x, y, w, h;
int dx, dy;
boolean goUp;
boolean goDown;
int JUMP_HEIGHT = 90;
int GROUND_LEVEL;
public GRectangle(int x, int y, int w, int h) {
super(x, y, w, h);
this.x = x;
this.y = y;
this.w = w;
this.h = h;
GROUND_LEVEL = y;
dx = 2;
dy = 3;
}
public void keyPressed(KeyEvent e) {
int key = e.getKeyCode();
jump(key);
/*if (key == KeyEvent.VK_LEFT) {
System.out.println("Got left");
x -= dx;
}
if (key == KeyEvent.VK_RIGHT) {
System.out.println("Got right");
x += dx;
}*/
}
public void keyReleased(KeyEvent e) {
}
public void draw(Graphics g) {
if (goUp) {
y -= dy;
}
if (goDown) {
y += dy;
}
//g.clearRect(0, 0, 640, 480);
g.setColor(Color.RED);
g.fillRect(x, y, w, h);
if (GROUND_LEVEL - y >= JUMP_HEIGHT) {
goUp = false;
goDown = true;
}
if ( y >= GROUND_LEVEL) {
goDown = false;
}
}
void jump(int key) {
if (!goUp && !goDown) {
if (key == KeyEvent.VK_UP) {
goUp = true;
}
}
}
}