Advertisement
Guest User

Untitled

a guest
Jan 18th, 2018
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 27.50 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.setEditable(false);
  297. ins.setText("INSTRUCTIONS\n\nCreate your own beatmap by importing .wav files\nor load a pre-existing map.\n" +
  298. "Begin to press the keys (d, f, spacebar, j, and k)\nonce the song starts to play.\n" +
  299. "Try to match your presses with the descending keys.\n" +
  300. "Press any key once more after the game to see\nyour highscore update.\n" +
  301. "");
  302. c.add(ins, BorderLayout.CENTER);
  303.  
  304.  
  305. }
  306. }
  307. class MapCreator extends JFrame implements ActionListener, KeyListener{
  308. private JButton btn_open = new JButton("Open Song");
  309. private JButton btn_save = new JButton("Save");
  310. private JButton btn_start = new JButton("Start Recording");
  311. private Container c;
  312. private JPanel control, side, main, creator;
  313. private JLabel selectedSong;
  314. private int currentSong;
  315. private JFileChooser fc;
  316. private SongSelect selector;
  317. private long startTime, currentTime, noteTime, songTime;
  318. private BeatMap map1;
  319. private boolean isRecording = false;
  320. private int noteKey;
  321. private Timer songTimer;
  322. private JButton keys[];
  323. private String keyText[] = {"d", "f", "space", "j", "k"};
  324.  
  325.  
  326. public MapCreator(String s, int currentSong) {
  327. this.currentSong = currentSong;
  328. fc = new JFileChooser();
  329. control = new JPanel();
  330. main = new JPanel();
  331. creator = new JPanel();
  332. side = new JPanel();
  333. selectedSong = new JLabel();
  334. c = getContentPane();
  335. control.setLayout(new GridLayout(3,1,10,10));
  336. creator.setLayout(new GridLayout(1, 5, 20,10));
  337. main.setLayout(new FlowLayout(FlowLayout.CENTER, 20,100));
  338. side.setLayout(new FlowLayout(FlowLayout.CENTER, 20, 150));
  339. songTimer = new Timer(10, this);
  340. btn_open.setBackground(new Color(246,246,246));
  341. btn_open.setFont(new Font("Helvetica Nue", Font.PLAIN, 12));
  342. btn_open.setBorderPainted(false);
  343. btn_open.addActionListener(this);
  344. control.add(btn_open);
  345.  
  346. btn_save.setBackground(new Color(246,246,246));
  347. btn_save.setFont(new Font("Helvetica Nue", Font.PLAIN, 12));
  348. btn_save.setBorderPainted(false);
  349. btn_save.addActionListener(this);
  350. control.add(btn_save);
  351.  
  352. btn_start.setBackground(new Color(246,246,246));
  353. btn_start.setFont(new Font("Helvetica Nue", Font.PLAIN, 12));
  354. btn_start.setBorderPainted(false);
  355. btn_start.addActionListener(this);
  356. control.add(btn_start);
  357.  
  358. keys = new JButton[keyText.length];
  359. for (int i = 0; i < keyText.length; i++) {
  360. keys[i] = new JButton(keyText[i]);
  361. keys[i].setBackground(new Color(246,246,246));
  362. keys[i].setFont(new Font("Helvetica Nue", Font.PLAIN, 12));
  363. keys[i].setBorderPainted(false);
  364. main.add(keys[i]);
  365. }
  366.  
  367. if (currentSong == -1) {
  368. selectedSong.setText("Selected song: ");
  369. }else {
  370. selectedSong.setText("Selected song: " + MyUtil.currentSong(currentSong).getName());
  371. }
  372. main.add(selectedSong);
  373. side.add(control);
  374. creator.add(main);
  375.  
  376. main.addKeyListener(this);
  377. btn_save.addKeyListener(this);
  378. btn_open.addKeyListener(this);
  379. btn_start.addKeyListener(this);
  380. main.setFocusable(true);
  381.  
  382. c.add(side, BorderLayout.EAST);
  383. c.add(creator, BorderLayout.CENTER);
  384.  
  385. setTitle(s);
  386. setResizable(false);
  387. setSize(600,300);
  388. setVisible(true);
  389. //setDefaultCloseOperation (WindowConstants.EXIT_ON_CLOSE);
  390. }
  391. public MapCreator(String s) {
  392. this(s, -1);
  393. }
  394. public void actionPerformed (ActionEvent e) {
  395. if (isRecording) {
  396. currentTime = System.nanoTime()/10000000;
  397. if (currentTime - startTime > songTime) {
  398. isRecording = false;
  399. System.out.println("Song is over");
  400. songTimer.stop();
  401. }
  402. return;
  403. }
  404. JButton b = (JButton) e.getSource();
  405. if (b == btn_open) {
  406. selector = new SongSelect("Song Select");
  407. this.dispose();
  408.  
  409. } else if (b == btn_save) {
  410. try{
  411. int returnVal = fc.showSaveDialog(this); // need to import javax.swing.filechooser.*;
  412.  
  413. if (returnVal == JFileChooser.APPROVE_OPTION) { // if user did not click cancel and picked file name
  414. File file = fc.getSelectedFile(); // picked name
  415.  
  416. saveMethod(file);
  417. }
  418.  
  419. }catch(Exception ex){
  420. System.out.print("Error" + ex);
  421. }
  422. }
  423. else if (b == btn_start) {
  424. if (currentSong != -1) {
  425. System.out.println("Recording started");
  426. songTime = MyUtil.playSong(currentSong);
  427. System.out.println("Song time: " + songTime);
  428. map1 = new BeatMap(currentSong);
  429. startTime = System.nanoTime()/10000000;
  430. songTimer.start();
  431. isRecording = true;
  432. main.setFocusable(true);
  433. btn_start.setFocusable(false);
  434. }
  435. }
  436. }
  437. public void keyPressed(KeyEvent ev) {
  438. if (isRecording) {
  439. noteTime = currentTime - startTime;
  440. currentTime = System.nanoTime()/10000000;
  441. if (currentTime - startTime > songTime)
  442. isRecording = false;
  443. }
  444. if (ev.getKeyCode() == 68){
  445. noteKey = 1;
  446. keys[0].setBackground(Color.yellow);
  447. }
  448. if (ev.getKeyCode() == 70) {
  449. noteKey = 2;
  450. keys[1].setBackground(Color.yellow);
  451. }
  452. if (ev.getKeyCode() == 32) {
  453. noteKey = 3;
  454. keys[2].setBackground(Color.yellow);
  455. }
  456. if (ev.getKeyCode() == 74) {
  457. noteKey = 4;
  458. keys[3].setBackground(Color.yellow);
  459. }
  460. if (ev.getKeyCode() == 75) {
  461. noteKey = 5;
  462. keys[4].setBackground(Color.yellow);
  463. }
  464. if (noteKey != -1 && isRecording) {
  465. map1.addNote(noteTime, noteKey);
  466. System.out.println("Added note");
  467. System.out.println("Time: " + noteTime);
  468. System.out.println("Note:" + noteKey);
  469. }
  470. }
  471. public void keyReleased(KeyEvent ev) {
  472. if (isRecording) {
  473. int n = ev.getKeyCode();
  474. switch(n) {
  475. case 68: // d
  476. noteKey = 1;
  477. keys[0].setBackground(new Color(246, 246, 246));
  478. break;
  479. case 70: // f
  480. noteKey = 2;
  481. keys[1].setBackground(new Color(246, 246, 246));
  482. break;
  483. case 32: // spacebar
  484. noteKey = 3;
  485. keys[2].setBackground(new Color(246, 246, 246));
  486. break;
  487. case 74: // j
  488. noteKey = 4;
  489. keys[3].setBackground(new Color(246, 246, 246));
  490. break;
  491. case 75: // k
  492. noteKey = 5;
  493. keys[4].setBackground(new Color(246, 246, 246));
  494. break;
  495. default:
  496. noteKey = -1;
  497. break;
  498. }
  499.  
  500. }else {
  501. System.out.println("Recording is over");
  502. }
  503. }
  504. public void keyTyped(KeyEvent ev) {
  505. }
  506. public void saveMethod(File filePath){
  507. FileOutputStream fout = null;
  508. ObjectOutputStream oos = null;
  509.  
  510. try {
  511. fout = new FileOutputStream(filePath);
  512. oos = new ObjectOutputStream(fout);
  513. oos.writeObject(map1);
  514. fout.close();
  515. oos.close();
  516. } catch (Exception ex) {
  517. ex.printStackTrace();
  518. }
  519. }
  520.  
  521. }
  522.  
  523.  
  524. class SongSelect extends JFrame implements ActionListener {
  525. private Container c;
  526. private JPanel songSelect, southPanel, base;
  527. private JButton[] btnArr;
  528. private JButton selectSong, clearSong;
  529. private int currentSong;
  530.  
  531. public SongSelect(String s) {
  532. songSelect = new JPanel();
  533. southPanel = new JPanel();
  534. base = new JPanel();
  535. selectSong = new JButton("Select Song");
  536. selectSong.setBackground(new Color(246,246,246));
  537. selectSong.setFont(new Font("Helvetica Nue", Font.PLAIN, 12));
  538. selectSong.setBorderPainted(false);
  539. selectSong.addActionListener(this);
  540. clearSong = new JButton("Clear Song");
  541. clearSong.setBackground(new Color(246,246,246));
  542. clearSong.setFont(new Font("Helvetica Nue", Font.PLAIN, 12));
  543. clearSong.setBorderPainted(false);
  544. clearSong.addActionListener(this);
  545. c = getContentPane();
  546. southPanel.setLayout(new FlowLayout(FlowLayout.CENTER));
  547. southPanel.add(selectSong);
  548. southPanel.add(clearSong);
  549. songSelect.setLayout(new FlowLayout(FlowLayout.CENTER, 20, 150));
  550. btnArr = new JButton[MyUtil.songArr.size()];
  551. for (int i = 0; i < btnArr.length; i++) {
  552. btnArr[i] = new JButton(MyUtil.songArr.get(i).getName());
  553. songSelect.add(btnArr[i]);
  554. btnArr[i].setBackground(new Color(246,246,246));
  555. btnArr[i].setFont(new Font("Helvetica Nue", Font.PLAIN, 12));
  556. btnArr[i].setBorderPainted(false);
  557. btnArr[i].addActionListener(this);
  558. }
  559. c.add(songSelect, BorderLayout.CENTER);
  560. c.add(southPanel, BorderLayout.SOUTH);
  561. setTitle(s);
  562. setResizable(false);
  563. setSize(600,300);
  564. setVisible(true);
  565. //setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
  566.  
  567. }
  568.  
  569. public void actionPerformed(ActionEvent e) {
  570. //System.out.println("Action performed");
  571. JButton b = (JButton)e.getSource();
  572. if (b == clearSong) {
  573. try {
  574. MyUtil.clearSong();
  575. }catch(Exception ex) {}
  576. }else if(b == selectSong) {
  577. try {
  578. MyUtil.clearSong();
  579. }catch(Exception ex) {}
  580. MapCreator creator = new MapCreator("Map Creator", currentSong);
  581. this.dispose();
  582.  
  583. }
  584. for (int i = 0; i < btnArr.length; i++) {
  585. if (btnArr[i] == e.getSource()) {
  586. try {
  587. MyUtil.clearSong();
  588. }catch(Exception ex) {}
  589. MyUtil.playSong(i);
  590. currentSong = i;
  591. }
  592. }
  593. }
  594. }
  595.  
  596.  
  597. class MyUtil {
  598. static ArrayList<Song> songArr = new ArrayList<Song>();
  599. static File songFolder = new File("Songs");
  600. static AudioInputStream stream;
  601. static AudioFormat format;
  602. static DataLine.Info info;
  603. static Clip clip;
  604.  
  605. public static void importSongs() {
  606. File[] files = songFolder.listFiles();
  607. for (int i = 0; i < files.length; i++) {
  608. songArr.add(new Song(files[i], files[i].getName(), i));
  609. System.out.println("Adding new song: " + files[i].getName());
  610. }
  611. }
  612. public static long playSong(int i) {
  613. try {
  614. stream = AudioSystem.getAudioInputStream(songArr.get(i).getSongFile());
  615. format = stream.getFormat();
  616. info = new DataLine.Info(Clip.class, format);
  617. clip = (Clip) AudioSystem.getLine(info);
  618. clip.open(stream);
  619. clip.start();
  620. }catch(Exception e) {
  621. e.printStackTrace();
  622. }
  623. return clip.getMicrosecondLength()/10000;
  624. }
  625. public static void clearSong() {
  626. clip.close();
  627. }
  628. public static Song currentSong(int i) {
  629. return songArr.get(i);
  630. }
  631. }
  632. class Song implements Serializable{
  633. private File songFile;
  634. private String name;
  635. private int songNum;
  636.  
  637. public Song(File file, String name, int songNum) {
  638. songFile = file;
  639. this.name = name;
  640. this.songNum = songNum;
  641. }
  642. public Song(String name) {
  643. this(new File(""), name, 0);
  644. }
  645.  
  646. public String getName() {
  647. return name;
  648. }
  649. public File getSongFile() {
  650. return songFile;
  651. }
  652. public int getSongNum() {
  653. return songNum;
  654. }
  655. }
  656.  
  657. class BeatMap implements Serializable {
  658. private ArrayList<Note> notes;
  659. private Song song;
  660.  
  661. public BeatMap(){}
  662. public BeatMap(int i) {
  663. notes = new ArrayList<Note>();
  664. song = MyUtil.currentSong(i);
  665. addNote(0, 3);
  666. }
  667. public void addNote(long noteTime, int noteKey) {
  668. notes.add(new Note(noteTime, noteKey));
  669. }
  670. public ArrayList<Note> getAllNotes() {
  671. return notes;
  672. }
  673. public int getNumNotes() {
  674. return notes.size();
  675. }
  676. public Song getSong(){
  677. return song;
  678. }
  679. public Note getNextNote() {
  680. for (int i = 0; i < notes.size(); i++) {
  681. if (notes.get(i).hasPlayed == false) {
  682. System.out.print(notes.get(i).noteKey);
  683. notes.get(i).canPlay = true;
  684. return (notes.get(i));
  685. }
  686. }
  687. return null; //itll never reach this right
  688. }
  689. }
  690. class Note implements Serializable {
  691. private long noteTime;
  692. public int noteKey;
  693. public int moveY;
  694. private boolean onScreen;
  695. public boolean hasPlayed, canPlay;
  696. public Note(long noteTime, int noteKey) {
  697. this.noteTime = noteTime;
  698. this.noteKey = noteKey;
  699. moveY = (int)(noteTime*-1); //the spacing messes up the accuracy of the bars
  700. onScreen = true;
  701. hasPlayed = false;
  702. canPlay = false;
  703.  
  704. }
  705. public int calcScore(int range, long pNoteTime) {
  706.  
  707. if (range <= 1 || Math.abs(pNoteTime - noteTime) > range) {
  708. return 1;
  709. }
  710. else {
  711. return range + calcScore(range/2, pNoteTime);
  712. }
  713. }
  714. //compares the player's key presses to the created map for accuracy and returns score
  715. public int compareNote(long pNoteTime, int pNoteKey) {
  716. if (pNoteKey == noteKey) {
  717. System.out.println("Player note time: " + pNoteTime);
  718. System.out.println("Note time: " + noteTime);
  719. if (canPlay == true && Math.abs(pNoteTime - noteTime) < 100) {
  720. System.out.println(canPlay);
  721. canPlay = false;
  722. hasPlayed = true;
  723. System.out.println("good!");
  724. return 1;
  725. } else {
  726. System.out.println("bad");
  727. return 0;
  728.  
  729. }
  730. } else {
  731. System.out.println("bad");
  732. return 0;
  733.  
  734. }
  735. }
  736. public void draw(Graphics g, long currentTime){
  737. if (moveY + currentTime > 450 && onScreen) {
  738. //System.out.println("NoteTime: " + noteTime);
  739. onScreen = false;
  740. hasPlayed = true;
  741. }
  742. if (noteKey == 1) {
  743. g.fillRect(18, (int) (moveY + currentTime), 35, 5); ///lol this is not the right spacing
  744. } else if (noteKey == 2) {
  745. g.fillRect(64, (int) (moveY + currentTime) , 35, 5);
  746. } else if(noteKey == 3) {
  747. g.fillRect(109, (int) (moveY + currentTime), 60, 5);
  748. } else if (noteKey == 4){
  749. g.fillRect(178, (int) (moveY + currentTime), 35, 5);
  750. } else if (noteKey == 5) {
  751. g.fillRect(222, (int) (moveY + currentTime), 35, 5);
  752. }
  753. }
  754. }
  755. class GamePanel extends JPanel implements ActionListener{
  756. BeatMap map1;
  757. Timer gameTimer;
  758. public long startTime, currentTime;
  759. private boolean songPlaying;
  760.  
  761. public GamePanel() {}
  762.  
  763. public void setMap(BeatMap m){
  764. map1 = m;
  765. gameTimer = new Timer(10,this); //every tick (tick = 0.01s)
  766. gameTimer.start();
  767. startTime = System.nanoTime()/10000000;
  768. songPlaying = false;
  769. //MyUtil.playSong(m.getSong().getSongNum());
  770. }
  771.  
  772. public void actionPerformed(ActionEvent e) {
  773. currentTime = (System.nanoTime()/10000000) - startTime;
  774. //System.out.println("Current Time: " + currentTime);
  775. if (e.getSource() == gameTimer) {
  776. if (currentTime > 430 && !songPlaying) {
  777. MyUtil.playSong(map1.getSong().getSongNum());
  778. songPlaying = true;
  779. }
  780. for (int i = 0; i < map1.getNumNotes(); i++) {
  781. //map1.getAllNotes().get(i).moveY += 1; //lol also not the right speed
  782. //map1.getAllNotes().get(i).updatePos(currentTime);
  783. }
  784. repaint();
  785. }
  786. }
  787. public void paintComponent(Graphics g){
  788. super.paintComponent(g);
  789. for (int i = 0; i < map1.getAllNotes().size(); i++) {
  790. map1.getAllNotes().get(i).draw(g, currentTime);
  791. }
  792. repaint();
  793.  
  794. }
  795. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement