Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.edu4java.minitennis6;
- import java.awt.Graphics2D;
- import java.awt.Rectangle;
- public class Ball {
- private static final int DIAMETER = 30;
- int x = 365;
- int y = 350;
- int xa = -1;
- int ya = 1;
- private Game game;
- public Ball(Game game) {
- this.game= game;
- }
- void move() {
- if (x + xa < 0)
- game.gameOver();
- if (x + xa > game.getWidth() - DIAMETER)
- game.gameOver();
- if (y + ya < 0)
- ya = 1;
- if (y + ya > game.getHeight() - DIAMETER)
- ya = -1;
- if (collision()){
- xa = 1;
- x = game.racquet.getTopX() - DIAMETER;
- }
- x = x + xa;
- y = y + ya;
- }
- private boolean collision() {
- return game.racquet.getBounds().intersects(getBounds());
- }
- public void paint(Graphics2D g) {
- g.fillOval(x, y, DIAMETER, DIAMETER);
- }
- public Rectangle getBounds() {
- return new Rectangle(x, y, DIAMETER, DIAMETER);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment