버블버블게임 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
'swing' 카테고리의 다른 글
(JAVA) bubble - 6(바닥, 층 감지 기능 추가) (1) | 2025.05.07 |
---|---|
(JAVA)bubble - 5(물방울 동작 처리) (1) | 2025.05.02 |
(JAVA) 물방울생성 (0) | 2025.05.02 |
(JAVA)bubble - 3 (왼쪽, 오른쪽 벽 감지하기) (0) | 2025.05.01 |
(JAVA) Bubble Game-2(플레이어 움직이기) (0) | 2025.05.01 |