-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathFeedMeApple.java
More file actions
58 lines (49 loc) · 1.56 KB
/
FeedMeApple.java
File metadata and controls
58 lines (49 loc) · 1.56 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
import java.awt.*;
import javax.swing.*;
public class FeedMeApple
{
private int x, y; // locations of apple
private Image myImage; // my image
private boolean visible; // my visibility
private int width, height; // my bounding box dimensions
// constants
private final int BOARD_HEIGHT = 800; // game board dimension
private final int FALL_SPEED = 2; // number of pixels to fall each time
// constructor
public FeedMeApple( int xpos, int ypos )
{
// initialize private variables
ImageIcon ii = new ImageIcon( getClass().getResource( "apple.png" ) );
myImage = ii.getImage();
width = myImage.getWidth( null );
height = myImage.getHeight( null );
visible = true;
x = xpos;
y = ypos;
}
// accessors
public Image getImage() { return myImage; }
public int getX() { return x; }
public int getY() { return y; }
public boolean isVisible() { return visible; }
// returns the bounding box of the item
public Rectangle getBounds()
{
Rectangle box = new Rectangle( x, y, width, height );
return box;
}
// mutator
// if collision happens, the apple disappears
public void setVisible( boolean val )
{
visible = val;
}
// apple falls at a constant speed from top to bottom of board
public void move()
{
y = y + FALL_SPEED;
// when it moves beyond the board, it becomes invisible
if( y > BOARD_HEIGHT )
visible = false;
}
}