-
Notifications
You must be signed in to change notification settings - Fork 0
/
Computer.java
58 lines (46 loc) · 1.09 KB
/
Computer.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
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package pong;
import java.awt.Color;
import java.awt.Graphics;
/**
*
* @author Max
*/
public class Computer {
private GamePanel field;
private int y = Pong.WINDOW_HEIGHT / 2;
private int yVelocity = 0;
private int width = 10;
private int height = 40;
public Computer(GamePanel game) {
this.field = game;
}
public void update() {
if (field.getBall().getY() < this.y) {
//ball is above the computer
yVelocity = -3;
} else if (field.getBall().getY() > this.y) {
yVelocity = 3;
}
y = y + yVelocity;
}
public void paint(Graphics g) {
g.setColor(Color.red);
g.fillRect(Pong.WINDOW_WIDTH - (35 + width), y, width, height);
}
public int getX() {
return Pong.WINDOW_WIDTH - 6 - (35 + width);
}
public int getY() {
return y;
}
public int getWidth() {
return width;
}
public int getHeight() {
return height;
}
}