-
Notifications
You must be signed in to change notification settings - Fork 0
/
GameCourt.java
144 lines (122 loc) · 3.86 KB
/
GameCourt.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
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
/**
* CIS 120 HW10
* (c) University of Pennsylvania
* @version 2.0, Mar 2013
*/
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.ArrayList;
/**
* GameCourt
*
* This class holds the primary game logic for how different objects interact
* with one another. Take time to understand how the timer interacts with the
* different methods and how it repaints the GUI on every tick().
*
*/
@SuppressWarnings("serial")
public class GameCourt extends JPanel {
// the state of the game logic
private Square square; // the Black Square, keyboard control
private Circle snitch; // the Golden Snitch, bounces
private Poison poison; // the Poison Mushroom, doesn't move
public boolean playing = false; // whether the game is running
private JLabel status; // Current status text (i.e. Running...)
// Game constants
public static final int COURT_WIDTH = 300;
public static final int COURT_HEIGHT = 300;
public static final int SQUARE_VELOCITY = 4;
// Update interval for timer, in milliseconds
public static final int INTERVAL = 35;
public GameCourt(JLabel status) {
// creates border around the court area, JComponent method
setBorder(BorderFactory.createLineBorder(Color.BLACK));
// The timer is an object which triggers an action periodically
// with the given INTERVAL. One registers an ActionListener with
// this timer, whose actionPerformed() method will be called
// each time the timer triggers. We define a helper method
// called tick() that actually does everything that should
// be done in a single timestep.
Timer timer = new Timer(INTERVAL, new ActionListener() {
public void actionPerformed(ActionEvent e) {
tick();
}
});
timer.start(); // MAKE SURE TO START THE TIMER!
// Enable keyboard focus on the court area.
// When this component has the keyboard focus, key
// events will be handled by its key listener.
setFocusable(true);
// This key listener allows the square to move as long
// as an arrow key is pressed, by changing the square's
// velocity accordingly. (The tick method below actually
// moves the square.)
addKeyListener(new KeyAdapter() {
public void keyPressed(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_LEFT)
square.v_x = -SQUARE_VELOCITY;
else if (e.getKeyCode() == KeyEvent.VK_RIGHT)
square.v_x = SQUARE_VELOCITY;
else if (e.getKeyCode() == KeyEvent.VK_DOWN)
square.v_y = SQUARE_VELOCITY;
else if (e.getKeyCode() == KeyEvent.VK_UP)
square.v_y = -SQUARE_VELOCITY;
}
public void keyReleased(KeyEvent e) {
square.v_x = 0;
square.v_y = 0;
}
});
this.status = status;
}
/**
* (Re-)set the game to its initial state.
*/
public void reset() {
square = new Square(COURT_WIDTH, COURT_HEIGHT);
poison = new Poison(COURT_WIDTH, COURT_HEIGHT);
snitch = new Circle(COURT_WIDTH, COURT_HEIGHT);
playing = true;
status.setText("Running...");
// Make sure that this component has the keyboard focus
requestFocusInWindow();
}
/**
* This method is called every time the timer defined in the constructor
* triggers.
*/
void tick() {
if (playing) {
// advance the square and snitch in their
// current direction.
square.move();
snitch.move();
// make the snitch bounce off walls...
snitch.bounce(snitch.hitWall());
// ...and the mushroom
snitch.bounce(snitch.hitObj(poison));
// check for the game end conditions
if (square.intersects(poison)) {
playing = false;
status.setText("You lose!");
} else if (square.intersects(snitch)) {
playing = false;
status.setText("You win!");
}
// update the display
repaint();
}
}
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
square.draw(g);
poison.draw(g);
snitch.draw(g);
}
@Override
public Dimension getPreferredSize() {
return new Dimension(COURT_WIDTH, COURT_HEIGHT);
}
}