728x90
반응형
가위, 바위, 보 게임 메인 만들기
//가위, 바위, 보 게임 메인
public class GameMain extends JFrame implements ActionListener{
Member m = new Member();
File f = new File("id.txt");
private Map<String, Member> member = new HashMap<>();
private JPanel mainPanel, gamePanel;
private JButton loginBtn = new JButton("로그인"); //상단 로그인 버튼
private JButton logoutBtn = new JButton("로그아웃"); //상단 로그아웃 버튼
private JButton startBtn = new JButton("게임시작"); //상단 게임시작 버튼
private JLabel comImg, userImg, scoreLabel, resultLabel, mainLabel, myScoreLabel;
private JButton scissorsBtn, rockBtn, paperBtn;
private Font text1, text2;
private Timer changeImg = new Timer();
int score = 0;
int resultCoin = 0;
ImageIcon Icon[] = {
new ImageIcon("가위.png"),
new ImageIcon("바위.png"),
new ImageIcon("보.png")
};
ImageIcon Icon2[] = {
new ImageIcon("대기.gif"),
new ImageIcon("대기2.gif"),
new ImageIcon("가위2.png"),
new ImageIcon("바위2.png"),
new ImageIcon("보2.png")
};
public GameMain() {
setSize(850,850);
setTitle("Game Main");
setLocationRelativeTo(null); //화면 가운데 출력
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel mainContainer = new JPanel(); //새창을 띄우기 위한 설정
setContentPane(mainContainer);
mainContainer.setLayout(null); //로그인 버튼 위치 조절을 위함
myScoreLabel = new JLabel();
myScoreLabel.setBounds(20,10,400,20);
add(myScoreLabel);
if(!f.exists()){
loginBtn.setBounds(710, 10, 90, 36);
ActionListener listener = new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
new Login().setVisible(true);
}
};
loginBtn.addActionListener(listener);
mainContainer.add(loginBtn);
}
if(f.exists()){
logoutBtn.setBounds(710, 10, 90, 36);
ActionListener listener = new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
new LoginMain().ShowMessage("정말 로그아웃 하시겠습니까?");
dispose();
}
};
logoutBtn.addActionListener(listener);
mainContainer.add(logoutBtn);
}
text1 = new Font("배달의민족 도현", Font.BOLD, 60);
mainPanel = new JPanel();
mainPanel.setBounds(27, 60, 780, 720);
mainPanel.setLayout(null);
mainLabel = new JLabel("게임을 시작해주세요");
mainLabel.setBounds(110,120,600,300);
mainLabel.setFont(text1);
mainPanel.add(mainLabel);
add(mainPanel);
gamePanel = new JPanel();
gamePanel.setBounds(27, 60, 780, 720);
gamePanel.setBackground(Color.WHITE);
gamePanel.setLayout(null);
startBtn.setBounds(580,10,110,36);
add(startBtn);
startBtn.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if(!f.exists()){
LoginMain.ShowMessage("로그인을 해주세요!");
return;
}
List<Member> list = new ArrayList<>(new LoginMain().fileRead().values());
System.out.println(new LoginMain().idRead());
for(int i=0; i<list.size(); i++) {
if(list.get(i).getUserId().equals(new LoginMain().idRead())) {
if(list.get(i).getCoin()>=10000) {
remove(mainPanel);
add(gamePanel);
repaint();
revalidate();
TimerTask changeImg_task = new TimerTask() {
@Override
public void run() {
comImg.setIcon(Icon2[0]);
userImg.setIcon(Icon2[1]);
}
};
changeImg.schedule(changeImg_task, 0);
resultLabel.setText("가위 바위 보!");
resultCoin = 1000;
myScoreLabel.setText(new LoginMain().idRead()+"님의 게임머니는 "+new LoginMain().resultScore()+"원 입니다.");
} else {LoginMain.ShowMessage("게임머니가 10만원 미만입니다.");}
}
}
}
});
text2 = new Font("배달의민족 도현", Font.BOLD, 30);
scoreLabel = new JLabel("게임시작");
scoreLabel.setBounds(240, 40, 300, 50);
gamePanel.add(scoreLabel);
scoreLabel.setFont(text2);
scoreLabel.setHorizontalAlignment(JLabel.CENTER);
JLabel userLabel = new JLabel("YOU");
userLabel.setBounds(620,350,200,200);
userLabel.setFont(text2);
JLabel comLabel = new JLabel("COM");
comLabel.setBounds(120,350,200,200);
comLabel.setFont(text2);
gamePanel.add(userLabel);
gamePanel.add(comLabel);
comImg = new JLabel(new ImageIcon());
comImg.setBounds(40, 100, 300, 300);
gamePanel.add(comImg);
userImg = new JLabel(new ImageIcon());
userImg.setBounds(450, 100, 300, 300);
gamePanel.add(userImg);
resultLabel = new JLabel();
resultLabel.setBounds(240, 220, 300, 300);
resultLabel.setFont(text2);
gamePanel.add(resultLabel);
resultLabel.setHorizontalAlignment(JLabel.CENTER);
scissorsBtn = new JButton("가위");
scissorsBtn.setBounds(50, 500, 150, 150);
rockBtn = new JButton("바위");
rockBtn.setBounds(325, 500, 150, 150);
paperBtn = new JButton("보");
paperBtn.setBounds(600, 500, 150, 150);
gamePanel.add(scissorsBtn);
gamePanel.add(rockBtn);
gamePanel.add(paperBtn);
scissorsBtn.addActionListener(this);
rockBtn.addActionListener(this);
paperBtn.addActionListener(this);
}
@Override
public void actionPerformed(ActionEvent e) {
Object ob = e.getSource(); //이벤트를 발생시킨 이벤트 소스 객체를 얻어옴
if(ob == scissorsBtn) {
userImg.setIcon(Icon2[2]);
result(1);
}else if(ob == rockBtn) {
userImg.setIcon(Icon2[3]);
result(2);
}else if(ob == paperBtn) {
userImg.setIcon(Icon2[4]);
result(3);
}
}
public void result(int user) {
int com = (int)(Math.random()*3+1);
comImg.setIcon(Icon[com-1]);
List<Member> list = new ArrayList<>(new LoginMain().fileRead().values());
System.out.println(new LoginMain().fileRead());
for(int i=0; i<list.size(); i++) {
if(list.get(i).getUserId().equals(new LoginMain().idRead())) {
if(com==user) {
scoreLabel.setText("한 번 더!");
TimerTask changeImg_task = new TimerTask() {
@Override
public void run() {
comImg.setIcon(Icon2[0]);
userImg.setIcon(Icon2[1]);
}
};
changeImg.schedule(changeImg_task, 1000);
}else if((com==3 && user==1) || (com==1 && user==2) || (com==2 && user==3)) {
score++;
resultCoin *= 2;
scoreLabel.setText(score+"승!");
int result = JOptionPane.showConfirmDialog(null, "계속하시겠습니까? 현재 "+resultCoin+"원!\n다음판 이기면 X2, 지면 "+resultCoin/4+"원", "Confirm", JOptionPane.YES_NO_OPTION);
if(result == JOptionPane.NO_OPTION) {
scoreLabel.setText("게임 끝!");
remove(gamePanel);
add(mainPanel);
repaint();
revalidate();
add(gamePanel);
Member m = new Member(list.get(i).getUserId(), list.get(i).getUserPwd(), list.get(i).getBirthDay(), list.get(i).geteMail(), list.get(i).getCoin()+resultCoin);
new LoginMain().fileSave(list.get(i).getUserId(), m);
myScoreLabel.setText(new LoginMain().idRead()+"님의 게임머니는 "+new LoginMain().resultScore()+"원 입니다.");
} else {
TimerTask changeImg_task = new TimerTask() {
@Override
public void run() {
comImg.setIcon(Icon2[0]);
userImg.setIcon(Icon2[1]);
}
};
changeImg.schedule(changeImg_task, 500);
}
} else if((com==2 && user==1) || (com==3 && user==2) || (com==1 && user==3)){
scoreLabel.setText("게임 끝!");
resultCoin = resultCoin/4;
LoginMain.ShowMessage("GAME OVER\n"+resultCoin+"원을 벌었습니다.");
remove(gamePanel);
add(mainPanel);
repaint();
revalidate();
add(gamePanel);
score = 0;
m = new Member(list.get(i).getUserId(), list.get(i).getUserPwd(), list.get(i).getBirthDay(), list.get(i).geteMail(), list.get(i).getCoin()+resultCoin);
new LoginMain().fileSave(list.get(i).getUserId(), m);
myScoreLabel.setText(new LoginMain().idRead()+"님의 게임머니는 "+new LoginMain().resultScore()+"원 입니다.");
}
System.out.println(new LoginMain().fileRead());
}
}
}
}
반응형
'프로그래밍 > JAVA' 카테고리의 다른 글
11.26(Java와 JavaScript 차이점) (0) | 2020.11.26 |
---|---|
11.25(추상 클래스와 인터페이스) (0) | 2020.11.25 |
05.07(Mini Project - 자바 스윙을 이용하여 게임 만들기2) (0) | 2020.05.07 |
05.06(Mini Project - 자바 스윙을 이용하여 게임 만들기1) (0) | 2020.05.06 |
05.05(IO, List예제) (0) | 2020.05.05 |