SStoyanov22

Untitled

Sep 14th, 2014
298
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.87 KB | None | 0 0
  1. package com.edu4java.minitennis6;
  2.  
  3. import java.awt.Graphics2D;
  4. import java.awt.Rectangle;
  5.  
  6. public class Ball {
  7. private static final int DIAMETER = 30;
  8. int x = 365;
  9. int y = 350;
  10. int xa = -1;
  11. int ya = 1;
  12. private Game game;
  13.  
  14. public Ball(Game game) {
  15. this.game= game;
  16. }
  17.  
  18. void move() {
  19. if (x + xa < 0)
  20. game.gameOver();
  21. if (x + xa > game.getWidth() - DIAMETER)
  22. game.gameOver();
  23. if (y + ya < 0)
  24. ya = 1;
  25. if (y + ya > game.getHeight() - DIAMETER)
  26. ya = -1;
  27. if (collision()){
  28. xa = 1;
  29. x = game.racquet.getTopX() - DIAMETER;
  30. }
  31. x = x + xa;
  32. y = y + ya;
  33. }
  34.  
  35. private boolean collision() {
  36. return game.racquet.getBounds().intersects(getBounds());
  37. }
  38.  
  39. public void paint(Graphics2D g) {
  40. g.fillOval(x, y, DIAMETER, DIAMETER);
  41. }
  42.  
  43. public Rectangle getBounds() {
  44. return new Rectangle(x, y, DIAMETER, DIAMETER);
  45. }
  46. }
Advertisement
Add Comment
Please, Sign In to add comment