如何显示我的Applet的个别动作?

时间:2015-05-23 14:53:52

标签: java applet

我目前正在制作一个模拟Tortoise vs. Hare Race的applet。他们每个人都有随机挑选的个人动作。我的小程序有效,但它只显示了Tortoise赢得的比赛结束。我希望它显示乌龟/野兔的个别动作,几乎就像一个GIF。

继承我的代码:

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;
int tortoiseXPos = 180, hareXPos = 180;
final int tortoiseYPos = 50, hareYPos = 400, SQUARE = 20;
int move;

public void init()
{
    tortoise = getImage(getDocumentBase(), "tortoise.gif");
    hare = getImage(getDocumentBase(), "hare.gif");
}

public void gameControl()
{
    //1200 is finish line
    while((tortoiseXPos < 1200) || (hareXPos < 1200))
    {
        move =  (int)(Math.random() * 10);
        tortoiseMoves(move);
        hareMoves(move);

        for(int i = 0; i < 10; i++)
        {
            delay();
        }
    }


}
public void paint(Graphics field)
{
    drawField(field);
    drawMove(field);
    //Display winner when they get to the finish line
    if(tortoiseXPos >= 1200)
    {
            field.setFont(new Font("Times New Roman", Font.ITALIC, 72));
            field.drawString("Tortoise Wins", 650, 240);
        }
        else if(hareXPos >= 1200)
        {
            field.setFont(new Font("Times New Roman", Font.ITALIC, 72));
            field.drawString("Tortoise 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 < 25; i++)
    {
        field.fillRect(x, 50, SQUARE, 50);
        field.fillRect(x, 400, SQUARE, 50);
        x += (SQUARE * 2);
    }
    field.setColor(Color.white);
    x = 200;
    for(int i = 0; i < 25; i++)
    {
        field.fillRect(x, 50, SQUARE, 50);
        field.fillRect(x, 400, SQUARE, 50);
        x += (SQUARE * 2);
    }
}

public void clearMove(Graphics s)
{
}

public void drawMove(Graphics s)
{
    gameControl();
    s.drawImage(tortoise, tortoiseXPos, 50, this);
    s.drawImage(hare, hareXPos, 400, this);

}
public void tortoiseMoves(int move)
{
    //Moves for Tortoise
    if(move <= 5)
    {
        tortoiseXPos += (3 * SQUARE);
    }
    else if(move <= 8)
    {
        tortoiseXPos += SQUARE;
    }
    else if(move <= 10)
    {
        tortoiseXPos -= (6 * SQUARE);
    }

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

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

public void hareMoves(int move)
{
    //Moves for Hare
    if(move <= 2)
    {
        hareXPos += (9 * SQUARE);
    }
    if(move <= 5)
    {
        hareXPos += (SQUARE);
    }
    if(move <= 6)
    {
        hareXPos -= (SQUARE);
    }
    if(move <= 8)
    {
        hareXPos -= (2 * SQUARE);
    }
    if(move <= 10)
    {
        hareXPos = hareXPos;
    }

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

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

public void delay()
{
    //To see individual moves
    for(int i = 0; i <= 90000000; i++)
    {}
}
}

如果你们能给我一些关于使用什么方法或者我应该怎么做的一些指示,我会很感激。感谢

1 个答案:

答案 0 :(得分:1)

向您的班级添加两个阵列,并为每个赛车手存储每个动作。然后,&#34;画&#34;随着比赛的进行,屏幕上每个赛车的积分。