这个简单小游戏涉及到的对象:单个字符OneChar,Explode(击中某个字符时播放出的声音),Game( 游戏主体),在此程序中需要光标定位,因此采用了之前的有声有色文本库JavaConsole,来达到我们预期的效果。
/**
*模拟单个字符
*/
public class OneChar
{
private int x; //字符的x坐标
private int y; //字符的y坐标
private int color; //字符的颜色
private char ch; //字符值
public OneChar()
{
}
public OneChar(char ch, int x, int y, int color)
{
this.ch = ch;
this.x = x;
this.y = y;
this.color = color;
}
public char getCh()
{
return this.ch;
}
public int getX()
{
return this.x;
}
public int getY()
{
return this.y;
}
public int getColor()
{
return this.color;
}
//该字符往下掉一行
public void down()
{
if(y < 22)
{
y++; //该字符的y坐标加一
}
else //该字符调到底了,重新在最上面产生一个新的字符
{
this.ch = (char)((int)(Math.random() * 26) + 'a');
this.color = (int)(Math.random() * 15) + 1;
this.y = 1;
}
}
}
/**
*击中某个字符时播放的声音
*/
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
{
private final int N = 10; //待打的字符总数
private long starttime; //游戏开始时间
private long nowtime; //游戏现在时间
private long timeused; //游戏已经进行的时间,单位:秒
private int score; //玩家成功击中字符数
private int nkeys; //玩家击键总数
private OneChar[] chars; //待打的字符数组对象
private boolean isOver; //游戏是否结束
private boolean isHit; //是否击中字符
private Explode explode; //击中时播放的声音
private OneChar delchar; //击中后需要删除的字符
public Game()
{
//初始化击中的声音
explode = new Explode();
//初始化所有的字符
chars = new OneChar[N];
for(int i = 0; i < N; i++)
{
chars[i] = new OneChar((char)((int)(Math.random() * 26) + 'a'), i * 8 + 3, 1,
(int)(Math.random() * 15) + 1);
}
score = 0;
nkeys = 0;
timeused = 0;
isOver = false;
isHit = false;
hide_cursor();
set_title("一分钟定时打字游戏");
set_size(80,25);
set_color(0, 0xa);
cls(); //清屏
//获取启动时间
starttime = System.currentTimeMillis();
}
public static void main(String[] args)
{
Game game = new Game();
game.start();
}
//游戏主控方法
public void start()
{
while(!isOver)
{
//更新内部状态
update();
delay(100);
//根据内部状态进行渲染
render();
}
}
//更新内部状态
public void update()
{
if(timeused <= 60) //定时打字一分钟
{
if(kbhit()) //是否按键
{
char ch = (char)getkey();
nkeys++;
//判断玩家输入的键是否击中某个字符
for(int i = 0; i < chars.length; i++)
{
if(ch == chars[i].getCh())
{
//delchar = chars[i]; //保存待删除的字符信息,这只是引用,真正的对象没有保存下来
delchar = new OneChar(chars[i].getCh(), chars[i].getX(), chars[i].getY(),
chars[i].getColor());
score++;
isHit = true;
//重新生成待打的字符
chars[i] = new OneChar((char)((int)(Math.random() * 26) + 'a'), i * 8 + 3, 1,
(int)(Math.random() * 15) + 1);
break;
}
}
}
else //玩家没有按键,随机选择一个字符往下落。
{
int i = (int)(Math.random() * N);
delchar = new OneChar(chars[i].getCh(), chars[i].getX(), chars[i].getY(),
chars[i].getColor());
chars[i].down();
}
nowtime = System.currentTimeMillis();
timeused = (nowtime - starttime) / 1000;
}
else
{
isOver = true; //超时,游戏结束
}
}
//根据内部状态渲染
public void render()
{
//渲染提示信息
set_color(0, 0xa);
gotoxy(20,23);
System.out.printf(" 用时:%2d秒!", timeused);
gotoxy(0, 23);
System.out.printf("按键:%2d 击中: %2d", nkeys, score);
//擦除待擦除的字符
gotoxy(delchar.getX(), delchar.getY());
System.out.print(" ");
//击中字符
if(isHit)
{
explode.play(); //播放击中的声音
isHit = false;
}
//渲染所有的字符
for(int i = 0; i < N; i++)
{
gotoxy(chars[i].getX(), chars[i].getY());
set_color(0, chars[i].getColor());
System.out.print(chars[i].getCh());
}
//渲染游戏结束的信息
if(isOver)
{
gotoxy(24, 13);
set_color(0, 0xa);
System.out.printf("最后成绩: 按键%2d,击中%2d次!", nkeys, score);
set_color(0, 7);
getkey(); //按任意键,退出游戏
System.exit(0);
}
}
}