본문 바로가기

개인프로젝트/리듬게임만들기

[내가 만든 프로젝트 코드 분석 | 리듬게임 만들기] 8.게임화면으로 이동하기

곡 선택화면에서 게임화면으로 이동할 수 있는 버튼을 만듭니다

private ImageIcon easyButtonEnteredImage = new ImageIcon(Main.class.getResource("../images/easyButtonEntered.png"));
private ImageIcon easyButtonBasicImage = new ImageIcon(Main.class.getResource("../images/easyButtonBasic.png"));
private ImageIcon hardButtonEnteredImage = new ImageIcon(Main.class.getResource("../images/hardButtonEntered.png"));
private ImageIcon hardButtonBasicImage = new ImageIcon(Main.class.getResource("../images/hardButtonBasic.png")); 	

private JButton easyButton = new JButton(easyButtonBasicImage);
private JButton hardButton = new JButton(hardButtonBasicImage);
// 버튼 변수 추가
easyButton.setVisible(false);
		easyButton.setBounds(375, 580, 250, 67);
		easyButton.setBorderPainted(false);
		easyButton.setContentAreaFilled(false);
		easyButton.setFocusPainted(false);
		easyButton.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseEntered(MouseEvent e) {
				easyButton.setIcon(easyButtonEnteredImage);
				easyButton.setCursor(new Cursor(Cursor.HAND_CURSOR));
				Music buttinEnteredMusic = new Music("buttonEnteredMusic.mp3", false);
				buttinEnteredMusic.start();
			}
			@Override
			public void mouseExited(MouseEvent e) {
				easyButton.setIcon(easyButtonBasicImage);
				easyButton.setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
			}
			@Override
			public void mousePressed(MouseEvent e) {
				Music buttinEnteredMusic = new Music("buttonEnteredMusic.mp3", false);
				buttinEnteredMusic.start();
				gameStart(nowSelected, "easy"); // 쉬운 난이도 시작
			}
		});
		add(easyButton);
hardButton.setVisible(false);
		hardButton.setBounds(655, 580, 250, 67);
		hardButton.setBorderPainted(false);
		hardButton.setContentAreaFilled(false);
		hardButton.setFocusPainted(false);
		hardButton.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseEntered(MouseEvent e) {
				hardButton.setIcon(hardButtonEnteredImage);
				hardButton.setCursor(new Cursor(Cursor.HAND_CURSOR));
				Music buttinEnteredMusic = new Music("buttonEnteredMusic.mp3", false);
				buttinEnteredMusic.start();
			}
			@Override
			public void mouseExited(MouseEvent e) {
				hardButton.setIcon(hardButtonBasicImage);
				hardButton.setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
			}
			@Override
			public void mousePressed(MouseEvent e) {
				Music buttinEnteredMusic = new Music("buttonEnteredMusic.mp3", false);
				buttinEnteredMusic.start();
				gameStart(nowSelected, "hard"); // 어려운 난이도 시작
			}
		});
		add(hardButton);
public void gameStart(int nowSelected, String difficulty) {	
	if(selectedMusic != null) 
			selectedMusic.close(); // 실행중인 음악 끄기
		isMainScreen = false; // 메인화면 아님 
		leftButton.setVisible(false);
		rightButton.setVisible(false);
		easyButton.setVisible(false);
		hardButton.setVisible(false);
		// 모든 버튼 가리기
		background = new ImageIcon(Main.class.getResource("../images/" + trackList.get(nowSelected).getGameImage())).getImage();
	// 선택된 곡 게임 이미지 가져오기	
	}