파일(이미지 파일 따로 제작하기)
인터페이스 파일
[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 final int SPEED = 4;
private final int JUMPSPEED = 2;
//setter
public void setLeft(boolean left) {
this.left = left;
}
public void setRight(boolean right) {
this.right = right;
}
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;
setIcon(playerR);
setSize(50, 50);
setLocation(x,y);
}
private void setInitLayout() {
}
@Override
public void left() {
left = true;
setLocation(x, y);
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();
}
private void initData() {
backgroundMap = new JLabel(new ImageIcon("img/backgroundMap.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:
player.left();
break;
case KeyEvent.VK_RIGHT:
player.rigth();
break;
case KeyEvent.VK_UP:
player.up();
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;
}
}// end of KeyReleased
});
}
// 코드 테스트
public static void main(String[] args) {
new BubbleFrame();
}// end of main
}
실행 결과__________________
'JAVA > Java 응용' 카테고리의 다른 글
[JAVA] BUBBLE GAME(버블 게임) 5단계 (0) | 2024.05.03 |
---|---|
[JAVA] BUBBLE GAME(버블게임) 4단계 (0) | 2024.05.03 |
[JAVA] BUBBLE GAME(버블 게임) 3단계 (1) | 2024.05.02 |
[JAVA] BUBBLE GAME (버블 게임) 1단계 (0) | 2024.05.02 |
[JAVA]로또 게임 만들기 (0) | 2024.04.30 |