문제

I need help trying to sort how this collision detection in my code.

Here is my code and I have had a little assistance from various sources online but nothing with a definite explanation to it yet, but all in all I have made a lot of progress. So basically a quick thank you internet.

What I have to do is make a game where I control an object, in this case a blue circle and then touch another on screen object, in this case a yellow circle and when they touch the yellow circle will dissapear and the word "YUM" will take its place then the yellow circle will move randomly to a new location.

This is a lot like the game snake but I can't get the collision to work right and also if I did have it working I have no idea how I would update the location to a new location randomly on the screen.

here is the code:

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.util.Random;
import javax.swing.JFrame;

import org.w3c.dom.css.Rect;


public class javagame2 extends JFrame {

    private Image dbimage;
    private Graphics dbg;

    Font font = new Font("Arial", Font.BOLD, 30);
    static Random generator = new Random();
    static int randomIndex0 = generator.nextInt(900);
    static int randomIndex1 = generator.nextInt(400);

    public static class player
    {
        static int x = 150;
        static int y = 150;
        static final int Width = 50;
        static final int Height = 50;
    }

    public static class pizza
    {
        static final int x1 = randomIndex0;
        static final int y1 = randomIndex1;
        int Width = 50;
        int Height = 50;
        public boolean intercepts(player p) {
            // TODO Auto-generated method stub
            return false;
        }
    }

    public class AL extends KeyAdapter
    {
        public void keyPressed(KeyEvent e)
        {
            int keycode = e.getKeyCode();
            if(keycode == e.VK_LEFT){
                if(player.x <= 0)
                {
                    player.x = 0;
                }
                else
                {
                    player.x += -10;
                }
            }
            if(keycode == e.VK_RIGHT){
                if(player.x >= 950)
                {
                    player.x = 950;
                }
                else
                {
                    player.x += 10;
                }
            }
            if(keycode == e.VK_UP){
                if(player.y <= 20)
                {
                    player.y = 20;
                }
                else
                {
                    player.y += -10;
                }
            }
            if(keycode == e.VK_DOWN){
                if(player.y >= 450)
                {
                    player.y = 450;
                }
                else
                {
                    player.y += 10;
                }
            }
        }
        public void keyReleased(KeyEvent e){}
    }
    public int getX()
    {
        return player.x;
    }
    public int getY()
    {
        return player.y;
    }
    /*
    public boolean collide(pizza f)
    {
        if(f.X <= (player.x+40) && f.Y >= (player.y+40))
        {
            System.out.println("they collided");
            return true;
        }
        else
        {
            return false;
        }
    }
    */

    public boolean collide(pizza f, player p)
    {
        if(f.x1 <= p.x && f.y1 >= p.y)
        {
            System.out.println("they collided");
            return true;
        }
        else
        {
            return false;
        }
    }

    public javagame2()
    {
        addKeyListener(new AL());
        setTitle("Ballon Popper");
        setSize(1000, 500);
        setResizable(false);
        setVisible(true);
        setBackground(Color.BLACK);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

    public void paintcomponent(Graphics g)
    {
        g.setColor(Color.WHITE);
        g.drawRect(pizza.x1, pizza.y1, 50, 50);

        g.setFont(font);
        g.setColor(Color.RED);
        g.drawString("Eat the Pizza!", 350, 50);
        g.setColor(Color.BLUE);
        g.drawRect(getX(), getY(), 50, 50);
        g.fillOval(getX(), getY(), 50, 50);

        g.setColor(Color.YELLOW);
        g.fillOval(pizza.x1, pizza.y1, 50, 50);
        repaint();
    }

    public void paint(Graphics g)
    {
        dbimage = createImage(getWidth(), getHeight());
        dbg = dbimage.getGraphics();
        paintcomponent(dbg);
        g.drawImage(dbimage, 0, 0, this);
    }

    public static void main(String arg[])
    {
        new javagame2();
    }

}

Here is the updated code I just worked out a little bit, it does run but I'm a bit confused on how to go about removing the yellow circle once I have touched or even what to do, I tried using .intersects but that didn't seem to do anything.

도움이 되었습니까?

해결책

I don't know java, but this seems straightforward.

Two circles collide when the distance between their centers is less than the sum of their radii. (And if the circles are the same size, we needn't worry about whether their "locations" are actually at their centers or not.)

public boolean collide(pizza f, player p)
{
    if(Math.pow(f.x1-p.x, 2) + Math.pow(f.y1-p.y, 2) <= Math.pow(f.Width/2 + p.Width/2, 2))
    {
        System.out.println("they collided");
        return true;
    }
    else
    {
        return false;
    }
}
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top