Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.awt.Color;
- import java.awt.Cursor;
- import java.awt.Font;
- import java.awt.FontFormatException;
- import java.awt.Graphics;
- import java.awt.Graphics2D;
- import java.awt.GraphicsEnvironment;
- import java.awt.Point;
- import java.awt.Toolkit;
- import java.awt.event.KeyEvent;
- import java.awt.event.KeyListener;
- import java.io.File;
- import java.io.FileInputStream;
- import java.io.IOException;
- import java.io.InputStream;
- import java.text.DecimalFormat;
- import javax.swing.ImageIcon;
- import javax.swing.JFrame;
- import javax.swing.JOptionPane;
- import javax.swing.JPanel;
- import javafx.scene.media.Media;
- import javafx.scene.media.MediaPlayer;
- import java.io.File;
- public class DuckHunt extends JPanel {
- private ImageIcon imgBackground, imgForeground, imgCursor;
- private Cursor cursor;
- private int score, hits;
- private double accuracy;
- private DecimalFormat df;
- private Font f;
- private static final int PANEL_WIDTH = 640;
- private static final int PANEL_HEIGHT = 480;
- private boolean start = false;
- public static void main(String[] args) {
- new DuckHunt();
- }
- public DuckHunt() {
- df = new DecimalFormat("#%");
- f = new Font("m29", Font.PLAIN, 18);
- imgBackground = new ImageIcon("images\\background.png");
- imgForeground = new ImageIcon("images\\foreground.png");
- imgCursor = new ImageIcon("images\\cursor.png");
- cursor = Toolkit.getDefaultToolkit().createCustomCursor(imgCursor.getImage(),
- new Point(imgCursor.getIconWidth() / 2, imgCursor.getIconHeight() / 2), "");
- String musicFile = "mainscreen.mp3";
- Media sound = new Media(new File(musicFile).toURI().toString());
- MediaPlayer mainscreen = new MediaPlayer(sound);
- mainscreen.play();
- setLayout(null);
- setCursor(cursor);
- setFocusable(true);
- requestFocus();
- this.addKeyListener(new KeyListener() {
- public void keyPressed(KeyEvent e) {
- if (e.getKeyCode() == KeyEvent.VK_SPACE) {
- start = true;
- }
- repaint();
- }
- public void keyReleased(KeyEvent e) {
- }
- public void keyTyped(KeyEvent e) {
- }
- });
- JFrame frame = new JFrame();
- frame.setContentPane(this);
- frame.setTitle("Duck Hunt © Nintendo 1985");
- frame.setSize(PANEL_WIDTH, PANEL_HEIGHT);
- frame.setResizable(false);
- frame.setLocationRelativeTo(null);
- frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- frame.setFocusable(false);
- frame.setVisible(true);
- }
- public void paintComponent(Graphics p) {
- super.paintComponent(p);
- Graphics2D p2 = (Graphics2D) p;
- Font customFont = null;
- try {
- // Create the font to use
- customFont = Font.createFont(Font.TRUETYPE_FONT, new File("m29.TTF")).deriveFont(18f);
- GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
- // Register the font
- ge.registerFont(customFont);
- } catch (IOException e) {
- e.printStackTrace();
- } catch (FontFormatException e) {
- e.printStackTrace();
- }
- p2.setFont(customFont);
- p2.drawImage(imgBackground.getImage(), 0, 0, this);
- p2.setColor(new Color(128, 208, 16));
- p2.drawImage(imgForeground.getImage(), 0, 0, this);
- p2.drawString("PRESS SPACE TO START "
- + " " + score, PANEL_WIDTH - 480, PANEL_HEIGHT - 50);
- if (start == true) {
- p2.drawImage(imgBackground.getImage(), 0, 0, this);
- p2.setFont(customFont);
- p2.setColor(new Color(128, 208, 16));
- p2.drawImage(imgForeground.getImage(), 0, 0, this);
- p2.drawString("SCORE: " + score, 20, PANEL_HEIGHT - 50);
- p2.drawString("HITS: " + hits, 250, PANEL_HEIGHT - 50);
- p2.drawString("ACCURACY: " + df.format(accuracy), 430, PANEL_HEIGHT - 50);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement