在模拟弹球小游戏中设计的对象:弹棒(Stick),小球(Ball),击中播放声音(Explode),游戏的组织进行(Game)。各个类的构造方法以及相关代码如下:
/**
*模拟弹棒
*/
public class Stick
{
private int x; //棒子左端x坐标
private int y; //棒子右端的y坐标
private int len; //棒子的长度
public Stick()
{
}
public Stick(int x, int y, int len)
{
this.x = x;
this.y = y;
this.len = len;
}
public int getX()
{
return this.x;
}
public int getY()
{
return this.y;
}
public void setX(int x)
{
this.x = x;
}
public int getLen()
{
return this.len;
}
public void setLen(int len)
{
this.len = len;
}
//根据玩家的方向键移动棒子:1向右 -1向左
public void move(int dir)
{
x = x + dir * 4;
if(x < 4)
{
x = 0;
}
if(x >= 68)
{
x = 71;
}
}
}
/**
*模拟小球
*/
public class Ball
{
private int x; //小球的x坐标
private int y; //小球的y坐标
private int xdir; //小球的运动方向:1向右 -1向左
private int ydir; //小球的运动方向:1向下 -1向上
public Ball()
{
}
public Ball(int x, int y, int xdir, int ydir)
{
this.x = x;
this.y = y;
this.xdir = xdir;
this.ydir = ydir;
}
public int getX()
{
return this.x;
}
public int getY()
{
return this.y;
}
public void setX(int x)
{
this.x = x;
}
public void setY(int y)
{
this. y = y;
}
public int getXdir()
{
return this.xdir;
}
public int getYdir()
{
return this.ydir;
}
public void setXdir(int xdir)
{
this.xdir = xdir;
}
public void setYdir(int ydir)
{
this.ydir = ydir;
}
//小球移动,返回是否到底了
public boolean move()
{
//移动修改坐标
x = x + xdir;
y = y + ydir;
//边界判断,小球变向
if(x <= 0 || x >= 79) //左右边界判断
{
xdir = -xdir;
}
if(y <= 0 || y >= 23) //上下边界判断
{
ydir = -ydir;
}
return (y >= 23); //是否到底
}
}
/**
*棒子弹中小球时播放声音
*/
import java.applet.Applet;
import java.applet.AudioClip;
public class Explode
{
private AudioClip explode;
public Explode()
{
explode = Applet.newAudioClip(Explode.class.getResource("Boing.wav"));
}
//播放击中的声音
public void play()
{
explode.play();
}
}
/**
*弹小球游戏2023版
*/
import static javaconsole.JavaConsole.*;
public class Game
{
public static final int KEY_LEFT = 75; //向左的光标键
public static final int KEY_RIGHT = 77; //向右的光标键
private Ball ball; //小球
private Stick stick; //棒子
private Explode sound; //弹中时的声音
private int score; //玩家的得分,即棒子弹中小球的次数
private boolean isHit; //是否弹中
public Game()
{
score = 0;
isHit = false;
ball = new Ball(0, 0, 1, 1);
stick = new Stick(40, 23, 8);
sound = new Explode();
set_size(80,25);
set_title("Java控制台弹球小游戏");
hide_cursor();
set_color(0, 0xa);
//绘制初始的全部砖块
draw_init_blocks();
}
public static void main(String[] args)
{
Game game = new Game();
game.start();
}
//游戏主控方法
public void start()
{
while(true)
{
//更新内部状态数据
update();
//根据内部状态数据进行渲染
render();
}
}
//更新内部状态数据
public void update()
{
int dir = -1; //棒子的运动方向
//移动小球
if(ball.move()) //到底了吗?
{
check_hit(); //弹中小球了吗?
}
//根据玩家的方向键移动棒子
if(kbhit())
{
int ch = getkey();
switch(ch)
{
case KEY_LEFT: //向左的光标键
dir = -1;
break;
case KEY_RIGHT: //向右的光标键
dir = 1;
}
stick.move(dir); //移动棒子
}
}
//判断是否弹中小球
public void check_hit()
{
if((ball.getX() < ball.getX()) || (ball.getX() > stick.getX() + stick.getLen())) //没弹中
{
//游戏结束
end_game();
}
else //弹中小球
{
score++;
isHit = true;
}
}
//游戏结束
public void end_game()
{
gotoxy(30, 10);
System.out.printf("游戏结束,总共得分%d分!", score);
set_color(0,7); //恢复黑底白字
getkey(); //按任意键退出游戏
System.exit(0);
}
//初始化小方块
public void draw_init_blocks()
{
for(int x = 5; x < 75; x += 2)
{
gotoxy(x, 0);
set_color(0, (int)(Math.random() * 15) + 1);
System.out.print("■");
}
}
//根据内部状态渲染
public void render()
{
//如果弹中播放声音
if(isHit)
{
sound.play(); //播放声音
isHit = false;
}
//显示玩家得分
gotoxy(0, 0);
set_color(0, 0xa);
System.out.printf("得分:%d", score);
//绘制小球
int x = ball.getX();
int y = ball.getY();
gotoxy(x,y);
System.out.print("o");
delay(60);
gotoxy(x, y);
System.out.print(" ");
//绘制棒子
gotoxy(0, 23);
for(int i = 0; i < 80; i++)
{
System.out.print(" ");
}
gotoxy(stick.getX(), stick.getY());
for(int i = 0; i < stick.getLen(); i++)
{
System.out.print("==");
}
}
}