图像在多边形

时间:2016-04-12 20:39:35

标签: java graphics

对于一个项目,我需要编写一个简单的2D游戏,飞机必须保护城市免受坠落的炸弹袭击。首先,我创建了所有游戏对象的多边形,以便拥有一个" hitbox"现在我想把实际的精灵放在上面。现在的问题是图片直接在Polygon下绘制,而不是它应该如何做,它让我发疯。

玩家等级

 public class Player extends GameObject {

private GameState gameState = new GameState();


public Player ()
{
    this.active=true;
    this.gameRelevant=true;

    this.height=game.GameSettings.playerHeight;
    this.width = game.GameSettings.playerWidth;
    this.color=Color.BLACK;

    this.position= new Point2D.Double(game.GameSettings.gamePanelWidth/2,game.GameSettings.gamePanelHeight/2);
    this.damage=0;
    this.polygon = game.GameController.BuildingType.getShape(game.GameSettings.playerPolygonXValues, game.GameSettings.playerPolygonYValues);
    this.img = io.ImageLoader.getPlayerLookingToRightImage(this.width, this.height);
}
    public void draw (Graphics2D g)
{
    AffineTransform spieler = new AffineTransform();
    spieler.translate(this.position.x, this.position.y);
    Shape s = spieler.createTransformedShape(this.polygon);

    g.setColor(this.color);
    g.fill(s);
    g.drawImage(img,spieler,null);



    g.draw(s);



}

用于创建多边形的GetShape方法

    public static Polygon getShape (double[] varx, double[] vary)
    {   
        Polygon hitbox= new Polygon();


                if(varx == GameSettings.churchPolygonXValues)       
                {
                    for(int j=0; j<GameSettings.churchPolygonXValues.length;j++)
                    {
                        hitbox.addPoint((int)(varx[j]*GameSettings.churchWidth), (int)(vary[j]*GameSettings.churchHeight));
                    }

                }


                if(varx == GameSettings.houseBluePolygonXValues)
                {   
                    for(int j=0; j<GameSettings.houseBluePolygonXValues.length;j++)
                    {
                        hitbox.addPoint((int)(varx[j]*GameSettings.houseBlueWidth), (int)(vary[j]*GameSettings.houseBlueHeight));
                    }
                }

                if(varx == GameSettings.houseRedPolygonXValues)
                {
                    for(int j=0; j<GameSettings.houseRedPolygonXValues.length;j++)
                    {
                        hitbox.addPoint((int)(varx[j]*GameSettings.houseRedWidth), (int)(vary[j]*GameSettings.houseRedHeight));
                    }

                }

                if(varx == GameSettings.houseYellowPolygonXValues)
                {
                    for(int j=0; j<GameSettings.houseYellowPolygonXValues.length;j++)
                    {
                        hitbox.addPoint((int)(varx[j]*GameSettings.houseYellowWidth), (int)(vary[j]*GameSettings.houseYellowHeight));
                    }

                }

                if(varx == GameSettings.playerPolygonXValues)
                {
                    for(int j=0; j<GameSettings.playerPolygonXValues.length;j++)
                    {
                        hitbox.addPoint((int)(varx[j]*GameSettings.playerWidth), (int)(vary[j]*GameSettings.playerHeight));
                    }

                }


        return hitbox;
    }
}

运行游戏
enter image description here

所有其他变量,如面板宽度和高度或图像都作为项目说明给出,因此它们可能是错误的。如果您需要更多信息,请告诉我。有没有人有想法?

0 个答案:

没有答案