📄 ball.java
字号:
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 + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -