各位大佬下面这代码添加实现按下空格暂停,再按下一次继续
public class TypeFrame extends Frame {
public long start,end;//游戏开始与刷新时的时间,用于统计打字速度
public int typecount=0;//打字个数
public Image paintplace = null; //绘制其他图形的底图
public static final int GAME_HEIGHT = 666;// 游戏高度
public static final int GAME_WIDTH = 956; // 游戏宽度
public ArrayList<Emery> apples = new ArrayList<>(); //放置所有存活的苹果的容器
public static int score = 0; //分数
public Image bg = Toolkit.getDefaultToolkit().getImage("bg.jpg");//背景图片
TypeFrame tf=this; //得到当前对象以便后面使用
public TypeFrame() {
setTitle("拯救苹果");
start=System.currentTimeMillis(); //获取系统当前的毫秒
setVisible(true);
setBounds(200, 20, GAME_WIDTH, GAME_HEIGHT);//设置界面
addWindowListener(new WindowAdapter() { //设置关闭监听
public void windowClosing(WindowEvent e) {
dispose();
}
});
addKeyListener(new KeyAdapter() {
public void keyReleased(KeyEvent e) {
for (int i = 0; i < apples.size(); ) {
if (e.getKeyChar() == apples.get(i).c) {//如果与输入的字母一致,则移除苹果并加10分
apples.get(i).alive = false;
apples.remove(i);
score+=10;
typecount++;
apples.add(new Emery(tf));
}
i++;
}
}
});
for (int i = 0; i < 10; i++) {
apples.add(new Emery(this));
}
new Thread(new PaintThread()).start();
}
public void update(Graphics g) {
if (paintplace == null) {
paintplace = createImage(GAME_WIDTH, GAME_HEIGHT);
}
Graphics gback = paintplace.getGraphics(); // 得到背景图片画笔
Color c = gback.getColor();
gback.setColor(Color.gray);
gback.fillRect(0, 0, GAME_WIDTH, GAME_HEIGHT);
gback.setColor(c);
gback.drawImage(bg, 0, 0, null); //绘制背景图片
gback.setColor(Color.black);
gback.setFont(new Font("Dialog", 5, 25));
gback.drawString("Score:"+score, 30, 70);//绘制分数
end=System.currentTimeMillis(); //获取系统此刻的毫秒数
gback.drawString("打字速度:"+(typecount*60000/(end-start))+"字/分", 30, 100);
paint(gback);
g.drawImage(paintplace, 0, 0, null); //绘制画板
}
public void paint(Graphics g) {
for (int i = 0; i < apples.size(); ) {
if(apples.get(i).y<=GAME_HEIGHT+20){ //如果苹果还在界内,则正常绘制
apples.get(i).draw(g);
i++;
}
else{ //否则移除出容器,扣5分,并且加入新的苹果
apples.get(i).alive = false;
apples.remove(i);
score-=5;
apples.add(new Emery(tf));
}
}
}
class PaintThread implements Runnable {
@Override
public void run() {
Emery emery = null;
while (true) { //控制苹果坐标改变
for (int i = 0; i < apples.size(); i++) {
emery = apples.get(i);
emery.y += emery.YSPEED;
if (emery.turnleft) {
emery.x -= emery.XSPEED;
if (Math.abs((emery.center - emery.x)) > 20) {
emery.turnleft = false;
}
} else {
emery.x += emery.XSPEED;
if (Math.abs((emery.center - emery.x)) > 20) {
emery.turnleft = true;
}
}
}
repaint(); //重绘整个界面
try {
Thread.sleep(40);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
public static void main(String[] args) {
TypeFrame tf = new TypeFrame();
}
}
苹果类
public class Emery {
public char c; //苹果上的字母
public int x = 60, y = 0; // 敌人出现的坐标
public final int XSPEED=5,YSPEED=2; //苹果xy方向移动的速度
public int center; //初始中心值
public boolean turnleft = true; //是否向左移动
public boolean alive = true; //是否活着
public Random ran = new Random(); //随机数的种子
public TypeFrame tf=null; //所属的框架
public Image appleimg = null; //苹果的图片
public Image bg = Toolkit.getDefaultToolkit().getImage("bg.jpg"); //背景图片
public Emery(TypeFrame tf) {
this.tf=tf;
x = randomlocation(); //得到随机合格的随机x坐标
y=ran.nextInt(20); //得到随机的y坐标
if(ran.nextInt(2)==0){
turnleft=true;
}else
{
turnleft=false;
}
center = x; //设置初始中心值为x
c=randomchar(); //得到随机的字母值
try {
appleimg = ImageIO.read(new File("apple.gif")); //苹果的图片
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void draw(Graphics g) {
Color color = g.getColor(); //得到上下文颜色
g.setColor(Color.red); //设置上下文颜色
g.setFont(new Font("Dialog", 4, 40)); //设置字体
if (alive) {
g.drawImage(appleimg, x, y, null); //绘制苹果图片
g.drawString(c+ "", x + 20, y + 60); //绘制苹果字母
}
g.setColor(color); //将上下文颜色设置回去
}
public int randomlocation(){ //产生苹果的随机横坐标的函数
int x1=ran.nextInt(TypeFrame.GAME_WIDTH - 40);
for (int i = 0; i < tf.apples.size(); i++) {
if(Math.abs(x1-tf.apples.get(i).x)<60){
return randomlocation();
}
}
return x1;
}
public char randomchar(){ //产生不与存在的苹果字母相同的字母的方法
char ch=(char)('a'+ran.nextInt(26));
for (int i = 0; i < tf.apples.size(); i++) {
if(ch==tf.apples.get(i).c)
return randomchar();
}
return ch;
}
}