Pong Java Game
Pada postingan saya kali ini saya akan mencoba membuat game pong dengan bahasa Java di BlueJ IDE. Ide berikut saya kutip dari sini .
Hal yang pertama yang perlu dilakukan adalah dengan membuat sebuah package bernama Pong lalu membuat beberapa class-classnya sebagai berikut :
Hal yang pertama yang perlu dilakukan adalah dengan membuat sebuah package bernama Pong lalu membuat beberapa class-classnya sebagai berikut :
1. Pong
1: package pong;
2: import java.awt.BasicStroke;
3: import java.awt.Color;
4: import java.awt.Font;
5: import java.awt.Graphics2D;
6: import java.awt.RenderingHints;
7: import java.awt.event.ActionEvent;
8: import java.awt.event.ActionListener;
9: import java.awt.event.KeyEvent;
10: import java.awt.event.KeyListener;
11: import java.util.Random;
12: import javax.swing.JFrame;
13: import javax.swing.Timer;
14: public class Pong implements ActionListener, KeyListener
15: {
16: public static Pong pong;
17: public int width = 700, height = 700;
18: public Renderer renderer;
19: public Paddle player1;
20: public Paddle player2;
21: public Ball ball;
22: public boolean bot = false, selectingDifficulty;
23: public boolean w, s, up, down;
24: public int gameStatus = 0, scoreLimit = 7, playerWon; //0 = Menu, 1 = Paused, 2 = Playing, 3 = Over
25: public int botDifficulty, botMoves, botCooldown = 0;
26: public Random random;
27: public JFrame jframe;
28: public Pong()
29: {
30: Timer timer = new Timer(20, this);
31: random = new Random();
32: jframe = new JFrame("Pong");
33: renderer = new Renderer();
34: jframe.setSize(width + 15, height + 35);
35: jframe.setVisible(true);
36: jframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
37: jframe.add(renderer);
38: jframe.addKeyListener(this);
39: timer.start();
40: }
41: public void start()
42: {
43: gameStatus = 2;
44: player1 = new Paddle(this, 1);
45: player2 = new Paddle(this, 2);
46: ball = new Ball(this);
47: }
48: public void update()
49: {
50: if (player1.score >= scoreLimit)
51: {
52: playerWon = 1;
53: gameStatus = 3;
54: }
55: if (player2.score >= scoreLimit)
56: {
57: gameStatus = 3;
58: playerWon = 2;
59: }
60: if (w)
61: {
62: player1.move(true);
63: }
64: if (s)
65: {
66: player1.move(false);
67: }
68: if (!bot)
69: {
70: if (up)
71: {
72: player2.move(true);
73: }
74: if (down)
75: {
76: player2.move(false);
77: }
78: }
79: else
80: {
81: if (botCooldown > 0)
82: {
83: botCooldown--;
84: if (botCooldown == 0)
85: {
86: botMoves = 0;
87: }
88: }
89: if (botMoves < 10)
90: {
91: if (player2.y + player2.height / 2 < ball.y)
92: {
93: player2.move(false);
94: botMoves++;
95: }
96: if (player2.y + player2.height / 2 > ball.y)
97: {
98: player2.move(true);
99: botMoves++;
100: }
101: if (botDifficulty == 0)
102: {
103: botCooldown = 20;
104: }
105: if (botDifficulty == 1)
106: {
107: botCooldown = 15;
108: }
109: if (botDifficulty == 2)
110: {
111: botCooldown = 10;
112: }
113: }
114: }
115: ball.update(player1, player2);
116: }
117: public void render(Graphics2D g)
118: {
119: g.setColor(Color.BLACK);
120: g.fillRect(0, 0, width, height);
121: g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
122: if (gameStatus == 0)
123: {
124: g.setColor(Color.WHITE);
125: g.setFont(new Font("Arial", 1, 50));
126: g.drawString("PONG", width / 2 - 75, 50);
127: if (!selectingDifficulty)
128: {
129: g.setFont(new Font("Arial", 1, 30));
130: g.drawString("Press Space to Play", width / 2 - 150, height / 2 - 25);
131: g.drawString("Press Shift to Play with Bot", width / 2 - 200, height / 2 + 25);
132: g.drawString("<< Score Limit: " + scoreLimit + " >>", width / 2 - 150, height / 2 + 75);
133: }
134: }
135: if (selectingDifficulty)
136: {
137: String string = botDifficulty == 0 ? "Easy" : (botDifficulty == 1 ? "Medium" : "Hard");
138: g.setFont(new Font("Arial", 1, 30));
139: g.drawString("<< Bot Difficulty: " + string + " >>", width / 2 - 180, height / 2 - 25);
140: g.drawString("Press Space to Play", width / 2 - 150, height / 2 + 25);
141: }
142: if (gameStatus == 1)
143: {
144: g.setColor(Color.WHITE);
145: g.setFont(new Font("Arial", 1, 50));
146: g.drawString("PAUSED", width / 2 - 103, height / 2 - 25);
147: }
148: if (gameStatus == 1 || gameStatus == 2)
149: {
150: g.setColor(Color.WHITE);
151: g.setStroke(new BasicStroke(5f));
152: g.drawLine(width / 2, 0, width / 2, height);
153: g.setStroke(new BasicStroke(2f));
154: g.drawOval(width / 2 - 150, height / 2 - 150, 300, 300);
155: g.setFont(new Font("Arial", 1, 50));
156: g.drawString(String.valueOf(player1.score), width / 2 - 90, 50);
157: g.drawString(String.valueOf(player2.score), width / 2 + 65, 50);
158: player1.render(g);
159: player2.render(g);
160: ball.render(g);
161: }
162: if (gameStatus == 3)
163: {
164: g.setColor(Color.WHITE);
165: g.setFont(new Font("Arial", 1, 50));
166: g.drawString("PONG", width / 2 - 75, 50);
167: if (bot && playerWon == 2)
168: {
169: g.drawString("The Bot Wins!", width / 2 - 170, 200);
170: }
171: else
172: {
173: g.drawString("Player " + playerWon + " Wins!", width / 2 - 165, 200);
174: }
175: g.setFont(new Font("Arial", 1, 30));
176: g.drawString("Press Space to Play Again", width / 2 - 185, height / 2 - 25);
177: g.drawString("Press ESC for Menu", width / 2 - 140, height / 2 + 25);
178: }
179: }
180: @Override
181: public void actionPerformed(ActionEvent e)
182: {
183: if (gameStatus == 2)
184: {
185: update();
186: }
187: renderer.repaint();
188: }
189: public static void main(String[] args)
190: {
191: pong = new Pong();
192: }
193: @Override
194: public void keyPressed(KeyEvent e)
195: {
196: int id = e.getKeyCode();
197: if (id == KeyEvent.VK_W)
198: {
199: w = true;
200: }
201: else if (id == KeyEvent.VK_S)
202: {
203: s = true;
204: }
205: else if (id == KeyEvent.VK_UP)
206: {
207: up = true;
208: }
209: else if (id == KeyEvent.VK_DOWN)
210: {
211: down = true;
212: }
213: else if (id == KeyEvent.VK_RIGHT)
214: {
215: if (selectingDifficulty)
216: {
217: if (botDifficulty < 2)
218: {
219: botDifficulty++;
220: }
221: else
222: {
223: botDifficulty = 0;
224: }
225: }
226: else if (gameStatus == 0)
227: {
228: scoreLimit++;
229: }
230: }
231: else if (id == KeyEvent.VK_LEFT)
232: {
233: if (selectingDifficulty)
234: {
235: if (botDifficulty > 0)
236: {
237: botDifficulty--;
238: }
239: else
240: {
241: botDifficulty = 2;
242: }
243: }
244: else if (gameStatus == 0 && scoreLimit > 1)
245: {
246: scoreLimit--;
247: }
248: }
249: else if (id == KeyEvent.VK_ESCAPE && (gameStatus == 2 || gameStatus == 3))
250: {
251: gameStatus = 0;
252: }
253: else if (id == KeyEvent.VK_SHIFT && gameStatus == 0)
254: {
255: bot = true;
256: selectingDifficulty = true;
257: }
258: else if (id == KeyEvent.VK_SPACE)
259: {
260: if (gameStatus == 0 || gameStatus == 3)
261: {
262: if (!selectingDifficulty)
263: {
264: bot = false;
265: }
266: else
267: {
268: selectingDifficulty = false;
269: }
270: start();
271: }
272: else if (gameStatus == 1)
273: {
274: gameStatus = 2;
275: }
276: else if (gameStatus == 2)
277: {
278: gameStatus = 1;
279: }
280: }
281: }
282: @Override
283: public void keyReleased(KeyEvent e)
284: {
285: int id = e.getKeyCode();
286: if (id == KeyEvent.VK_W)
287: {
288: w = false;
289: }
290: else if (id == KeyEvent.VK_S)
291: {
292: s = false;
293: }
294: else if (id == KeyEvent.VK_UP)
295: {
296: up = false;
297: }
298: else if (id == KeyEvent.VK_DOWN)
299: {
300: down = false;
301: }
302: }
303: @Override
304: public void keyTyped(KeyEvent e)
305: {
306: }
307: }
2. Ball
1: package pong;
2: import java.awt.Color;
3: import java.awt.Graphics;
4: import java.util.Random;
5: public class Ball
6: {
7: public int x, y, width = 25, height = 25;
8: public int motionX, motionY;
9: public Random random;
10: private Pong pong;
11: public int amountOfHits;
12: public Ball(Pong pong)
13: {
14: this.pong = pong;
15: this.random = new Random();
16: spawn();
17: }
18: public void update(Paddle paddle1, Paddle paddle2)
19: {
20: int speed = 5;
21: this.x += motionX * speed;
22: this.y += motionY * speed;
23: if (this.y + height - motionY > pong.height || this.y + motionY < 0)
24: {
25: if (this.motionY < 0)
26: {
27: this.y = 0;
28: this.motionY = random.nextInt(4);
29: if (motionY == 0)
30: {
31: motionY = 1;
32: }
33: }
34: else
35: {
36: this.motionY = -random.nextInt(4);
37: this.y = pong.height - height;
38: if (motionY == 0)
39: {
40: motionY = -1;
41: }
42: }
43: }
44: if (checkCollision(paddle1) == 1)
45: {
46: this.motionX = 1 + (amountOfHits / 5);
47: this.motionY = -2 + random.nextInt(4);
48: if (motionY == 0)
49: {
50: motionY = 1;
51: }
52: amountOfHits++;
53: }
54: else if (checkCollision(paddle2) == 1)
55: {
56: this.motionX = -1 - (amountOfHits / 5);
57: this.motionY = -2 + random.nextInt(4);
58: if (motionY == 0)
59: {
60: motionY = 1;
61: }
62: amountOfHits++;
63: }
64: if (checkCollision(paddle1) == 2)
65: {
66: paddle2.score++;
67: spawn();
68: }
69: else if (checkCollision(paddle2) == 2)
70: {
71: paddle1.score++;
72: spawn();
73: }
74: }
75: public void spawn()
76: {
77: this.amountOfHits = 0;
78: this.x = pong.width / 2 - this.width / 2;
79: this.y = pong.height / 2 - this.height / 2;
80: this.motionY = -2 + random.nextInt(4);
81: if (motionY == 0)
82: {
83: motionY = 1;
84: }
85: if (random.nextBoolean())
86: {
87: motionX = 1;
88: }
89: else
90: {
91: motionX = -1;
92: }
93: }
94: public int checkCollision(Paddle paddle)
95: {
96: if (this.x < paddle.x + paddle.width && this.x + width > paddle.x && this.y < paddle.y + paddle.height && this.y + height > paddle.y)
97: {
98: return 1; //bounce
99: }
100: else if ((paddle.x > x && paddle.paddleNumber == 1) || (paddle.x < x - width && paddle.paddleNumber == 2))
101: {
102: return 2; //score
103: }
104: return 0; //nothing
105: }
106: public void render(Graphics g)
107: {
108: g.setColor(Color.WHITE);
109: g.fillOval(x, y, width, height);
110: }
111: }
3. Paddle
1: package pong;
2: import java.awt.Color;
3: import java.awt.Graphics;
4: public class Paddle
5: {
6: public int paddleNumber;
7: public int x, y, width = 50, height = 250;
8: public int score;
9: public Paddle(Pong pong, int paddleNumber)
10: {
11: this.paddleNumber = paddleNumber;
12: if (paddleNumber == 1)
13: {
14: this.x = 0;
15: }
16: if (paddleNumber == 2)
17: {
18: this.x = pong.width - width;
19: }
20: this.y = pong.height / 2 - this.height / 2;
21: }
22: public void render(Graphics g)
23: {
24: g.setColor(Color.WHITE);
25: g.fillRect(x, y, width, height);
26: }
27: public void move(boolean up)
28: {
29: int speed = 15;
30: if (up)
31: {
32: if (y - speed > 0)
33: {
34: y -= speed;
35: }
36: else
37: {
38: y = 0;
39: }
40: }
41: else
42: {
43: if (y + height + speed < Pong.pong.height)
44: {
45: y += speed;
46: }
47: else
48: {
49: y = Pong.pong.height - height;
50: }
51: }
52: }
53: }
4. Renderer
1: package pong;
2: import java.awt.Graphics;
3: import java.awt.Graphics2D;
4: import javax.swing.JPanel;
5: public class Renderer extends JPanel
6: {
7: private static final long serialVersionUID = 1L;
8: @Override
9: protected void paintComponent(Graphics g)
10: {
11: super.paintComponent(g);
12: Pong.pong.render((Graphics2D) g);
13: }
14: }
Lalu jalankan fungsi main pada class Pong, dan viola game pong bisa dimainkan.
Komentar
Posting Komentar