swing

(JAVA)bubble - 7 (물방울 벽 감지, 천장 감지,물방울 터짐 및 제거)

mynote6676 2025. 5. 8. 17:25

버블버블게임 2025-05-08 17-24-24.mp4
3.80MB

 

BackgroundBubbleService 설계 (쓰레드 아님)

package bubble.text07;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

/**
 * BackgroundBubbleService 는 스레드가 계속 돌고 있다.
 * BackgroundBubbleService 는 스레드가 너무 많이 발생해서 게임에 너무
 * 많은 영향을 끼칠 가능성 다분하다. 즉 너무 느려질 가능성 업!!!
 */
public class BackgroundBubbleService {

    private BufferedImage image;
    private Bubble bubble;

    public BackgroundBubbleService(Bubble bubble) {
        try {
            this.bubble = bubble;
            image = ImageIO.read(new File("img/backgroundMapService.png"));
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    // 왼쪽 벽 확인
    public boolean leftWall(){

        Color leftColor = new Color(image.getRGB(bubble.getX() + 10 ,bubble.getY() + 25 ));
        // 빨간색 --> RGB (255,0,0)---> 왼쪽벽 판단 됨
        if(leftColor.getRed() == 255 && leftColor.getBlue() ==0 && leftColor.getGreen() == 0){
            //왼쪽벽에 붙음
            return true;
        }
        return false;
    }
    // 오른쪽 벽 확인
    public boolean rightWall(){

        Color rightColor = new Color(image.getRGB(bubble.getX() + 60, bubble.getY() +25));
        if (rightColor.getRed() == 255 && rightColor.getBlue() == 0 && rightColor.getGreen() == 0 ){
            return true;
        }
        return false;
    }
    public boolean upWall(){

        Color upColor = new Color(image.getRGB(bubble.getX() + 35,bubble.getY()) );
        if(upColor.getRed() == 255 && upColor.getBlue() == 0 && upColor.getGreen() ==0){
            return true;
        }
        return false;
    }

}

 

 

Bubble 코드 추가

 

package bubble.text07;

import javax.swing.*;

public class Bubble extends JLabel implements Moveable {

    private int x;
    private int y;

    //물방울 움직임 상태
    private boolean left;
    private boolean right;
    private boolean up;

    private boolean isLeft; // true, false

    private ImageIcon bubble; // 기본 물방울
    private ImageIcon bomb;

    private Player player;

    private BackgroundBubbleService backgroundBubbleService;


    // 생성자 통해서 player 객체의 주소값을 주입받기 생성지 의존주입
    public Bubble(Player player){
        this.player = player;
        this.backgroundBubbleService = new BackgroundBubbleService(this);

        initDate();
        setInitLayout();
        // 버블은 스레드가 하나면 된다.
        bubbleStartThread();
    }

    private void bubbleStartThread(){
        new Thread(new Runnable() {
            @Override
            public void run() {

                if(player.getPlayerWay() == PlayerWay.LEFT){ // 캐릭터의 방향만을
                    left();// 물풍선의 방향만을
                }else {
                    right();// 물풍선의 방향만을
                }
            }
        }).start();
    }

    private void initDate() {
        bubble = new ImageIcon("img/bubble.png");
        bomb = new ImageIcon("img/bomb.png");
        left = false;
        right = false;
        up = false;

    }

    private void setInitLayout() {
        x = player.getX();
        y = player.getY();
        //ToDO 추후수정예정
        setIcon(bubble);
        //setIcon(bomb);
        setSize(50,50);
        setLocation(x,y);
    }

    @Override
    public int getX() {
        return x;
    }

    // getter
    @Override
    public int getY() {
        return y;
    }

    public boolean isLeft() {
        return left;
    }

    public boolean isRight() {
        return right;
    }

    public boolean isUp() {
        return up;
    }

    public ImageIcon getBubble() {
        return bubble;
    }

    public Player getPlayer() {
        return player;
    }

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

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

    public void setLeft(boolean left) {
        this.left = left;
    }

    public void setRight(boolean right) {
        this.right = right;
    }

    public void setUp(boolean up) {
        this.up = up;
    }

    public void setBubble(ImageIcon bubble) {
        this.bubble = bubble;
    }

    public void setPlayer(Player player) {
        this.player = player;
    }

    @Override
    public void left() {
        left = true;
        for(int i =0; i < 400; i++){
            x--;
            setLocation(x,y);
            if(backgroundBubbleService.leftWall() == true){
                //왼쪽벽이다
                break;
            }
            try {
                Thread.sleep(1);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }
        up();
    }

    @Override
    public void right() {
        right = true;
        for(int i =0; i < 400; i++){
            x++;
            // 좌표 오른쪽으로 1 움직였는데 오른쪽 벽인지 아닌지 매번 확인
            setLocation(x,y);
            if(backgroundBubbleService.rightWall() == true){
                break;
            }
            try {
                Thread.sleep(1);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }
        up();
    }

    @Override
    public void up() {
        up = true;
        while(true){
            y--;
            setLocation(x,y);
            if(backgroundBubbleService.upWall() == true){
                //TODO 추후수정 예정
                try {
                    Thread.sleep(500);
                    setIcon(bomb);
                    Thread.sleep(500);
                    setIcon(null);
                    bubble = null;
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
                break;
            }
            try {
                Thread.sleep(1);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }

    }

}// end of class