(1)游戏界面:
package domain;
import constant.Constant;
import java.awt.*;
/**
* 球类
*/
public class Ball {
// 大小
private int ballSize = Constant.BALL_SIZE;
// 颜色
private Color color = Constant.BALL_COLOR;
// 坐标位置
private int ballX;
private int ballY;
// 速度大小
private int speedX = Constant.speedX;
private int speedY = Constant.speedY;
public Ball(int ballX, int ballY) {
this.ballX = ballX;
this.ballY = ballY;
}
/**
* 小球移动
* @param racketX :球拍位置,目前游戏中上球拍和下球拍racketX位置相同
*/
public void moveBall(int racketX) {
// 小球速度方向改变
if (ballX <= 0 || ballX >= Constant.TABLE_WIDTH - ballSize) { // X轴方向速度
speedX = -speedX;
}
if ((ballY <= Constant.racketHeight || ballY >= Constant.TABLE_HEIGHT - Constant.racketHeight - ballSize)
&& ballX > racketX - ballSize && ballX < racketX + Constant.racketWidth) { // Y轴方向速度
speedY = -speedY;
}
ballX += speedX;
ballY += speedY;
}
public int getBallSize() {
return ballSize;
}
public Color getColor() {
return color;
}
public void setColor(Color color) {
this.color = color;
}
public int getBallX() {
return ballX;
}
public void setBallX(int ballX) {
this.ballX = ballX;
}
public int getBallY() {
return ballY;
}
public void setBallY(int ballY) {
this.ballY = ballY;
}
public int getSpeedX() {
return speedX;
}
public void setSpeedX(int speedX) {
this.speedX = speedX;
}
public int getSpeedY() {
return speedY;
}
public void setSpeedY(int speedY) {
this.speedY = speedY;
}
}
(2)部件大小设置
package constant;
import java.awt.*;
import java.util.Random;
public class Constant {
// 桌面宽高
public static final int TABLE_WIDTH = 450;
public static final int TABLE_HEIGHT = 600;
// 桌面宽度 扩展部分
public static final int TABLE_WIDTH_EXTEND = 150;
// 球的大小
public static final int BALL_SIZE = 16;
// 球的颜色
public static final Color BALL_COLOR = Color.RED;
// 球的速度
public static final int speedX = new Random().nextInt(5) + 5;
public static final int speedY = new Random().nextInt(5) + 10;
// 球拍长宽
public static final int racketWidth = 60;
public static final int racketHeight = 20;
// 球拍颜色
public static final Color RACKET_COLOR = Color.LIGHT_GRAY;
// 球拍每次移动距离
public static final int moveLength = 10;
}
(3)小球运动情况设置
package domain;
import constant.Constant;
import java.awt.*;
/**
* 球类
*/
public class Ball {
// 大小
private int ballSize = Constant.BALL_SIZE;
// 颜色
private Color color = Constant.BALL_COLOR;
// 坐标位置
private int ballX;
private int ballY;
// 速度大小
private int speedX = Constant.speedX;
private int speedY = Constant.speedY;
public Ball(int ballX, int ballY) {
this.ballX = ballX;
this.ballY = ballY;
}
/**
* 小球移动
* @param racketX :球拍位置,目前游戏中上球拍和下球拍racketX位置相同
*/
public void moveBall(int racketX) {
// 小球速度方向改变
if (ballX <= 0 || ballX >= Constant.TABLE_WIDTH - ballSize) { // X轴方向速度
speedX = -speedX;
}
if ((ballY <= Constant.racketHeight || ballY >= Constant.TABLE_HEIGHT - Constant.racketHeight - ballSize)
&& ballX > racketX - ballSize && ballX < racketX + Constant.racketWidth) { // Y轴方向速度
speedY = -speedY;
}
ballX += speedX;
ballY += speedY;
}
public int getBallSize() {
return ballSize;
}
public Color getColor() {
return color;
}
public void setColor(Color color) {
this.color = color;
}
public int getBallX() {
return ballX;
}
public void setBallX(int ballX) {
this.ballX = ballX;
}
public int getBallY() {
return ballY;
}
public void setBallY(int ballY) {
this.ballY = ballY;
}
public int getSpeedX() {
return speedX;
}
public void setSpeedX(int speedX) {
this.speedX = speedX;
}
public int getSpeedY() {
return speedY;
}
public void setSpeedY(int speedY) {
this.speedY = speedY;
}
}
(4)游戏界面设计
public class GameView extends Canvas {
// 桌面宽高
private final int TABLE_WIDTH = Constant.TABLE_WIDTH;
private final int TABLE_HEIGHT = Constant.TABLE_HEIGHT;
// 游戏规则
private Rule rule;
// 球
private Ball ball;
// 上球拍
private Racket racketTop;
// 下球拍
private Racket racketBottom;
// 声明定时器
private Timer timer;
// 分数
private int score;
public GameView(Ball ball, Racket racketTop, Racket racketBotton) {
// 设置桌面
this.setPreferredSize(new Dimension(TABLE_WIDTH + Constant.TABLE_WIDTH_EXTEND, TABLE_HEIGHT));
this.rule = new Rule();
this.ball = ball;
this.racketTop = racketTop;
this.racketBottom = racketBotton;
// 定时刷新任务
timer = new Timer(100, event -> {
// 小球移动
ball.moveBall(racketBotton.getRacketX());
// 统计分数
if (rule.scoreAdd(ball)) {
score++;
}
// 绘制界面
this.repaint();
});
timer.start();
}
@Override
public void paint(Graphics g) {
if (!rule.isOver(ball, racketBottom.getRacketX())) { // 游戏进行
paintGame(g);
} else { // 游戏结束
paintGameOver(g);
timer.stop();
}
}
/**
* 绘制游戏
*/
private void paintGame(Graphics g) {
// 绘制小球
g.setColor(ball.getColor());
g.fillOval(ball.getBallX(), ball.getBallY(), ball.getBallSize(), ball.getBallSize());
// 绘制上球拍
g.setColor(racketTop.getColor());
g.fillRect(racketTop.getRacketX(), racketTop.getRacketY(), racketTop.getRacketWidth(), racketTop.getRacketHeight());
// 绘制下球拍
g.setColor(racketBottom.getColor());
g.fillRect(racketBottom.getRacketX(), racketBottom.getRacketY(), racketBottom.getRacketWidth(), racketBottom.getRacketHeight());
// 绘制直线
g.setColor(Color.LIGHT_GRAY);
g.fillRect(452, 0, 2, 600);
// 绘制分数板
g.setColor(Color.LIGHT_GRAY);
g.fillRect(475, 130, 100,100);
// 绘制分数
g.setColor(Color.black);
g.setFont(new Font("隶书", Font.BOLD, 30));
g.drawString(Integer.toString(score), 515, 190);
}
/**
* 绘制游戏结束
*/
private void paintGameOver(Graphics g) {
g.setColor(Color.BLUE);
g.setFont(new Font("隶书", Font.BOLD, 30));
g.drawString("游戏结束", 130, 300);
// 绘制直线
g.setColor(racketBottom.getColor());
g.fillRect(452, 0, 2, 600);
// 绘制分数板
g.setColor(Color.LIGHT_GRAY);
g.fillRect(475, 130, 100,100);
// 绘制分数
g.setColor(Color.black);
g.setFont(new Font("隶书", Font.BOLD, 30));
g.drawString(Integer.toString(score), 515, 190);
}
/**
* 游戏的开始和暂停
*/
public void runStatus() {
if (timer.isRunning()) {
timer.stop();
} else {
timer.start();
}
}
}
(5)球拍设计
public class Racket {
// 长宽
private int racketWidth = Constant.racketWidth;
private int racketHeight = Constant.racketHeight;
// 颜色
private Color color = Constant.RACKET_COLOR;
// 位置
private int racketX;
private int racketY;
public Racket(int racketX, int racketY) {
this.racketX = racketX;
this.racketY = racketY;
}
/**
* 左移动球拍
* @param moveLength 移动长度
*/
public void moveLeft(int moveLength) {
if (racketX > 0) {
racketX -= moveLength;
}
}
/**
* 右移动球拍
* @param moveLength 移动长度
*/
public void moveRight(int moveLength) {
if (racketX < Constant.TABLE_WIDTH - racketWidth) {
racketX += moveLength;
}
}
public int getRacketWidth() {
return racketWidth;
}
public void setRacketWidth(int racketWidth) {
this.racketWidth = racketWidth;
}
public int getRacketHeight() {
return racketHeight;
}
public void setRacketHeight(int racketHeight) {
this.racketHeight = racketHeight;
}
public Color getColor() {
return color;
}
public void setColor(Color color) {
this.color = color;
}
public int getRacketX() {
return racketX;
}
public void setRacketX(int racketX) {
this.racketX = racketX;
}
public int getRacketY() {
return racketY;
}
public void setRacketY(int racketY) {
this.racketY = racketY;
}
}
(6)游戏规则设置
public class Rule {
private int preSpeed = Constant.speedY;
public Rule() {
}
/**
* 游戏是否结束
* @param ball 球类
* @param racketX 球拍位置,目前游戏中上球拍和下球拍racketX位置相同
*/
public boolean isOver(Ball ball, int racketX) {
// 游戏结束
if ((ball.getBallY() <= Constant.racketHeight || ball.getBallY() >= Constant.TABLE_HEIGHT - Constant.racketHeight - ball.getBallSize())
&& (ball.getBallX() <= racketX - ball.getBallSize() || ball.getBallX() >= racketX + Constant.racketWidth)) {
return true;
}
return false;
}
/**
* 是否加分
* @param ball 球类
*/
public boolean scoreAdd(Ball ball) {
if (preSpeed != ball.getSpeedY()) {
this.preSpeed = ball.getSpeedY();
return true;
}
return false;
}
}