[Java] 73. Swing: 버블버블 게임 만들기(3)
이전 글: https://whatsthatsound.tistory.com/132
https://whatsthatsound.tistory.com/133
https://whatsthatsound.tistory.com/134
[java] 72. Swing: 버블버블 게임 만들기(3)
[Java] 72. Swing: 버블버블 게임 만들기(3)이전 글: https://whatsthatsound.tistory.com/132https://whatsthatsound.tistory.com/133 [Java] 71. Swing: 버블버블 게임 만들기(2)[Java] 71. Swing: 버블버블 게임 만들기(2)이전 글: htt
whatsthatsound.tistory.com
[Java] 71. Swing: 버블버블 게임 만들기(2)
[Java] 71. Swing: 버블버블 게임 만들기(2)이전 글: https://whatsthatsound.tistory.com/132 [Java] 70. Swing: 버블버블 게임 만들기(1)[Java] 70. Swing: 버블버블 게임 만들기(1) 자바의 GUI Swing을 사용해 버블버블
whatsthatsound.tistory.com
[Java] 70. Swin 버블버블 게임 만들기(1)
[Java] 70. Swing: 버블버블 게임 만들기(1) 자바의 GUI Swing을 사용해 버블버블 게임을 구현해보자. (1) Moveable 인터페이스 구현하기(2) Player 클래스 및 메서드 구현하기(3) BubbleFrame 내에서 배경 및
whatsthatsound.tistory.com
1. 목차
자바의 GUI Swing을 사용해 이전의 코드를 보완해보자.
(1) 스페이스 바를 눌렀을 때, 버블이 생성되도록 해보자.
(2) 점프시, 파란색 바닥에 닿으면 올라갈 수 있게 해보자.
(3)BackgroundMap의 사진을 바꿔보자.
2. 구현해야 할 동작들
3. 코드 블록
(1) Bubble : 버블 클래스 및 메서드 생성
(2) BackgroundPlayerService: 플레이어의 움직임 관찰 & 제어 쓰레드 생성!
(3) BubbleFrame : 프레임 / 키 이벤트 / 시범 실행
(4) Player : player 객체 및 메서드 생성 (moveable 구현), 움직임 쓰레드 구현(Run-Start)
(5) Moveable : 인터페이스
package BubbleBubble.test.ex05;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.ImageIcon;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
public class BubbleFrame extends JFrame {
private JLabel backgroundMap;
private Player player;
public BubbleFrame() {
initData();
setInitLayout();
addEventListener();
// Player 백그라운드 서비스 시작
new Thread(new BackgroundPlayerService(player)).start();
}
private void initData() {
// todo-이미지 변경
backgroundMap = new JLabel(new ImageIcon("img/backgroundMapService.png"));
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// Frame --> root Panel
setContentPane(backgroundMap);
setSize(1000, 640);
player = new Player();
}
private void setInitLayout() {
// 좌표값으로 배치
setLayout(null);
setResizable(false); // 프레임 조절 불가
setLocationRelativeTo(null); // JFrame을 여러분 모니터 가운데에 자동 배치
setVisible(true);
add(player);
}
private void makeBubble(Player player) {
Bubble bubble = new Bubble(player);
add(bubble);
}
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:
// 왼쪽으로 방향키를 누르고 있다면,
// 키보드 이벤트가 계속 왼쪽으로 간다. <- <- <-
// 왼쪽 상태가 아니라면,?
if(!player.isLeft() && !player.isLeftWallCrash()) {
player.left();
}
// 왼쪽 벽에 충돌한 게 아니라면,?
break;
case KeyEvent.VK_RIGHT:
if(!player.isRight() && !player.isRightWallCrash()) {
player.right();
}
break;
case KeyEvent.VK_UP:
player.up();
break;
case KeyEvent.VK_SPACE:
makeBubble(player); // 내가 짠 코드
add(new Bubble(player)); // 선생님이 짜신 코드
default:
break;
}
} // end of key pressed
@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;
case KeyEvent.VK_UP:
player.setUp(false);
break;
default:
break;
}
} // end of keyReleased
}); // end of keyAdapter
}
// 코드 테스트
public static void main(String[] args) {
new BubbleFrame();
}
}
package BubblePractice.ex05;
import java.awt.Color;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
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) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
@Override
public void run() {
while(true) {
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));
// Color bottomColor = new Color(image.getRGB(player.getX(), player.getY()));
// 흰색이면 바닥 RGB => 255 255 255
// 바닥인 경우 --> 255 0 0 (바닥이라고 판단 가능)
// 바닥인 경우 --> 0 0 255 (바닥이라고 판단 가능)
int bottomColorLeft = image.getRGB(player.getX() + 20, player.getY() + 50 + 5);
int bottomColorRight = image.getRGB(player.getX() + 50 - 10, player.getY() + 50 + 5);
// 하얀색 ------> int 값이 - 2
if(bottomColorLeft + bottomColorRight != -2) {
// 여기는 멈추야 한다. ( 빨간 바닥 또는 파란색 바닥)
player.setDown(false);
}
// 왼쪽 벽에 충돌함
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);
}
// 위 두 조건이 아니면, player 마음대로 움직일 수 있다.
try {
Thread.sleep(10);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
// TODO Auto-generated method stub
}
}
package BubbleBubble.test.ex05;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.ImageIcon;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
public class BubbleFrame extends JFrame {
private JLabel backgroundMap;
private Player player;
public BubbleFrame() {
initData();
setInitLayout();
addEventListener();
// Player 백그라운드 서비스 시작
new Thread(new BackgroundPlayerService(player)).start();
}
private void initData() {
// todo-이미지 변경
backgroundMap = new JLabel(new ImageIcon("img/backgroundMapService.png"));
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// Frame --> root Panel
setContentPane(backgroundMap);
setSize(1000, 640);
player = new Player();
}
private void setInitLayout() {
// 좌표값으로 배치
setLayout(null);
setResizable(false); // 프레임 조절 불가
setLocationRelativeTo(null); // JFrame을 여러분 모니터 가운데에 자동 배치
setVisible(true);
add(player);
}
private void makeBubble(Player player) {
Bubble bubble = new Bubble(player);
add(bubble);
}
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:
// 왼쪽으로 방향키를 누르고 있다면,
// 키보드 이벤트가 계속 왼쪽으로 간다. <- <- <-
// 왼쪽 상태가 아니라면,?
if(!player.isLeft() && !player.isLeftWallCrash()) {
player.left();
}
// 왼쪽 벽에 충돌한 게 아니라면,?
break;
case KeyEvent.VK_RIGHT:
if(!player.isRight() && !player.isRightWallCrash()) {
player.right();
}
break;
case KeyEvent.VK_UP:
player.up();
break;
case KeyEvent.VK_SPACE:
makeBubble(player);
add(new Bubble(player));
default:
break;
}
} // end of key pressed
@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;
case KeyEvent.VK_UP:
player.setUp(false);
break;
default:
break;
}
} // end of keyReleased
}); // end of keyAdapter
}
// 코드 테스트
public static void main(String[] args) {
new BubbleFrame();
}
}
package BubbleBubble.test.ex05;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
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
// setter 메서드
public void setLeft(boolean left) {
this.left=left;
}
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 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 boolean isLeft() {
return left;
}
public boolean isRight() {
return right;
}
public boolean isUp() {
return up;
}
public int getSPEED() {
return SPEED;
}
public int getJUMPSPEED() {
return JUMPSPEED;
}
public void setRight(boolean right) {
this.right=right;
}
public void setUp(boolean up) {
this.up=up;
}
public Player() {
initData();
setInitLayout();
}
private void initData() {
playerR = new ImageIcon("img/playerR.png");
playerL = new ImageIcon("img/playerL.png");
// 처음 실행 시 초기 값 셋팅
x = 450;
y = 540;
leftWallCrash=false;
rightWallCrash=false;
// 플레이어가 가만히 멈춘 상태
left=false;
right=false;
up=false;
down=false;
setIcon(playerR);
this.setSize(50, 50);
setLocation(x, y);
}
private void setInitLayout() {
}
@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) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} // end of while
} // end of run
}).start(); // end of Thread
}
@Override
public void right() {
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) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} // end of while
} // end of run
}).start(); // end of Rannable
} // end of right
@Override
public void up() {
System.out.println("점프");
up=true;
setIcon(playerR);
new Thread(new Runnable() {
@Override
public void run() {
for(int i=0; i<130/JUMPSPEED; i++) {
y=y-JUMPSPEED;
setLocation(x,y);
try {
Thread.sleep(5);
} catch (InterruptedException e) {
e.printStackTrace();
}
} // end of for
up=false;
down();
} // end of run
}).start();
//객체의 상태값을 잘 조절해야 한다.
}
@Override
public void down() {
System.out.println("다운");
down=true;
new Thread(new Runnable() {
@Override
public void run() {
for(int i=0; i<130/JUMPSPEED; i++) {
y=y+JUMPSPEED;
setLocation(x,y);
try {
Thread.sleep(3);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}).start();
// 상태 처리
down = false;
}
}
package BubbleBubble.test.ex05;
public interface Moveable {
// public abstract 생략 가능
public abstract void left();
public abstract void right();
public abstract void up();
public abstract void down();
}
'Java > 자바 Swing' 카테고리의 다른 글
[java] 72. Swing: 버블버블 게임 만들기(3) (0) | 2024.05.02 |
---|---|
[Java] 71. Swing: 버블버블 게임 만들기(2) (0) | 2024.05.02 |
[Java] 70. Swing: 버블버블 게임 만들기(1) (0) | 2024.05.02 |
[Java] 66. Swing: 랜덤 숫자 게임 만들기 (0) | 2024.04.30 |
[Java] 65. Swing: 키보드 리스너 응용하기 (0) | 2024.04.30 |