0

基本上,我正在制作一个简单的 asteroids-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 回答