Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package il.co.tomer.game;
- import java.awt.Color;
- import java.awt.Graphics;
- import java.awt.Graphics2D;
- import java.awt.Point;
- import java.awt.RenderingHints;
- import java.awt.event.KeyEvent;
- import java.awt.event.KeyListener;
- import java.awt.event.MouseEvent;
- import java.awt.event.MouseListener;
- import java.awt.event.MouseMotionListener;
- import java.util.Timer;
- import java.util.TimerTask;
- import javax.swing.JFrame;
- import javax.swing.JPanel;
- public class Game extends JPanel implements MouseMotionListener{
- private static final long serialVersionUID = 1L;
- public static final int FPS = 60;
- public static final int WINDOW_LEFT_BORDER_SIZE = 8;
- public static final int WINDOW_TOP_BORDER_SIZE = 20;
- private Point target;
- private float ballHorizontalLocation = 50f;
- private float ballVerticalLocation = 50f;
- private int ballWidth = 150;
- private int ballHeight = 150;
- public static void main(String[] args) // first function to be called
- {
- new Game();
- }
- private Game() // constructor for the Game object (also our JPanel and MouseMotionListener)
- {
- initialize(); //initialize our game - set the window settings and add listeners
- startGame(); // start our game loops
- }
- private void initialize()
- {
- JFrame jf = new JFrame("Tomer and Gal's Game");
- jf.setVisible(true);
- jf.setSize(800, 600);
- jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- jf.setLocationRelativeTo(null);
- jf.add(this);
- jf.addMouseMotionListener(this);
- }
- private void startGame()
- {
- new Timer().scheduleAtFixedRate(new TimerTask(){
- @Override
- public void run() {
- updateGame(); //update game 60 times a second
- drawGame(); //draw game 60 times a second
- }}, 0, 1000 / FPS); // 60 FPS
- }
- private void drawGame()
- {
- repaint();
- }
- private void updateGame()
- {
- if (target != null)
- {
- if (this.ballHorizontalLocation < target.getX()) // if the ball is left to the mouse
- {
- this.ballHorizontalLocation += 1; // minimize the gap
- }
- if (this.ballHorizontalLocation > target.getX()) // if the ball is right to the mouse
- {
- this.ballHorizontalLocation -= 1; // minimize the gap
- }
- if (this.ballVerticalLocation < target.getY()) // if the ball is above the mouse
- {
- this.ballVerticalLocation += 1; // minimize the gap
- }
- if (this.ballVerticalLocation > target.getY()) // if the ball is below the mouse
- {
- this.ballVerticalLocation -= 1; // minimize the gap
- }
- }
- }
- public void paintComponent(Graphics g)
- {
- //set ANTIALIASING
- Graphics2D g2 = (Graphics2D)g;
- RenderingHints rh = new RenderingHints(
- RenderingHints.KEY_ANTIALIASING,
- RenderingHints.VALUE_ANTIALIAS_ON);
- g2.setRenderingHints(rh);
- //draw pink background
- g2.setColor(Color.PINK);
- g2.fillRect(0, 0, 800, 600);
- //draw our ball
- g2.setColor(Color.WHITE);
- g2.fillOval((int)ballHorizontalLocation, (int)ballVerticalLocation, ballWidth, ballHeight);
- }
- @Override
- public void mouseDragged(MouseEvent arg0) {
- }
- @Override
- public void mouseMoved(MouseEvent e) {
- //if we do not have a target yet - create one
- if (this.target == null)
- {
- target = new Point();
- }
- //set new values for our ball's target
- target.x = e.getX() - ballWidth / 2 - WINDOW_LEFT_BORDER_SIZE;
- target.y = e.getY() - ballHeight / 2 - WINDOW_TOP_BORDER_SIZE;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement