-1

大学で1学期のコースを受講した後、ゲームプログラミングを学び始めたばかりなので、準備ができていると思います。本当にやりたいと思っています。まず、次のことを行います。1)学習に役立つリソースは何でしょうか。Ive googlefはたくさんあり、2冊の本があります。JavaでのキラーゲームプログラミングとJavaSE6ゲームプログラミングの開始です。1つは過度に具体的であると同時に具体的ではなく、もう1つはほとんど説明されていないため、何が何であるかを理解するのは困難です。特にレンダリング、バッファ、アプレット、フレーム、パネルへのレンダリング方法について。どんな助けでも大いに感謝されるでしょう;)ありがとう!

私は非常に基本的なコードを書きました。ボックスを移動するために、すべてが順調ですが、ボックスは本来あるべき真ん中にありません。

オブジェクト:

       package milk;


    public class Thing 
    {
        double x,y;
        Shape shape;
        int[] thingx={-5,5,5,-5};
        int[] thingy={5,5,-5,-5};

    Thing(double x, double y)
    {
        setX(x);
        setY(y);
        setShape();
    }

    public void setX(double x)
    {
        this.x=x;
    }

    public void setY(double y)
    {
        this.y=y;
    }

    public void setShape()
    {
        this.shape=new Polygon(thingx,thingy,thingx.length);
    }

    public double getX()
    {
        return x;
    }

    public double getY()
    {
        return y;
    }

    public Shape getShape()
    {
        return shape;
    }

    public void incX(int i)
    {
        this.x+=i;
    }

    public void incY(int i)
    {
        this.y+=i;
    }

}

パネル:

    package milk;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.geom.AffineTransform;

import javax.swing.JPanel;

@SuppressWarnings("serial")
public class MilkPanel extends JPanel implements Runnable, KeyListener{

    Thread animator;
    Graphics2D g2d;
    Thing thing=new Thing(320,240);
    AffineTransform identity = new AffineTransform();

    MilkPanel()
    {
        setSize(640,480);
        setBackground(Color.black);
        setFocusable(true);
        requestFocus();
        addKeyListener(this);
    }

    public void addNotify() 
    {
        super.addNotify();
        animator = new Thread(this);
        animator.start();
    }

    public void paint(Graphics g)
    {
        g2d=(Graphics2D)g;

        g2d.setColor(Color.black);
        g2d.fillRect(0, 0, getSize().width,getSize().height);
        drawThing();
        g.dispose();
    }

    public void drawThing()
    {
        g2d.setTransform(identity);
        g2d.translate(thing.getX(), thing.getY());
        g2d.setColor(Color.orange);
        g2d.draw(thing.getShape());
    }
    public void run()
    {
        while(true)
        {
            try
            {
                Thread.sleep(20);
            }
            catch(Exception e)
            {

            }
            repaint();
        }
    }

    public void keyPressed(KeyEvent e) 
    {
        int key=e.getKeyCode();
        switch(key)
        {
        case KeyEvent.VK_UP:
            thing.incY(-5);
        break;

        case KeyEvent.VK_DOWN:
            thing.incY(5);
        break;

        case KeyEvent.VK_RIGHT:
            thing.incX(5);
        break;

        case KeyEvent.VK_LEFT:
            thing.incX(-5);
        break;
        }
    }

    public void keyReleased(KeyEvent e) {   }

    public void keyTyped(KeyEvent e) {  }




}

メイン:

  package milk;

import java.awt.Color;
import java.awt.Dimension;
import javax.swing.JFrame;

@SuppressWarnings("serial")
public class MilkIt extends JFrame {

    public MilkIt() {

        add(new MilkPanel());

        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setSize(640,480);
        setLocationRelativeTo(null);
        setVisible(true);
    }

    public static void main(String[] args) {
        new MilkIt();
    }
}
4

1 に答える 1

3

It looks like you have set the position of the box to the point (320,240), which is indeed half of (640,480). The X,Y position of an object will actually be its topleft corner, however. Additionally, you will likely want to make a method for setting this information generically, as opposed to hard-coding it.

If you want to find an object's center position on a given axis (this works for X, Y, or Z, whether you're working in 2D or 3D (or more!?)), you want to take half of its size on that axis and subtract if from its position (which is actually a corner); the result will be the center.

The algorithm you're looking for is essentially this:

xPos = (screenXSize / 2) - (xSize / 2);
yPos = (screenYSize / 2) - (ySize / 2);

To standardize it even further, consider putting your variables in arrays based on how many dimensions you're using - then what I said about using either 2D or 3D automatically applies no matter what you're doing (you can even mix and match 2D and 3D elements in the same game, as is common for certain reasons).

for (int i = 0; i < DIMENSIONS; i++) {
    pos[i] = (screenSize[i] / 2) - (size[i] / 2);
}
于 2013-01-09T16:21:23.610 に答える