【问题标题】:java space ship gamejava太空飞船游戏
【发布时间】:2013-11-29 16:57:18
【问题描述】:

这里是 Ship、Asteroids、BaseShapeClass 类的完整代码。 Ship Class 继承自 BaseShapeClass 的形状。 Asteroid 类是声明 Graphics2D 对象的主要源代码,AffineTransform(用于标识创建),声明双图像缓冲区... BaseShapeClass 的代码..

package baseshapeclass;
import java.awt.Shape;


public class BaseShapeClass {
    private Shape shape;
    private double x, y;
    private double velX, velY;
    private double moveAngle, faceAngle;
    private boolean alive;

    //accessors and mutators
    public Shape getShape(){return shape;}
    public void setShape(Shape shape){ this.shape = shape; }

    public double getX() { return x; }
    public void setX(double x) { this.x = x; }
    public void incX(double ix) { this.x += ix; }

    public double getY() { return y; }
    public void setY(double y) { this.y = y; }
    public void incY(double iy) { this.y += iy; }

    public double getVelX() { return velX; }
    public void setVelX(double velX) { this.velX = velX; }
    public void incVelX(double ivX) { this.velX += ivX; }

    public double getVelY() { return velY; }
    public void setVelY(double velY) { this.velY = velY; }
    public void incVelY(double ivY) { this.velY += ivY; }
    //MoveAngle refers to the objects angular movement
    public double getMoveAngle() { return moveAngle; }
    public void setMoveAngle(double mAngle) { this.moveAngle = mAngle; }
    public void incMoveAngle(double imAngle) { this.moveAngle += imAngle; }
    //FaceAngle refers to the objects face/heads angular movement
    public double getFaceAngle() { return faceAngle; }
    public void setFaceAngle(double fAngle) { this.faceAngle = fAngle; }
    public void incFaceAngle(double ifAngle) { this.faceAngle += ifAngle; }

    public boolean isAlive() { return alive; }
    public void setAlive(boolean alive) { this.alive = alive; }

    //default constructor everything will be set to original state
    //when update is called everything will start to move
    BaseShapeClass(){
        setShape(null);
        setAlive(false);
        //all of them are set to '0' representing their initial position,
        //which will be called during the update() Event of the graphics objects
        setX(0.0);
        setY(0.0);
        setVelX(0.0);
        setVelY(0.0);
        setMoveAngle(0.0);
        setFaceAngle(0.0);
    }
}

船级代码...

package baseshapeclass;
import java.awt.Rectangle;
import java.awt.Polygon;
public class Ship extends BaseShapeClass {
    //ships shape along the x and y cordinates
    private final int[] shipx = {-6,3,0,3,6,0};
    private final int[] shipy = {6,7,7,7,6,-7};

    public Rectangle getBounds(){
        Rectangle r = new Rectangle((int)getX()-6, (int)getY()-6, 12, 12);
        return r;
    }
    Ship(){
        setShape(new Polygon(shipx, shipy, shipx.length));
        setAlive(true);
    }
}

Asteroid 代码(主要源代码)...

package baseshapeclass;
import java.awt.*;
import java.awt.image.*;
import java.awt.geom.*;
import java.awt.event.*;
import java.applet.*;
import java.util.*;
public abstract class Asteroid extends Applet implements Runnable, KeyListener {
    BufferedImage backbuffer;
    Graphics2D g2d;
    Ship ship = new Ship();
    boolean showBounds= true;
    AffineTransform identity = new AffineTransform();

    @Override public void init(){
        backbuffer = new BufferedImage(640,480,BufferedImage.TYPE_INT_RGB);
        g2d = backbuffer.createGraphics();

        ship.setX(320);
        ship.setY(240);

        addKeyListener(this);
    }
    @Override public void update(Graphics g){
        g2d.setTransform(identity);
        g2d.setColor(Color.BLACK);
        g2d.fillRect(0, 0, getSize().width, getSize().height);
        g2d.setColor(Color.WHITE);
        g2d.drawString("Ship: "+Math.round(ship.getX())+" , "+Math.round(ship.getY()),2, 150);
        g2d.drawString("Face Angle: "+Math.toRadians(ship.getFaceAngle()),5, 30);
        g2d.drawString("Move Angle: "+Math.toRadians(ship.getMoveAngle())+90,5,50);

        drawShip();
        paint(g);
    }
    public void drawShip(){
        g2d.setTransform(identity);
        g2d.translate(ship.getX(),ship.getY());
        g2d.rotate(Math.toRadians(ship.getFaceAngle()));
        g2d.setColor(Color.ORANGE);
        g2d.fill(ship.getShape());
    }
}

我希望你们对所有代码都有一个更好的想法。只是想知道 Ship 类的部分为什么船的 x 和 y 坐标如下:

public class ship extends BaseShapeClass{
private int[] shipx = {-6,3,0,3,6,0};
private int[] shipy = {6,7,7,7,6,-7};
}

我无法理解这些值将如何构成多边形??

【问题讨论】:

  • i will pinpoint the part which i dont get clearly..: 你想踢球,但你不知道它在哪里!我怎么会知道去哪里找自己的球
  • 这段代码是从哪里来的?在不了解上下文的情况下很难确定这些数组的全部内容。如果这来自教程或类似的东西,肯定会有一些评论或文档。基本答案是它似乎被用来创建一个六边形(或更一般地说,一个具有 6 个顶点的多边形),但除此之外无法告诉您任何其他内容。
  • @Teeg 是的,这来自一个教程,但没有任何解释!
  • 如需尽快获得更好的帮助,请发帖SSCCE

标签: java awt


【解决方案1】:
Ship(){
  setShape(new Polygon(shipx,shipy,shipx.length));
  setAlive(true);
}

你可以看到你混淆的两个数组进入了Polygon.的初始化这两个数组,作为一对,给出了Polygon中每个点的x和y坐标。

【讨论】:

  • @nishpystoned 查看这篇文章about drawing a polygon in java
  • 是的,我知道它被分配给多边形。但我的疑问是为什么 x 和 y 的值是以这种方式分配的??
【解决方案2】:

这篇文章是为了回答您在 Kronion 的回答中的评论;我本来打算将它作为评论发布,但要说的太多,我想向您展示一些代码,这些代码在 cmets 中不那么清晰。

正如 Kronion 所说,Polygon 类确实接受 X 坐标数组和 Y 坐标数组。原因是 X 和 Y 坐标存储在两个数组中的相同位置。所以如果int index = 0,那么X,Y坐标对就是xArray[index]yArray[index]

如果这没有任何意义,请检查Polygon class source code。例如,您会在 contains 方法中看到这种情况,这里:

for (int i = 0; i < npoints; lastx = curx, lasty = cury, i++) {

    curx = xpoints[i];
    cury = ypoints[i];

    // remainder of loop
}

所以简而言之,它们以这种方式分配是因为 X 和 Y 通过它们的索引位置配对。

希望对您有所帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-06-06
    • 1970-01-01
    • 1970-01-01
    • 2014-01-18
    • 1970-01-01
    相关资源
    最近更新 更多