0

基本的に、私は単純な小惑星-yスタイルのサイドスクローラーを作成しています。Javaの経験は少しありますが、特定の実装に関してはあまり良くありません。だから、私はランダムな時間とランダムなy位置でスポーンする小惑星の束を作りたいと思っています。そのための最善の方法は、ある種の配列を実行することだと思いますが、実際にそれを実装する方法がわからず、配列全体の衝突チェックを実行します。これが私の敵と競技場のクラスの関連部分です。

ボーナスポイントは、テーマ音楽を機能させるのを手伝ってくれる人を指します。

import java.awt.*;
import javax.swing.ImageIcon;

public class Enemy 
{
    int x, y;
    Image img;
    boolean isAlive = true;


    public Enemy(int startX, int startY, String location)
    {
        x = startX;
        y = startY;
        ImageIcon l = new ImageIcon(location);
        img = l.getImage();
    }

    public Rectangle getBounds()
    {
        return new Rectangle(x, y, 60, 60);
    }

public class Board extends JPanel implements ActionListener
{
    Player p;
    Image img;
    Timer time;
    Thread animator;
    Enemy en1, en2;
    boolean lost = false;
    static Font font = new Font("SanSerif", Font.BOLD, 24);
    public AudioClip theme;

    public static int score = 0;
    public static int lives = 3;

    public Board() 
    {
        //theme = Applet.newAudioClip(getClass().getResource("images/theme.mid"));
        //theme.play();
        p = new Player();       
        addKeyListener(new ActionListener());
        setFocusable(true);
        ImageIcon i = new ImageIcon("images/background.png");
        img = i.getImage();
        time = new Timer(5, this);
        time.start();
        int v =  172;
        en1 = new Enemy(p.x + 600, 260, "images/enemy1.png");
        en2 = new Enemy(p.x + 600, 200, "images/asteroid.gif");
    }

    public void actionPerformed(ActionEvent e)
    {
        checkCollisions();
        ArrayList bullets = Player.getBullets();

        for(int i = 0; i < bullets.size(); i++)
        {
            Bullet b = (Bullet)bullets.get(i);

            if(b.isVisible() == true)
            {
                b.move();
            }
            else
            {
                bullets.remove(i);
            }
        }

        p.move();

        if(p.x > 400)
        {
            en1.move(p.getdx());
        }
        if(p.x > 500)
        {
            en2.move(p.getdx());
        }
        repaint();
    }

    public void paint(Graphics g)
    {
        if(lost)
        {
            System.out.println("You Lose");
        }
        super.paint(g);
        Graphics2D g2d = (Graphics2D) g;
        if((p.getX() - 590) % 2400 == 0 || (p.getX() - 590) % 2400 == 1)
        {
            p.nx = 0;
        }
        if((p.getX() - 1790) % 2400 == 0 ||(p.getX() - 1790) % 2400 == 1)
        {
            p.nx2 = 0;
        }

        g2d.drawImage(img, 685-p.nx2, 0, null);

        if(p.getX() >= 590)
        {
            g2d.drawImage(img, 685-p.nx, 0, null);
        }

        g2d.drawImage(p.getImage(), p.edge, p.getY(), null);

        ArrayList bullets = Player.getBullets();
        for(int i = 0; i < bullets.size(); i++)
        {
            Bullet b = (Bullet)bullets.get(i);
            g2d.drawImage(b.getImg(), b.getX(), b.getY(), null);
        }
        if(p.x > 400)
        {
            if(en1.isAlive == true)
            {
                g2d.drawImage(en1.getImg(), en1.getX(), en1.getY(), null);
            }
        }
        if(p.x > 500)
        {
            if(en2.isAlive == true)
            {
                g2d.drawImage(en2.getImg(), en2.getX(), en2.getY(), null);
            }           
        }
    }
    public void checkCollisions()
    {
            Rectangle r1 = en1.getBounds();
            Rectangle r2 = en2.getBounds();
            ArrayList bullets = Player.getBullets();

            for (int i = 0; i < bullets.size(); i++)
            {
                Bullet m = (Bullet) bullets.get(i);
                Rectangle m1 = m.getBounds();

                if (r1.intersects(m1) && en1.isAlive())
                {
                    score++;
                    en1.isAlive = false;
                    m.visible = false;
                 }
                 else if (r2.intersects(m1)&& en2.isAlive())
                 {
                     en2.isAlive = false;
                     m.visible = false;
                 }
            }

            Rectangle d = p.getBounds();
            if (d.intersects(r1) || d.intersects(r2))
            {
            lives--;
                    if(lives < 0)
                    {
                        lost = true;
                    }
            }

        }
4

2 に答える 2

0

A while ago, I made a little Asteroids game. There I used a Vector as a collection of Asteriord objects. Then in the LaserBeam class (the starship has an laser gun!), I have something like this:

// collision with an asteroid?
for (Asteroid a : getAsteroids())
    if (a.getCp().getBounds2D().intersects(xPos, yPos, width, height))  
    {
        a.newHit();
        break;
    }   

So the beam iterates through the asteroids and check if some one collides with the beam. In that case, the life of the beam and the asteroid ends.

If you are interested, you can find the complete source code here, and here is the applet game.

于 2012-05-06T00:15:18.870 に答える
0

配列に関するこのチュートリアルをご覧になることをお勧めします: http://docs.oracle.com/javase/tutorial/java/nutsandbolts/arrays.html

ArrayList も検討したいかもしれませんが、配列に問題があるため、最初に基本に固執します。

于 2012-05-05T23:13:36.710 に答える