0

私は Java 入門で、私のスキルは限られているので、先生は私たちにスクリーンセーバーを作ってほしいと言っています。ここでの私の目標は、複数の熱気球オブジェクトが同時に画面の周りを跳ね返り、壁にぶつかると方向がランダムに変わることです。私はランダムにバウンドする風船を1つ手に入れました.
私が助けを必要としている問題は、アプレットに 2 番目の画像を追加すると、両方の画像がリンクしているように見え、まったく同じように動き、一方が方向を変えると、もう一方も同じように動くということです。別々に動く?ここに私のコードがあります。

***
import acm.program.*;
import acm.graphics.*;
import java.awt.Color;

public class HotAirBalloons extends GraphicsProgram
{

    private static final int APPLET_WIDTH = 800;
    private static final int APPLET_HEIGHT = 600;
    private int speedX = 1;
    private int speedY = 1;

    public void init()
    {
        setSize(APPLET_WIDTH,APPLET_HEIGHT);
        setBackground(new Color(100,210,255));
    }

    public void moveRandomDirection()
    {
        double direction = Math.random() * 2.0 * Math.PI;
        double speed = 3.0;
        speedX = (int) (speed * Math.cos(direction));
        speedY = (int) (speed * Math.sin(direction));
    }

    public void run()
    {

        GImage img1 = new GImage("balloon.jpg");
        add(img1, 0, 0);
        GImage img2 = new GImage("balloon.jpg");
        add(img2, 200, 200);

        while(true)
        {
            pause(15);
            img1.move(speedX, speedY);
            img2.move(speedX, speedY);

            if (img1.getX() > APPLET_WIDTH - 50)
            {
                moveRandomDirection(); 
            }

            if (img1.getX() < 1)
            {
                moveRandomDirection(); 
            }

            if (img1.getY() +85 > APPLET_HEIGHT)
            {
                moveRandomDirection(); 
            }
            if (img1.getY() < 1)
            {
                moveRandomDirection(); 
            }
            if (img2.getX() > APPLET_WIDTH - 50)
            {
                moveRandomDirection(); 
            }

            if (img2.getX() < 1)
            {
                moveRandomDirection(); 
            }

            if (img2.getY() +85 > APPLET_HEIGHT)
            {
                moveRandomDirection(); 
            }
            if (img2.getY() < 1)
            {
                moveRandomDirection(); 
            }
        }   
    }
}
4

2 に答える 2

2

これはうまくいくはずです:

import acm.program.*;
import acm.graphics.*;
import java.awt.Color;
import java.awt.Point;

public class HotAirBalloons extends GraphicsProgram
{
    private int speed1 = new Point(1, 1);
    private int speed2 = new Point(1, 1);

    public Point moveRandomDirection()
        {
            double direction = Math.random() * 2.0 * Math.PI;
            double speed = 3.0;
            return new Point((int) (speed * Math.cos(direction)), (int) (speed * Math.sin(direction)));
        }

    public void run()
    {

        GImage img1 = new GImage("balloon.jpg");
        add(img1, 0, 0);
        GImage img2 = new GImage("balloon.jpg");
        add(img2, 200, 200);

        while(true)
        {
            pause(15);
            img1.move(speed1.x, speed1.y);
            img2.move(speed2.x, speed2.y);



            if (img1.getX() > APPLET_WIDTH - 50 || img1.getX() < 1)
            {
                speed1 = moveRandowmDirection();
            }

            if (img1.getY() +85 > APPLET_HEIGHT || img1.getY() < 1)
            {
                speed1 = moveRandomDirection(); 
            }

            if (img2.getX() > APPLET_WIDTH - 50 || img2.getX() < 1)
            {
                speed2 = moveRandomDirection(); 
            }

            if (img2.getY() +85 > APPLET_HEIGHT || img2.getY() < 1)
            {
                speed2 = moveRandomDirection(); 
            }
        }   
    }
}

編集:これにより、「リンクされた」動作が修正され、画像がエッジと衝突したときの問題を修正するためのRob Wattsの応答に賛成票を投じました。

于 2013-04-17T17:51:15.800 に答える
1

You're using the same speedX and speedY variable for both of the balloons. Give them their own speed variables.

Also, the reason why the balloons "wig out sometimes" is that when you choose a random direction, you don't specify that it can't be in the direction that you're trying to get them to go away from.

To fix that issue, I'd suggest that you create a moveRandomDirection method for each balloon, an pass in a value that represents which edge they have hit. Then restrict the direction they can move to be any of the other directions.

Try this:

public void moveBalloonOneInRandomDirection(int whichEdge)
{
    double direction = 0;
    switch(whichEdge) {
    case(0): // Top edge
        direction = Math.random() * Math.PI + ONLY_ALLOW_DOWN_VAL;
    case(1): // Left edge
        direction = Math.random() * Math.PI + ONLY_ALLOW_RIGHT_VAL;
    case(2): // Bottom edge
        direction = Math.random() * Math.PI + ONLY_ALLOW_UP_VAL;
    case(3): // Right edge
        direction = Math.random() * Math.PI + ONLY_ALLOW_LEFT_VAL;
    }
    double direction = Math.random() * 2.0 * Math.PI;
    double speed = 3.0;
    speedX1 = (int) (speed * Math.cos(direction));
    speedY1 = (int) (speed * Math.sin(direction));
}

Then in your if statements, you would have something like this:

if (img1.getX() > APPLET_WIDTH - 50) // Right edge
{
    moveBalloonOneInRandomDirection(3);
}

You should probably also use constants, so that instead of the "magic numbers", you would have

private static final int RIGHT_EDGE = 3;
...
moveBalloonOneInRandomDirection(RIGHT_EDGE);
于 2013-04-17T17:48:25.120 に答える