Advertisement
Guest User

Untitled

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