java做打字游戏的代码 java打字小游戏

程序设计:简单字母打字游戏(JAVA编写)/*
游戏开始,从天上掉下10个有标号(字母)的炸弹,
下面有10颗子弹(就是可以按10个字母).
如果所按字母包含在炸弹中,就找到相应的炸弹并
消灭它,消灭后返回继续利用.如果没有就一直飞到天上最后才返回.
*/
import java.net.*;
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import javax.swing.*;
public class paodan extends Applet implements Runnable,KeyListener
{
private Image bgImage;
private Graphics bg;
static Thread myThread;
static AudioClip ac1,ac2;
static JFrame md;
String Zi26="";
String pd[]={"a","a","a","a","a","a","a","a","a","a"}; //用来存储10个字母做落下的炮弹
String fj[]={"","","","","","","","","",""}; //用来存储10个字母做反击的防空
int x[]=new int[10];
int y[]=new int[10];
//炸弹的坐标
int x1[]=new int[10];
int x2[]=new int[10];
int y1[]=new int[10];
//火箭的坐标
int life=100;//生命
Toolkit KIT=this.getToolkit();
Image paodan=KIT.createImage("paodan.jpg");
Image huojian=KIT.createImage("huojian.jpg");
public paodan() //用来生成一个包含26个英文字母的字符串Zi26
{for (int i=0;i26;i)
Zi26 =(char)((int)'A' i);
}
public void cushi() //初始各种数据,如炮弹与火箭的坐标.
{
try
{URL u1=this.getClass().getResource("huishou.wav");
URL u2=this.getClass().getResource("lanjie.wav");
ac1=Applet.newAudioClip(u1);
ac2=Applet.newAudioClip(u2);
}catch(Exception e){}
for (int i=0;i10;i)
{pd[i]=noRepeat();
x[i]=noHit(i,10);
if (i=5)
y[i]=5 (int)(Math.random()*50);
else
y[i]=60 (int)(Math.random()*50);
x1[i]=220 i*32 1; y1[i]=550;
x2[i]=x1[i];
}
}
public void panduan() //用来判断pd[i]的y值是否要到达屏幕下边,如果是就把Y值变为5,并把所有Y值加3.
{ for(int i=0;i10;i)
{ if (y[i]=500)
{life--;ac1.play();y[i]=5;}
y[i] =3;
if (life==0) System.exit(0);
}
y[9] =3;
md.setTitle("打字训练室你的生命为" life "提示:白色的字母表示未按,粉红色的表示已按.");
}
public void fk() //防空程序,用来击毁落下的炸弹
{ int i=0,j=0;
for (i=0;i10;i)
{if (!fj[i].equals(""))
{
for (j=0;j10;j)
if (fj[i].equals(pd[j]))
{if (!(x1[i]=x[j]x1[i]=x[j] 32 || x1[i] 32=x[j]x1[i] 32=x[j] 32))
if (x1[i]x[j])
x1[i]-=4;
else x1[i] =4;
if (!(y1[i]=y[j]y1[i]=y[j] 32 || y1[i] 32=y[j]y1[i] 32=y[j] 32))
if (y1[i]y[j])
y1[i]-=3;
else y1[i] =4;
if (y[j]=380Math.abs(x[j]-x1[i])5)
if (x[j]x1[i]) x1[i] =5;
else if (x[j]x1[i]) x1[i]-=5;
if (y[j]=380Math.abs(y[j]-y1[i])5)
if (y[j]y1[i]) y1[i] =5;
else if (y[j]y1[i]) y1[i]-=5;
if ((x1[i]=x[j]x1[i]=x[j] 32 || x1[i] 32=x[j]x1[i] 32=x[j] 32)(y1[i]=y[j]y1[i]=y[j] 32 || y1[i] 32=y[j]y1[i] 32=y[j] 32))
{life;ac2.play();pd[j]=noRepeat(); x[j]=noHit(i,10); y[j]=5; x1[i]=x2[i];y1[i]=550;fj[i]="";}
break;
}
if (j==10)y1[i]-=3;
if (y1[i]=5) {y1[i]=550; fj[i]="";}
}
}
}
public void paint(Graphics g)
//根据相应的坐标在屏幕上画炸弹和吃炸弹的娃娃,并保证白色的字母表示未按,粉红色的表示已按
{int i=0,j=0;
g.setFont(new Font("Vrinda",1,21));
for(;ipd.length;i)
{ for (j=0;jfj.length;j)
if (pd[i].equals(fj[j]))
{g.setColor(new Color(255,93,55));
g.drawImage(paodan,x[i],y[i],this);
g.drawString(pd[i],x[i] 11,y[i] 20);
break;
}
if (j==fj.length)
{g.setColor(new Color(255,255,255));
g.drawImage(paodan,x[i],y[i],this);
g.drawString(pd[i],x[i] 11,y[i] 20);
}
g.setColor(new Color(255,93,55));
g.drawImage(huojian,x1[i],y1[i],this);
g.drawString(fj[i],x1[i] 10,y1[i] 22);
}
}
//没有按的显红色,按了的显白色
public void keyPressed(KeyEvent k) //当用户按的是字母时,就生成一个防空字母.
{char z=k.getKeyChar();
int i=0,j=0;
if ((z='A'z='Z') || (z='a'z='z'))
{if (z-32=65) z-=32;
String zc="" z;
for(;ifj.length;i)
{if (zc.equals(fj[i])) break;
} //保证字母不重复
if (i==fj.length)
for(i=0;ifj.length;i)
if (fj[i].equals("")) {fj[i]=zc;break;}
}
}
public void keyReleased(KeyEvent k){}
public void keyTyped(KeyEvent k){}
public String noRepeat() //生成一个不重复的字母.
{int zc,i;
String xinx;
while(true)
{zc=(int)(Math.random()*26);
xinx=Zi26.substring(zc,zc 1);
for(i=0;i10;i)
if (pd[i].equals(xinx))break;
【java做打字游戏的代码 java打字小游戏】if(i==10)return xinx;
}
}
int noHit(int i,int langth) //让字母保持车距.
{int tmpX,j,k,width=32;
while (true)
{ tmpX=(int)(Math.random()*768) 1;
for(j=0;j10;j)
{if (tmpX widthx[j] || tmpXx[j] width) continue; //如果欲生成的位置不与当前J所占的位置相交就判断下一个J.
if ((tmpX=x[j]tmpX=x[j] width) || (tmpX width=x[j]tmpX width=x[j] width)) break;
//如果欲生成的位置与当前x[j]所占据的范围相交就结束循环,重新生成一个起始位置.
}
if (j==10) {return tmpX;}
}
}
public void update(Graphics g)
{if (bgImage==null)
{bgImage=createImage(this.getSize().width,this.getSize().height);
bg=bgImage.getGraphics();
}
bg.setColor(getBackground());
bg.fillRect(0,0,this.getSize().width,this.getSize().height);
//bg.setColor(getForeground());
paint(bg);
g.drawImage(bgImage,0,0,this);
}
public void run() //用线程循环更改Y值,并根据新的坐标在屏幕上重画pd[I];
{
while(true)
{fk();
panduan();
repaint();
try{ myThread.sleep(50);}catch(Exception e) {}
}
}
public static void main(String args[])
{paodan m=new paodan();
md=new JFrame("打字训练室");
md.setSize(800,600);
md.addKeyListener(m);
md.getContentPane().add(m);
m.cushi();
md.setVisible(true);
md.setResizable(false);
myThread=new Thread(m);
myThread.start();
}
}
注:源代码取自编程中国
本人一点建议:其实你这样学Java是没用的,你应该知道你做的项目思想,先做什么后做什么,然后在亲自动手去敲代码!一点建议!
在java中怎样随机生成3个字母 , 在编写打字游戏中使用import java.util.*;
public class RandomChar {
public static void main(String[] args) {
Random random =new Random();
int r = 0;
while(true){
r = random.nextInt(57)65;
if(r90r97||r==0)continue;
break;
}
char a = (char)r;
System.out.print(a);
}
}
它可以随机产生一个字母java做打字游戏的代码,返回的有可能是大写或者小写 。因为大写字母是从65-90小写是从97-122,所以从65-122一共是57个 。
但是91-96这中间的几个不是字母,所以假如取到它们java做打字游戏的代码了的话,就重新取值 。
求一个简单的JAVA游戏代码,100行左右,谢谢!import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Painter extends JFrame{
/**
*
*/
private static final long serialVersionUID = 8160427604782702376L;
CanvasPanel canvas = new CanvasPanel();;
public Painter() {
super("Star");
this.add(canvas);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.pack();
this.setResizable(false);
this.setLocationRelativeTo(null);
this.setVisible(true);
}
public static void main(String[] args) {
new Painter();
}
}
class CanvasPanel extends JPanel implements ActionListener{
/**
*
*/
private static final long serialVersionUID = -4642528854538741028L;
private JButton[] btn = new JButton[4];
private String[] btn_name = {" ", "-", "R", "L"};
private int center_x = 200, center_y = 200, radius = 100, degree = 0;
public CanvasPanel() {
this.setPreferredSize(new Dimension(400, 500));
this.setLayout(null);
for(int i = 0; i4; i) {
btn[i] = new JButton(btn_name[i]);
btn[i].setBounds(160i * 60, 425, 50, 50);
btn[i].addActionListener(this);
this.add(btn[i]);
}
}
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
for(int i = 0; i5; i) {
g.drawLine( (int) (center_xradius * Math.sin(Math.toRadians(degree72 * i))),
(int) (center_y - radius * Math.cos(Math.toRadians(degree72 * i))),
(int) (center_xradius * Math.sin(Math.toRadians(degree72 * i144))),
(int) (center_y - radius * Math.cos(Math.toRadians(degree72 * i144))));
}
}
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if(e.getActionCommand() == " ") {
if(radius200)
radius= 2;
repaint();
} else if(e.getActionCommand() == "-") {
if(radius0)
radius -= 2;
repaint();
} else if(e.getActionCommand() == "R") {
degree = (degree2) % 360;
repaint();
} else if(e.getActionCommand() == "L") {
degree = (degree - 2) % 360;
repaint();
}
}
}
求一个“打字守城堡”游戏的 JAVA源代码import java.applet.Applet;
import java.applet.AudioClip;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Toolkit;
import javax.sound.sampled.AudioFileFormat;
import javax.sound.sampled.AudioSystem;
import javax.swing.JFrame;
import javax.swing.JPanel;
import java.awt.Rectangle;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JOptionPane;
import javax.swing.JSlider;
import javax.swing.JLabel;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.io.File;
import java.util.Vector;
public class Frame extends JFrame implements Runnable {
JPanel contentPane;
JPanel jPanel1 = new JPanel();
JButton jButton1 = new JButton();
JSlider jSlider1 = new JSlider();
JLabel jLabel1 = new JLabel();
JButton jButton2 = new JButton();
JLabel jLabel2 = new JLabel();
int count = 1, rapidity = 80; // count 当前进行的个数, rapidity 游标的位置
int zhengque = 0, cuowu = 0;
int rush[] = { 10 ,20 ,30 };//游戏每关的个数 可以自由添加.列 { 10 ,20 ,30 ,40,50}
int rush_count = 0;//记录关数
char list[] = { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L',
'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y',
'Z', '1', '2', '3', '4', '5', '6', '7', '8', '9' };//随机出现的数字 可以自由添加
Vector number = new Vector();
String paiduan = "true";
AudioClip Musci_anjian, Music_shibai, Music_chenggong;
public Frame() {
try {
setDefaultCloseOperation(EXIT_ON_CLOSE);
//-----------------声音文件---------------------
Musci_anjian = Applet.newAudioClip(new File("sounds//anjian.wav")
.toURL());
Music_shibai = Applet.newAudioClip(new File("sounds//shibai.wav")
.toURL());
Music_chenggong = Applet.newAudioClip(new File(
"sounds//chenggong.wav").toURL());
//---------------------------------------
jbInit();
} catch (Exception exception) {
exception.printStackTrace();
}
}
/**
* Component initialization.
*
* @throws java.lang.Exception
*/
private void jbInit() throws Exception {
contentPane = (JPanel) getContentPane();
contentPane.setLayout(null);
setSize(new Dimension(588, 530));
setTitle("Frame Title");
jPanel1.setBorder(BorderFactory.createEtchedBorder());
jPanel1.setBounds(new Rectangle(4, 4, 573, 419));
jPanel1.setLayout(null);
jButton1.setBounds(new Rectangle(277, 442, 89, 31));
jButton1.setText("开始");
jButton1.addActionListener(new Frame1_jButton1_actionAdapter(this));
jSlider1.setBounds(new Rectangle(83, 448, 164, 21));
jSlider1.setMaximum(100);
jSlider1.setMinimum(1);
jSlider1.setValue(50);
jLabel1.setText("速度");
jLabel1.setBounds(new Rectangle(35, 451, 39, 18));
jButton2.setBounds(new Rectangle(408, 442, 89, 31));
jButton2.setText("结束");
jButton2.addActionListener(new Frame1_jButton2_actionAdapter(this));
jLabel2.setText("第一关:100个");
jLabel2.setBounds(new Rectangle(414, 473, 171, 21));
contentPane.add(jPanel1);
contentPane.add(jButton2);
contentPane.add(jButton1);
contentPane.add(jSlider1);
contentPane.add(jLabel1);
contentPane.add(jLabel2);
this.addKeyListener(new MyListener());
jButton1.addKeyListener(new MyListener());
jSlider1.addKeyListener(new MyListener());
jSlider1.addChangeListener(new ChangeListener() {
public void stateChanged(ChangeEvent e) {
rapidity = jSlider1.getValue();
}
});
}
public void run() {
number.clear();
zhengque = 0;
cuowu = 0;
paiduan = "true";
while (count = rush[rush_count]) {
try {
Thread t = new Thread(new Tthread());
t.start();
count= 1;
Thread.sleep(1000(int) (Math.random() * 2000)); // 生产下组停顿时间
// 最快1快.最慢2秒
} catch (InterruptedException e) {
e.printStackTrace();
}
}
while (true) { // 等待最后一个字符消失
if (number.size() == 0) {
break;
}
}
if (zhengque == 0) { // 为了以后相除..如果全部正确或者错误就会出现错误. 所以..
zhengque = 1;
}
if (cuowu == 0) {
cuowu = 1;
}
if (paiduan.equals("true")) { // 判断是否是自然结束
if (zhengque / cuowu = 2) {
JOptionPane.showMessageDialog(null, "恭喜你过关了");
rush_count= 1; // 自动加1关
if (rush_countrush.length) {
if (rapidity10) { // 当速度大于10的时候在-5提加速度.怕速度太快
rapidity -= 5; // 速度自动减10毫秒
jSlider1.setValue(rapidity); // 选择位置
}
Thread t = new Thread(this);
t.start();
} else {
JOptionPane.showMessageDialog(null, "牛B...你通关了..");
rush_count = 0;
count = 0;
}
} else {
JOptionPane.showMessageDialog(null, "请再接再励");
rush_count = 0;
count = 0;
}
} else {
rush_count = 0;
count = 0;
}
}
public void jButton1_actionPerformed(ActionEvent e) {
Thread t = new Thread(this);
t.start();
}
public void jButton2_actionPerformed(ActionEvent e) {
count = rush[rush_count]1;
paiduan = "flase";
}
class Tthread implements Runnable {
public void run() {
boolean fo = true;
int Y = 0, X = 0;
JLabel show = new JLabel();
show.setFont(new java.awt.Font("宋体", Font.PLAIN, 33));
jPanel1.add(show);
X = 10(int) (Math.random() * 400);
String parameter = list[(int) (Math.random() * list.length)]"";
Bean bean = new Bean();
bean.setParameter(parameter);
bean.setShow(show);
number.add(bean);
show.setText(parameter);
while (fo) {
// ---------------------数字下移--------------------
show.setBounds(new Rectangle(X, Y= 2, 33, 33));
try {
Thread.sleep(rapidity);
} catch (InterruptedException e) {
e.printStackTrace();
}
if (Y = 419) {
fo = false;
for (int i = number.size() - 1; i = 0; i--) {
Bean bn = ((Bean) number.get(i));
if (parameter.equalsIgnoreCase(bn.getParameter())) {
cuowu= 1;
jLabel2.setText("正确:"zhengque"个,错误:"cuowu
"个");
number.removeElementAt(i);
Music_shibai.play();
break;
}
}
}
}
}
}
class MyListener extends KeyAdapter {
public void keyPressed(KeyEvent e) {
String uu = e.getKeyChar()"";
for (int i = 0; inumber.size(); i) {
Bean bean = ((Bean) number.get(i));
if (uu.equalsIgnoreCase(bean.getParameter())) {
zhengque= 1;
number.removeElementAt(i);
bean.getShow().setVisible(false);
jLabel2.setText("正确:"zhengque"个,错误:"cuowu"个");
Music_chenggong.play();
break;
}
}
Musci_anjian.play();
}
}
public static void main(String[] args) {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (Exception exception) {
exception.printStackTrace();
}
Frame frame = new Frame();
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
Dimension frameSize = frame.getSize();
if (frameSize.heightscreenSize.height) {
frameSize.height = screenSize.height;
}
if (frameSize.widthscreenSize.width) {
frameSize.width = screenSize.width;
}
frame.setLocation((screenSize.width - frameSize.width) / 2,
(screenSize.height - frameSize.height) / 2);
frame.setVisible(true);
}
}
class Frame1_jButton2_actionAdapter implements ActionListener {
private Frame adaptee;
Frame1_jButton2_actionAdapter(Frame adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e) {
adaptee.jButton2_actionPerformed(e);
}
}
class Frame1_jButton1_actionAdapter implements ActionListener {
private Frame adaptee;
Frame1_jButton1_actionAdapter(Frame adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e) {
adaptee.jButton1_actionPerformed(e);
}
}
class Bean {
String parameter = null;
JLabel show = null;
public JLabel getShow() {
return show;
}
public void setShow(JLabel show) {
this.show = show;
}
public String getParameter() {
return parameter;
}
public void setParameter(String parameter) {
this.parameter = parameter;
}
}
求大神求大神本周之前,要怎么改?。坷肑ava多线程机制编写一个汉字打字游戏 。主线程中创建一个Fr三个类:MainFrame WordThread 和ScoreThread
import java.awt.BorderLayout;
import java.awt.GridLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class MainFrame extends JFrame {
//全局变量
public JPanel jPanel = new JPanel();
public JLabel jLabel = new JLabel();
public JTextField jTextField = new JTextField();
public JLabel jLabel2 = new JLabel("分数:");//得分

public MainFrame(){
//构造方法
jPanel.setLayout(new GridLayout(2, 2));
jPanel.add(jLabel);
jPanel.add(jTextField);
jPanel.add(jLabel2);
this.add(jPanel);

//启动线程,每2秒生成一个汉字
new WordThread(this).start();
new ScoreThread(this).start();
}
/**
* 显示
* @param word
*/
public void showWord(String word){
jLabel.setText(word);
}
public JPanel getjPanel() {
return jPanel;
}
public void setjPanel(JPanel jPanel) {
this.jPanel = jPanel;
}
public JLabel getjLabel() {
return jLabel;
}
public void setjLabel(JLabel jLabel) {
this.jLabel = jLabel;
}
public JTextField getjTextField() {
return jTextField;
}
public void setjTextField(JTextField jTextField) {
this.jTextField = jTextField;
}
public JLabel getjLabel2() {
return jLabel2;
}
public void setjLabel2(JLabel jLabel2) {
this.jLabel2 = jLabel2;
}
}
------------------------
public class WordThread extends Thread {
private MainFrame mainFrame;
private char word;
private int k = 19968;
public WordThread(MainFrame mainFrame){
this.mainFrame = mainFrame;
}
public void run() {
while(true){
if(k29968) k = 19968;
word = (char)k;
mainFrame.showWord("" word);
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
-------------------
public class ScoreThread extends Thread {
private MainFrame mainFrame;
public ScoreThread(MainFrame mainFrame){
this.mainFrame = mainFrame;
}
public void run(){
while(true){
//获取汉字,对比,并进行比较,给出得分
String word = mainFrame.getjLabel().getText();
//输入的汉字
String inputWord = mainFrame.getjTextField().getText();
if(null != inputWord!"".equals(inputWord)){
if(inputWord.equals(word)){
mainFrame.getjLabel2().setText("得分:100");
}else{
mainFrame.getjLabel2().setText("得分:0");
}
}
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
求大神用javaEE写一个简单的打字游戏的源代码,越简单越好,最好有一些注释 , 万分感谢 。您好java做打字游戏的代码,附件里面是源码java做打字游戏的代码!
java做打字游戏的代码的介绍就聊到这里吧 , 感谢你花时间阅读本站内容,更多关于java打字小游戏、java做打字游戏的代码的信息别忘了在本站进行查找喔 。

    推荐阅读