-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathFeedMeBoard.java
More file actions
158 lines (137 loc) · 4.99 KB
/
FeedMeBoard.java
File metadata and controls
158 lines (137 loc) · 4.99 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
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
import java.awt.*;
import java.awt.event.*;
import javax.swing.JPanel;
import javax.swing.Timer;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import java.util.ArrayList;
import java.util.Random;
public class FeedMeBoard extends JPanel implements ActionListener
{
private Timer timer; // a timer to handle events
private FeedMeSprite monster; // the dog that eats everything
private FeedMeDrop dropper; // manages when food items drop from sky
private Image bkgrd; // background image
private int score; // keeps track of game score
private boolean happy; // changes animation of monster after eating
private boolean gameOver; // keeps track of when game is over
public FeedMeBoard()
{
// setup basic GUI layout
setLayout( null );
setFocusable( true );
setDoubleBuffered( true );
// apply my own event handler
MyKeyAdapter myListener = new MyKeyAdapter();
addKeyListener( myListener );
// setup background
ImageIcon ii = new ImageIcon( getClass().getResource( "sky.png" ) );
bkgrd = ii.getImage();
// initialize the game pieces
gameOver = false;
happy = false;
score = 0;
dropper = new FeedMeDrop( 3 ); // itms fall at 3 seconds apart
monster = new FeedMeSprite(); // create monster eater
timer = new Timer( 5, this ); // 5ms delay, "this" as the listener object
timer.setInitialDelay( 5 ); // 5ms initial delay before timer starts
timer.setDelay( 3 ); // 3ms delay between checks
timer.start();
}
// all the drawing is done in this method
public void paint( Graphics g )
{
// setup basic panel for drawing
super.paint( g );
Graphics2D g2d = ( Graphics2D )g;
// do drawings
g2d.drawImage( bkgrd, 0, 0, null );
// draw the corresponding monster image depending on whether he's happy or not
if( monster.getHappy() )
g2d.drawImage( monster.getHappyImage(), monster.getX(), monster.getY(), this );
else
g2d.drawImage( monster.getImage(), monster.getX(), monster.getY(), this );
// draw each food item at given locations
ArrayList food = dropper.getTargets();
for( int i=0; i<food.size(); i++ )
{
FeedMeApple a = ( FeedMeApple )food.get( i );
g2d.drawImage( a.getImage(), a.getX(), a.getY(), this );
}
// change to ending scene if game is over
// ending scene will display credits, game score, and stops the music
if( gameOver )
{
g2d.setColor( Color.WHITE );
g2d.setFont( new Font( "Comic Sans MS", Font.BOLD, 48 ));
g2d.drawString( "Game Over", 370, 350 );
g2d.drawString( "Your Score: " + score + " / " + dropper.getMax(), 280, 400 );
g2d.setColor( Color.GRAY );
g2d.setFont( new Font( "Comic Sans MS", Font.BOLD, 24 ));
g2d.drawString( "Game made by Bowen Hui", 20, 20 );
g2d.drawString( "Music from Kevin MacLeod", 20, 50 );
g2d.drawString( "Sound effects from MediaCollege.com", 20, 80 );
SoundEffect.SONG.stoploop();
}
}
// animate sprite according to the timer event
// execute this method everytime an event is detected
public void actionPerformed( ActionEvent e )
{
monster.move(); // move monster accordingly
// remove item from arraylist if not visible, else let it fall
ArrayList food = dropper.getTargets();
for( int i=0; i<food.size(); i++ )
{
FeedMeApple a = ( FeedMeApple )food.get( i );
if( a.isVisible() )
a.move();
else
food.remove( i );
}
checkCollision(); // check if monster collides with food item
checkGameOver(); // check if game is over
repaint(); // effectively, calls paint()
}
// if nothing is left to drop then game is over
private void checkGameOver()
{
ArrayList food = dropper.getTargets();
if( food.size() == 0 )
gameOver = true;
}
// if the bounding boxes of two items intersect, then there is a collision
private void checkCollision()
{
// get bounding box of monster
Rectangle r1 = monster.getBounds();
// get bounding box of all possible food items
ArrayList food = dropper.getTargets();
for( int i=0; i<food.size(); i++ )
{
FeedMeApple a = ( FeedMeApple )food.get( i );
Rectangle r2 = a.getBounds();
// checks intersection - if so, monster eats food item
if( r1.intersects( r2 ) )
{
a.setVisible( false );
SoundEffect.EAT.play();
monster.setHappy( true );
score++;
}
}
}
// my own listener to handle keyboard events
// the sprite will animate according to the event given
private class MyKeyAdapter extends KeyAdapter
{
public void keyReleased( KeyEvent e )
{
monster.keyReleased( e );
}
public void keyPressed( KeyEvent e )
{
monster.keyPressed( e );
}
}
}