-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathBrick.java
78 lines (69 loc) · 1.78 KB
/
Brick.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
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.awt.Color;
/**
* A brick sits there looking colorful until the ball breaks it,
* adding the value of the brick to the user's score.
*
* @author Cody A. Ray
* @version December 8, 2012
*/
public class Brick extends Actor
{
static final int LENGTH = 50;
static final int HEIGHT = 20;
private final Color color;
private final int value;
/**
* Create a new brick with the given color and value
* (points added to the user's score when broken).
*
* @param color the color of the brick
* @param value the value to the user's score
*/
public Brick(Color color, int value)
{
this.color = color;
this.value = value;
// create brick image
GreenfootImage image = new GreenfootImage(LENGTH, HEIGHT);
image.setColor(color);
image.fill();
// add borders
image.setColor(Color.BLACK);
image.drawRect(0, 0, LENGTH, HEIGHT);
setImage(image);
}
/**
* Copy constructor creates a new brick with the
* same color and value as the given one.
*/
public Brick(Brick brick)
{
this(brick.color, brick.value);
}
/**
* Bricks don't do nothin'
*/
public void act()
{
// Nothing to do
}
/**
* A hit breaks the brick, removing it from the jail
* and adding its value to the user's score.
*/
public void hit()
{
Jail jail = (Jail) getWorld();
jail.addScore(value);
jail.removeObject(this);
}
/**
* Returns the value to be added to the user's score when broken.
* @return the brick's value
*/
public int getValue()
{
return value;
}
}