⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ball.java

📁 一款小游戏 希望能帮助初学者 一些基本的知识 是作业作品
💻 JAVA
字号:
/**
  * Add a comment
*/
import java.awt.*;

public class Ball {
		//You may find some of these constants helpful
	public static final int MAX_CHANGE_AMOUNT = 7;
	public static final int MIN_CHANGE_AMOUNT = 2;

	public static final int REBOUND_EAST_WALL  = 0;
	public static final int REBOUND_WEST_WALL = 1;
	public static final int REBOUND_NORTH_WALL  = 2;
	public static final int REBOUND_SOUTH_WALL = 3;
	public static final int REBOUND_PADDLE = 4;

	public static final int BALL_SIZE = BreakOutConstants.BALL_SIZE;

	public Rectangle ball;
	private int[] xValues = {-7 , -6, -5, -4, -3, -2, 2, 3, 4, 5, 6, 7};
	private int[] yValues = { -2, -3, -4, -5, -6, -7};
	private int changeInX, changeInY;

	public Ball(){
		ball = new Rectangle(BreakOutConstants.BALL_START_RECT.x , BreakOutConstants.BALL_START_RECT.y , BALL_SIZE , BALL_SIZE);
	}

	private int getRandomXValue(){
		int XValue = (int)(Math.random()*12-1);
		changeInX = xValues[XValue];
		return XValue;
	}

	private int getRandomYValue(){
		int YValue = (int)(Math.random()*6-1);
		changeInY = yValues[YValue];
		return YValue;
	}


	public void move(){
		ball.x = ball.x + changeInX;
		ball.y = ball.y + changeInY;
	}

	public void drawBall(Graphics g) {
		int x, y, width, height;
		g.setColor(Color.RED);
		x = ball.x;
		y = ball.y;
		width = ball.width;
		height = ball.height;
		g.fillOval( x, y, width, height );
	}
}

⌨️ 快捷键说明

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