swing
(JAVA)bubble - 5(물방울 동작 처리)
mynote6676
2025. 5. 2. 17:17
버블버블게임 2025-05-02 17-15-49.mp4
3.81MB
enum 타입에 사용
package bubble.text05;
/**
* enum을 사용하는 방법
* 대문지 사용 권징(내부적으로 상수로 취급한다.)
*/
public enum PlayerWay {
LEFT, RIGHT
}
Player 에 현재 방향 상태 정보 추가
package bubble.text05;
import javax.swing.*;
public class Player extends JLabel implements Moveable {
private int x;
private int y;
private ImageIcon playerR;
private ImageIcon playerL;
// 플레이어의 속도 상태
private final int SPEED = 4;
private final int JUMP_SPEED = 2;
// 플레이어의 움직인 상태
private boolean left;
private boolean right;
private boolean up;
private boolean down;
// 벽에 충돌한 상태
private boolean leftWallCrash;
private boolean rightWallCrash;
//플레이어 방향 상태(enum 타입 사용법 1 -선언 )
private PlayerWay playerWay;
//playerWay - getter만 생성
public PlayerWay getPlayerWay() {
return playerWay;
}
@Override
public int getX() {
return x;
}
@Override
public int getY() {
return y;
}
public ImageIcon getPlayerR() {
return playerR;
}
public ImageIcon getPlayerL() {
return playerL;
}
public int getSPEED() {
return SPEED;
}
public int getJUMP_SPEED() {
return JUMP_SPEED;
}
public boolean isLeft() {
return left;
}
public boolean isRight() {
return right;
}
public boolean isUp() {
return up;
}
public boolean isDown() {
return down;
}
public boolean isLeftWallCrash() {
return leftWallCrash;
}
public boolean isRightWallCrash() {
return rightWallCrash;
}
public void setX(int x) {
this.x = x;
}
public void setY(int y) {
this.y = y;
}
public void setPlayerR(ImageIcon playerR) {
this.playerR = playerR;
}
public void setPlayerL(ImageIcon playerL) {
this.playerL = playerL;
}
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 setDown(boolean down) {
this.down = down;
}
public void setLeftWallCrash(boolean leftWallCrash) {
this.leftWallCrash = leftWallCrash;
}
public void setRightWallCrash(boolean rightWallCrash) {
this.rightWallCrash = rightWallCrash;
}
public Player() {
initData();
setInitLayout();
}
private void initData() {
playerR = new ImageIcon("img/playerR.png");
playerL = new ImageIcon("img/playerL.png");
// 플레이어 초기 상태 설정
x = 55;
y = 535;
left = false;
right = false;
up = false;
down = false;
}
private void setInitLayout() {
setSize(50, 50);
setIcon(playerR);
setLocation(x, y);
}
@Override
public void left() {
//클래스 이름으로 접근한다.
playerWay = PlayerWay.LEFT;
left = true;
setIcon(playerL);
new Thread(new Runnable() {
@Override
public void run() {
while (left) {
x = x - SPEED;
setLocation(x, y);
try {
Thread.sleep(10);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
} // end of while
} // end of run()
}).start();
}
@Override
public void right() {
playerWay = PlayerWay.RIGHT;
right = true; // 움직임 상태값 변경
setIcon(playerR);
// 익명 클래스 - thread.start() ---> run() 메서안에 구문 동작된다.
new Thread(new Runnable() {
@Override
public void run() {
while (right) {
x = x + SPEED;
setLocation(x, y);
try {
Thread.sleep(10);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
}
}).start();
}
@Override
public void up() {
System.out.println("점프!");
up = true;
new Thread(new Runnable() {
@Override
public void run() {
for(int i = 0; i < 130 / JUMP_SPEED; i++) {
y = y - JUMP_SPEED;
setLocation(x, y);
try {
Thread.sleep(5);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
} // end of for
up = false; // 상태값을 잘 다루어야 버그가 없다.
down();
}
}).start();
}
@Override
public void down() {
down = true;
new Thread(new Runnable() {
@Override
public void run() {
for(int i = 0; i < 130 / JUMP_SPEED; i++) {
y = y + JUMP_SPEED;
setLocation(x, y);
try {
Thread.sleep(3);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
} // end of for
down = false; // 상태값을 확실하게 처리하자.
}
}).start();
}
}
Bubble 움직임 설계
package bubble.text05;
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 Player player;
// 생성자 통해서 player 객체의 주소값을 주입받기 생성지 의존주입
public Bubble(Player player){
this.player = player;
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");
left = false;
right = false;
up = false;
}
private void setInitLayout() {
x = player.getX();
y = player.getY();
setIcon(bubble);
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);
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++;
setLocation(x,y);
try {
Thread.sleep(1);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
up();
}
@Override
public void up() {
up = true;
while(true){
y--;
setLocation(x,y);
try {
Thread.sleep(1);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
}
}// end of class