Advertisement
hunkyhari

SO23571375-Ball.java

May 9th, 2014
194
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.98 KB | None | 0 0
  1. package com.shreesoft.so;
  2.  
  3. import java.awt.Color;
  4. import java.awt.Graphics;
  5. import java.awt.Rectangle;
  6.  
  7. public class Ball {
  8.  
  9.     int x = 0;
  10.     int xs = -1;
  11.     int ys = -1;
  12.     int y = 0;
  13.  
  14.     static final int DIAMETER = 30;
  15.     JalPan jpn;
  16.  
  17.     Ball(JalPan jpn) {
  18.         this.jpn = jpn;
  19.     }
  20.  
  21.     public void move() {
  22.  
  23.         if (x + xs < 0) {
  24.             xs = 1;
  25.         } else if (y + ys < 0) {
  26.             ys = 1;
  27.         } else if (x + xs > jpn.getWidth() - 30) {
  28.             xs = -1;
  29.         } else if (y + ys > jpn.getHeight() - 30) {
  30.             ys = -1;
  31.         }
  32.         x = x + xs;
  33.         y = y + ys;
  34.         if (collision()) {
  35.             ys = -1;
  36.             y = jpn.plate.getTop() - DIAMETER;
  37.         }
  38.         if (y + ys > 490) {
  39.             ((JalPan) jpn).gameOver();
  40.         }
  41.  
  42.     }
  43.  
  44.     public void paint(Graphics g) {
  45.         g.setColor(Color.RED);
  46.         g.fillRoundRect(x, y, DIAMETER, DIAMETER, DIAMETER, DIAMETER);
  47.     }
  48.  
  49.     public Rectangle getBounds() {
  50.         return new Rectangle(x, y, DIAMETER, DIAMETER);
  51.     }
  52.  
  53.     public boolean collision() {
  54.         return jpn.plate.getBounds().intersects(getBounds());
  55.     }
  56. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement