왼쪽, 오른쪽 벽 감지
인터페이스 파일
[Player]
import javax.swing.ImageIcon;
public class Player extends JLabel implements Moveable {
private int x;
private int y;
private ImageIcon playerR, playerL;
// 움직임의 상태
private boolean left;
private boolean right;
private boolean up;
private boolean down;
// 벽에 충돌한 상태
private boolean leftWallCrash;
private boolean rightWallCrash;
// 플레이어 속도 상태
private final int SPEED = 4;
private final int JUMPSPEED = 2;
// get,set
public Player() {
initData();
setInitLayout();
}
public int getX() {
return x;
}
public void setX(int x) {
this.x = x;
}
public int getY() {
return y;
}
public void setY(int y) {
this.y = y;
}
public ImageIcon getPlayerR() {
return playerR;
}
public void setPlayerR(ImageIcon playerR) {
this.playerR = playerR;
}
public ImageIcon getPlayerL() {
return playerL;
}
public void setPlayerL(ImageIcon playerL) {
this.playerL = playerL;
}
public boolean isLeft() {
return left;
}
public void setLeft(boolean left) {
this.left = left;
}
public boolean isRight() {
return right;
}
public void setRight(boolean right) {
this.right = right;
}
public boolean isUp() {
return up;
}
public void setUp(boolean up) {
this.up = up;
}
public boolean isDown() {
return down;
}
public void setDown(boolean down) {
this.down = down;
}
public boolean isLeftWallCrash() {
return leftWallCrash;
}
public void setLeftWallCrash(boolean leftWallCrash) {
this.leftWallCrash = leftWallCrash;
}
public boolean isRightWallCrash() {
return rightWallCrash;
}
public void setRightWallCrash(boolean rightWallCrash) {
this.rightWallCrash = rightWallCrash;
}
public int getSPEED() {
return SPEED;
}
public int getJUMPSPEED() {
return JUMPSPEED;
}
private void initData() {
playerR = new ImageIcon("img/playerR.png");
playerL = new ImageIcon("img/playerL.png");
// 처음 실행 시 초기값 셋팅
x = 450;
y = 535;
// 플레이어가 가만이 멈춤 상태
left = false;
right = false;
up = false;
down = false;
leftWallCrash = false;
rightWallCrash = false;
}
private void setInitLayout() {
setIcon(playerR);
setSize(50, 50);
setLocation(x, y);
}
@Override
public void 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) {
e.printStackTrace();
}
}
}
}).start();
}
@Override
public void rigth() {
right = true;
setIcon(playerR);
new Thread(new Runnable() {
@Override
public void run() {
while (right) {
x = x + SPEED;
setLocation(x, y);
try {
Thread.sleep(10);
} catch (InterruptedException e) {
e.printStackTrace();
} // 0.01
}
}
}).start();
}// end of right
@Override
public void up() {
System.out.println("점프");
up = true;
new Thread(new Runnable() {
@Override
public void run() {
for (int i = 0; i < 120 / JUMPSPEED; i++) {
y = y - JUMPSPEED;
setLocation(x, y);
try {
Thread.sleep(5);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
// 객체의 상태값을 잘 조절해야 한다
up = false;
down();
}
}).start();
}
@Override
public void down() {
System.out.println("다운");
down = true;
new Thread(new Runnable() {
@Override
public void run() {
for (int i = 0; i < 120 / JUMPSPEED; i++) {
y = y + JUMPSPEED;
setLocation(x, y);
try {
Thread.sleep(3);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}).start();
// 상태값 처리를 확실히 하자
down = false;
}
}
[BubbleFrame]
import java.awt.event.KeyAdapter;
public class BubbleFrame extends JFrame {
private JLabel backgroundMap;
private Player player;
public BubbleFrame() {
initData();
setInitLayout();
addEventListener();
// 백그라운드 서비스 시작
new Thread(new BackgroundPlayerService(player)).start();
}
private void initData() {
// todo 이미지 변경
backgroundMap = new JLabel(new ImageIcon("img/backgroundMapService.png"));
// backgroundMap = new JLabel(new ImageIcon("img/test.png"));
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// Frame -> root Panal
setContentPane(backgroundMap); // add 처리
setSize(1000, 640);
player = new Player();
}
private void setInitLayout() {
// 좌표 값으로 배치
setLayout(null);
setResizable(false); // 프레임 크기 조절 불가
setLocationRelativeTo(null); // JFrame 여러분 모니터 가운데 자동 배치
setVisible(true);
add(player);
}
private void addEventListener() {
this.addKeyListener(new KeyAdapter() {
@Override
public void keyPressed(KeyEvent e) {
System.out.println("key code : " + e.getKeyCode());
switch (e.getKeyCode()) {
case KeyEvent.VK_LEFT:
// 왼쪽으로 방향키 누르고 있다면
// key 이벤트가 계속 <- <- <- <- <-
// 왼쪽 상태가 아니라면
// 왼쪽 벽에 충돌 한게 아니하면
if (!player.isLeft() && !player.isLeftWallCrash()) {
player.left();
}
break;
case KeyEvent.VK_RIGHT:
if (!player.isRight() && !player.isRightWallCrash()) {
player.rigth();
}
break;
case KeyEvent.VK_UP:
player.up();
break;
default:
break;
// case KeyEvent.VK_DOWN :
// player.down();
}
}// end of KeyPressed
@Override
public void keyReleased(KeyEvent e) {
switch (e.getKeyCode()) {
case KeyEvent.VK_LEFT:
// 왼쪽으로 가능 상태 멈춤
player.setLeft(false);
break;
case KeyEvent.VK_RIGHT:
//오른쪽을 가능 상태 멈춤
player.setRight(false);
break;
default:
break;
}
}// end of KeyReleased
});
}
// 코드 테스트
public static void main(String[] args) {
new BubbleFrame();
}// end of main
}
[BackgroundPlayerService]
import java.awt.Color;
//현재 메인 쓰레는 너~무 바쁨
//백그라운드에서 계속 Player 에 움직임을 관찰할 예정
public class BackgroundPlayerService implements Runnable{
private BufferedImage image;
private Player player;
public BackgroundPlayerService(Player player) {
this.player = player;
try {
image = ImageIO.read(new File("img/backgroundMapService.png"));
} catch (IOException e) {
e.printStackTrace();
}
}
@Override
public void run() {
while(true) {
// 색상 확인 todo (보정값 필요)
Color leftColor = new Color(image.getRGB(player.getX() + 10, player.getY() + 25));
Color rightColor = new Color(image.getRGB(player.getX() + 50 + 10, player.getY() + 25));
// 왼쪽에 충돌함
if(leftColor.getRed() == 255 && leftColor.getGreen() == 0 && leftColor.getBlue() == 0) {
System.out.println("왼쪽벽에 출동 함");
player.setLeftWallCrash(true);
player.setLeft(false);
} else if (rightColor.getRed() == 255 && rightColor.getGreen() == 0 && rightColor.getBlue() == 0) {
System.out.println("오른쪽벽에 충돌 함");
player.setRightWallCrash(true);
player.setRight(false);
}else {
player.setLeftWallCrash(false);
player.setRightWallCrash(false);
}
try {
Thread.sleep(10);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
실행 결과_________
'JAVA > Java 응용' 카테고리의 다른 글
[JAVA] BUBBLE GAME(버블 게임) 5단계 (0) | 2024.05.03 |
---|---|
[JAVA] BUBBLE GAME(버블게임) 4단계 (0) | 2024.05.03 |
[JAVA] BUBBLE GAME(버블 게임) 2단계 (0) | 2024.05.02 |
[JAVA] BUBBLE GAME (버블 게임) 1단계 (0) | 2024.05.02 |
[JAVA]로또 게임 만들기 (0) | 2024.04.30 |