1、 打字游戏制作 一个纯代码游戏没有一张图片,如果你愿意可以自己添加图片,本游戏是学习java的时候自己做的打字游戏为了提高打字速度,也为巩固所学知识,现将源码上传供大家学习参考,本人也是新手,有许多不足之处望见谅。源码如下 package Dzgame; /** @author tx*/ import java.awt.Color; import java.awt.Container; import java.awt.Font; import java.awt.Graphics; import java.awt.ev
2、ent.KeyEvent; import java.awt.event.KeyListener; import java.util.Arrays; import java.util.Timer; import java.util.TimerTask; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.JTextField; /** * * @author Administrator * */ public class GameTest extends J
3、Panel implements KeyListener { public static boolean kg; /** 声明一个文本框用于输入和显示字母 */ public static JTextField tx; /** * zm类的一个数组,长度为零 * */ public static ZM[] zm = {}; public GameTest(){ this.addKeyListener(this); } /** * 子弹数组,长度为零 * */ public static Bullet[
4、] bullets = {}; public void paint(Graphics g) { super.paint(g); paintzm(g); paintb(g); } /** 绘出随机的字母图象 */ public void paintzm(Graphics g) { super.paint(g); g.setColor(Color.PINK ); g.setFont(new Font(Font.SANS_SERIF, Font.BOLD, 45)); for (int i = 0; i < zm.length; i
5、) { ZM z1 = zm[i]; g.drawString(z1.str, z1.x, z1.y); } } public void paintb(Graphics g) { g.setFont(new Font(Font.SANS_SERIF, Font.BOLD, 25)); for (int i = 0; i < bullets.length; i++) { Bullet z1 = bullets[i]; g.drawString(z1.str, z1.x, z1.y); } } int
6、k = 0; /** 该方法用于不停的加入随机字母 */ public void tjzm() { k++; if (k % 15 == 0) { zm = Arrays.copyOf(zm, zm.length + 1); zm[zm.length - 1] = new ZM(); zm[zm.length - 1].getx(); zm[zm.length - 1].gety(); zm[zm.length - 1].gets(); } } /** * 该方法用于移动已经生成的字母; * */
7、 public void movezm(Object obj) { if (obj instanceof ZM[]) { for (int i = 0; i < zm.length; i++) { zm[i].y += 3; } } else if (obj instanceof Bullet[]) { if (bullets.length > 0) { for (int i = 0; i < bullets.length; i++) { bullets[i].y += -8; } }
8、} } /** 该方法用于移除已经越界的字母 */ public void remp(Object obj) { if (obj instanceof ZM[]) { ZM z = new ZM(); ZM z1 = new ZM(); for (int i = 0; i < zm.length; i++) { if (zm[i].y > 400) { z = zm[i]; zm[i] = zm[zm.length - 1]; zm[zm.length - 1] = z; zm =
9、Arrays.copyOf(zm, zm.length - 1); } } } if (obj instanceof Bullet[]) { Bullet z = new Bullet(); for (int j = 0; j < bullets.length; j++) { if (bullets[j].y < -10) { z = bullets[j]; bullets[j] = bullets[bullets.length - 1]; bullets[bullets.length
10、 1] = z; bullets = Arrays.copyOf(bullets, bullets.length - 1); } } } } public void fs(String s,int n) { bullets = Arrays.copyOf(bullets, bullets.length + 1); bullets[bullets.length - 1] = new Bullet(); bullets[bullets.length - 1].str = s; bullets[bullet
11、s.length - 1].x = n;
bullets[bullets.length - 1].y = 340;
}
public boolean jc( Bullet bt){
boolean fj = false;
ZM z=new ZM();
for(int i=0;i 12、rrays.copyOf(zm, zm.length - 1);
}else{fj=false;}
}
return fj;
}
public void pzjc(){
Bullet z=new Bullet();
for(int i=0;i 13、ets = Arrays.copyOf(bullets, bullets.length - 1);
}
}
}
/** 运行主程序,由定时器负责定时绘图并刷新屏幕 */
public void zmAction() {
Timer timer = new Timer();
timer.schedule(new TimerTask() {
public void run() {
tjzm();
movezm(zm);
remp(zm);
movezm(bullets);
14、 remp(bullets);
pzjc();
repaint();
}
}, 10, 83);
}
public static void main(String[] args) {
JFrame jf = new JFrame("打字游戏");
jf.setBounds(40, 150, 400, 580);
jf.setVisible(true);
jf.setLayout(null);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
15、
GameTest test = new GameTest();
test.setLocation(0, 0);
test.setBounds(0, 0, 400, 400);
jf.add(test);
tx=new JTextField();
tx.setBackground(Color.ORANGE);
tx.setBounds(50, 450, 250, 30);
tx.setFocusable(false);
jf.add(tx);
test.zmAct 16、ion();
test.requestFocus();
}
public void keyTyped(KeyEvent e) {
}
public static String[]sz=new String[1];
public static String str;
public char z;
public void dd(){
if(kg==true){
for(int i=0;i 17、}
}
public void keyPressed(KeyEvent e) {
int k = e.getKeyCode();
z = e.getKeyChar();
str=""+z;
tx.setText(str);
for(int i=0;i






