欢迎访问我的个人博客 https://jialaner.cn/
在上次的博客中谈到了面向对象的开发思想,在我的理解中面向对象编程思想,就意味着现实与程序的抽象对接,也就是很多人说的万事万物皆对象了。
就像在今天的博客中写到的弹球游戏一样,把实际存在的具有相同属性和行为的事物抽象为一个类,此类就拥有了其中的属性和行为,在使用它时就可以通过它的对象进行操作类中的一些属性和方法。弹球游戏中我们可以将球、弹板、背景抽象为几个类。
●Ball球类:属性:坐标、半径、颜色、X轴Y轴运动的方向速度 方法:球的运动
●Boald弹板类:属性:状态、坐标、长宽、颜色 方法:弹板的移动
●GameView类:此类就是窗口的创建,执行游戏的画图,以及重画。
●GameRun类:此类为刷新界面类,就是控制重画的速度。
●GameControl类:此类是监听键盘类,此类控制着弹板的移动。
上一个效果图:
现在开始上代码;
Ball类:
public class Ball {
public static boolean ballstate = true;
private int ballRX;
private int ballRY;
private final int BALL_R = 10;
private Color BALL_COLOR;
private int directionX = -1;
private int directionY = 1;
private Boald boaldTop;
private Boald boaldDown;
public Ball(int ballRX, int ballRY, Color BALL_COLOR, Boald boaldTop, Boald boaldDown) {
this.setBallRX(ballRX);
this.setBallRY(ballRY);
this.setBALL_COLOR(BALL_COLOR);
this.boaldTop = boaldTop;
this.boaldDown = boaldDown;
}
public int getBallRX() {
return ballRX;
}
public void setBallRX(int ballRX) {
this.ballRX = ballRX;
}
public int getBallRY() {
return ballRY;
}
public void setBallRY(int ballRY) {
this.ballRY = ballRY;
}
public Color getBALL_COLOR() {
return BALL_COLOR;
}
public void setBALL_COLOR(Color bALL_COLOR) {
BALL_COLOR = bALL_COLOR;
}
public int getBALL_R() {
return BALL_R;
}
public void ballMove() {
if(ballstate) {
if(ballRX - BALL_R<= 0 || ballRX + BALL_R>= GameView.VIEW_WIDTH) {
directionX = -directionX;
}
if(ballRY - BALL_R <= boaldTop.getBoaldY() + boaldTop.getBOALD_HEIGHT() && ballRY - BALL_R >= boaldTop.getBoaldY() &&
ballRX + BALL_R >= boaldTop.getBoaldX() && boaldTop.getBoaldX()+ boaldTop.getBOALD_WIDTH() >= ballRX - BALL_R) {
directionY = -directionY;
}
if(ballRY + BALL_R >= boaldDown.getBoaldY() && ballRY + BALL_R <= boaldDown.getBoaldY() + boaldDown.getBOALD_HEIGHT()&&
ballRX + BALL_R >= boaldDown.getBoaldX() && boaldDown.getBoaldX() + boaldDown.getBOALD_WIDTH() >= ballRX - BALL_R){
directionY = -directionY;
}
if(ballRY - BALL_R >= GameView.VIEW_HEIGHT || ballRY + - BALL_R <= 0) {
ballstate = false;
}
ballRX = ballRX + directionX;
ballRY = ballRY + directionY;
}
}
}
Boald类:
public class Boald {
//0静止 ,4左边 6右边
private int boaldState = 0;
private int boaldX;
private int boaldY;
private Color color = Color.blue;
private final int BOALD_WIDTH = 130;
private final int BOALD_HEIGHT = 20;
public Boald(int boaldX, int boaldY) {
this.boaldX = boaldX;
this.boaldY = boaldY;
}
public int getBoaldX() {
return boaldX;
}
public void setBoaldX(int boaldX) {
this.boaldX = boaldX;
}
public int getBoaldY() {
return boaldY;
}
public void setBoaldY(int boaldY) {
this.boaldY = boaldY;
}
public Color getColor() {
return color;
}
public void setColor(Color color) {
this.color = color;
}
public int getBOALD_WIDTH() {
return BOALD_WIDTH;
}
public int getBOALD_HEIGHT() {
return BOALD_HEIGHT;
}
public void setBoaldState(int boaldState) {
this.boaldState = boaldState;
}
public void boaldMove() {
switch(GameControl.state) {
case 4:
boaldX = boaldX - 5;
break;
case 5:
break;
case 6:
boaldX = boaldX + 5;
break;
}
}
}
GameView类:
public class GameView extends Frame{
private static final long serialVersionUID = 1L;
private Random random = new Random();
public static final int VIEW_WIDTH = 600;
public static final int VIEW_HEIGHT = 1000;
//位图
private Image ImageBuffer = null;
private Graphics GraImage = null;
private Boald boaldTop;
private Boald boaldDown;
private Ball ball;
private GameRun gameRun;
public GameView() {
boaldTop = new Boald(0,0);
boaldDown = new Boald(0,0);
ball = new Ball(0, 0, Color.WHITE, boaldTop, boaldDown);
boaldTop.setBoaldX(random.nextInt((int)(VIEW_WIDTH - boaldTop.getBOALD_WIDTH())));
boaldTop.setBoaldY(boaldTop.getBOALD_HEIGHT() * 3);
boaldDown.setBoaldX(boaldTop.getBoaldX());
boaldDown.setBoaldY((int)(VIEW_HEIGHT-boaldDown.getBOALD_HEIGHT()*3));
ball.setBallRX((int)(boaldTop.getBoaldX()+boaldTop.getBOALD_WIDTH()/2));
ball.setBallRY(boaldTop.getBoaldY() + boaldTop.getBOALD_HEIGHT() + ball.getBALL_R() + 5);
gameRun = new GameRun(this, boaldTop, boaldDown, ball);
gameRun.start();
}
public static void main(String[] args) {
new GameView().init();
}
public void init() {
this.setTitle("弹球游戏");
this.setSize(VIEW_WIDTH, VIEW_HEIGHT);
this.setBackground(Color.BLACK);
this.addKeyListener(new GameControl());
this.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent we) {
System.exit(0);
}
});
this.setVisible(true);
}
//画图
public void paint(Graphics g) {
g.setColor(ball.getBALL_COLOR());
g.fillOval(ball.getBallRX() - ball.getBALL_R(), ball.getBallRY() - ball.getBALL_R(), ball.getBALL_R() * 2, ball.getBALL_R() * 2);
g.setColor(boaldTop.getColor());
g.fillRect(boaldTop.getBoaldX(), boaldTop.getBoaldY(), boaldTop.getBOALD_WIDTH(), boaldTop.getBOALD_HEIGHT());
g.setColor(boaldDown.getColor());
g.fillRect(boaldDown.getBoaldX(), boaldDown.getBoaldY(), boaldDown.getBOALD_WIDTH(), boaldDown.getBOALD_HEIGHT());
if(Ball.ballstate == false) {
g.setFont(new Font("隶书",Font.BOLD,40));
g.drawString("游戏结束", 210, 500);
}
}
public void update(Graphics g) {
//创建图形缓冲区
ImageBuffer = createImage(this.getWidth(), this.getHeight());
//获取图形缓冲区的图形上下文
GraImage = ImageBuffer.getGraphics();
//用paint方法中编写的绘图过程对图形缓冲区绘图
paint(GraImage);
//释放图形上下文资源
GraImage.dispose();
//将图形缓冲区绘制到屏幕上
g.drawImage(ImageBuffer, 0, 0, this);
}
}
GameRun类:
public class GameRun extends Thread{
private int sleepTime = 3;
private GameView gameView;
private Boald boaldTop;
private Boald boaldDown;
private Ball ball;
public GameRun(GameView gameView, Boald boaldTop,Boald boaldDown, Ball ball) {
this.gameView = gameView;
this.boaldTop = boaldTop;
this.boaldDown = boaldDown;
this.ball = ball;
}
public void run() {
while(true) {
if(Ball.ballstate) {
gameView.repaint();
boaldTop.boaldMove();
boaldDown.boaldMove();
ball.ballMove();
try {
Thread.sleep(sleepTime);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
}
GameControl类:
public class GameControl implements KeyListener{
public static int state = 5;
@Override
public void keyTyped(KeyEvent e) {
// TODO Auto-generated method stub
}
@Override
public void keyPressed(KeyEvent e) {
switch(e.getKeyCode()) {
case KeyEvent.VK_LEFT:
state = 4;
break;
case KeyEvent.VK_RIGHT:
state = 6;
break;
default:
break;
}
}
@Override
public void keyReleased(KeyEvent e) {
state = 5;
}
}
这个游戏还有一些边界处理的问题没有解决。勿喷!