资源描述
打字游戏制作
一个纯代码游戏没有一张图片,如果你愿意可以自己添加图片,本游戏是学习java的时候自己做的打字游戏为了提高打字速度,也为巩固所学知识,现将源码上传供大家学习参考,本人也是新手,有许多不足之处望见谅。源码如下
package Dzgame;
/** @author tx*/
import java.awt.Color;
import java.awt.Container;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.event.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 JPanel implements KeyListener {
public static boolean kg;
/** 声明一个文本框用于输入和显示字母 */
public static JTextField tx;
/**
* zm类的一个数组,长度为零
*
*/
public static ZM[] zm = {};
public GameTest(){
this.addKeyListener(this);
}
/**
* 子弹数组,长度为零
*
*/
public static Bullet[] 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++) {
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 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();
}
}
/**
* 该方法用于移动已经生成的字母;
* */
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;
}
}
}
}
/** 该方法用于移除已经越界的字母 */
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 = 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 - 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[bullets.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<zm.length ;i++){
if(zm[i].hitby(bt)){
fj=true;
z=zm[i];
zm[i] = zm[zm.length - 1];
zm[zm.length - 1] = z;
zm = Arrays.copyOf(zm, zm.length - 1);
}else{fj=false;}
}
return fj;
}
public void pzjc(){
Bullet z=new Bullet();
for(int i=0;i<bullets.length ;i++){
if(jc(bullets[i])){
z = bullets[i];
bullets[i] = bullets[bullets.length - 1];
bullets[bullets.length - 1] = z;
bullets = 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);
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);
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.zmAction();
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<zm.length ;i++){
if(zm[i].str==sz[0]){
}}
}
}
public void keyPressed(KeyEvent e) {
int k = e.getKeyCode();
z = e.getKeyChar();
str=""+z;
tx.setText(str);
for(int i=0;i<zm.length;i++){
if(zm[i].str.equals (str)){
fs(str,zm[i].x) ; }
}
if (k==KeyEvent.VK_SPACE) {
kg=true;
}
}
public void keyReleased(KeyEvent e) {
kg=false;
}
}
///////////////////////////////////////////////////////////////
展开阅读全文