Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * To change this template, choose Tools | Templates
- * and open the template in the editor.
- */
- package javaapplication38;
- import java.awt.Color;
- import java.awt.Graphics;
- import java.awt.Rectangle;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- import java.awt.event.MouseEvent;
- import java.awt.event.MouseListener;
- import java.awt.event.MouseMotionListener;
- import java.awt.image.BufferedImage;
- import java.io.File;
- import java.io.IOException;
- import java.util.Random;
- import java.util.logging.Level;
- import java.util.logging.Logger;
- import javax.imageio.ImageIO;
- import javax.swing.*;
- /**
- *
- * @author didiamantis
- */
- public class Game extends JFrame implements ActionListener, MouseMotionListener, MouseListener {
- private MyPanel panel;
- private Timer timer;
- public Game() {
- this.setBounds(0, 0, 800, 600);
- this.setTitle("Hello unicorn");
- this.setDefaultCloseOperation(EXIT_ON_CLOSE);
- this.panel = new MyPanel(this);
- this.add(this.panel);
- this.setVisible(true);
- this.timer = new Timer(15000, this);
- this.timer.start();
- this.addMouseMotionListener(this);
- this.addMouseListener(this);
- }
- @Override
- public void actionPerformed(ActionEvent e) {
- // this.panel.setUnicornX(this.panel.getUnicornX()+1);
- this.repaint();
- }
- @Override
- public void mouseDragged(MouseEvent e) {
- }
- @Override
- public void mouseMoved(MouseEvent e) {
- }
- @Override
- public void mouseClicked(MouseEvent e) {
- this.panel.doesCollide(e.getX(), e.getY());
- }
- @Override
- public void mousePressed(MouseEvent e) {
- }
- @Override
- public void mouseReleased(MouseEvent e) {
- }
- @Override
- public void mouseEntered(MouseEvent e) {
- }
- @Override
- public void mouseExited(MouseEvent e) {
- }
- private class MyPanel extends JPanel {
- private Game gameInstance;
- private BufferedImage image;
- private int unicornX, unicornY;
- private Random random;
- public int getUnicornX() {
- return unicornX;
- }
- public void setUnicornX(int unicornX) {
- this.unicornX = unicornX;
- }
- public int getUnicornY() {
- return unicornY;
- }
- public void setUnicornY(int unicornY) {
- this.unicornY = unicornY;
- }
- public MyPanel(Game game) {
- this.gameInstance = game;
- this.random = new Random();
- try {
- this.image = (BufferedImage) ImageIO.read(new File("C:\\Users\\didiamantis\\Downloads\\jerry.jpg"));
- } catch (IOException ex) {
- }
- }
- int score = 0;
- @Override
- public void paint(Graphics g) {
- g.setColor(Color.WHITE);
- this.setUnicornX( this.random.nextInt(this.gameInstance.getWidth() - this.image.getWidth()));
- this.setUnicornY( this.random.nextInt(this.gameInstance.getHeight() - this.image.getHeight()));
- g.fillRect(0, 0, this.gameInstance.getWidth(), this.gameInstance.getHeight());
- g.drawImage(
- image,
- this.getUnicornX(),
- this.getUnicornY(),
- this);
- g.setColor(Color.BLACK);
- g.drawString("Score: " + this.score, 50, 50);
- }
- private void doesCollide(int x, int y) {
- Rectangle r = new Rectangle(x, y, 550, 550);
- Rectangle imageR = new Rectangle(this.getUnicornX(), this.getUnicornY(), this.image.getWidth(), this.image.getHeight());
- if (r.intersects(imageR)) {
- this.score += 10;
- this.gameInstance.timer.setDelay(this.gameInstance.timer.getDelay() - 250);
- } else {
- this.score -= 1;
- }
- this.gameInstance.repaint();
- }
- }
- public static void main(String[] args) {
- Game g = new Game();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement