Advertisement
Guest User

Untitled

a guest
Jan 18th, 2018
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 27.47 KB | None | 0 0
  1. import java.awt.BorderLayout;
  2. import java.awt.Color;
  3. import java.awt.Container;
  4. import java.awt.FlowLayout;
  5. import java.awt.Font;
  6. import java.awt.Graphics;
  7. import java.awt.Graphics2D;
  8. import java.awt.GridLayout;
  9. import java.awt.Rectangle;
  10. import java.awt.event.ActionEvent;
  11. import java.awt.event.ActionListener;
  12. import java.awt.event.KeyEvent;
  13. import java.awt.event.KeyListener;
  14. import java.io.BufferedReader;
  15. import java.io.BufferedWriter;
  16. import java.io.File;
  17. import java.io.FileInputStream;
  18. import java.io.FileOutputStream;
  19. import java.io.FileReader;
  20. import java.io.FileWriter;
  21. import java.io.IOException;
  22. import java.io.ObjectInputStream;
  23. import java.io.ObjectOutputStream;
  24. import java.io.Serializable;
  25. import java.util.ArrayList;
  26.  
  27. import javax.sound.sampled.AudioFormat;
  28. import javax.sound.sampled.AudioInputStream;
  29. import javax.sound.sampled.AudioSystem;
  30. import javax.sound.sampled.Clip;
  31. import javax.sound.sampled.DataLine;
  32. import javax.swing.ImageIcon;
  33. import javax.swing.JButton;
  34. import javax.swing.JFileChooser;
  35. import javax.swing.JFrame;
  36. import javax.swing.JLabel;
  37. import javax.swing.JPanel;
  38. import javax.swing.JTextArea;
  39. import javax.swing.WindowConstants;
  40. import javax.swing.Timer;
  41.  
  42. public class ICS4U1_BeatGame {
  43.  
  44. public static void main(String[] args) {
  45. MyUtil.importSongs();
  46. MenuFrame frame = new MenuFrame("M U S I C G A M E");
  47. }
  48. }
  49.  
  50.  
  51. class MenuFrame extends JFrame implements ActionListener, KeyListener{
  52. private JPanel menu, menuButtons;
  53. private Container c;
  54. private JButton buttonArray[];
  55. private String buttons[] = {"Load Game", "Create BeatMap", "Instructions"};
  56. private ImageIcon programIcon;
  57. private JFileChooser fc;
  58. private BeatMap mapIn;
  59. private GamePanel gamePanel;
  60. private JPanel keys;
  61. private JButton keyButtons[];
  62. private String keyText[] = {"d", "f", "space", "j", "k"};
  63. private Timer playerTimer;
  64. private long playerStartTime, playerCurrentTime, playerNoteTime, playerSongTime;
  65. private int playerNoteKey;
  66. private Note playerNote;
  67. private int score, bestScore;
  68. private boolean donePlaying;
  69. private JTextArea scoreInfo;
  70.  
  71. public MenuFrame(String s) {
  72. setTitle(s);
  73. fc = new JFileChooser();
  74. mapIn = new BeatMap();
  75. programIcon = new ImageIcon("programIcon.png");//this is the thumbnail for the program (design one if you want)
  76. setIconImage(programIcon.getImage());
  77. buttonArray = new JButton[buttons.length];
  78. c = getContentPane();
  79. gamePanel = new GamePanel();
  80. scoreInfo = new JTextArea();
  81. //scoreInfo.setEditable(false);
  82. keys = new JPanel();
  83.  
  84. keyButtons = new JButton[keyText.length];
  85. for (int i = 0; i < keyText.length; i++) {
  86. keyButtons[i] = new JButton(keyText[i]);
  87. keyButtons[i].setBackground(new Color(246,246,246));
  88. keyButtons[i].setFont(new Font("Helvetica Nue", Font.PLAIN, 12));
  89. keyButtons[i].setBorderPainted(false);
  90. keyButtons[i].addKeyListener(this);
  91. keys.add(keyButtons[i]);
  92. }
  93. keys.addKeyListener(this);
  94. menu = new JPanel(new FlowLayout(FlowLayout.CENTER, 20,170));
  95. menuButtons = new JPanel();
  96. menuButtons.setLayout(new GridLayout(3,1,10,10));
  97. for (int i = 0; i < buttons.length; i++) {
  98. buttonArray[i] = new JButton(buttons[i]);
  99. buttonArray[i].setBackground(new Color(246,246,246));
  100. buttonArray[i].setFont(new Font("Helvetica Nue", Font.PLAIN, 12));
  101. buttonArray[i].setBorderPainted(false);
  102. buttonArray[i].addActionListener(this);
  103. menuButtons.add(buttonArray[i]);
  104.  
  105. }
  106. menu.add(menuButtons);
  107. menu.setBackground(new Color(156,149,131));
  108. menuButtons.setBackground(new Color(156,149,131));
  109. c.add(menu, BorderLayout.CENTER);
  110. setSize(300,500);
  111. setResizable(false);
  112. setVisible(true);
  113. setDefaultCloseOperation (WindowConstants.EXIT_ON_CLOSE);
  114. }
  115. public void actionPerformed(ActionEvent e) {
  116.  
  117. if (e.getSource() == playerTimer) {
  118. }else {
  119. JButton b = (JButton) e.getSource();
  120. if (b == buttonArray[1]) {
  121. //stop initial menu music
  122. MapCreator creator = new MapCreator("Map Creator");
  123. } else if (b == buttonArray[0]) {
  124.  
  125. try{
  126. int returnVal = fc.showOpenDialog(this);
  127. if (returnVal == JFileChooser.APPROVE_OPTION) {
  128. File file = fc.getSelectedFile();
  129. openMethod(file);
  130. //menu.setVisible(false);
  131. remove(menu);
  132. updateHighscore(score);
  133. scoreInfo.setText(" High Score: " + bestScore + " Current Score: " + score);
  134. c.add(scoreInfo, BorderLayout.NORTH);
  135. c.add(gamePanel, BorderLayout.CENTER);
  136. c.add(keys, BorderLayout.SOUTH);
  137. keys.setFocusable(true);
  138. keys.requestFocusInWindow();
  139. revalidate();
  140. playerTimer = new Timer(10,this);
  141. playerStartTime = System.nanoTime()/10000000;
  142. playerTimer.start();
  143. score = 0;
  144. donePlaying = false;
  145. }
  146.  
  147. }catch(Exception ex){
  148. System.out.print("Error" + ex);
  149. }
  150. } else if (b == buttonArray[2]) {
  151. InstructionFrame instruc = new InstructionFrame("Instructions");
  152.  
  153. }
  154. }
  155. }
  156. public void openMethod(File filePath){
  157. FileInputStream fileIn = null;
  158. ObjectInputStream objectIn = null;
  159. try{
  160. fileIn = new FileInputStream(filePath);
  161. objectIn = new ObjectInputStream(fileIn);
  162. mapIn = (BeatMap)(objectIn.readObject());
  163. gamePanel.setMap(mapIn);
  164.  
  165. objectIn.close();
  166. } catch (Exception ex) {
  167. ex.printStackTrace();
  168. }
  169. }
  170.  
  171. public void paint(Graphics g) {
  172. super.paint(g);
  173. g.setColor(new Color(246,246,246));
  174. g.setFont(new Font("Helvetica Nue", Font.PLAIN, 12));
  175. g.drawString("M U S I C G A M E", 87, 140);
  176.  
  177. //Rectangle
  178. g.fillRect(222, 473, 3, 20);
  179.  
  180. //Leaves
  181. g.fillArc(222,470, 20,15, -180, -230 );
  182. g.fillArc(206, 470, 20, 15, 0, 230 );
  183. }
  184.  
  185. public void keyTyped(KeyEvent e) {}
  186.  
  187. public void keyPressed(KeyEvent e) {
  188. playerCurrentTime = System.nanoTime()/10000000 - 430;
  189. playerNoteTime = playerCurrentTime - playerStartTime;
  190. System.out.println("Player Current Time: " + playerNoteTime);
  191. if (e.getKeyCode() == 68){
  192. keyButtons[0].setBackground(Color.yellow);
  193. playerNoteKey = 1;
  194. }
  195. if (e.getKeyCode() == 70) {
  196. keyButtons[1].setBackground(Color.yellow);
  197. playerNoteKey = 2;
  198. }
  199. if (e.getKeyCode() == 32) {
  200. keyButtons[2].setBackground(Color.yellow);
  201. playerNoteKey = 3;
  202. }
  203. if (e.getKeyCode() == 74) {
  204. keyButtons[3].setBackground(Color.yellow);
  205. playerNoteKey = 4;
  206. }
  207. if (e.getKeyCode() == 75) {
  208. keyButtons[4].setBackground(Color.yellow);
  209. playerNoteKey = 5;
  210. }
  211. playerNote = mapIn.getNextNote();
  212. if (playerNote != null) {
  213. if (playerNote.compareNote(playerNoteTime, playerNoteKey) == 1) {
  214. score += playerNote.calcScore(50, playerNoteTime);
  215. scoreInfo.setText(" High Score: " + bestScore + " Current Score: " + score);
  216. }
  217. }
  218. try {
  219. if (playerNoteTime> MyUtil.clip.getMicrosecondLength()/10000) {
  220. System.out.println(MyUtil.clip.getMicrosecondLength()/10000);
  221. donePlaying = true;
  222. System.out.println("song is over");
  223. updateHighscore(score);
  224. scoreInfo.setText(" High Score: " + bestScore + " Current Score: " + score);
  225. }
  226. }
  227. catch(Exception ex) {}
  228.  
  229. }
  230.  
  231. public void keyReleased(KeyEvent e) {
  232. if (e.getKeyCode() == 68){
  233. keyButtons[0].setBackground(new Color(246, 246, 246));
  234. }
  235. if (e.getKeyCode() == 70) {
  236. keyButtons[1].setBackground(new Color(246, 246, 246));
  237. }
  238. if (e.getKeyCode() == 32) {
  239. keyButtons[2].setBackground(new Color(246, 246, 246));
  240. }
  241. if (e.getKeyCode() == 74) {
  242. keyButtons[3].setBackground(new Color(246, 246, 246));
  243. }
  244. if (e.getKeyCode() == 75) {
  245. keyButtons[4].setBackground(new Color(246, 246, 246));
  246. }
  247. }
  248. public void paintComponent(Graphics gr) {
  249. super.paintComponents(gr);
  250. gr.setColor(Color.black);
  251.  
  252. gr.drawString("Highscore: " + bestScore + " Your Score: " + score, 20, 30);
  253. }
  254. public void updateHighscore (int score) {
  255. File myFile = new File("a.out");
  256. FileReader fr;
  257. BufferedReader reader;
  258. String lineIn="0";
  259. try {
  260. fr = new FileReader(myFile);
  261. reader = new BufferedReader(fr);
  262. lineIn = reader.readLine();
  263. reader.close();
  264. fr.close();
  265. }
  266. catch (Exception e) {
  267. System.out.println(e);
  268. }
  269.  
  270. bestScore = Integer.parseInt(lineIn);
  271.  
  272. if (bestScore < score) {
  273. System.out.println("New High Score!");
  274. BufferedWriter bw;
  275. try {
  276. bw=new BufferedWriter(new FileWriter ("a.out"));
  277. bw.write("" + score);
  278. bw.close();
  279. } catch (Exception e) {
  280. System.out.println(e);
  281. }
  282. }
  283. }
  284.  
  285. }
  286. class InstructionFrame extends JFrame {
  287. private JTextArea ins;
  288. private Container c;
  289. public InstructionFrame(String s) {
  290. setTitle(s);
  291. setResizable(false);
  292. setSize(300,500);
  293. setVisible(true);
  294. c=getContentPane();
  295. ins = new JTextArea();
  296. ins.setText("INSTRUCTIONS\n\nCreate your own beatmap by importing .wav files\nor load a pre-existing map.\n" +
  297. "Begin to press the keys (d, f, spacebar, j, and k)\nonce the song starts to play.\n" +
  298. "Try to match your presses with the descending keys.\n" +
  299. "Press any key once more after the game to see\nyour highscore update.\n" +
  300. "");
  301. c.add(ins, BorderLayout.CENTER);
  302.  
  303.  
  304. }
  305. }
  306. class MapCreator extends JFrame implements ActionListener, KeyListener{
  307. private JButton btn_open = new JButton("Open Song");
  308. private JButton btn_save = new JButton("Save");
  309. private JButton btn_start = new JButton("Start Recording");
  310. private Container c;
  311. private JPanel control, side, main, creator;
  312. private JLabel selectedSong;
  313. private int currentSong;
  314. private JFileChooser fc;
  315. private SongSelect selector;
  316. private long startTime, currentTime, noteTime, songTime;
  317. private BeatMap map1;
  318. private boolean isRecording = false;
  319. private int noteKey;
  320. private Timer songTimer;
  321. private JButton keys[];
  322. private String keyText[] = {"d", "f", "space", "j", "k"};
  323.  
  324.  
  325. public MapCreator(String s, int currentSong) {
  326. this.currentSong = currentSong;
  327. fc = new JFileChooser();
  328. control = new JPanel();
  329. main = new JPanel();
  330. creator = new JPanel();
  331. side = new JPanel();
  332. selectedSong = new JLabel();
  333. c = getContentPane();
  334. control.setLayout(new GridLayout(3,1,10,10));
  335. creator.setLayout(new GridLayout(1, 5, 20,10));
  336. main.setLayout(new FlowLayout(FlowLayout.CENTER, 20,100));
  337. side.setLayout(new FlowLayout(FlowLayout.CENTER, 20, 150));
  338. songTimer = new Timer(10, this);
  339. btn_open.setBackground(new Color(246,246,246));
  340. btn_open.setFont(new Font("Helvetica Nue", Font.PLAIN, 12));
  341. btn_open.setBorderPainted(false);
  342. btn_open.addActionListener(this);
  343. control.add(btn_open);
  344.  
  345. btn_save.setBackground(new Color(246,246,246));
  346. btn_save.setFont(new Font("Helvetica Nue", Font.PLAIN, 12));
  347. btn_save.setBorderPainted(false);
  348. btn_save.addActionListener(this);
  349. control.add(btn_save);
  350.  
  351. btn_start.setBackground(new Color(246,246,246));
  352. btn_start.setFont(new Font("Helvetica Nue", Font.PLAIN, 12));
  353. btn_start.setBorderPainted(false);
  354. btn_start.addActionListener(this);
  355. control.add(btn_start);
  356.  
  357. keys = new JButton[keyText.length];
  358. for (int i = 0; i < keyText.length; i++) {
  359. keys[i] = new JButton(keyText[i]);
  360. keys[i].setBackground(new Color(246,246,246));
  361. keys[i].setFont(new Font("Helvetica Nue", Font.PLAIN, 12));
  362. keys[i].setBorderPainted(false);
  363. main.add(keys[i]);
  364. }
  365.  
  366. if (currentSong == -1) {
  367. selectedSong.setText("Selected song: ");
  368. }else {
  369. selectedSong.setText("Selected song: " + MyUtil.currentSong(currentSong).getName());
  370. }
  371. main.add(selectedSong);
  372. side.add(control);
  373. creator.add(main);
  374.  
  375. main.addKeyListener(this);
  376. btn_save.addKeyListener(this);
  377. btn_open.addKeyListener(this);
  378. btn_start.addKeyListener(this);
  379. main.setFocusable(true);
  380.  
  381. c.add(side, BorderLayout.EAST);
  382. c.add(creator, BorderLayout.CENTER);
  383.  
  384. setTitle(s);
  385. setResizable(false);
  386. setSize(600,300);
  387. setVisible(true);
  388. //setDefaultCloseOperation (WindowConstants.EXIT_ON_CLOSE);
  389. }
  390. public MapCreator(String s) {
  391. this(s, -1);
  392. }
  393. public void actionPerformed (ActionEvent e) {
  394. if (isRecording) {
  395. currentTime = System.nanoTime()/10000000;
  396. if (currentTime - startTime > songTime) {
  397. isRecording = false;
  398. System.out.println("Song is over");
  399. songTimer.stop();
  400. }
  401. return;
  402. }
  403. JButton b = (JButton) e.getSource();
  404. if (b == btn_open) {
  405. selector = new SongSelect("Song Select");
  406. this.dispose();
  407.  
  408. } else if (b == btn_save) {
  409. try{
  410. int returnVal = fc.showSaveDialog(this); // need to import javax.swing.filechooser.*;
  411.  
  412. if (returnVal == JFileChooser.APPROVE_OPTION) { // if user did not click cancel and picked file name
  413. File file = fc.getSelectedFile(); // picked name
  414.  
  415. saveMethod(file);
  416. }
  417.  
  418. }catch(Exception ex){
  419. System.out.print("Error" + ex);
  420. }
  421. }
  422. else if (b == btn_start) {
  423. if (currentSong != -1) {
  424. System.out.println("Recording started");
  425. songTime = MyUtil.playSong(currentSong);
  426. System.out.println("Song time: " + songTime);
  427. map1 = new BeatMap(currentSong);
  428. startTime = System.nanoTime()/10000000;
  429. songTimer.start();
  430. isRecording = true;
  431. main.setFocusable(true);
  432. btn_start.setFocusable(false);
  433. }
  434. }
  435. }
  436. public void keyPressed(KeyEvent ev) {
  437. if (isRecording) {
  438. noteTime = currentTime - startTime;
  439. currentTime = System.nanoTime()/10000000;
  440. if (currentTime - startTime > songTime)
  441. isRecording = false;
  442. }
  443. if (ev.getKeyCode() == 68){
  444. noteKey = 1;
  445. keys[0].setBackground(Color.yellow);
  446. }
  447. if (ev.getKeyCode() == 70) {
  448. noteKey = 2;
  449. keys[1].setBackground(Color.yellow);
  450. }
  451. if (ev.getKeyCode() == 32) {
  452. noteKey = 3;
  453. keys[2].setBackground(Color.yellow);
  454. }
  455. if (ev.getKeyCode() == 74) {
  456. noteKey = 4;
  457. keys[3].setBackground(Color.yellow);
  458. }
  459. if (ev.getKeyCode() == 75) {
  460. noteKey = 5;
  461. keys[4].setBackground(Color.yellow);
  462. }
  463. if (noteKey != -1 && isRecording) {
  464. map1.addNote(noteTime, noteKey);
  465. System.out.println("Added note");
  466. System.out.println("Time: " + noteTime);
  467. System.out.println("Note:" + noteKey);
  468. }
  469. }
  470. public void keyReleased(KeyEvent ev) {
  471. if (isRecording) {
  472. int n = ev.getKeyCode();
  473. switch(n) {
  474. case 68: // d
  475. noteKey = 1;
  476. keys[0].setBackground(new Color(246, 246, 246));
  477. break;
  478. case 70: // f
  479. noteKey = 2;
  480. keys[1].setBackground(new Color(246, 246, 246));
  481. break;
  482. case 32: // spacebar
  483. noteKey = 3;
  484. keys[2].setBackground(new Color(246, 246, 246));
  485. break;
  486. case 74: // j
  487. noteKey = 4;
  488. keys[3].setBackground(new Color(246, 246, 246));
  489. break;
  490. case 75: // k
  491. noteKey = 5;
  492. keys[4].setBackground(new Color(246, 246, 246));
  493. break;
  494. default:
  495. noteKey = -1;
  496. break;
  497. }
  498.  
  499. }else {
  500. System.out.println("Recording is over");
  501. }
  502. }
  503. public void keyTyped(KeyEvent ev) {
  504. }
  505. public void saveMethod(File filePath){
  506. FileOutputStream fout = null;
  507. ObjectOutputStream oos = null;
  508.  
  509. try {
  510. fout = new FileOutputStream(filePath);
  511. oos = new ObjectOutputStream(fout);
  512. oos.writeObject(map1);
  513. fout.close();
  514. oos.close();
  515. } catch (Exception ex) {
  516. ex.printStackTrace();
  517. }
  518. }
  519.  
  520. }
  521.  
  522.  
  523. class SongSelect extends JFrame implements ActionListener {
  524. private Container c;
  525. private JPanel songSelect, southPanel, base;
  526. private JButton[] btnArr;
  527. private JButton selectSong, clearSong;
  528. private int currentSong;
  529.  
  530. public SongSelect(String s) {
  531. songSelect = new JPanel();
  532. southPanel = new JPanel();
  533. base = new JPanel();
  534. selectSong = new JButton("Select Song");
  535. selectSong.setBackground(new Color(246,246,246));
  536. selectSong.setFont(new Font("Helvetica Nue", Font.PLAIN, 12));
  537. selectSong.setBorderPainted(false);
  538. selectSong.addActionListener(this);
  539. clearSong = new JButton("Clear Song");
  540. clearSong.setBackground(new Color(246,246,246));
  541. clearSong.setFont(new Font("Helvetica Nue", Font.PLAIN, 12));
  542. clearSong.setBorderPainted(false);
  543. clearSong.addActionListener(this);
  544. c = getContentPane();
  545. southPanel.setLayout(new FlowLayout(FlowLayout.CENTER));
  546. southPanel.add(selectSong);
  547. southPanel.add(clearSong);
  548. songSelect.setLayout(new FlowLayout(FlowLayout.CENTER, 20, 150));
  549. btnArr = new JButton[MyUtil.songArr.size()];
  550. for (int i = 0; i < btnArr.length; i++) {
  551. btnArr[i] = new JButton(MyUtil.songArr.get(i).getName());
  552. songSelect.add(btnArr[i]);
  553. btnArr[i].setBackground(new Color(246,246,246));
  554. btnArr[i].setFont(new Font("Helvetica Nue", Font.PLAIN, 12));
  555. btnArr[i].setBorderPainted(false);
  556. btnArr[i].addActionListener(this);
  557. }
  558. c.add(songSelect, BorderLayout.CENTER);
  559. c.add(southPanel, BorderLayout.SOUTH);
  560. setTitle(s);
  561. setResizable(false);
  562. setSize(600,300);
  563. setVisible(true);
  564. //setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
  565.  
  566. }
  567.  
  568. public void actionPerformed(ActionEvent e) {
  569. //System.out.println("Action performed");
  570. JButton b = (JButton)e.getSource();
  571. if (b == clearSong) {
  572. try {
  573. MyUtil.clearSong();
  574. }catch(Exception ex) {}
  575. }else if(b == selectSong) {
  576. try {
  577. MyUtil.clearSong();
  578. }catch(Exception ex) {}
  579. MapCreator creator = new MapCreator("Map Creator", currentSong);
  580. this.dispose();
  581.  
  582. }
  583. for (int i = 0; i < btnArr.length; i++) {
  584. if (btnArr[i] == e.getSource()) {
  585. try {
  586. MyUtil.clearSong();
  587. }catch(Exception ex) {}
  588. MyUtil.playSong(i);
  589. currentSong = i;
  590. }
  591. }
  592. }
  593. }
  594.  
  595.  
  596. class MyUtil {
  597. static ArrayList<Song> songArr = new ArrayList<Song>();
  598. static File songFolder = new File("Songs");
  599. static AudioInputStream stream;
  600. static AudioFormat format;
  601. static DataLine.Info info;
  602. static Clip clip;
  603.  
  604. public static void importSongs() {
  605. File[] files = songFolder.listFiles();
  606. for (int i = 0; i < files.length; i++) {
  607. songArr.add(new Song(files[i], files[i].getName(), i));
  608. System.out.println("Adding new song: " + files[i].getName());
  609. }
  610. }
  611. public static long playSong(int i) {
  612. try {
  613. stream = AudioSystem.getAudioInputStream(songArr.get(i).getSongFile());
  614. format = stream.getFormat();
  615. info = new DataLine.Info(Clip.class, format);
  616. clip = (Clip) AudioSystem.getLine(info);
  617. clip.open(stream);
  618. clip.start();
  619. }catch(Exception e) {
  620. e.printStackTrace();
  621. }
  622. return clip.getMicrosecondLength()/10000;
  623. }
  624. public static void clearSong() {
  625. clip.close();
  626. }
  627. public static Song currentSong(int i) {
  628. return songArr.get(i);
  629. }
  630. }
  631. class Song implements Serializable{
  632. private File songFile;
  633. private String name;
  634. private int songNum;
  635.  
  636. public Song(File file, String name, int songNum) {
  637. songFile = file;
  638. this.name = name;
  639. this.songNum = songNum;
  640. }
  641. public Song(String name) {
  642. this(new File(""), name, 0);
  643. }
  644.  
  645. public String getName() {
  646. return name;
  647. }
  648. public File getSongFile() {
  649. return songFile;
  650. }
  651. public int getSongNum() {
  652. return songNum;
  653. }
  654. }
  655.  
  656. class BeatMap implements Serializable {
  657. private ArrayList<Note> notes;
  658. private Song song;
  659.  
  660. public BeatMap(){}
  661. public BeatMap(int i) {
  662. notes = new ArrayList<Note>();
  663. song = MyUtil.currentSong(i);
  664. addNote(0, 3);
  665. }
  666. public void addNote(long noteTime, int noteKey) {
  667. notes.add(new Note(noteTime, noteKey));
  668. }
  669. public ArrayList<Note> getAllNotes() {
  670. return notes;
  671. }
  672. public int getNumNotes() {
  673. return notes.size();
  674. }
  675. public Song getSong(){
  676. return song;
  677. }
  678. public Note getNextNote() {
  679. for (int i = 0; i < notes.size(); i++) {
  680. if (notes.get(i).hasPlayed == false) {
  681. System.out.print(notes.get(i).noteKey);
  682. notes.get(i).canPlay = true;
  683. return (notes.get(i));
  684. }
  685. }
  686. return null; //itll never reach this right
  687. }
  688. }
  689. class Note implements Serializable {
  690. private long noteTime;
  691. public int noteKey;
  692. public int moveY;
  693. private boolean onScreen;
  694. public boolean hasPlayed, canPlay;
  695. public Note(long noteTime, int noteKey) {
  696. this.noteTime = noteTime;
  697. this.noteKey = noteKey;
  698. moveY = (int)(noteTime*-1); //the spacing messes up the accuracy of the bars
  699. onScreen = true;
  700. hasPlayed = false;
  701. canPlay = false;
  702.  
  703. }
  704. public int calcScore(int range, long pNoteTime) {
  705.  
  706. if (range <= 1 || Math.abs(pNoteTime - noteTime) > range) {
  707. return 1;
  708. }
  709. else {
  710. return range + calcScore(range/2, pNoteTime);
  711. }
  712. }
  713. //compares the player's key presses to the created map for accuracy and returns score
  714. public int compareNote(long pNoteTime, int pNoteKey) {
  715. if (pNoteKey == noteKey) {
  716. System.out.println("Player note time: " + pNoteTime);
  717. System.out.println("Note time: " + noteTime);
  718. if (canPlay == true && Math.abs(pNoteTime - noteTime) < 100) {
  719. System.out.println(canPlay);
  720. canPlay = false;
  721. hasPlayed = true;
  722. System.out.println("good!");
  723. return 1;
  724. } else {
  725. System.out.println("bad");
  726. return 0;
  727.  
  728. }
  729. } else {
  730. System.out.println("bad");
  731. return 0;
  732.  
  733. }
  734. }
  735. public void draw(Graphics g, long currentTime){
  736. if (moveY + currentTime > 450 && onScreen) {
  737. //System.out.println("NoteTime: " + noteTime);
  738. onScreen = false;
  739. hasPlayed = true;
  740. }
  741. if (noteKey == 1) {
  742. g.fillRect(18, (int) (moveY + currentTime), 35, 5); ///lol this is not the right spacing
  743. } else if (noteKey == 2) {
  744. g.fillRect(64, (int) (moveY + currentTime) , 35, 5);
  745. } else if(noteKey == 3) {
  746. g.fillRect(109, (int) (moveY + currentTime), 60, 5);
  747. } else if (noteKey == 4){
  748. g.fillRect(178, (int) (moveY + currentTime), 35, 5);
  749. } else if (noteKey == 5) {
  750. g.fillRect(222, (int) (moveY + currentTime), 35, 5);
  751. }
  752. }
  753. }
  754. class GamePanel extends JPanel implements ActionListener{
  755. BeatMap map1;
  756. Timer gameTimer;
  757. public long startTime, currentTime;
  758. private boolean songPlaying;
  759.  
  760. public GamePanel() {}
  761.  
  762. public void setMap(BeatMap m){
  763. map1 = m;
  764. gameTimer = new Timer(10,this); //every tick (tick = 0.01s)
  765. gameTimer.start();
  766. startTime = System.nanoTime()/10000000;
  767. songPlaying = false;
  768. //MyUtil.playSong(m.getSong().getSongNum());
  769. }
  770.  
  771. public void actionPerformed(ActionEvent e) {
  772. currentTime = (System.nanoTime()/10000000) - startTime;
  773. //System.out.println("Current Time: " + currentTime);
  774. if (e.getSource() == gameTimer) {
  775. if (currentTime > 430 && !songPlaying) {
  776. MyUtil.playSong(map1.getSong().getSongNum());
  777. songPlaying = true;
  778. }
  779. for (int i = 0; i < map1.getNumNotes(); i++) {
  780. //map1.getAllNotes().get(i).moveY += 1; //lol also not the right speed
  781. //map1.getAllNotes().get(i).updatePos(currentTime);
  782. }
  783. repaint();
  784. }
  785. }
  786. public void paintComponent(Graphics g){
  787. super.paintComponent(g);
  788. for (int i = 0; i < map1.getAllNotes().size(); i++) {
  789. map1.getAllNotes().get(i).draw(g, currentTime);
  790. }
  791. repaint();
  792.  
  793. }
  794. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement