import java.awt.*; public class test1 extends BufferedApplet { int w = 0, h = 0, x1 = 0, y1 = 0, y2 = 0; int diameter = 40, border = 4; int score = 0, winningScore = 21; int boosted = 0, scored = 0; double mx = 0, my = 0, dx = 6, dy = 3; Font font = new Font("Helvetica", Font.BOLD, 24); public void render(Graphics g) { if (w == 0) { w = bounds().width; h = bounds().height; startGame(); } g.setColor(Color.white); g.fillRect(0, 0, w, h); g.setColor(scored-- > 0 ? Color.red : Color.black); g.fillRect(x1 - 10, 0, 20, h); g.setColor(Color.white); g.fillRect(x1 - 10, y1, 20, y2 - y1); g.setColor(Color.blue); g.setFont(font); g.drawString("Score: " + score + " out of 21", 5, 24); Color diskColor = boosted > 0 ? Color.red : Color.green; int d = (int)(boosted > 0 ? 1.2 * diameter : diameter); drawDisk(g, (int)mx, (int)my, d, (int)border, diskColor); if (boosting) boostAmount++; if (boosted == 5) dy -= boostAmount; if (boosted > 0) { boosted--; if (boosted == 0) boostAmount = 5; } dy++; dy *= 0.99; mx += dx; my += dy; if (mx + diameter/2 + dx > w) dx = -dx; if (mx - diameter/2 + dx < 0) dx = -dx; if (mx < x1 && my < y1 || my > y2) if (dx > 0 && mx + diameter/2 > x1 - 10) dx = -dx; if (score < winningScore && mx > x1) { score++; scored = 5; mx = 20; dx = 6 + score; y1 += 5; y2 -= 5; } if (score == winningScore) { g.setColor(Color.red); g.drawString("YOU WON! Hit SPACE to play again", 85, h / 2); } if (my + diameter/2 + dy > h) dy = -dy; if (my - diameter/2 + dy < 0) dy = -dy; my = Math.min(h - diameter/2, my); animating = true; } public void drawDisk(Graphics g, int x, int y, int d, int b, Color color) { g.setColor(Color.black); g.fillOval(x - d/2, y - d/2, d, d); g.setColor(color); d -= 2 * b; g.fillOval(x - d/2, y - d/2, d, d); } void startGame() { mx = w / 4; my = h / 2; score = 0; x1 = w - 40; y1 = h / 2 - 5 * winningScore; y2 = h / 2 + 5 * winningScore; } int boostAmount = 5; boolean boosting = false; public boolean mouseDown(Event e, int x, int y) { boosting = true; return true; } public boolean mouseUp(Event e, int x, int y) { boosting = false; boosted = 5; return true; } public boolean keyUp(Event e, int key) { if (key == ' ') startGame(); return true; } }