代码中的矩形半径

时间:2015-09-27 20:02:53

标签: java android

我需要你的帮助。我有一个简单的Android游戏,其中障碍物是矩形,但我想为这些矩形增加半径,所以游戏将有点“平滑”#34;看。

这是我的代码:

Rectangle.java

    package com.mygame.mygame;

    import com.badlogic.gdx.graphics.g2d.Sprite;

    public class Rectangle {

        public Sprite sp;

        public Rectangle(float x, float width){

            sp = new Sprite(GeneralStorage.pixel);
            sp.setSize(GeneralStorage.w * width, GeneralStorage.height_obstacles);
            sp.setPosition(GeneralStorage.w * x, GeneralStorage.h);
            sp.setColor(GeneralStorage.color_bars);

        }


        public void move(){

            sp.setPosition(sp.getX(), sp.getY() - RunningUpdate.pixels_fall);


        }

        public void draw(){


            sp.draw(GeneralStorage.batch);

        }


    }

Obstacle.java

package com.mygame.mygame;

import com.badlogic.gdx.math.MathUtils;

import java.util.ArrayList;

public class Obstacle {

public ArrayList<com.mygame.mygame.Rectangle> rectangles;
public boolean moving = false;

public Obstacle(){

    initilize_rectangles();

}


public void initilize_rectangles(){

    rectangles = new ArrayList<com.mygame.mygame.Rectangle>();
    moving = false;

    switch(MathUtils.random(1,7)){


        case 1:

            rectangles.add(new com.mygame.mygame.Rectangle(0.2f, 0.6f));

            break;

        case 2:

            rectangles.add(new com.mygame.mygame.Rectangle(0.1f, 0.3f));
            rectangles.add(new com.mygame.mygame.Rectangle(0.6f, 0.3f));

            break;

        case 3:

            rectangles.add(new com.mygame.mygame.Rectangle(0.0f, 0.35f));
            rectangles.add(new com.mygame.mygame.Rectangle(0.65f, 0.35f));

            break;

        case 4:

            rectangles.add(new com.mygame.mygame.Rectangle(0.3f, 0.4f));

            break;

        case 5:

            rectangles.add(new com.mygame.mygame.Rectangle(0, 0.4f));

            break;

        case 6:

            rectangles.add(new com.mygame.mygame.Rectangle(0.6f, 0.4f));

            break;

        case 7:

            rectangles.add(new com.mygame.mygame.Rectangle(0, 0.1f));
            rectangles.add(new com.mygame.mygame.Rectangle(0.35f, 0.3f));
            rectangles.add(new com.mygame.mygame.Rectangle(0.9f, 0.1f));

            break;



    }



}

public void move(){

    if(moving) {
        for (com.mygame.mygame.Rectangle rectangle : rectangles)
            rectangle.move();
      // Gdx.app.log("muevo", "muevo");
    }

    if(rectangles.get(0).sp.getY() <= - rectangles.get(0).sp.getHeight()){
        moving = false;
        initilize_rectangles();
    }

}

public void change_possible(){

    if(rectangles.get(0).sp.getY() + rectangles.get(0).sp.getHeight() < GeneralStorage.meatball.big_ball.sp.getY()){

        if(com.mygame.mygame.RunningUpdate.possible_crash == GeneralStorage.obstacles.size() - 1)
            com.mygame.mygame.RunningUpdate.possible_crash = 0;
        else com.mygame.mygame.RunningUpdate.possible_crash++;

        com.mygame.mygame.RunningUpdate.score++;
        com.mygame.mygame.RunningUpdate.increase_difficulty();
    }

}


public void draw(){

    if(moving)
    for(com.mygame.mygame.Rectangle rectangle: rectangles)
        rectangle.draw();

}

}

我会非常感谢任何帮助!

1 个答案:

答案 0 :(得分:0)

我认为拉米有你的解决方案;只是将它与矩形放在一起的问题。

所以我为你做了一些挖掘。我希望我能帮到你。我不是想偷走拉米的答案。我喜欢数学和游戏。

https://github.com/libgdx/libgdx/blob/fa21bbd665de1d25f326bc0f78acc862e6ca2a7b/gdx/src/com/badlogic/gdx/graphics/Pixmap.javafillCircle的定义:

/** Fills a circle with the center at x,y and a radius using the current color.
 * 
 * @param x The x-coordinate of the center
 * @param y The y-coordinate of the center
 * @param radius The radius in pixels */

public void fillCircle (int x, int y, int radius) 
{
    pixmap.fillCircle(x, y, radius, color);
}

fillRectangle

/** Fills a rectangle starting at x, y extending by width to the right and 
     * by height downwards (y-axis points downwards) using the current color.
 * 
 * @param x The x coordinate
 * @param y The y coordinate
 * @param width The width in pixels
 * @param height The height in pixels */

public void fillRectangle (int x, int y, int width, int height) 
{
    pixmap.fillRect(x, y, width, height, color);
}

我认为您只需要调整他引用的代码,方法是添加(调用中的x坐标)您想要舍入的矩形的实际x位置。不知道如何处理y;希望你这样做。

Rami引用的示例,为了更好地理解而格式化:

public static Pixmap getPixmapRoundedRectangle(int width, int height, int radius, int color) {

 pixmap = new Pixmap(width, height, Format.RGBA8888);
 pixmap.setColor(color);


// pink rectangle

 pixmap.fillRectangle(0     ,radius, pixmap.getWidth()           , pixmap.getHeight()-2*radius);

// green rectangle

 pixmap.fillRectangle(radius,0     , pixmap.getWidth() - 2*radius, pixmap.getHeight()         );


// Bottom-left circle

 pixmap.fillCircle(radius, radius                                     , radius);

// Top-left circle

pixmap.fillCircle(radius                   , pixmap.getHeight()-radius, radius);

// Bottom-right circle

 pixmap.fillCircle(pixmap.getWidth()-radius, radius                   , radius);

// Top-right circle

 pixmap.fillCircle(pixmap.getWidth()-radius, pixmap.getHeight()-radius, radius);

 return pixmap;
}

如果你不想涉及另一个图书馆,也许这些想法会让你走上正确的道路来制作自己的fillCircle