Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.awt.BorderLayout;
- import java.awt.Color;
- import java.awt.EventQueue;
- import java.awt.Graphics;
- import java.awt.Graphics2D;
- import java.awt.Image;
- import java.awt.Point;
- import java.awt.event.KeyEvent;
- import java.awt.event.KeyListener;
- import java.io.File;
- import java.io.IOException;
- import java.util.LinkedList;
- import javax.imageio.ImageIO;
- import javax.sound.sampled.AudioInputStream;
- import javax.sound.sampled.AudioSystem;
- import javax.sound.sampled.Clip;
- import javax.sound.sampled.LineUnavailableException;
- import javax.sound.sampled.UnsupportedAudioFileException;
- import javax.swing.JFrame;
- import javax.swing.JPanel;
- import javax.swing.border.EmptyBorder;
- public class Snake extends JFrame {
- long FPS_LIMITER = 240;
- ///////////////////////////////////
- // CODE GOES HERE \/
- /////////////////////////////////
- LinkedList<Point> snake = new LinkedList<>();
- void start() {
- snake.add(new Point(15, 15));
- snake.add(new Point(15, 30));
- snake.add(new Point(30, 30));
- }
- void update() {
- }
- void draw(Graphics2D g) {
- // for each point p in snake:
- for (Point p : snake) {
- // draw a square at point p
- g.fillRect(p.x, p.y, 15, 15);
- }
- }
- /////////////////////////////////
- /////////////////////////////////
- /////////////////////////////////
- private boolean[] isKeyDown = new boolean[65536];
- private MyPanel contentPane;
- /**
- * Launch the application.
- */
- public static void main(String[] args) {
- Snake frame = new Snake();
- EventQueue.invokeLater(new Runnable() {
- @Override
- public void run() {
- try {
- frame.setVisible(true);
- new Thread(frame::gameLoop).start();
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- });
- }
- int screenWidth() {
- return contentPane.getWidth();
- }
- int screenHeight() {
- return contentPane.getHeight();
- }
- long lt = 0, l2;
- void gameLoop() {
- l2 = System.nanoTime();
- long nsPerFrame = 1000000000l / FPS_LIMITER;
- while (true) {
- long ct = System.nanoTime();
- // update((ct - l2) / 1000000000.0);
- l2 = ct;
- if (ct - lt >= nsPerFrame) {
- lt = ct;
- update();
- contentPane.repaint();
- }
- }
- }
- /**
- * Create the frame.
- */
- public Snake() {
- start();
- setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- setBounds(100, 100, 1000, 500);
- contentPane = new MyPanel();
- contentPane.setBorder(new EmptyBorder(0, 0, 0, 0));
- contentPane.setLayout(new BorderLayout(0, 0));
- contentPane.setDoubleBuffered(true);
- addKeyListener(new KeyListener() {
- @Override
- public void keyTyped(KeyEvent e) {
- }
- @Override
- public void keyReleased(KeyEvent e) {
- isKeyDown[e.getKeyCode()] = false;
- }
- @Override
- public void keyPressed(KeyEvent e) {
- isKeyDown[e.getKeyCode()] = true;
- }
- });
- setContentPane(contentPane);
- }
- private class MyPanel extends JPanel {
- @Override
- public void paintComponent(Graphics g) {
- g.setColor(Color.black);
- g.fillRect(0, 0, getWidth(), getHeight());
- g.setColor(Color.white);
- draw((Graphics2D) g);
- }
- }
- private static Image loadImage(String name) {
- try {
- return ImageIO.read(new File(name));
- } catch (IOException e) {
- e.printStackTrace();
- System.err.println("Error loading image. Check the following things: ");
- System.err.println(" - Your file name is wrong");
- System.err.println(" - You placed the file in the wrong place");
- System.exit(-1);
- return null;
- }
- }
- private class Sound {
- private Clip clip;
- private void load(String name) {
- // from a wave File
- File soundFile = new File(name);
- AudioInputStream audioIn;
- try {
- audioIn = AudioSystem.getAudioInputStream(soundFile);
- } catch (UnsupportedAudioFileException e) {
- e.printStackTrace();
- System.err.println("Error loading sound. Your audio file is in the wrong format:");
- System.err.println(" - It must be in .wav format");
- System.err.println(" - Please use https://convertio.co/mp3-wav/ to convert it");
- System.exit(-1);
- return;
- } catch (IOException e) {
- e.printStackTrace();
- System.err.println("Error loading sound. Check the following things: ");
- System.err.println(" - Your file name is wrong");
- System.err.println(" - You placed the file in the wrong place");
- System.exit(-1);
- return;
- }
- try {
- clip = AudioSystem.getClip();
- clip.open(audioIn);
- } catch (LineUnavailableException | IOException e) {
- e.printStackTrace();
- System.err.println("Fatal error loading sound. Rip");
- System.exit(-1);
- return;
- }
- }
- public Sound(String name) {
- load(name);
- }
- public void play() {
- stop();
- clip.setFramePosition(0);
- clip.start();
- }
- public void loop() {
- clip.loop(Clip.LOOP_CONTINUOUSLY);
- }
- public void stop() {
- clip.stop();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement