gpt4 book ai didi

Java:在龟兔赛跑小程序中正确显示位置

转载 作者:行者123 更新时间:2023-11-30 03:25:10 24 4
gpt4 key购买 nike

我目前正在开发一个项目,该项目以 Applet 形式实时显示龟兔赛跑。选择一个随机数字,动物根据该数字要么向前、向后移动,要么根本不移动。我的问题是如何实时看到动物。目前它会在起始位置显示图像,并说明谁获胜。任何关于如何完成这项工作的指示都会很棒。

我的代码:

import java.applet.Applet;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Color;
import java.awt.Font;

public class Project2 extends Applet
{
Image tortoise, hare;
Image scaledTortoise, scaledHare;
final int tortoiseYPos = 50, hareYPos = 400, SQUARE = 20, END = 1200;
int tortoiseXPos = 180, hareXPos = 180;

public void init()
{
tortoise = getImage(getDocumentBase(), "tortoise.gif");
hare = getImage(getDocumentBase(), "hare.gif");
scaledTortoise = tortoise.getScaledInstance(20, 50, Image.SCALE_SMOOTH);
scaledHare = hare.getScaledInstance(20, 50, Image.SCALE_SMOOTH);
}

public void paint(Graphics field)
{
drawField(field);
drawMoves(field);

field.setFont(new Font("Times New Roman", Font.ITALIC, 72));
//Display winner when they get to the finish line
if(tortoiseXPos >= END)
{
field.drawString("Tortoise Wins!!", 650, 240);
}
else if(hareXPos >= END)
{
field.drawString("Hare Wins!!", 650, 240);
}
}

public void drawField(Graphics field)
{
setBackground(Color.green);
Font f = new Font("Times New Roman", Font.BOLD, 48);
field.setFont(f);
field.drawString("Tortoise", 0, 75);
field.drawString("Hare", 0, 425);

//fill alternating black and white rectangles
field.setColor(Color.black);
int x = 180;
for(int i = 0; i < 50; i++)
{
field.fillRect(x, 50, SQUARE, 50);
field.fillRect(x, 400, SQUARE, 50);
x += (SQUARE);
}


field.drawImage(scaledTortoise, 180, tortoiseYPos, this);
field.drawImage(scaledHare, 180, hareYPos, this);
}

public void drawMoves(Graphics s)
{
while(tortoiseXPos < END && hareXPos < END)
{
int move = (int)(Math.random() * 10);
tortoiseMoves(move); hareMoves(move);
s.drawImage(scaledTortoise, tortoiseXPos, tortoiseYPos, this);
s.drawImage(scaledHare, hareXPos, hareYPos, this);
delay(); delay(); delay();
}
}
public void tortoiseMoves(int move)
{ //Moves for Tortoise, 180 is start, 1200 is finish
if(move <= 5)
{
tortoiseXPos += (3 * SQUARE);
}
else if(move <= 8)
{
tortoiseXPos += SQUARE;
}
else if(move <= 10)
{
tortoiseXPos -= (6 * SQUARE);
}

if(tortoiseXPos < 180)
{
tortoiseXPos = 180;
}

if(tortoiseXPos > END)
{
tortoiseXPos = END;
}

}

public void hareMoves(int move)
{ //Moves for Hare, 180 is start, 1200 is finish
if(move <= 2)
{
hareXPos += (9 * SQUARE);
}
else if(move <= 5)
{
hareXPos += (SQUARE);
}
else if(move <= 6)
{
hareXPos -= (12 * SQUARE);
}
else if(move <= 8)
{
hareXPos -= (2 * SQUARE);
}
else if(move <= 10)
{
hareXPos = hareXPos;
}

if(hareXPos < 180)
{
hareXPos = 180;
}

if(hareXPos > END)
{
hareXPos = END;
}
}

public void delay()
{
for(int i = 0; i <= 90000000; i++)
{
}
}
}

我应该注意,我不熟悉也不应该使用 java swing,因为我应该只使用 awt。

最佳答案

问题出在你的延迟函数上。输入循环会使小程序继续运行您的代码,并且它没有机会在移动之间呈现显示。如果您允许小程序运行的线程 hibernate ,那么它就有机会在运行时显示。

public void delay()
{
try {
Thread.sleep(100);
} catch (Exception e) {}
}

关于Java:在龟兔赛跑小程序中正确显示位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30429467/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com