📄 ball.java
字号:
package pinball;import javax.microedition.lcdui.Graphics;/** * * <p>Title: 弹球</p> * * <p>Description: 该类实现了弹球的显示和属性的维护,包括弹球的半径,坐标。 * 此外,该类还实现了弹球的自绘功能。</p> * */public class Ball extends Sprite { private int dx; //x方向的坐标增量 private int dy; //y方向的坐标增量 private int Ox; //原点x坐标 private int Oy; //原点y坐标 public static final int RADIUS = Math.max(2, Screen.width / 55); //直径 private ThreeDColor color; private ThreeDColor brighter; private ThreeDColor darker; private int counter; private int offset; public Ball(int x, int y, int dx, int dy) { moveTo(x, y); setSteps(dx, dy); offset = 0; width = 2 * RADIUS; height = 2 * RADIUS; this.dx = dx; this.dy = dy; Ox = Oy = 0; color = ThreeDColor.red; brighter = color.brighter(); darker = color.darker(); } public void setSteps(int dx, int dy) { this.dx = dx; this.dy = dy; } public int getXStep() { return dx; } public int getYStep() { return dy; } public void move() { x = x + dx + Ox; y = y + dy + Oy; Ox = Oy = 0; } /** * 水平反弹 */ public void bounceHorizontal() { dx = -dx; if (RandomNum.getRandomInt(0, 1000) < 70) { Ox = dx < 0 ? -1 : 1; } } /** * 竖直反弹 */ public void bounceVertical() { dy = -dy; if (RandomNum.getRandomInt(0, 1000) < 70) { Oy = dy < 0 ? -1 : 1; } } /** * 一般反弹 */ public void bounce(Sprite other) { int cx, cy; cx = getCenterX(); cy = getCenterY(); if (dx < 0) { //原来向左移动 if (cy >= other.y && cy < other.y + other.height && x < other.x + other.width) { dx = Math.abs(dx); //反弹后向右 } } else { //原来向右移动 if (cy >= other.y && cy < other.y + other.height && x + width >= other.x) { dx = -Math.abs(dx); //反弹后向左 } } if (dy < 0) { //原来向上移动 if (cx >= other.x && cx < other.x + other.width && y < other.y + other.height) { dy = Math.abs(dy); //反弹后向下移动 } } else { //原来向下移动 if (cx >= other.x && cx < other.x + other.width && y + height >= other.y) { dy = -Math.abs(dy); //反弹后向上移动 } } //以一定的概率偏离原来的方向,防止形成"乒乓效应" if (RandomNum.getRandomInt(0, 1000) < 70) { Ox = dx < 0 ? -1 : 1; } if (RandomNum.getRandomInt(0, 1000) < 70) { Oy = dy < 0 ? -1 : 1; } } /** * 绘制阴影 */ public void paintShadow(Graphics g) { g.setColor(ThreeDColor.black.getRGB()); g.fillArc(x + shadow, y + shadow, width, height, 0, 360); } /** * 自绘函数 */ public void paint(Graphics g) { if (++counter == 6) { counter = 0; //循环计数 offset = 1 - offset; //决定阴影的偏移方向 } g.setColor(color.getRGB()); g.fillArc(x, y, width, height, 0, 360); g.setColor(brighter.getRGB()); g.drawArc(x + 2, y + 2, width - 4, height - 4, 180 + offset, -90); g.setColor(darker.getRGB()); g.drawArc(x, y, width, height, 0, -90); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -