Advertisement
Guest User

Untitled

a guest
Jun 16th, 2019
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.86 KB | None | 0 0
  1. import java.awt.BorderLayout;
  2. import java.awt.Color;
  3. import java.awt.EventQueue;
  4. import java.awt.Graphics;
  5. import java.awt.Graphics2D;
  6. import java.awt.Image;
  7. import java.awt.Point;
  8. import java.awt.event.KeyEvent;
  9. import java.awt.event.KeyListener;
  10. import java.io.File;
  11. import java.io.IOException;
  12. import java.util.LinkedList;
  13.  
  14. import javax.imageio.ImageIO;
  15. import javax.sound.sampled.AudioInputStream;
  16. import javax.sound.sampled.AudioSystem;
  17. import javax.sound.sampled.Clip;
  18. import javax.sound.sampled.LineUnavailableException;
  19. import javax.sound.sampled.UnsupportedAudioFileException;
  20. import javax.swing.JFrame;
  21. import javax.swing.JPanel;
  22. import javax.swing.border.EmptyBorder;
  23.  
  24. public class Snake extends JFrame {
  25. long FPS_LIMITER = 240;
  26.  
  27. ///////////////////////////////////
  28. // CODE GOES HERE \/
  29. /////////////////////////////////
  30.  
  31. LinkedList<Point> snake = new LinkedList<>();
  32.  
  33. void start() {
  34. snake.add(new Point(15, 15));
  35. snake.add(new Point(15, 30));
  36. snake.add(new Point(30, 30));
  37. }
  38.  
  39. void update() {
  40. }
  41.  
  42. void draw(Graphics2D g) {
  43. // for each point p in snake:
  44. for (Point p : snake) {
  45. // draw a square at point p
  46. g.fillRect(p.x, p.y, 15, 15);
  47. }
  48. }
  49.  
  50. /////////////////////////////////
  51. /////////////////////////////////
  52. /////////////////////////////////
  53.  
  54. private boolean[] isKeyDown = new boolean[65536];
  55. private MyPanel contentPane;
  56.  
  57. /**
  58. * Launch the application.
  59. */
  60. public static void main(String[] args) {
  61. Snake frame = new Snake();
  62. EventQueue.invokeLater(new Runnable() {
  63. @Override
  64. public void run() {
  65. try {
  66. frame.setVisible(true);
  67. new Thread(frame::gameLoop).start();
  68. } catch (Exception e) {
  69. e.printStackTrace();
  70. }
  71. }
  72. });
  73. }
  74.  
  75. int screenWidth() {
  76. return contentPane.getWidth();
  77. }
  78.  
  79. int screenHeight() {
  80. return contentPane.getHeight();
  81. }
  82.  
  83. long lt = 0, l2;
  84.  
  85. void gameLoop() {
  86. l2 = System.nanoTime();
  87. long nsPerFrame = 1000000000l / FPS_LIMITER;
  88. while (true) {
  89. long ct = System.nanoTime();
  90. // update((ct - l2) / 1000000000.0);
  91. l2 = ct;
  92. if (ct - lt >= nsPerFrame) {
  93. lt = ct;
  94. update();
  95. contentPane.repaint();
  96. }
  97. }
  98. }
  99.  
  100. /**
  101. * Create the frame.
  102. */
  103. public Snake() {
  104. start();
  105.  
  106. setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  107. setBounds(100, 100, 1000, 500);
  108. contentPane = new MyPanel();
  109. contentPane.setBorder(new EmptyBorder(0, 0, 0, 0));
  110. contentPane.setLayout(new BorderLayout(0, 0));
  111. contentPane.setDoubleBuffered(true);
  112. addKeyListener(new KeyListener() {
  113.  
  114. @Override
  115. public void keyTyped(KeyEvent e) {
  116.  
  117. }
  118.  
  119. @Override
  120. public void keyReleased(KeyEvent e) {
  121. isKeyDown[e.getKeyCode()] = false;
  122. }
  123.  
  124. @Override
  125. public void keyPressed(KeyEvent e) {
  126. isKeyDown[e.getKeyCode()] = true;
  127. }
  128. });
  129. setContentPane(contentPane);
  130. }
  131.  
  132. private class MyPanel extends JPanel {
  133. @Override
  134. public void paintComponent(Graphics g) {
  135. g.setColor(Color.black);
  136. g.fillRect(0, 0, getWidth(), getHeight());
  137. g.setColor(Color.white);
  138. draw((Graphics2D) g);
  139. }
  140. }
  141.  
  142. private static Image loadImage(String name) {
  143. try {
  144. return ImageIO.read(new File(name));
  145. } catch (IOException e) {
  146. e.printStackTrace();
  147. System.err.println("Error loading image. Check the following things: ");
  148. System.err.println(" - Your file name is wrong");
  149. System.err.println(" - You placed the file in the wrong place");
  150. System.exit(-1);
  151. return null;
  152. }
  153. }
  154.  
  155. private class Sound {
  156. private Clip clip;
  157.  
  158. private void load(String name) {
  159. // from a wave File
  160. File soundFile = new File(name);
  161. AudioInputStream audioIn;
  162. try {
  163. audioIn = AudioSystem.getAudioInputStream(soundFile);
  164. } catch (UnsupportedAudioFileException e) {
  165. e.printStackTrace();
  166. System.err.println("Error loading sound. Your audio file is in the wrong format:");
  167. System.err.println(" - It must be in .wav format");
  168. System.err.println(" - Please use https://convertio.co/mp3-wav/ to convert it");
  169. System.exit(-1);
  170. return;
  171. } catch (IOException e) {
  172. e.printStackTrace();
  173. System.err.println("Error loading sound. Check the following things: ");
  174. System.err.println(" - Your file name is wrong");
  175. System.err.println(" - You placed the file in the wrong place");
  176. System.exit(-1);
  177. return;
  178. }
  179. try {
  180. clip = AudioSystem.getClip();
  181. clip.open(audioIn);
  182. } catch (LineUnavailableException | IOException e) {
  183. e.printStackTrace();
  184. System.err.println("Fatal error loading sound. Rip");
  185. System.exit(-1);
  186. return;
  187. }
  188. }
  189.  
  190. public Sound(String name) {
  191. load(name);
  192. }
  193.  
  194. public void play() {
  195. stop();
  196. clip.setFramePosition(0);
  197. clip.start();
  198. }
  199.  
  200. public void loop() {
  201. clip.loop(Clip.LOOP_CONTINUOUSLY);
  202. }
  203.  
  204. public void stop() {
  205. clip.stop();
  206. }
  207. }
  208.  
  209. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement