ball.java

来自「不但实现了所有的游戏功能」· Java 代码 · 共 67 行

JAVA
67
字号

import java.awt.Color;

import acm.graphics.GOval;
import acm.graphics.GPoint;
import acm.util.*;

public class Ball extends GOval{
	/** Radius of the ball in pixels */
	private static final int BALL_RADIUS = 10;
	private double vx, vy;
	
	public double getVx() {
		return vx;
	}

	public void setVx(double vx) {
		this.vx = vx;
	}

	public double getVy() {
		return vy;
	}

	public void setVy(double vy) {
		this.vy = vy;
	}

	public Ball() {
		super(BALL_RADIUS, BALL_RADIUS);
		this.setFilled(true);
		this.setFillColor(Color.RED);
		setOV();
		
	}
	
	/**
	 * set original velocity of the ball
	 *
	 */
	public void setOV(){
		vy = -1.0;
		setRandomVx();
		
	}
	
	public void setRandomVx(){
		vx = rgen.nextDouble(0.3, 0.5);
		if(rgen.nextBoolean(0.5)) vx = -vx;
	}
	
	public GPoint getRightUpPoint(){
		return new GPoint((this.getX() + this.getWidth()), this.getY());
	}
	
	public GPoint getLeftLowerPoint(){
		return new GPoint(this.getX(), (this.getY() + this.getHeight()));
	}
	
	public GPoint getRightLowerPoint(){
		return new GPoint( (this.getX() + this.getWidth()), (this.getY() + this.getHeight()) );
	}

	private RandomGenerator rgen = RandomGenerator.getInstance();

}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?