-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPuddle.java
More file actions
69 lines (56 loc) · 1.51 KB
/
Puddle.java
File metadata and controls
69 lines (56 loc) · 1.51 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
62
63
64
65
66
67
68
//package bricks;
import java.awt.Image;
import java.awt.Rectangle;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
/* Игровая лопатка */
class Puddle extends MovableSprite implements KeyListener {
static final int LEFT = 37;
static final int RIGHT = 39;
static final int alpha = 10;
public Puddle(PlayField p, Image pic) {
super(p, pic, new Rectangle(p.getWidth() / 2, p.getHeight() - 20, pic.getWidth(p), pic.getHeight(p)), 0, 10);
_pf.addKeyListener(this);
}
public void move() {
if (_isMoving) {
Rectangle b = _pf.getBoundary();
_pos.x += _v.getSpeedX();
if (_pos.x < b.x)
_pos.x = b.x;
else if (_pos.x + _pos.width > b.x + b.width)
_pos.x = b.x + b.width - _pos.width;
}
}
public void hitBy(Puck p) {
if ( p.getDirection() == 90 ) {
p.setDirection(270 + alpha);
} else {
int px = p.getBounds().x + p.getBounds().width/2;
int l = (int) (_pos.x +_pos.width*(1.0/3));
int r = (int) (_pos.x +_pos.width*(2.0/3));
if ( px < l || px > r ) {
p.getVelocity().reverse();
} else {
p.getVelocity().reverseY();
}
}
}
/* Обработка нажатия клавиши */
public void keyPressed(KeyEvent e) {
if (e.getKeyCode() == LEFT) {
startMoving();
_v.setDirection(180);
} else if (e.getKeyCode() == RIGHT) {
startMoving();
_v.setDirection(0);
}
}
/* Обработка отжатия клавиши */
public void keyReleased(KeyEvent e) {
stopMoving();
}
public void keyTyped(KeyEvent e) {
;
}
}