首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >代码中的矩形半径

代码中的矩形半径
EN

Stack Overflow用户
提问于 2015-09-27 20:02:53
回答 1查看 352关注 0票数 0

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

这是我的密码:

Rectangle.java

代码语言:javascript
运行
复制
    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

代码语言:javascript
运行
复制
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();

}

}

我会非常感谢你的帮助!

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-09-27 21:48:08

我认为Rami有你的解决方案,只是一个把它和你的矩形放在一起的问题。

所以我为你做了些调查。希望我能帮上忙。我不是想偷拉米的答案。我只是喜欢数学和游戏。

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

代码语言:javascript
运行
复制
/** 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

代码语言:javascript
运行
复制
/** 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引用的例子是:

代码语言:javascript
运行
复制
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方面走上正轨。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/32812278

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档