-
Notifications
You must be signed in to change notification settings - Fork 0
/
adhocBall
93 lines (78 loc) · 1.93 KB
/
adhocBall
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
import java.awt.*;
/**
* Created by g1429087 on 23.06.15.
*/
public class Ball {
private int x;
private int y;
private Color color;
private int size;
private float speed;
private int direction_x;
private int direction_y;
private Aufgabe5 controller;
public Ball( Aufgabe5 controller, int size, Color color, float speed) {
this.size=size;
this.color=color;
this.speed=speed;
this.x=(int)(Math.random()*Aufgabe5.SIZE_X);
this.y=(int)(Math.random()*Aufgabe5.SIZE_Y);
this.direction_x=1;
this.direction_y=1;
this.controller=controller;
}
public void moveBall() {
checkCollision();
x = (int)(x + 1*speed*direction_x);
y = (int)(y + 1*speed*direction_y);
}
/**
* Checks collision with wall and changes direction of ball.
*/
private void checkCollision() {
if( Aufgabe5.isCollided(this, controller.getBalls())) {
if( Math.random()*10>5 ) direction_y*=-1;
if( Math.random()*11>6 ) direction_x*=-1;
}
if( this.x<0 ) {
direction_x = 1;
}else if( this.x>Aufgabe5.SIZE_X ) {
direction_x=-1;
}
if( this.y < 0) {
direction_y=1;
}else if( this.y>Aufgabe5.SIZE_Y) {
direction_y=-1;
}
}
public int getX() {
return x;
}
public void setX(int x) {
this.x = x;
}
public int getY() {
return y;
}
public void setY(int y) {
this.y = y;
}
public Color getColor() {
return color;
}
public void setColor(Color color) {
this.color = color;
}
public int getSize() {
return size;
}
public void setSize(int size) {
this.size = size;
}
public float getSpeed() {
return speed;
}
public void setSpeed(float speed) {
this.speed = speed;
}
}