ball.java

来自「a Pong applet by zimardi」· Java 代码 · 共 43 行

JAVA
43
字号
import java.awt.*;

public class Ball
{
	int ballSize;
	Color ballColor;
	double posX;
	double posY;
	double dirX;
	double dirY;
	
	public Ball(int posX, int posY)
	{
		this.posX = posX;
		this.posY = posY;
		
		ballSize = (int)(Math.random()*10)+10;
		ballColor = new Color((int)(Math.random()*255+1),(int)(Math.random()*255+1),(int)(Math.random()*255+1),(int)(Math.random()*255+1));
		dirX = (Math.random()*4)+1;
		dirY = (Math.random()*4)+1;
		if(Math.random() < .5)
			dirX = dirX*-1;
		if(Math.random() < .5)
			dirY = dirY*-1;
		
	
	}
	
	public void updatePosition(int h){
		//these ifs make sure the ball is not off the side of the window
		//if(posX <= 0)
			//dirX = dirX*-1;
		if(posY <=0 || posY > h-ballSize)
			dirY = dirY * -1;
			
		//these reposition the ball
		posX = posX + dirX;
		posY = posY + dirY;
	}
		
	
}
	

⌨️ 快捷键说明

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