SHOW:
|
|
- or go back to the newest paste.
1 | import java.awt.Font; | |
2 | import java.awt.Color; | |
3 | import java.awt.Graphics; | |
4 | import java.awt.event.ActionEvent; | |
5 | import java.awt.event.ActionListener; | |
6 | import java.awt.event.KeyEvent; | |
7 | import java.awt.event.KeyListener; | |
8 | import java.util.Random; | |
9 | ||
10 | import javax.swing.JPanel; | |
11 | import javax.swing.Timer; | |
12 | ||
13 | public class PongPanel extends JPanel implements ActionListener, KeyListener { | |
14 | ||
15 | private Game game; | |
16 | ||
17 | private boolean running = true; | |
18 | ||
19 | private boolean upPressed = false, downPressed = false, wPressed = false, sPressed = false; | |
20 | ||
21 | private int ballX = 400, ballY = 200; | |
22 | private int diameter = 20; | |
23 | private int ballDeltaX = -5, ballDeltaY = 5; | |
24 | ||
25 | private int playerOneX = 25, playerOneY = 220; | |
26 | private int playerOneWidth = 20, playerOneHeight = 60; | |
27 | ||
28 | private int playerTwoX = 750, playerTwoY = 220; | |
29 | private int playerTwoWidth = 20, playerTwoHeight = 60; | |
30 | ||
31 | private int paddleSpeed = 10; | |
32 | ||
33 | Random random = new Random(); | |
34 | private final float hue = random.nextFloat(); | |
35 | private final float saturation = 0.9f; | |
36 | private float luminance = 1.0f; | |
37 | ||
38 | private int deaths; | |
39 | private int rallyScore = 0; | |
40 | private int rallyInterval = 5; | |
41 | ||
42 | private long currentTime = System.currentTimeMillis(); | |
43 | public static Thread thread; | |
44 | ||
45 | private int[] Colors = new int[getWidth() * getHeight()]; | |
46 | ||
47 | public PongPanel() { | |
48 | Random rand = new Random(); | |
49 | setBackground(new Color(rand.nextInt(0xFF4AF6))); | |
50 | ||
51 | setFocusable(true); | |
52 | addKeyListener(this); | |
53 | ||
54 | Timer timer = new Timer(1000 / 60, this); | |
55 | timer.start(); | |
56 | } | |
57 | ||
58 | public void actionPerformed(ActionEvent e) { | |
59 | step(); | |
60 | } | |
61 | ||
62 | public void step() { | |
63 | ||
64 | if (upPressed) { | |
65 | if (playerTwoY - paddleSpeed > 0) { | |
66 | playerTwoY -= paddleSpeed; | |
67 | } | |
68 | } | |
69 | if (downPressed) { | |
70 | if (playerTwoY + paddleSpeed + playerTwoHeight < getHeight()) { | |
71 | playerTwoY += paddleSpeed; | |
72 | } | |
73 | } | |
74 | ||
75 | if (wPressed) { | |
76 | if (playerOneY - paddleSpeed > 0) { | |
77 | playerOneY -= paddleSpeed; | |
78 | } | |
79 | } | |
80 | if (sPressed) { | |
81 | if (playerOneY + paddleSpeed + playerOneHeight < getHeight()) { | |
82 | playerOneY += paddleSpeed; | |
83 | } | |
84 | } | |
85 | ||
86 | int nextBallLeft = ballX + ballDeltaX; | |
87 | int nextBallRight = ballX + diameter + ballDeltaX; | |
88 | int nextBallTop = ballY + ballDeltaY; | |
89 | int nextBallBottom = ballY + diameter + ballDeltaY; | |
90 | ||
91 | int playerOneRight = playerOneX + playerOneWidth; | |
92 | int playerOneTop = playerOneY; | |
93 | int playerOneBottom = playerOneY + playerOneHeight; | |
94 | ||
95 | float playerTwoLeft = playerTwoX; | |
96 | float playerTwoTop = playerTwoY; | |
97 | float playerTwoBottom = playerTwoY + playerTwoHeight; | |
98 | ||
99 | if (nextBallTop < 0 || nextBallBottom > getHeight()) { | |
100 | ballDeltaY *= -1; | |
101 | ||
102 | } | |
103 | ||
104 | if (nextBallLeft < playerOneRight) { | |
105 | ||
106 | if (nextBallTop > playerOneBottom || nextBallBottom < playerOneTop) { | |
107 | ||
108 | System.out.println("You died. Your rally was " + rallyScore + "."); | |
109 | deaths++; | |
110 | ballX = 400; | |
111 | ballY = 200; | |
112 | } else { | |
113 | rallyScore++; | |
114 | ballDeltaX *= -1; | |
115 | } | |
116 | } | |
117 | ||
118 | if (nextBallRight > playerTwoLeft) { | |
119 | ||
120 | if (nextBallTop > playerTwoBottom || nextBallBottom < playerTwoTop) { | |
121 | ||
122 | System.out.println("You died. Your rally was " + rallyScore + "."); | |
123 | ||
124 | deaths++; | |
125 | ||
126 | ballX = 400; | |
127 | ballY = 200; | |
128 | } else { | |
129 | rallyScore++; | |
130 | ballDeltaX *= -1; | |
131 | } | |
132 | } | |
133 | ||
134 | ballX += ballDeltaX; | |
135 | ballY += ballDeltaY; | |
136 | ||
137 | if (rallyScore % 5 == 0) { | |
138 | - | if (rallyScore % rallyInterval == 1) { |
138 | + | |
139 | ballDeltaY = ballDeltaY + 2; | |
140 | paddleSpeed = paddleSpeed + 2; | |
141 | } | |
142 | ||
143 | repaint(); | |
144 | } | |
145 | ||
146 | public synchronized void Score() { | |
147 | ||
148 | } | |
149 | ||
150 | public void RandomColor() { | |
151 | ||
152 | } | |
153 | ||
154 | public void paintComponent(Graphics g) { | |
155 | ||
156 | super.paintComponent(g); | |
157 | Random rand = new Random(); | |
158 | ||
159 | for (int lineY = 0; lineY < getHeight(); lineY += 50) { | |
160 | g.setColor(new Color(rand.nextInt(0xFF4AF6))); | |
161 | g.fillRoundRect(400, lineY, 20, 800, 0, 0); | |
162 | } | |
163 | ||
164 | g.setColor(Color.WHITE); | |
165 | g.fillRoundRect(ballX, ballY, 25, 25, 5, 25); | |
166 | for (int tick = 0; tick > getHeight(); tick--) { | |
167 | ||
168 | g.setColor(new Color(rand.nextInt(0xFF4AF6))); | |
169 | } | |
170 | g.setColor(new Color(rand.nextInt(0xFF4AF6))); | |
171 | g.fillRoundRect(playerOneX, playerOneY, playerOneWidth, | |
172 | playerOneHeight, 50, 5); | |
173 | ||
174 | g.fillRoundRect(playerTwoX, playerTwoY, playerTwoWidth, | |
175 | playerTwoHeight, 50, 5); | |
176 | ||
177 | g.setColor(Color.WHITE); | |
178 | g.fillRoundRect(335, 180, 150, 150, 50, 50); | |
179 | g.setFont(new Font(Font.DIALOG, Font.BOLD, 150)); | |
180 | String rally = " " + rallyScore; | |
181 | g.setColor(new Color(0xFF2172)); | |
182 | g.drawString(rally, 328, 310); | |
183 | g.dispose(); | |
184 | } | |
185 | ||
186 | public void keyTyped(KeyEvent e) { | |
187 | } | |
188 | ||
189 | public void keyPressed(KeyEvent e) { | |
190 | if (e.getKeyCode() == KeyEvent.VK_UP) { | |
191 | upPressed = true; | |
192 | } else if (e.getKeyCode() == KeyEvent.VK_DOWN) { | |
193 | downPressed = true; | |
194 | } else if (e.getKeyCode() == KeyEvent.VK_W) { | |
195 | wPressed = true; | |
196 | } else if (e.getKeyCode() == KeyEvent.VK_S) { | |
197 | sPressed = true; | |
198 | } | |
199 | } | |
200 | ||
201 | public void keyReleased(KeyEvent e) { | |
202 | if (e.getKeyCode() == KeyEvent.VK_UP) { | |
203 | upPressed = false; | |
204 | } else if (e.getKeyCode() == KeyEvent.VK_DOWN) { | |
205 | downPressed = false; | |
206 | } else if (e.getKeyCode() == KeyEvent.VK_W) { | |
207 | wPressed = false; | |
208 | } else if (e.getKeyCode() == KeyEvent.VK_S) { | |
209 | sPressed = false; | |
210 | } | |
211 | } | |
212 | } |