forked from StRobertCHSCS/ICS4U-PCP
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBall
More file actions
66 lines (51 loc) · 1.36 KB
/
Ball
File metadata and controls
66 lines (51 loc) · 1.36 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
package com.example.luimi.breakout;
import android.graphics.RectF;
import java.util.Random;
public class Ball {
RectF rect;
float xVelocity;
float yVelocity;
float ballWidth = 10;
float ballHeight = 10;
public Ball(int screenX, int screenY){
xVelocity = 200;
yVelocity = -400;
rect = new RectF();
}
public RectF getRect(){
return rect;
}
public void update(long fps){
rect.left = rect.left + (xVelocity / fps);
rect.top = rect.top + (yVelocity / fps);
rect.right = rect.left + ballWidth;
rect.bottom = rect.top - ballHeight;
}
public void reverseYVelocity(){
yVelocity = -yVelocity;
}
public void reverseXVelocity(){
xVelocity = - xVelocity;
}
public void setRandomXVelocity(){
Random generator = new Random();
int answer = generator.nextInt(2);
if(answer == 0){
reverseXVelocity();
}
}
public void clearObstacleY(float y){
rect.bottom = y;
rect.top = y - ballHeight;
}
public void clearObstacleX(float x){
rect.left = x;
rect.right = x + ballWidth;
}
public void reset(int x, int y){
rect.left = x / 2;
rect.top = y - 20;
rect.right = x / 2 + ballWidth;
rect.bottom = y - 20 - ballHeight;
}
}