//NHL SLIME by KTG import java.applet.Applet; import java.awt.*; public class nhl extends Applet implements Runnable { private int nWidth; private int nHeight; private int p1Score; private int p2Score; private int p1X; private int p2X; private int p1Y; private int p2Y; private int p1Col; private int p2Col; private Color[] slimeColours; private Color[] shirts; private String slimeColText[] = { "New Jersey Devils", "Vancouver Canucks", "NY Rangers", "NY Islanders", "Colorado Avalanche", "Detroit Red Wings", "Chicago Blackhawks", "LA Kings", "Boston Bruins", "Toronto Maple Leafs", "Ottawa Senators", "Pittsburgh Penguins", "Washington Capitals", "Edmonton Oilers", "Carolina Hurricanes", "Calgary Flames", "Anaheim Mighty Ducks", "Dallas Stars", "San Jose Sharks", "Philadelphia Flyers", "Atlanta Thrashers", "Buffalo Sabres", "Columbus Blue Jackets", "Florida Panthers", "Minnesota Wild", "Montreal Canadiens", "Nashville Predators", "Phoenix Coyotes", "St Louis Blues", "Tampa Bay Lightning" }; private int p1OldX; private int p2OldX; private int p1OldY; private int p2OldY; private int p1XV; private int p2XV; private int p1YV; private int p2YV; private int ballX; private int ballY; private int ballVX; private int ballVY; private int ballOldX; private int ballOldY; private Graphics screen; private String promptMsg; private int replayData[][]; private int replayPos; private int replayStart; private boolean mousePressed; private boolean fCanChangeCol; private boolean fInPlay; private int p1Blink; private int p2Blink; private boolean fP1Sticky; private boolean fP2Sticky; private boolean fP1Touched; private boolean fP2Touched; private int p1TouchingGoal; private int p2TouchingGoal; private Thread gameThread; private boolean fEndGame; private boolean fPlayOn; private int nScoreX; private long startTime; private long gameTime; private int scoringRun; private int frenzyCol = 0; private int playOnTicks; private Image backBuffer; private final int SMILE_DIFF = 3; private final int DAMPING = 7; private final int MAX_TICKS_TOUCHING_GOAL = 60; private int gameLength; private boolean fSuperSlime; private boolean doubleBuffered; public void initStuff() { fEndGame = true; p1X = 200; p1Y = 0; p2X = 800; p2Y = 0; p1XV = 0; p1YV = 0; p2XV = 0; p2YV = 0; p1Score = 0; p2Score = 0; ballOldX = ballX = 500; ballOldY = ballY = 400; ballVX = 0; ballVY = 0; ballOldX = 500; ballOldY = 400; replayStart = replayPos = 0; fP1Touched = fP2Touched = false; playOnTicks = 10; fPlayOn = false; gameLength = 120000; } private void drawButtons() { String[] buttons = {"1 minute", "2 minutes", "4 minutes", "8 minutes" }; FontMetrics fm = screen.getFontMetrics(); Color darkBlue = new Color(0, 0, 128); for (int i=0; i<4; i++) { screen.setColor(darkBlue); screen.fillRect(((2*i+1)*nWidth/8)-nWidth/12, nHeight*2/10, nWidth/6, nHeight/10); screen.setColor(Color.white); screen.drawString(buttons[i], ((2*i+1)*nWidth/8)-fm.stringWidth(buttons[i]) / 2, nHeight*5/20+fm.getHeight()/2); } flip(); } private boolean testButton(int x, int y) { for (int i=0; i<4; i++) { if ((x > ((2*i+1)*nWidth/8)-nWidth/12) && (x < ((2*i+1)*nWidth/8)+nWidth/12) && (y > (nHeight*2/10)) && (y < nHeight*3/10)) { gameLength = (1< 0) p1XV = 0; break; case Event.LEFT: case 74: // 'J' case 106: // 'j' if(p2XV < 0) p2XV = 0; break; case Event.RIGHT: case 76: // 'L' case 108: // 'l' if(p2XV > 0) p2XV = 0; break; } } return false; } private void DrawSlimers() { int k1 = nWidth / 10; int j2 = nHeight / 10; int i3 = nWidth / 50; int j3 = nHeight / 25; int k3 = (ballX * nWidth) / 1000; int l3 = (4 * nHeight) / 5 - (ballY * nHeight) / 1000; int i = (p1OldX * nWidth) / 1000 - k1 / 2; int l = (7 * nHeight) / 10 - (p1OldY * nHeight) / 1000; screen.setColor(Color.blue); screen.fillRect(i, l, k1, j2); i = (p2OldX * nWidth) / 1000 - k1 / 2; l = (7 * nHeight) / 10 - (p2OldY * nHeight) / 1000; screen.setColor(Color.blue); screen.fillRect(i, l, k1, j2); if (!fEndGame) MoveBall(); i = (p1X * nWidth) / 1000 - k1 / 2; l = (7 * nHeight) / 10 - (p1Y * nHeight) / 1000; screen.setColor(fSuperSlime ? slimeColours[frenzyCol = ((frenzyCol + 1) % slimeColours.length)] : slimeColours[p1Col]); screen.fillArc(i, l, k1, 2 * j2, 0, 180); int l4 = p1X + 38; int i5 = p1Y - 60; i = (l4 * nWidth) / 1000; l = (7 * nHeight) / 10 - (i5 * nHeight) / 1000; int i4 = i - k3; int j4 = l - l3; int k4 = (int)Math.sqrt(i4 * i4 + j4 * j4); boolean flag = Math.random() < 0.01D; if(flag) p1Blink = 5; if(p1Blink == 0) { screen.setColor(Color.white); screen.fillOval(i - i3, l - j3, i3, j3); if(k4 > 0 && !flag) { screen.setColor(Color.black); screen.fillOval(i - (4 * i4) / k4 - (3 * i3) / 4, l - (4 * j4) / k4 - (3 * j3) / 4, i3 / 2, j3 / 2); } } else { p1Blink--; } if(p1Score > p2Score + SMILE_DIFF) { int j = (p1X * nWidth) / 1000; int i1 = (7 * nHeight) / 10 - ((p1Y - 40) * nHeight) / 1000; int l1 = nWidth / 20; int k2 = nHeight / 20; int j5 = 0; do { screen.setColor(Color.black); screen.drawArc(j, i1 + j5, l1, k2, -30, -150); } while(++j5 < 3); } i = (p2X * nWidth) / 1000 - k1 / 2; l = (7 * nHeight) / 10 - (p2Y * nHeight) / 1000; screen.setColor(fSuperSlime ? slimeColours[frenzyCol = ((frenzyCol + 1) % slimeColours.length)] : slimeColours[p2Col]); screen.fillArc(i, l, k1, 2 * j2, 0, 180); l4 = p2X - 18; i5 = p2Y - 60; i = (l4 * nWidth) / 1000; l = (7 * nHeight) / 10 - (i5 * nHeight) / 1000; i4 = i - k3; j4 = l - l3; k4 = (int)Math.sqrt(i4 * i4 + j4 * j4); flag = Math.random() < 0.01D; if(flag) p2Blink = 5; if(p2Blink == 0) { screen.setColor(flag ? Color.gray : Color.white); screen.fillOval(i - i3, l - j3, i3, j3); if(k4 > 0 && !flag) { screen.setColor(Color.black); screen.fillOval(i - (4 * i4) / k4 - (3 * i3) / 4, l - (4 * j4) / k4 - (3 * j3) / 4, i3 / 2, j3 / 2); } } else { p2Blink--; } if(p2Score > p1Score + SMILE_DIFF) { int i2 = nWidth / 20; int l2 = nHeight / 20; int k = (p2X * nWidth) / 1000 - i2; int j1 = (7 * nHeight) / 10 - ((p2Y - 40) * nHeight) / 1000; int k5 = 0; do { screen.setColor(Color.black); screen.drawArc(k, j1 + k5, i2, l2, -10, -150); } while(++k5 < 3); } } public void paint(Graphics g) { nWidth = size().width; nHeight = size().height; screen.setColor(Color.blue); screen.fillRect(0, 0, nWidth, (4 * nHeight) / 5); screen.setColor(Color.gray); screen.fillRect(0, (4 * nHeight) / 5, nWidth, nHeight / 5); screen.setColor(Color.white); // g.fillRect(nWidth / 2 - 2, (7 * nHeight) / 10, 4, nHeight / 10 + 5); drawScores(); if (!fInPlay) { DrawSlimers(); drawButtons(); } DrawGoals(); drawPrompt(); if(!fInPlay) { FontMetrics fontmetrics = screen.getFontMetrics(); screen.setColor(Color.white); screen.drawString("NHL Slime!", nWidth / 2 - fontmetrics.stringWidth("Slime Hockey!") / 2, nHeight / 2 - fontmetrics.getHeight()); screen.setColor(Color.white); fontmetrics = screen.getFontMetrics(); screen.drawString("Written by KTG", nWidth / 2 - fontmetrics.stringWidth("Written by KTG") / 2, nHeight / 2 + fontmetrics.getHeight() * 2); } flip(); } public void destroy() { gameThread.stop(); gameThread = null; } private void ReplayFrame(int i, int j, int k, int l, int i1, boolean flag) { if(flag) { ballX = ballOldX = 0xfd050f80; ballY = ballOldY = 0x186a0; p1OldX = p1OldY = p2OldX = p2OldY = -10000; } else { int j1 = i != 0 ? i - 1 : 199; p1OldX = replayData[j1][0]; p1OldY = replayData[j1][1]; p2OldX = replayData[j1][2]; p2OldY = replayData[j1][3]; ballOldX = replayData[j1][4]; ballOldY = replayData[j1][5]; } p1X = replayData[i][0]; p1Y = replayData[i][1]; p2X = replayData[i][2]; p2Y = replayData[i][3]; ballX = replayData[i][4]; ballY = replayData[i][5]; p1Col = replayData[i][6]; p2Col = replayData[i][7]; ballVX = 0; ballVY = 1; if((i / 10) % 2 > 0) { screen.setColor(Color.red); screen.drawString("Replay...", j, k); } else { screen.setColor(Color.blue); screen.fillRect(j, k - i1, l, i1 * 2); } DrawSlimers(); DrawGoals(); try { Thread.sleep(20L); return; } catch(InterruptedException _ex) { return; } } private String MakeTime(long l) { long l1 = (l / 10L) % 100L; long l2 = (l / 1000L) % 60L; long l3 = (l / 60000L) % 60L; String s = ""; if(l3 < 10L) s += "0"; s += l3; s += ":"; if(l2 < 10L) s += "0"; s += l2; s += ":"; if(l1 < 10L) s += "0"; s += l1; return s; } private void MoveSlimers() { p1X += p1XV; if(p1X < 50) p1X = 50; if(p1X > 950) p1X = 950; if(p1YV != 0) { p1Y += p1YV -= (fSuperSlime) ? 4 : 2; if(p1Y < 0) { p1Y = 0; p1YV = 0; } } p2X += p2XV; if(p2X > 950) p2X = 950; if(p2X < 50) p2X = 50; if(p2YV != 0) { p2Y += p2YV -= (fSuperSlime) ? 4 : 2; if(p2Y < 0) { p2Y = 0; p2YV = 0; } } } public nhl() { p2Col = 1; replayData = new int[200][8]; } private void MoveBall() { int k = (30 * nHeight) / 1000; int i = (ballOldX * nWidth) / 1000; int j = (4 * nHeight) / 5 - (ballOldY * nHeight) / 1000; screen.setColor(Color.blue); screen.fillOval(i - k, j - k, k * 2, k * 2); ballY += --ballVY; ballX += ballVX; if(!fEndGame) { int l1 = (ballX - p1X) * 2; int i2 = ballY - p1Y; int j2 = l1 * l1 + i2 * i2; int k2 = ballVX - p1XV; int l2 = ballVY - p1YV; if(i2 > 0 && j2 < 15625 && j2 > 25) { int l = (int)Math.sqrt(j2); int j1 = (l1 * k2 + i2 * l2) / l; ballX = p1X + (l1 * 63) / l; ballY = p1Y + (i2 * 125) / l; if(j1 <= 0) { if (!fP1Sticky) { ballVY += p1YV - (2 * i2 * j1) / l; ballVX += (p1XV - (2 * l1 * j1) / l) * DAMPING / 10; } else { ballVX = 0; ballVY = 0; } if(ballVX < -15) ballVX = -15; if(ballVX > 15) ballVX = 15; if(ballVY < -22) ballVY = -22; if(ballVY > 22) ballVY = 22; } fP1Touched = true; } l1 = (ballX - p2X) * 2; i2 = ballY - p2Y; j2 = l1 * l1 + i2 * i2; k2 = ballVX - p2XV; l2 = ballVY - p2YV; if(i2 > 0 && j2 < 15625 && j2 > 25) { int i1 = (int)Math.sqrt(j2); int k1 = (l1 * k2 + i2 * l2) / i1; ballX = p2X + (l1 * 63) / i1; ballY = p2Y + (i2 * 125) / i1; if(k1 <= 0) { if (!fP2Sticky) { ballVX += (p2XV - (2 * l1 * k1) / i1) * DAMPING / 10; ballVY += p2YV - (2 * i2 * k1) / i1; } else { ballVX = 0; ballVY = 0; } if(ballVX < -15) ballVX = -15; if(ballVX > 15) ballVX = 15; if(ballVY < -22) ballVY = -22; if(ballVY > 22) ballVY = 22; } fP2Touched = true; } if(ballX < 15) { ballX = 15; ballVX = -ballVX; } if(ballX > 985) { ballX = 985; ballVX = -ballVX; } if ((ballX <= 50) || (ballX >= 950)) { if (((ballY > 200) && (ballOldY < 200)) || ((ballY < 200) && (ballOldY > 200))) { ballY = 200; ballVY *= -1; } if ((ballY > 180) && (ballY < 220)) { if ((ballX > 40) && (ballX < 50) && (ballVX < 0)) { ballX = 50; ballVX *= -1; } if ((ballX < 960) && (ballX > 950) && (ballVX > 0)) { ballX = 950; ballVX *= -1; } } } if (ballY < 34) { ballY = 34; ballVY = -ballVY * DAMPING / 10; ballVX = ballVX * DAMPING / 10; } } i = (ballX * nWidth) / 1000; j = (4 * nHeight) / 5 - (ballY * nHeight) / 1000; screen.setColor(Color.yellow); screen.fillOval(i - k, j - k, k * 2, k * 2); } private void DrawGoals() { screen.setColor(Color.white); screen.fillRect(nWidth / 20, 4 * nHeight / 5 - 200 * nHeight / 1000, 5, 200 * nHeight / 1000); screen.fillRect(nWidth - nWidth / 20 - 5, 4 * nHeight / 5 - 200 * nHeight / 1000, 5, 200 * nHeight / 1000); screen.fillRect(0, 4*nHeight/5 + 2, nWidth/10, 2); screen.fillRect(nWidth*9/10, 4*nHeight/5 + 2, nWidth/10, 2); for (int i=0; i= 200) replayPos = 0; if(replayStart == replayPos) replayStart++; if(replayStart >= 200) replayStart = 0; } private void drawScores() { Graphics g = screen; int k = nHeight / 20; FontMetrics fm = screen.getFontMetrics(); int i = fm.stringWidth("Replay..."); g.setColor(Color.blue); g.fillRect(0, 0, nWidth, k + 22); g.setColor(slimeColours[p1Col]); g.drawString(slimeColText[p1Col] + " : " + p1Score, nWidth/20, k); String p2ScrStr = p2Score + " : " + slimeColText[p2Col]; g.setColor(slimeColours[p2Col]); g.drawString(p2ScrStr, nWidth-nWidth/20-fm.stringWidth(p2ScrStr), k); } public boolean checkScored() { if ((ballY < 200) && ((ballX < 40) || (ballX > 960))) { nScoreX = ballX; fPlayOn = true; playOnTicks = 10; return true; } return false; } public void run() { replayPos = replayStart = 0; scoringRun = 0; fP1Touched = fP2Touched = false; gameTime = 0; startTime = System.currentTimeMillis(); fEndGame = false; fCanChangeCol = false; gameTime = gameLength; while(gameTime > 0) { gameTime = startTime + gameLength - System.currentTimeMillis(); if (gameTime < 0) gameTime = 0; SaveReplayData(); p1OldX = p1X; p1OldY = p1Y; p2OldX = p2X; p2OldY = p2Y; ballOldX = ballX; ballOldY = ballY; MoveSlimers(); DrawSlimers(); DrawGoals(); DrawStatus(); flip(); if (p1X < 150) p1TouchingGoal++; else p1TouchingGoal = 0; if (p2X > 850) p2TouchingGoal++; else p2TouchingGoal = 0; if (fPlayOn) playOnTicks--; else fPlayOn = checkScored(); if((playOnTicks == 0) || (p1TouchingGoal > MAX_TICKS_TOUCHING_GOAL) || (p2TouchingGoal > MAX_TICKS_TOUCHING_GOAL)) { long l = System.currentTimeMillis(); if (p1TouchingGoal > MAX_TICKS_TOUCHING_GOAL) { p2Score++; promptMsg = slimeColText[p1Col] + " pinged for goal hanging!"; } else if (p2TouchingGoal > MAX_TICKS_TOUCHING_GOAL) { p1Score++; promptMsg = slimeColText[p2Col] + " pinged for goal hanging!"; } else if (nScoreX < 500) { p2Score++; promptMsg = slimeColText[p2Col] + " Score!"; } else { p1Score++; promptMsg = slimeColText[p1Col] + " Score!"; } drawPrompt(); drawPrompt("Click mouse for replay...", 1); flip(); mousePressed = false; if(gameThread != null) try { Thread.sleep(2500L); } catch(InterruptedException _ex) { } if(mousePressed) { SaveReplayData(); DoReplay(); } promptMsg = ""; drawPrompt(); playOnTicks = 10; fPlayOn = false; startTime += System.currentTimeMillis() - l; ballX = 500; ballY = 400; ballVX = 0; ballVY = 0; p1X = 200; p1Y = 0; p2X = 800; p2Y = 0; repaint(); } if(gameThread != null) try { if (fPlayOn) Thread.sleep(120L); else Thread.sleep(20); } catch(InterruptedException _ex) { } } fEndGame = true; drawPrompt("And that's the final whistle!", 0); if (p1Score == p2Score) drawPrompt("It's a draw at full time, here at Slime Stadium!", 1); else if (p1Score < p2Score) drawPrompt(slimeColText[p2Col] + " (" + p2Score + ") def. " + slimeColText[p1Col] + " (" + p1Score + ")", 1); else drawPrompt(slimeColText[p1Col] + " (" + p1Score + ") def. " + slimeColText[p2Col] + " (" + p2Score + ")", 1); flip(); try { Thread.sleep(5000); } catch (InterruptedException e) { } initStuff(); fCanChangeCol = true; fInPlay = false; repaint(); } public void init() { nWidth = size().width; nHeight = size().height; fInPlay = fEndGame = false; fCanChangeCol = true; initStuff(); promptMsg = "Click the mouse to play..."; backBuffer = createImage(nWidth, nHeight); screen = getGraphics(); screen.setFont(new Font(screen.getFont().getName(), 4, 15)); // set colors to teams slimeColours = new Color[slimeColText.length]; java.util.Random rand = new java.util.Random(15); for (int p=0; p>16; int g = (rand.nextInt()&0x00FF0000)>>16; int b = 255 - r - g; if (b < 255) b = 0; slimeColours[p] = new Color(r, g, b); } } private void toggleBuffering() { if (doubleBuffered = !doubleBuffered) { screen = backBuffer.getGraphics(); screen.setFont(new Font(screen.getFont().getName(), 1, 15)); } else { screen = getGraphics(); screen.setFont(new Font(screen.getFont().getName(), 1, 15)); } repaint(); } private void DoReplay() { FontMetrics fontmetrics = screen.getFontMetrics(); int i = fontmetrics.stringWidth("Replay..."); int j = fontmetrics.getHeight(); int k = nWidth / 2 - i / 2; int l = nHeight / 2 - j; promptMsg = "Click the mouse to continue..."; mousePressed = false; int i1 = replayPos - 1; while(!mousePressed) { if(++i1 >= 200) i1 = 0; if(i1 == replayPos) { try { Thread.sleep(1000L); } catch(InterruptedException _ex) { } i1 = replayStart; paint(getGraphics()); } ReplayFrame(i1, k, l, i, j, false); flip(); } promptMsg = ""; paint(getGraphics()); } private void flip() { if (doubleBuffered) getGraphics().drawImage(backBuffer, 0, 0, null); } private void DoFatality() { // PLEASE, someone put something funny/interesting here :) } }