AfterSchool/자바 방과후
방과후 8일차_경마 게임 만들기
노 코딩 노 라이프
2023. 1. 4. 23:27
경마 게임 만들기
- GUI(JPanel - JLabe(말그림)3개)lbl1.setLocation(x, y)
- lbl1.setSize(50,50)
- pan.setLayout(null)
- Thread
- 끝까지 결승점 도착
- Random하게 하기
- 말 선택 → 선택한 말에 이름 해당말 밑에 이름 보이게 하기
<실행 화면>
<코드>
package race;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
public class RaceHorseFrame extends JFrame {
JLabel[] horses = new JLabel[5];
HorseThread[] hts = new HorseThread[horses.length];
int winnerIndex[] = new int[horses.length];
int index;
String comboStr[] = { "1번: Jerry", "2번: Turtle", "3번: Deer ", "4번: Wolf", "5번: Yellow bird" };
JComboBox<String> combo = new JComboBox<String>(comboStr);
int betingIndex;
public RaceHorseFrame() {
JPanel pan = new JPanel(null);
ImageIcon icon = null;
JLabel lineLbl = new JLabel(new ImageIcon("imgs/line.png"));
lineLbl.setBounds(540, 10, 5, 500);
JLabel flagLbl = new JLabel(new ImageIcon("imgs/flag.png"));
flagLbl.setBounds(540, 5, 20, 27);
pan.add(lineLbl);
pan.add(flagLbl);
JPanel panN = new JPanel();
JButton btnBeting = new JButton("게임배팅");
JButton btnStart = new JButton("게임시작");
btnBeting.addActionListener(btnL);
btnStart.addActionListener(btnL);
panN.add(combo);
panN.add(btnBeting);
panN.add(btnStart);
for (int i = 0; i < horses.length; i++) {
icon = new ImageIcon("imgs/horse" + (i + 1) + ".gif");
horses[i] = new JLabel(icon);
horses[i].setLocation(0, 50 + i * 85);
horses[i].setSize(60, 40);
pan.add(horses[i]);
}
add(pan, "Center");
add(panN, "North");
setTitle("경주마 게임");
setBounds(400, 100, 610, 520); // x, y, width, height
setVisible(true);
setResizable(false);
/*
* for (int i = 0; i < horses.length; i++) { hts[i] = new HorseThread(horses[i],
* "stop_horse" + (i + 1), i); hts[i].start(); }
*/
}
public static void main(String[] args) {
new RaceHorseFrame();
}
ActionListener btnL = new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
switch (e.getActionCommand()) {
case "게임배팅": // 게임베팅을 클릭했을 시
betingIndex = combo.getSelectedIndex();
break;
case "게임시작": // 게임시작을 클릭했을 시
for (int i = 0; i < horses.length; i++) {
HorseThread t = new HorseThread(horses[i], "stop_horse" + (i + 1), i);
t.start();
}
break;
}
}
};
public class HorseThread extends Thread {
JLabel lblHorse;
String stopImageName;
int randomValue;
int horseIndex;
public HorseThread(JLabel lblHorse, String stopImageName, int horseIndex) {
this.lblHorse = lblHorse;
this.stopImageName = stopImageName;
this.horseIndex = horseIndex;
}
@Override
public void run() {
while (true) {
lblHorse.setLocation(lblHorse.getX() + 5, lblHorse.getY());
if (lblHorse.getX() == 540) {
lblHorse.setIcon(new ImageIcon("imgs/" + stopImageName + ".png"));
winnerIndex[index++] = horseIndex; //제일 먼저 도착한 말이 index[0]에 들어감 -> 우승한 말의 값은 index[0]에 들어가 있음
if (index == horses.length - 1) { //말 5마리 -> 0 1 2 3 4
JOptionPane.showMessageDialog(RaceHorseFrame.this, (winnerIndex[0] + 1) + "번째 말이 우승!!!");
if (winnerIndex[0] == betingIndex) // 이긴말과 내가 고른 말이 번호가 같다면 배팅 성공
JOptionPane.showMessageDialog(RaceHorseFrame.this, "축하합니다. 배팅에 성공하였습니다.");
else
JOptionPane.showMessageDialog(RaceHorseFrame.this, "다음에 다시 배팅 부탁드려요~. 배팅에 실패하였습니다.");
index = 0; //게임 초기화 시키는 부분
for (int i = 0; i < horses.length; i++) {
horses[i].setLocation(0, horses[i].getY());
horses[i].setIcon(new ImageIcon("imgs/horse" + (i + 1) + ".gif"));
}
}
break;
}
try {
Random random = new Random();
randomValue = random.nextInt(10); //0~9 사이의 값
sleep(10 * randomValue);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
}
<개선하기>
- 배팅을 두 사람이 가능하게 변경
- 배팅하는 두 사람의 이름도 저장해서 승리한 Beting 자의 이름을 표시
<실행화면>
<코드>
package race;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
public class RaceHorseFrame extends JFrame {
JLabel[] horses = new JLabel[5];
HorseThread[] hts = new HorseThread[horses.length];
int winnerIndex[] = new int[horses.length];
int index;
String comboStr[] = { "1번: 귀염둥이 제리", "2번: 잠좀자자 자라", "3번: 맛있는 사슴 ", "4번: 꼬끼욧 늑대", "5번: 노랑노랑 새" };
JComboBox<String> combo = new JComboBox<String>(comboStr);
int betingIndex;
BetingPerson bet1, bet2;
public RaceHorseFrame() {
JPanel pan = new JPanel(null);
ImageIcon icon = null;
JLabel lineLbl = new JLabel(new ImageIcon("imgs/line.png"));
lineLbl.setBounds(540, 10, 5, 500);
JLabel flagLbl = new JLabel(new ImageIcon("imgs/flag.png"));
flagLbl.setBounds(540, 5, 20, 27);
pan.add(lineLbl);
pan.add(flagLbl);
JPanel panN = new JPanel();
JButton btnBeting1 = new JButton("게임배팅1");
JButton btnBeting2 = new JButton("게임배팅2");
JButton btnStart = new JButton("게임시작");
btnBeting1.addActionListener(btnL);
btnBeting2.addActionListener(btnL);
btnStart.addActionListener(btnL);
panN.add(combo);
panN.add(btnBeting1);
panN.add(btnBeting2);
panN.add(btnStart);
for (int i = 0; i < horses.length; i++) {
icon = new ImageIcon("imgs/horse" + (i + 1) + ".gif");
horses[i] = new JLabel(icon);
horses[i].setLocation(0, 50 + i * 85);
horses[i].setSize(60, 40);
pan.add(horses[i]);
}
add(pan, "Center");
add(panN, "North");
setTitle("경주마 게임");
setDefaultCloseOperation(EXIT_ON_CLOSE);
setBounds(400, 100, 610, 520); // x, y, width, height
setVisible(true);
setResizable(false);
/*
* for (int i = 0; i < horses.length; i++) { hts[i] = new HorseThread(horses[i],
* "stop_horse" + (i + 1), i); hts[i].start(); }
*/
}
public static void main(String[] args) {
new RaceHorseFrame();
}
ActionListener btnL = new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
switch (e.getActionCommand()) {
case "게임배팅1":
bet1 = new BetingPerson();
bet1.setOrderNum(1);
bet1.setName(JOptionPane.showInputDialog("배팅하는 사람의 이름을 입력하세요"));
bet1.setBetingIndex(combo.getSelectedIndex());
break;
case "게임배팅2":
bet2 = new BetingPerson();
bet2.setOrderNum(2);
bet2.setName(JOptionPane.showInputDialog("배팅하는 사람의 이름을 입력하세요"));
bet2.setBetingIndex(combo.getSelectedIndex());
break;
case "게임시작": // 게임시작을 클릭했을 시
for (int i = 0; i < horses.length; i++) {
HorseThread t = new HorseThread(horses[i], "stop_horse" + (i + 1), i);
t.start();
}
break;
}
}
};
public class HorseThread extends Thread {
JLabel lblHorse;
String stopImageName;
int randomValue;
int horseIndex;
public HorseThread(JLabel lblHorse, String stopImageName, int horseIndex) {
this.lblHorse = lblHorse;
this.stopImageName = stopImageName;
this.horseIndex = horseIndex;
}
@Override
public void run() {
while (true) {
lblHorse.setLocation(lblHorse.getX() + 5, lblHorse.getY());
if (lblHorse.getX() == 540) {
lblHorse.setIcon(new ImageIcon("images/" + stopImageName + ".gif"));
winnerIndex[index++] = horseIndex;
if (index == horses.length - 1) {
JOptionPane.showMessageDialog(RaceHorseFrame.this, (winnerIndex[0] + 1) + "말이 우승!!!");
if (winnerIndex[0] == bet1.getBetingIndex())
JOptionPane.showMessageDialog(RaceHorseFrame.this,
"축하합니다. " + bet1.getName() + "님 배팅에 성공하였습니다.");
else if (winnerIndex[0] == bet2.getBetingIndex())
JOptionPane.showMessageDialog(RaceHorseFrame.this,
"축하합니다. " + bet2.getName() + "님 배팅에 성공하였습니다.");
else
JOptionPane.showMessageDialog(RaceHorseFrame.this, "다음에 다시 배팅 부탁드려요~. 모두 배팅에 실패하였습니다.");
index = 0;
for (int i = 0; i < horses.length; i++) {
// System.out.println(winnerIndex[i]);
horses[i].setLocation(0, horses[i].getY());
horses[i].setIcon(new ImageIcon("imgs/horse" + (i + 1) + ".gif"));
}
}
break;
}
try {
Random random = new Random();
randomValue = random.nextInt(10); // 0~9 사이의 값
sleep(10 * randomValue);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
}
package race;
public class BetingPerson {
private int orderNum;
private String name;
private int betingIndex;
public int getBetingIndex() {
return betingIndex;
}
public void setBetingIndex(int betingIndex) {
this.betingIndex = betingIndex;
}
public int getOrderNum() {
return orderNum;
}
public void setOrderNum(int orderNum) {
this.orderNum = orderNum;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}