Advertisement
Guest User

Untitled

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