📄 worldcupsoccerslime.java
字号:
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<nWidth / 20; i += 5)
{
screen.drawLine(i, 4 * nHeight / 5 - 200 * nHeight / 1000,
i, 4 * nHeight / 5);
screen.drawLine(nWidth-i, 4 * nHeight / 5 - 200 * nHeight / 1000,
nWidth-i, 4 * nHeight / 5);
}
for (int i=4 * nHeight / 5 - nHeight / 5; i<4 * nHeight / 5; i += 5)
{
screen.drawLine(0, i, nWidth/20, i);
screen.drawLine(nWidth, i, nWidth-nWidth/20, i);
}
int p1TickX = (MAX_TICKS_TOUCHING_GOAL - p1TouchingGoal)
* nWidth / (2 * MAX_TICKS_TOUCHING_GOAL);
screen.setColor(secondaryCols[p1Col]);
screen.fillRect(0, nHeight-5, p1TickX, 5);
screen.setColor(Color.gray);
screen.fillRect(p1TickX, nHeight-5, nWidth/2-p1TickX, 5);
int p2TickX = nWidth - (MAX_TICKS_TOUCHING_GOAL - p2TouchingGoal)
* nWidth / (2 * MAX_TICKS_TOUCHING_GOAL);
screen.setColor(secondaryCols[p2Col]);
screen.fillRect(p2TickX, nHeight-5, nWidth, 5);
screen.setColor(Color.gray);
screen.fillRect(nWidth/2, nHeight-5, p2TickX-nWidth/2, 5);
}
private void DrawStatus()
{
Graphics g = screen;
FontMetrics fontmetrics = screen.getFontMetrics();
String s=null, time = MakeTime(gameTime);
int i = nHeight / 20;
int k=0, kt = fontmetrics.stringWidth(time);
if (worldCup)
{
switch (worldCupRound)
{
case 1: s = "Quarter Finals"; break;
case 2: s = "Semi-Finals"; break;
case 3: s = "Final"; break;
default: s = "Qualifying";
}
if (fGoldenGoal) s += " [Golden Goal]";
else if (fExtraTime) s += " [Extra Time]";
k = fontmetrics.stringWidth(s);
}
int mw = (k>kt) ? k : kt;
g.setColor(Color.blue);
g.fillRect(nWidth/2 - mw / 2 - 5, 0, mw + 10, i + 22);
g.setColor(Color.white);
screen.drawString(time, nWidth/2 - kt / 2, fontmetrics.getAscent() + 20);
if (s != null) screen.drawString(s, nWidth/2 - k/2, fontmetrics.getAscent()+20-fontmetrics.getHeight());
}
public void drawPrompt()
{
screen.setColor(Color.gray);
screen.fillRect(0, (4 * nHeight) / 5 + 6, nWidth, nHeight / 5 - 10);
drawPrompt(promptMsg, 0);
}
public void drawPrompt(String s, int i)
{
FontMetrics fontmetrics = screen.getFontMetrics();
screen.setColor(Color.lightGray);
screen.drawString(s, (nWidth - fontmetrics.stringWidth(s)) / 2, (nHeight * 4) / 5 + fontmetrics.getHeight() * (i + 1) + 10);
}
private void promptBox(String msg1, String msg2)
{
FontMetrics fontmetrics = screen.getFontMetrics();
int len1 = fontmetrics.stringWidth(msg1);
int len2 = fontmetrics.stringWidth(msg2);
int maxlen = (len1 > len2) ? len1 : len2;
screen.setColor(Color.darkGray);
screen.fillRect(nWidth/2-maxlen/2-20, nHeight*2/5, maxlen+40, nHeight/5);
screen.setColor(Color.white);
screen.drawString(msg1, nWidth/2-len1/2, nHeight*9/20);
screen.drawString(msg2, nWidth/2-len2/2, nHeight*11/20);
flip();
}
private void SaveReplayData()
{
replayData[replayPos][0] = p1X;
replayData[replayPos][1] = p1Y;
replayData[replayPos][2] = p2X;
replayData[replayPos][3] = p2Y;
replayData[replayPos][4] = ballX;
replayData[replayPos][5] = ballY;
replayData[replayPos][6] = p1Col;
replayData[replayPos][7] = p2Col;
replayPos++;
if(replayPos >= 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(Color.white);
g.drawString(slimeColText[p1Col] + " : " + p1Score, nWidth/20, k);
String p2ScrStr = p2Score + " : " + slimeColText[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()
{
worldCupRound = 0;
do {
initStuff();
replayPos = replayStart = 0;
scoringRun = 0;
fP1Touched = fP2Touched = false;
gameTime = 0;
startTime = System.currentTimeMillis();
fEndGame = false;
fCanChangeCol = false;
mousePressed = false;
gameTime = gameLength;
fInPlay = true;
fEndGame = false;
if (worldCup)
{
paint(getGraphics());
do {
p2Col = (int)(Math.random() * slimaryCols.length / 4)
+ worldCupRound * slimaryCols.length / 4;
} while (p1Col == p2Col);
String vs = slimeColText[p1Col] + " vs. " + slimeColText[p2Col];
switch (worldCupRound)
{
case 0: promptBox("Qualifying Round", vs); gameLength = 30000; break;
case 1: promptBox("Quarter Finals", vs); gameLength = 120000; break;
case 2: promptBox("Semi-Finals", vs); gameLength = 120000; break;
case 3: promptBox("World Cup Final", vs); gameLength = 300000; break;
}
try { Thread.sleep(4000); } catch (Exception e) { }
repaint();
flip();
}
while((gameTime > 0) || (worldCup && (worldCupRound > 0) && (p1Score == p2Score)))
{
gameTime = startTime + gameLength - System.currentTimeMillis();
if (gameTime < 0) gameTime = 0;
if (worldCup && !fExtraTime && (gameTime <= 0) && (worldCupRound > 0) && (p1Score == p2Score)) //go into extra time
{
String score = (p1Score==0) ? " nil" : " "+p1Score;
promptBox("The score is " + slimeColText[p1Col] + score + ", "
+ slimeColText[p2Col] + score + ".",
"And the game goes into extra time...");
try { Thread.sleep(4000); } catch (Exception e) { }
repaint(); flip();
startTime += 30000; gameTime += 30000;
fExtraTime = true;
}
else if ((gameTime <= 0) && fExtraTime && !fGoldenGoal && (p1Score == p2Score))
{
fGoldenGoal = true;
String score = (p1Score==0) ? " nil" : " "+p1Score;
promptBox("The score is " + slimeColText[p1Col] + score + ", "
+ slimeColText[p2Col] + score + ", and the game goes into Golden Goal.",
"The next player to score will win the match!");
try { Thread.sleep(4000); } catch (Exception e) { }
repaint(); flip();
}
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] + " Scores!"; }
else
{ p1Score++; promptMsg = slimeColText[p1Col] + " Scores!"; }
drawPrompt();
drawPrompt("Click mouse for replay...", 1);
flip();
mousePressed = false;
if(gameThread != null)
try { Thread.sleep(2500L); }
catch(InterruptedException _ex) { }
if(mousePressed)
{
SaveReplayData();
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -