java - 为什么我的鱼不转头?

标签 java animation graphics

我无法将鱼移动到另一个地方并在到达右边缘或左边缘时转身。窗口大小为 600 x 400。在方法 void Swim(); 中,无论我如何更改,鱼都会到达相同的位置(鱼越过窗口的右边缘)。如何移动鱼,当它到达右边缘或左边缘时转身并移动到窗口的左边缘?

注意:我正在使用GWindow ,这是我学校提供的类(class)。

import java.awt.Color;
import java.util.Random;    
import uwcse.graphics.*;

/**
 * A fish in a graphics window
 */

public class Fish {
    public static final int LEFT_MOVING = 0;
    public static final int RIGHT_MOVING = 0;

    // The graphics window the fish belongs to
    private GWindow window;

    // The location of the fish
    private int x;

    private int y;

    // Scale of the drawing of the fish
    private double scale;

    private Color color;

    private int direction;

    private Oval O1; Oval O2;
    private Triangle tail1; Triangle tail2;
    private Line eye1; Line eye2;

    /**
     * Draws a Fish in a graphics window
     * 
     * @param x
     *            the x coordinate of the location of the fish
     * @param y
     *            the y coordinate of the location of the fish
     * @param scale
     *            the scale of the drawing of the fish
     * @param window
     *            the graphics window the fish belongs to
     */

    public Fish(int x, int y, double scale,int direction, GWindow window) {
        // Initialize the instance fields
        this.x  = x;
        this.y = y;
        this.scale = scale;
        this.window = window;
        this.color = randomColor();
        this.direction = direction;

        // Draw the fish
        this.draw();
    }
    private Color randomColor(){
        Random random = new Random();
        int R = random.nextInt(256);
        int G = random.nextInt(256);
        int B = random.nextInt(256);
        return new Color(R,B,G);            
    }       

    /**
     * Draws this fish
     */
    private void draw() {

        int Fsize = (int)(this.scale*15);

        this.O1 = new Oval(
                this.x - Fsize /3,                                        
                this.y - Fsize /7 ,               
                Fsize + 4*Fsize/3,                                        
                Fsize  + Fsize/5,                                              
                this.color,true);       
        this.tail1 =new Triangle(
                this.x - Fsize,                                                              
                this.y,                                                         
                this.x - 2/Fsize,                                                        
                this.y + Fsize/2,                                                        
                this.x - Fsize,                                                      
                this.y + Fsize,this.color,true);
        this.eye1 = new Line(
                this.x + 15*Fsize/10,                                       
                this.y + 5*Fsize/10  ,                                      
                this.x + 15 *Fsize/10,                                      
                this.y + 5* Fsize /10,                                      
                Color.BLACK);   
        Oval O2 = new Oval(
                this.x - 37*Fsize/5,                      
                this.y+ 27*Fsize/5 ,                     
                Fsize + 2*Fsize/3,    
                Fsize  + Fsize/5,                   
                this.color,true);       
        Line eye2 = new Line(                
                this.x - 35*Fsize/5,                 
                this.y + 30*Fsize/5,                
                this.x - 35*Fsize/5,                 
                this.y + 30*Fsize/5, Color.BLACK);                      
        Triangle tail2 =new Triangle(
                this.x - Fsize*5 ,              
                this.y + 13*Fsize/2,                 
                this.x - 10*Fsize/2,                 
                this.y + 11*Fsize/2,                 
                this.x - Fsize*6,                
                this.y + Fsize*6,this.color,true);          
        this.window.add(O1);
        this.window.add(O2);
        this.window.add(tail2);
        this.window.add(tail1);
        this.window.add(eye1);
        this.window.add(eye2);
    }


    public void moveBy (int dx, int dy){
        x += dx;
        y += dy;        
    }

    public void swim(){
        if (RIGHT_MOVING >= 250 || this.direction == RIGHT_MOVING){
            this.O1.moveBy(3,0);
            this.tail1.moveBy(3,0);
            this.eye1.moveBy(3,0);
        }
    }    
}    

最佳答案

您需要测试鱼是否与边界发生碰撞,如果是这样,则更改动画设置以更改移动方向。这需要在移动鱼的代码中使用一两个 if block 。在伪代码中,它看起来像:

if fish location is equal to or less than 0 
   change deltaX to a positive value
if fish location + fish width is equal to or greater than width of tank
   change deltaX to a negative value 

move fish by deltaX

我通常使用 detlaX 的绝对值,即

deltaX = Math.abs(deltaX);

deltaX = -Math.abs(deltaX);

而不是简单地交换符号。原因是,如果您只是交换标志,就有可能让鱼卡在角落里。

关于java - 为什么我的鱼不转头?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31549170/

相关文章:

javascript - 使用 "slide"效果显示/隐藏 <input>

python - 使用 PIL 将一张图片复制到另一张图片上

Java 图形在计时器中不工作

java - 使用 Apache POI 复制 Excel 工作表

java - "contains not only spaces"的正则表达式模式

python - 如何在 matplotlib 中更新 3D 箭头动画

animation - 在 ScrollView 中使用匹配几何效果动画的问题

javascript - Phaser - 显示图形对象 50 毫秒,并在同时按下一个或多个按键时销毁每个按键

java - 使用 JSF 在 Google App Engine 中丢失 session

Java泛型键盘输入