📄 worldcupsoccerslime.java
字号:
DoReplay();
}
promptMsg = "";
drawPrompt();
playOnTicks = 10;
fPlayOn = false;
startTime += System.currentTimeMillis() - l;
ballX = 490+(int)(Math.random()*20);
ballY = 190+(int)(Math.random()*20);
ballVX = 0;
ballVY = 0;
p1X = 200; p1Y = 0; p1YV = 0;
p2X = 800; p2Y = 0; p2YV = 0;
replayStart = replayPos = 0;
repaint();
}
if(gameThread != null)
try
{
if (fPlayOn) Thread.sleep(120L); else Thread.sleep(20);
}
catch(InterruptedException _ex) { }
}
fEndGame = true;
if (fPlayOn)
{
if (nScoreX < 500)
{
p2Score++;
promptMsg = slimeColText[p2Col] + " scores at the final whistle!";
}
else
{
p1Score++;
promptMsg = slimeColText[p1Col] + " scores at the final whistle!";
}
drawPrompt();
}
else
drawPrompt("And that's the final whistle!", 0);
if (worldCup)
{
if (p1Score == p2Score)
{
drawPrompt("It's a draw at full time, here at Slime Stadium!", 1);
promptBox("You played well, but a draw is not enough.", "You have been eliminated.");
worldCup = false;
flip();
}
else if (p1Score >= p2Score)
{
switch (worldCupRound)
{
case 0: drawPrompt(slimeColText[p1Col] + " qualifies for the world cup!", 1); break;
case 1: drawPrompt(slimeColText[p1Col] + " proceeds to the semi-finals!", 1); break;
case 2: drawPrompt(slimeColText[p1Col] + " is through to the final!!!", 1); break;
case 3: drawPrompt(slimeColText[p1Col] + " wins the WORLD CUP!!!!!", 1); break;
}
if (worldCupRound == 3)
{
worldCup = false;
promptBox("You win the world cup!!!", "Congratulations!");
}
else
worldCupRound++;
}
else
{
switch (worldCupRound)
{
case 0:
case 1: promptBox("You have been eliminated.", "Goodbye."); break;
case 2: promptBox("You have been knocked out of the semifinals.", "You played well."); break;
case 3: promptBox("You came second.", "Are you satisfied with that?"); break;
}
worldCup = false;
}
}
else
{
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();
} while (worldCup);
fCanChangeCol = true;
fInPlay = false;
repaint();
}
public void init()
{
nWidth = size().width;
nHeight = size().height;
fInPlay = fEndGame = false;
fCanChangeCol = true;
initStuff();
promptMsg = "Click on an option to play...";
backBuffer = createImage(nWidth, nHeight);
screen = getGraphics();
screen.setFont(new Font(screen.getFont().getName(), 1, 15));
}
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);
}
//AI helper: returns the X coordinate where the ball will land
private int getBallBounceX()
{
int t = ballVY + (int)Math.sqrt(ballVY*ballVY+2*ballY);
int ballBounceX = ballX + t * ballVX;
if (ballBounceX < 0) ballBounceX = -ballBounceX;
if (ballBounceX > 1000) ballBounceX = 1000-ballBounceX;
return ballBounceX;
}
//AI helper: returns the maximum Y coordinate the ball is expected to reach
private int getBallMaxY()
{
if (ballVY < 0) return ballY;
return ballY + ballVY * ballVY / 2;
}
//AIs start here.
//controlP2v0 - first crack at an AI.
private void controlP2v0()
{
p2XV = 0;
//try and make the ball go leftwards
if ((ballX>p2X+5) && (ballX<960)) fP2Sticky = true;
if (ballX>p2X-10) p2XV = SLIMEVEL;
if ((ballX+30>p2X) && (p2YV == 0)) { fP2Sticky = false; p2YV = JUMPVEL; }
if (ballX+50<p2X) { fP2Sticky = false; p2XV = -SLIMEVEL; }
if ((ballX > p2X + 50) && (p2YV == 0) && (ballY > 10) && (ballY < 150)) p2YV = JUMPVEL;
//if we're in our penalty box and we're running out of time, get out of it.
if ((p2TouchingGoal > 0) &&
((MAX_TICKS_TOUCHING_GOAL - p2TouchingGoal) < 3+(p2X-850) / SLIMEVEL)) p2XV = -SLIMEVEL;
}
//controlP2v1 - the previous AI with lots of the holes patched.
private void controlP2v1()
{
p2XV = 0;
int bounceX = getBallBounceX();
int ballMaxY = getBallMaxY();
int ballMaxYT = (ballVY < 1) ? 1 : ballVY;
//if the ball's going to land in our goal square, make sure we're running backwards
if (bounceX > 900) p2XV = SLIMEVEL;
//try and make the ball go leftwards
if (bounceX+20<p2X) { fP2Sticky = false; p2XV = -SLIMEVEL; }
if (ballX>p2X-10) p2XV = SLIMEVEL;
if ((ballX+30>p2X) && (p2YV == 0)) { fP2Sticky = false; p2YV = JUMPVEL; }
if ((bounceX > p2X + 50) && (p2YV == 0)) { p2XV = SLIMEVEL; }
if ((ballX>p2X) && (ballX<960)) fP2Sticky = true;
//if the opponent is up in the air, and about the same height as the ball, retreat
if ((p2YV==0) && (ballX>p1X-120) && (ballX<p1X+120) && (ballY > p1Y)
&& (ballY < p1Y+100) && (p1Y > 0)) p2XV = SLIMEVEL;
//if the opponent has the ball and we're near his end of the court,
//and we're not losing, back off and goal hang
if ((p2Score >= p1Score) && ((bounceX < 200) && (p2X > p1X)) ||
((bounceX < p1X+50) && (bounceX > p1X-50) &&
(ballVY/4 == 0) && (p1X < 400) && (p2X < 848)))
{
if (p2X < 900) p2XV = SLIMEVEL;
if ((ballX > 800) && (bounceX > 950) && (p2YV == 0) && (ballMaxY > 40))
p2YV = JUMPVEL;
}
//stop us jumping in various circumstances
if (p2YV == JUMPVEL)
{
if (ballMaxY < 110) p2YV = 0;
if (ballX < p2X-400) p2YV = 0;
if (ballY < 80) p2YV = 0;
if ((ballX < 900) && (p2X > 900)) p2YV = 0;
if (p2X < 150) p2YV = 0;
}
//if we're in our penalty box and we're running out of time, get out of it.
if ((p2TouchingGoal > 0) &&
((MAX_TICKS_TOUCHING_GOAL - p2TouchingGoal) < 3+(p2X-850) / SLIMEVEL)) p2XV = -SLIMEVEL;
}
//controlP2v2 - Yet more hole patching.
private void controlP2v2()
{
int bounceX = getBallBounceX();
int ballMaxY = getBallMaxY();
int ballMaxYT = (ballVY < 1) ? 1 : ballVY;
//by default, park at pos. 800
if (p2X < 790) p2XV = SLIMEVEL;
else if (p2X > 830) p2XV = -SLIMEVEL;
else p2XV = 0;
//if the ball's going to land in our goal square, make sure we're running backwards
if (bounceX > 900) p2XV = SLIMEVEL;
//try and make the ball go leftwards
if (bounceX+20<p2X) { fP2Sticky = false; p2XV = -SLIMEVEL; }
if (ballX>p2X-10) p2XV = SLIMEVEL;
if ((ballX+30>p2X) && (p2YV == 0)) { fP2Sticky = false; p2YV = JUMPVEL; }
if ((bounceX > p2X + 50) && (p2YV == 0)) { p2XV = SLIMEVEL; }
if ((ballX>p2X) && (ballX<960)) fP2Sticky = true;
//if the opponent is up in the air, and about the same height as the ball, retreat
if ((p2YV==0) && (ballX>p1X-120) && (ballX<p1X+120) && (ballY > p1Y)
&& (ballY < p1Y+100) && (p1Y > 0)) p2XV = SLIMEVEL;
//if the opponent has the ball and we're near his end of the court,
//and we're not losing, back off and goal hang
if ((p2Score >= p1Score) && ((bounceX < 200) && (p2X > p1X)) ||
((bounceX < p1X+50) && (bounceX > p1X-50) &&
(ballVY/4 == 0) && (p1X < 400) && (p2X < 848)))
{
if (p2X < 900) p2XV = SLIMEVEL;
if ((ballX > 800) && (bounceX > 950) && (p2YV == 0) && (ballMaxY > 40))
p2YV = JUMPVEL;
}
//stop us jumping in various circumstances
if (p2YV == JUMPVEL)
{
if (ballMaxY < 110) p2YV = 0;
if (ballX < p2X-400) p2YV = 0;
if (ballY < 80) p2YV = 0;
if ((ballX < 900) && (p2X > 900)) p2YV = 0;
}
//if we're up the front of the court and the ball is going to
//land way behind us, jump whatever happens
if ((p2YV == 0) && (p2X < 400) && (bounceX > 500) && (ballMaxY > 50)) p2YV = JUMPVEL;
//if we're in our penalty box and we're running out of time, get out of it.
if ((p2TouchingGoal > 0) &&
((MAX_TICKS_TOUCHING_GOAL - p2TouchingGoal) < 3+(p2X-850) / SLIMEVEL)) p2XV = -SLIMEVEL;
}
//controlP2v3 - One or two more holes patched
private void controlP2v3()
{
int SLIMEVEL = this.SLIMEVEL * 4 / 3;
int bounceX = getBallBounceX();
int ballMaxY = getBallMaxY();
int ballMaxYT = (ballVY < 1) ? 1 : ballVY;
//by default, park at pos. 800
if (p2X < 790) p2XV = SLIMEVEL;
else if (p2X > 830) p2XV = -SLIMEVEL;
else p2XV = 0;
//if the ball's going to land in our goal square, make sure we're running backwards
if (bounceX > 900) p2XV = SLIMEVEL;
//try and make the ball go leftwards
if (bounceX+20<p2X) { fP2Sticky = false; p2XV = -SLIMEVEL; }
if (ballX>p2X-10) p2XV = SLIMEVEL;
if ((ballX+30>p2X) && (p2YV == 0)) { fP2Sticky = false; p2YV = JUMPVEL; }
if ((bounceX > p2X + 50) && (p2YV == 0)) { p2XV = SLIMEVEL; }
if ((ballX>p2X) && (ballX<960)) fP2Sticky = true;
//if the opponent is up in the air, and about the same height as the ball, retreat
if ((p2YV==0) && (ballX>p1X-120) && (ballX<p1X+120) && (ballY > p1Y)
&& (ballY < p1Y+100) && (p1Y > 0)) p2XV = SLIMEVEL;
//if the opponent has the ball and we're near his end of the court,
//and we're not losing, back off and goal hang
if ((p2Score >= p1Score) && ((bounceX < 200) && (p2X > p1X)) ||
((bounceX < p1X+50) && (bounceX > p1X-50) &&
(ballVY/4 == 0) && (p1X < 400) && (p2X < 848)))
{
if (p2X < 900) p2XV = SLIMEVEL;
if ((ballX > 800) && (bounceX > 950) && (p2YV == 0) && (ballMaxY > 40))
p2YV = JUMPVEL;
}
//stop us jumping in various circumstances
if (p2YV == JUMPVEL)
{
if (ballMaxY < 110) p2YV = 0;
if (ballX < p2X-400) p2YV = 0;
if (ballY < 80) p2YV = 0;
if ((ballX < 900) && (p2X > 900)) p2YV = 0;
if ((p2XV > 0) && (ballMaxY > 200) && (bounceX > p2X + 300)) p2YV = 0;
}
//if we're up the front of the court and the ball is going to
//land way behind us, jump whatever happens
if ((p2YV == 0) && (p2X < 400) && (bounceX > p2X+400) && (ballMaxY > 50)) p2YV = JUMPVEL;
//if we're in our penalty box and we're running out of time, get out of it.
if ((p2TouchingGoal > 0) &&
((MAX_TICKS_TOUCHING_GOAL - p2TouchingGoal) < 3+(p2X-850) / SLIMEVEL)) p2XV = -SLIMEVEL;
}
private void p(String s) { System.out.println(s); }
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -