此项目为Java课程设计,在原来打字游戏的基础上加上了网络编程部分,通过特定语句"开始游戏"来实现游戏的开启。

package GAME;import java.io.IOException;
import java.net.*;public class Server {public static void main(String[] args) throws IOException {// TODO code application logic hereServerSocket ss=new ServerSocket(10000);        //创建服务器端套接字,指定该服务器程序的端口。while(true){Socket soc=null;        //客户端套接字对象,初始化为空。soc=ss.accept();        //服务器接收到客户端连接,返回该客户端对象,此方法会一直阻塞,直到接收到客户端的连接。if(soc!=null){      //客户端连接DealWithEveryClient dec=new DealWithEveryClient(soc);   //为每个客户端开启一个聊天窗口。dec.start();        //开启此线程}}}}
package GAME;import java.awt.*;
import java.awt.event.*;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import javax.swing.*;
import java.net.*;
import java.util.logging.Level;
import java.util.logging.Logger;
/*** 1、创建服务器ServerSocket对象(指定端口),服务器在正常运行时处于永久开启状态(while(true))。* 2、调用ServerSocket的方法接受客户端的连接请求。* 3、创建一个线程类,每当服务器接收到了一个客户端连接请求并成功连接时,为这个客户端打开一个聊天窗口。* 4、通过获取的客户端对象,获取与客户端进行数据通信的网络IO流。* 5、在处理客户端的线程run方法里开启另一种线程,用来接收客户端发来的信息。* 6、通过事件监听机制把文本框中的消息打包成字节数组,通过网络输出流写到网络中,由客户端读入。* 7、当客户端断开连接时,服务器断开与客户端的连接。***/
public class ServerFrame extends JFrame implements ActionListener,Runnable{Socket soc; //客户端对象,服务器接收到的客户端。JTextField jf;JTextArea jta;JButton jb;JScrollPane jsp;InputStream in;     //网络字节输入流。OutputStream out;       //网络字节输出流。public ServerFrame(Socket soc) throws IOException{super("服务器端聊天");this.soc=soc;       //传递服务器接收到的客户端套接字。in=soc.getInputStream();    //拿到客户端输入流out=soc.getOutputStream();  //拿的是客户端的输出流jf=new JTextField(20);jta=new JTextArea(20,20);jb=new JButton("发送");jsp=new JScrollPane(jta);this.setLayout(new FlowLayout());   //流式布局this.add(jf);this.add(jb);this.add(jsp);jb.addActionListener(this);     //监听器jf.addActionListener(this);this.setBounds(150,210,400,400);this.setVisible(true);this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);    //设置窗体默认关闭操作。}public void actionPerformed(ActionEvent e){String jfText=jf.getText();     //获取文本框中的内容if(jfText.length()>0){      //获取文本框字符串长度byte[] by=jfText.getBytes();        //将字符串变为字节数组try {out.write(by);      //将字节数组写入网络输出流中,由客户端来接收。jta.append("你说:"+jfText+"\n");   //显示文本jf.setText("");     //清空} catch (IOException ex) {Logger.getLogger(ServerFrame.class.getName()).log(Level.SEVERE, null, ex);  //一种异常处理,不必深究。}}}public void run(){while(true){byte[] by=new byte[1024];   //用来接收客户端发来的消息。try {int count=in.read(by);      //用网络输入流读取来自客户端的消息,返回读取的有效字节个数。jta.setText("客户端说:"+new String(by,0,count)+"\n");  //将客户端发来的消息显示在文本区中。if(new String(by,0,count).equals("开始游戏")){new UIJfram("网络打字游戏");this.setVisible(false);}} catch (IOException ex) {Logger.getLogger(ServerFrame.class.getName()).log(Level.SEVERE, null, ex);  //一种异常处理,不必深究。}}}}
package GAME;
import java.io.IOException;public class Client {public static void main(String[] args) throws IOException {// TODO code application logic hereClientFrame cf=new ClientFrame();Thread reciver=new Thread(cf);reciver.start();}}
package GAME;import java.awt.*;
import java.awt.event.*;
import java.io.*;
import javax.swing.*;
import java.net.*;
import java.util.logging.Level;
import java.util.logging.Logger;/**** 1、创建客户端Socket对象,指定要连接的服务器IP和端口号。* 2、建立连接后,通过Socket的方法获取网络IO流。* 3、通过事件监听机制把文本框中的消息打包成字节数组,通过网络输出流写到网络中,由服务器读入。* 4、事先开启一个线程,通过网络输入流,接收来自服务器的消息,并显示在聊天文本区域。* 5、当聊天窗口关闭时,断开与服务器的连接。**/
public class ClientFrame extends JFrame implements ActionListener,Runnable{Socket soc; //客户端套接字JTextField jf;JTextArea jta;JButton jb;JScrollPane jsp;InputStream in;     //输入流OutputStream out;   //输出流public ClientFrame() throws IOException{super("客户端聊天框");soc=new Socket("127.0.0.1",10000);  //实例化客户端套接字,指定要连接的服务器程序的IP和端口in=soc.getInputStream();    //接收数据out=soc.getOutputStream();  //发送数据jf=new JTextField(20);      //文本框,设置容量为20个字符。jta=new JTextArea(20,20);   //文本区域jb=new JButton("发送");jsp=new JScrollPane(jta);this.setLayout(new FlowLayout());   //流式布局this.add(jf);this.add(jb);this.add(jsp);jb.addActionListener(this); //按钮事件监听器jf.addActionListener(this); //文本框事件监听器this.setBounds(300,300,400,400);this.setVisible(true);this.setLocationRelativeTo(null);this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);    //设置窗体默认关闭操作。}public void actionPerformed(ActionEvent e){     //监听器String jfText=jf.getText(); //获取文本框中的内容。if(jfText.length()>0){  //字符串的长度必须大于0byte[] by=jfText.getBytes();    //将字符串变为字节数组。if(jfText.equals("开始游戏")){this.setVisible(false);}try {out.write(by);  //将字节数组写入网络输出流中,由服务器来接收。jta.append("你说:"+jfText+"\n");   //显示文本jf.setText("");     //置空} catch (IOException ex) {Logger.getLogger(ClientFrame.class.getName()).log(Level.SEVERE, null, ex);//一种异常处理}}}public void run(){while(true){byte[] b=new byte[1024];    //用来接收服务器发来的消息。try {int count=in.read(b);   //用网络输入流读取来自服务器的消息,返回读取的有效字节个数。jta.append("服务器说:"+new String(b,0,count)+"\n");    //显示文本} catch (IOException ex) {Logger.getLogger(ClientFrame.class.getName()).log(Level.SEVERE, null, ex);  //一种异常处理}}}}
package GAME;import java.io.IOException;
import java.net.*;
import java.util.logging.Level;
import java.util.logging.Logger;/*** 这个线程类是为了每当有一个客户端连接时,为这个客户端开启一个聊天窗口。*/
public class DealWithEveryClient extends Thread{Socket soc;     //客户端对象,服务器接收到的客户端。public DealWithEveryClient(Socket soc){this.soc=soc;       //传递服务器接收到的客户端套接字。}public void run(){try {ServerFrame sf=new ServerFrame(soc);    //创建一个服务器聊天窗口Thread thread=new Thread(sf);       //实例化接收客户端消息的线程对象。thread.start();     //开启该线程。} catch (IOException ex) {Logger.getLogger(DealWithEveryClient.class.getName()).log(Level.SEVERE, null, ex);      //一种异常处理,不必深究。}}
}
package GAME;import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
public class UIJfram extends JFrame {int width =800;int height =800;Font font=new Font("腾祥范笑歌楷书简繁合集",Font.ITALIC+Font.BOLD,50);JLabel jbstart=new JLabel("开始游戏");JLabel jbexit=new JLabel("退出游戏");JLabel jbregard=new JLabel("关于");JLabel jbchongzhi=new JLabel("充值入口");public UIJfram(String Text){super(Text);this.setLayout(null);this.setBounds(0,0,width,height);this.setResizable(false);this.setLocationRelativeTo(null);ImageIcon logo=new ImageIcon("D:\\Typing game\\iamge\\chuangtilogo.png");//获取logosetIconImage(logo.getImage());//设置logoImageIcon background=new ImageIcon("D:\\Typing game\\iamge\\beijingshouye.jpg");//获取背景JLabel bg=new JLabel(background);bg.setBounds(0,0,width,height);//设置标签JPanel jPanelbg=(JPanel) this.getContentPane();//将窗口转换为jpaneljPanelbg.setOpaque(false);this.getLayeredPane().setLayout(null);this.getLayeredPane().add(bg,new Integer(Integer.MIN_VALUE));//将最底层设为背景Title title=new Title();this.add(title);Thread thread=new Thread(title);thread.start();button();add_monitor();this.setVisible(true);}public void button(){//设置“开始游戏“的字体颜色、字体、位置jbstart.setForeground(SystemColor.orange);jbstart.setFont(font);jbstart.setBounds(315,50,width/2,height/3);this.add(jbstart);//设置“退出游戏“的字体颜色、字体、位置jbexit.setForeground(SystemColor.MAGENTA);jbexit.setFont(font);jbexit.setBounds(315,250,width/2,height/3);this.add(jbexit);//设置“退出游戏“的字体颜色、字体、位置jbregard.setForeground(SystemColor.red);jbregard.setFont(font);jbregard.setBounds(350,450,width/2,height/3);this.add(jbregard);//充值jbchongzhi.setForeground(Color.GREEN);jbchongzhi.setFont(new Font("楷体",Font.BOLD,20));jbchongzhi.setBounds(650,700,100,100);this.add(jbchongzhi);}public void add_monitor(){jbstart.addMouseListener(new MouseListener() {@Overridepublic void mouseClicked(MouseEvent e) {jsilder jsilder=new jsilder();setVisible(false);}@Overridepublic void mousePressed(MouseEvent e) {}@Overridepublic void mouseReleased(MouseEvent e) {}@Overridepublic void mouseEntered(MouseEvent e) {jbstart.setForeground(Color.BLACK);}@Overridepublic void mouseExited(MouseEvent e) {jbstart.setForeground(SystemColor.orange);}});jbexit.addMouseListener(new MouseListener() {@Overridepublic void mouseClicked(MouseEvent e) {System.exit(0);}@Overridepublic void mousePressed(MouseEvent e) {}@Overridepublic void mouseReleased(MouseEvent e) {}@Overridepublic void mouseEntered(MouseEvent e) {jbexit.setForeground(Color.black);}@Overridepublic void mouseExited(MouseEvent e) {jbexit.setForeground(SystemColor.MAGENTA);}});jbregard.addMouseListener(new MouseListener() {@Overridepublic void mouseClicked(MouseEvent e) {Rule rule=new Rule();}@Overridepublic void mousePressed(MouseEvent e) {}@Overridepublic void mouseReleased(MouseEvent e) {}@Overridepublic void mouseEntered(MouseEvent e) {jbregard.setForeground(Color.orange);}@Overridepublic void mouseExited(MouseEvent e) {jbregard.setForeground(SystemColor.red);}});jbchongzhi.addMouseListener(new MouseListener() {@Overridepublic void mouseClicked(MouseEvent e) {chongzhi chongzhi=new chongzhi("充值中心");}@Overridepublic void mousePressed(MouseEvent e) {}@Overridepublic void mouseReleased(MouseEvent e) {}@Overridepublic void mouseEntered(MouseEvent e) {}@Overridepublic void mouseExited(MouseEvent e) {}});}/*在一个面板上实现标题自动下落*/class Title extends JPanel implements Runnable {int width1 = 800;int height1 = 150;int N = 4;int[] x = new int[N];//存储标题中的每个字的横坐标int[] y = new int[N];//存储标题中的每个字的纵坐标String[] strs = new String[]{"打", "字", "游", "戏"};Title() {setBounds(0, 0, width1, height1);//设置面板大小setOpaque(false);//透明setplace();//设置标题每个字初始的横纵坐标}void setplace() {for (int i = 0; i < N; i++) {x[i] = (int) (width1 * 0.15 + i * 0.2 * width1);y[i] = 10;}}@Overridepublic void paint(Graphics g) {super.paint(g);g.setColor(Color.PINK);//设置画笔颜色为粉色g.setFont(new Font("华文行楷", Font.PLAIN+Font.BOLD, 100));//设置画笔字体for (int i = 0; i < N; i++) {g.drawString(strs[i], x[i], y[i]);//在指定位置画出标题的字y[i]++;//标题的字纵坐标下移一像素if (y[i] > height1-20 ) {//如果到达height-20,则保持在那个位置y[i] = height1-20 ;}}}@Overridepublic void run() {while (true) {try {Thread.sleep(10);//实现每10毫秒重绘一次} catch (InterruptedException e) {e.printStackTrace();}repaint();//调用重绘函数}}}}
package GAME;import javax.swing.*;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Hashtable;public class jsilder extends JFrame {private static int difficultyflag;private static int  lifeflag;private static int Level;public jsilder(){this.setTitle("难度和生命值选择");this.setSize(350,200);this.setLocationRelativeTo(null);this.setResizable(false);this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);FlowLayout flowLayout=new FlowLayout();this.setLayout(flowLayout);jpanl();jboutto();this.setVisible(true);setDifficultyflag(0);}public static int getLifeflag() {return lifeflag;}public static void setLifeflag(int lifeflag) {jsilder.lifeflag = lifeflag;}public static int getLevel() {return Level;}public static void setLevel(int level) {Level = level;}public void jpanl(){JPanel jPanel=new JPanel();//jPanel.setLayout(new GridLayout(1,2,0,0));final JSlider slider=new JSlider(1,3,1);slider.setMajorTickSpacing(1);slider.setPaintTicks(true);slider.setPaintLabels(true);slider.setOrientation(SwingConstants.CENTER);Hashtable<Integer,JComponent> hashtable=new Hashtable<Integer,JComponent>();hashtable.put(1,new JLabel("1"));hashtable.put(2,new JLabel("2"));hashtable.put(3,new JLabel("3"));slider.setLabelTable(hashtable);slider.addChangeListener(new ChangeListener() {@Overridepublic void stateChanged(ChangeEvent e) {System.out.println("当前值:"+slider.getValue());setDifficultyflag(slider.getValue());setLevel(slider.getValue());}});JLabel jLabel=new JLabel("难度等级:");jLabel.setFont(new Font("楷体",Font.BOLD,20));jLabel.setBounds(0,10,100,100);jPanel.add(jLabel);jPanel.add(slider);this.add(jPanel);}public void jboutto(){ButtonGroup buttonGroup=new ButtonGroup();//新建按钮实现互斥JRadioButton one=new JRadioButton("1",true);JRadioButton two=new JRadioButton("2",false);JRadioButton three=new JRadioButton("3",false);buttonGroup.add(one);buttonGroup.add(two);buttonGroup.add(three);JLabel jLabel=new JLabel("生命值:");jLabel.setFont(new Font("楷体",Font.BOLD,20));jLabel.setBounds(10,20,100,100);JPanel chooselife=new JPanel();chooselife.setLayout(new GridLayout(2,2,15,10));chooselife.setBounds(5,50,50,50);chooselife.add(jLabel);chooselife.add(one);chooselife.add(three);one.addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent e) {lifeflag=Integer.valueOf(one.getText());}});two.addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent e) {lifeflag=Integer.valueOf(two.getText());}});three.addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent e) {lifeflag=Integer.valueOf(three.getText());}});JButton jButton1=new JButton("返回主界面");jButton1.setBounds(0,100,50,50);chooselife.add(jButton1);jButton1.addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent e) {new UIJfram("网络打字游戏");setVisible(false);}});JButton jButton2=new JButton("进入游戏");jButton2.setBounds(50,100,50,50);chooselife.add(jButton2);jButton2.addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent e) {new Game();setVisible(false);}});this.add(chooselife);}public int getDifficultyflag() {return difficultyflag;}public void setDifficultyflag(int difficultyflag) {this.difficultyflag = difficultyflag;}}
package GAME;import javax.swing.*;
import java.awt.*;public class Rule extends JFrame {public Rule() {setruleJF();}public void setruleJF(){this.setTitle("规则");JLabel text1 = new JLabel("<html><body>"+"基本规则:点击开始游戏后可以选择选卡和生命值,确认后游戏正式开始游戏开始后会自动下落三个"+"数,通过敲击键盘来消除这些数," +"得分增加,并产生新数字,当数字掉落到屏幕底部时生命值减一,生命值为0游戏结束。" +"<br>"+"<br>"+"难度介绍:游戏难度会随着得分的增加而自动增加,同时也可自选关卡难度"+"<br>"+"<br>"+"<br>"+"<br>"+"好好享受吧!"+"</body></html>");text1.setVerticalAlignment(JLabel.NORTH);//使其文本位于JLabel顶部text1.setFont(new Font("华文行楷", Font.PLAIN, 20));ImageIcon logo=new ImageIcon("D:\\Typing game\\iamge\\rule.png");//获取logosetIconImage(logo.getImage());//设置logothis.add(text1);//显示规则的窗口this.setResizable(false);this.setSize(800, 300);this.setLocationRelativeTo(null);this.setVisible(true);}}
package GAME;import javax.swing.*;
import java.awt.*;public class chongzhi extends JFrame{JLabel ch=new JLabel("快速充值");public chongzhi(String text){super(text);this.setLayout(null);this.setBounds(0,0,500,500);this.setResizable(false);this.setLocationRelativeTo(null);ImageIcon logo=new ImageIcon("D:\\Typing game\\iamge\\moneylogo.png");//获取logosetIconImage(logo.getImage());//设置logoImageIcon background=new ImageIcon("D:\\Typing game\\iamge\\erweima.jpg");//获取背景JLabel bg=new JLabel(background);bg.setBounds(100,100,300,300);//设置标签JPanel jPanelbg=(JPanel) this.getContentPane();//将窗口转换为jpaneljPanelbg.setOpaque(false);this.getLayeredPane().setLayout(null);this.getLayeredPane().add(bg,new Integer(Integer.MIN_VALUE));button();this.setVisible(true);}public void button(){ch.setForeground(Color.orange);ch.setFont(new Font("方正舒体",Font.BOLD,50));ch.setBounds(150,-50,250,250);this.add(ch);}}
package GAME;import javax.swing.*;
import java.awt.*;public class Game extends JFrame{static jsilder jsilder1=new jsilder();public static int life=jsilder1.getLifeflag();public static int Level=jsilder.getLevel();public Game(){setTitle("---***GAME***---");if(life==0)life++;if (Level==0)Level++;switch (Level){case 1:WordPanel1 panel = new WordPanel1();this.add(panel);Thread t = new Thread(panel);t.start();panel.addKeyListener(panel);panel.setFocusable(true);break;case 2:WordPanel panel1=new WordPanel();this.add(panel1);Thread t1 = new Thread(panel1);t1.start();panel1.addKeyListener(panel1);panel1.setFocusable(true);break;case 3:WordPanel2 panel2=new WordPanel2();this.add(panel2);Thread t2 = new Thread(panel2);t2.start();panel2.addKeyListener(panel2);panel2.setFocusable(true);break;}this.setResizable(false);this.setSize(800, 600);this.setLocationRelativeTo(null);this.setBackground(Color.cyan);this.setDefaultCloseOperation(3);this.setVisible(true);}}
package GAME;import javax.swing.*;
import java.awt.*;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;import static GAME.Game.life;public class WordPanel extends JPanel implements Runnable, KeyListener {boolean gameover=true;int[] xx = new int[10];int[] yy = new int[10];char[] words = new char[10];Color[] colors = new Color[10];int score = 0;int speed = 10;long time;ImageIcon icon = new ImageIcon("D:\\Typing game\\iamge\\beijignyouxijiemian.jpg");public WordPanel() {for (int i = 0; i < 3; ++i) {this.xx[i] = (int) (Math.random() * 780);this.yy[i] = 10;this.colors[i] = this.randomColor();this.words[i] = (char) ((int) (Math.random() * 26 + 65));}}public Color randomColor() {int R = (int) (Math.random() * 255);int G = (int) (Math.random() * 255);int B = (int) (Math.random() * 255);Color color = new Color(R, G, B);return color;}public void paint(Graphics g) {super.paint(g);icon.paintIcon(this,g,0,0);Font ft = new Font("微软雅黑", 1, 28);g.setFont(ft);g.drawString("分数:" + this.score, 50, 500);for (int i = 0; i < 3; ++i) {g.setColor(this.colors[i]);g.drawString(String.valueOf(this.words[i]), this.xx[i], this.yy[i]);}g.drawString("生命值:" + life, 50, 550);if (gameover==false) {//游戏结束g.setColor(Color.RED);g.setFont(new Font("宋体", Font.BOLD, 35));g.drawString("游戏结束!", 200, 200);g.drawString("您的分数为"+score,200,250);}}public void run() {while (gameover) {for (int i = 0; i < 3; ++i) {this.yy[i]++;if(this.yy[i]>500&&life>0){this.yy[i] = 0;life--;if(life==0)gameover=false;}if(this.yy[i]>500){this.yy[i] = 0;}}try {Thread.sleep((long) this.speed);} catch (InterruptedException var2) {var2.printStackTrace();}this.repaint();}}public void keyTyped(KeyEvent e) {}public void keyPressed(KeyEvent e) {for (int i = 0; i < 3; ++i) {if (e.getKeyChar() == this.words[i]) {this.xx[i] = (int) (Math.random() * 780);this.yy[i] = 0;this.words[i] = (char) ((int) (Math.random() * 26 + 65));++this.score;break;}}if (this.score > 10 && this.score < 20) {this.speed = 10;} else if (this.score > 20) {this.speed = 5;}this.repaint();}public void keyReleased(KeyEvent e) {}
}
package GAME;import javax.swing.*;
import java.awt.*;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;import static GAME.Game.life;public class WordPanel1 extends JPanel implements Runnable , KeyListener {boolean gameover=true;int[] xx = new int[10];int[] yy = new int[10];char[] words = new char[10];Color[] colors = new Color[10];int score = 0;int speed = 10;long time;ImageIcon icon = new ImageIcon("D:\\Typing game\\iamge\\beijignyouxijiemian.jpg");public WordPanel1() {for (int i = 0; i < 3; ++i) {this.xx[i] = (int) (Math.random() * 780);this.yy[i] = 10;this.colors[i] = this.randomColor();this.words[i] = (char) ((int) (Math.random() * 10 + 48));}}public Color randomColor() {int R = (int) (Math.random() * 255);int G = (int) (Math.random() * 255);int B = (int) (Math.random() * 255);Color color = new Color(R, G, B);return color;}public void paint(Graphics g) {super.paint(g);icon.paintIcon(this,g,0,0);Font ft = new Font("微软雅黑", 1, 28);g.setFont(ft);g.drawString("分数:" + this.score, 50, 500);for (int i = 0; i < 3; ++i) {g.setColor(this.colors[i]);g.drawString(String.valueOf(this.words[i]), this.xx[i], this.yy[i]);}g.drawString("生命值:" + life, 50, 550);if (gameover==false) {//游戏结束g.setColor(Color.RED);g.setFont(new Font("宋体", Font.BOLD, 35));g.drawString("游戏结束!", 200, 200);g.drawString("您的分数为"+score,200,250);}}@Overridepublic void keyTyped(KeyEvent e) {}@Overridepublic void keyPressed(KeyEvent e) {for (int i = 0; i < 3; ++i) {if (e.getKeyChar() == this.words[i]) {this.xx[i] = (int) (Math.random() * 780);this.yy[i] = 0;this.words[i] = (char) ((int) (Math.random() * 10 + 48));++this.score;break;}}if (this.score > 10 && this.score < 20) {this.speed = 10;} else if (this.score > 20) {this.speed = 5;}this.repaint();}@Overridepublic void keyReleased(KeyEvent e) {}@Overridepublic void run() {while (gameover) {for (int i = 0; i < 3; ++i) {this.yy[i]++;if(this.yy[i]>500&&life>0){this.yy[i] = 0;life--;if(life==0)gameover=false;}if(this.yy[i]>500){this.yy[i] = 0;}}try {Thread.sleep((long) this.speed);} catch (InterruptedException var2) {var2.printStackTrace();}this.repaint();}}
}
package GAME;import javax.swing.*;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;import java.awt.*;import static GAME.Game.life;
public class WordPanel2 extends JPanel implements Runnable, KeyListener {ImageIcon icon = new ImageIcon("D:\\Typing game\\iamge\\beijignyouxijiemian.jpg");boolean gameover=true;int[] xx = new int[10];int[] yy = new int[10];String randomcode2 = "";String model = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";char[] m = model.toCharArray();char[] words = new char[10];Color[] colors = new Color[10];int score = 0;int speed = 10;long time;public WordPanel2() {for (int i = 0; i < 3; ++i) {this.xx[i] = (int) (Math.random() * 780);this.yy[i] = 10;this.colors[i] = this.randomColor();for (int j=0;j<3 ;j++ ) {this.words[i] = m[(int) (Math.random() * 52)];}}}public Color randomColor() {int R = (int) (Math.random() * 255);int G = (int) (Math.random() * 255);int B = (int) (Math.random() * 255);Color color = new Color(R, G, B);return color;}public void paint(Graphics g) {super.paint(g);icon.paintIcon(this,g,0,0);Font ft = new Font("微软雅黑", 1, 28);g.setFont(ft);g.drawString("分数:" + this.score, 50, 500);for (int i = 0; i < 3; ++i) {g.setColor(this.colors[i]);g.drawString(String.valueOf(this.words[i]), this.xx[i], this.yy[i]);}g.drawString("生命值:" + life, 50, 550);if (gameover==false) {//游戏结束g.setColor(Color.RED);g.setFont(new Font("宋体", Font.BOLD, 35));g.drawString("游戏结束!", 200, 200);g.drawString("您的分数为"+score,200,250);}}@Overridepublic void keyTyped(KeyEvent e) {}@Overridepublic void keyPressed(KeyEvent e) {for (int i = 0; i < 3; ++i) {if (e.getKeyChar()== this.words[i]) {this.xx[i] = (int) (Math.random() * 780);this.yy[i] = 0;this.words[i] = m[(int) (Math.random() * 52)];++this.score;break;}}if (this.score > 10 && this.score < 20) {this.speed = 10;} else if (this.score > 20) {this.speed = 5;}this.repaint();}@Overridepublic void keyReleased(KeyEvent e) {}@Overridepublic void run() {while (gameover) {for (int i = 0; i < 3; ++i) {this.yy[i]++;if(this.yy[i]>500&&life>0){this.yy[i] = 0;life--;if(life==0)gameover=false;}if(this.yy[i]>500){this.yy[i] = 0;}}try {Thread.sleep((long) this.speed);} catch (InterruptedException var2) {var2.printStackTrace();}this.repaint();}}}

Java课程设计网络打字游戏相关推荐

  1. JAVA课程设计(小游戏贪吃蛇)完整源码附素材(二)

    目录 JAVA课程设计(小游戏贪吃蛇)完整源码附素材(一) JAVA课程设计(小游戏贪吃蛇)完整源码附素材(二) JAVA课程设计(小游戏贪吃蛇)完整源码附素材(三) 前言 1. 任务描述 1.1  ...

  2. java猜数字游戏总结,java课程设计——猜数字游戏

    java课程设计--猜数字游戏 目目 录录 前言. 1 正文. 1 1 1.设计任务与要求.设计任务与要求 1 1.1 1.1 设计任务与要求设计任务与要求 1 1.2 1.2 选题目的与意义选题目的 ...

  3. java课程设计 猜数游戏 个人

    1.团队课程设计博客链接 /http://www.cnblogs.com/ohanna/p/7064305.html 2.个人负责模块说明 1.进入界面: 2.成功玩家信息的录入: 3.信息的读出 3 ...

  4. java课程设计——扫雷小游戏

    1.项目简介 采用java swing设计扫雷游戏软件,设计目标如下: 扫雷游戏分为初级.中级和高级三个级别,初级模式9*9个方块中有10个雷.中级模式16*16个方块中有40个雷.高级模式16*30 ...

  5. JAVA课程设计——华容道小游戏

    华容道游戏简介: 华容道,古老的中国游戏,以其变化多端.百玩不厌的特点与魔方.独立钻石棋一起被国外智力专家并称为"智力游戏界的三个不可思议".它与七巧板.九连环等中国传统益智玩具还 ...

  6. java课程设计之小游戏贪吃蛇

    "贪吃蛇"小游戏的原理(效果图如下) 下代码为页面设置(gui) import java.awt.Graphics;import javax.swing.JFrame;public ...

  7. java课程设计五子棋小游戏(1)

    1. 前言 该项目为经典版本的五子棋游戏和自创的毁灭玩法所结合,总体而言是一个休闲的小游戏.其中的规则不难,主要是为了丰富大家的文娱生活,让大家在忙碌的学习课后可以轻松一小下.这就是本程序的编写初衷. ...

  8. java蜘蛛纸牌课程设计_Java课程设计-蜘蛛纸牌游戏.doc

    Java课程设计-蜘蛛纸牌游戏.doc 还剩 33页未读, 继续阅读 下载文档到电脑,马上远离加班熬夜! 亲,很抱歉,此页已超出免费预览范围啦! 如果喜欢就下载吧,价低环保! 内容要点: Java 课 ...

  9. java中国象棋网络对弈,java课程设计---中国象棋对弈系统

    java课程设计---中国象棋对弈系统 1 目目 录录 摘要 1 关键字 1 正文 2 1.程序设计说明. 2 1.1 程序的设计及实现 2 1.1.1搜索引擎的实现(engine包) . 2 1.1 ...

最新文章

  1. jQuery的deferred对象详解
  2. hashlist java_java中集合类HashSet、ArrayList、LinkedList总结
  3. Python多任务(7.多进程的应用:文件的拷贝器例子)
  4. 系统架构设计师证书含金量_计算机专科生不能错过的两个证书,含金量比较高,出社会有益...
  5. 联想u盘linux安装教程,联想笔记本用U盘安装 winXP系统教程
  6. 牛客题霸 [分糖果问题] C++题解/答案
  7. SpringBoot集成MongoDB
  8. wireshark抓组播数据_HCIE学习笔记--组播路由协议PIM-DM工作机制解析
  9. 如何将html转为report,如何把Html5 Report Viewer添加到Web项目
  10. Software--Spring Boot--Contact 项目初期
  11. springboot毕业实习信息管理系统的设计与实现
  12. 实验四--项目技术指标(招标文件)
  13. Android强制系统横屏的原理和实现
  14. 淘宝全自动下单——解放双手
  15. 夜天之书 #78 共建的神话
  16. 【C语言】基础练习题
  17. Linux 内核clk 硬件相关层
  18. IJPay微信退款协议不正确 No appropriate protocol
  19. eclipse新建java项目报错:Failed to init ct.sym for ....../jrt-fs.jar
  20. networkx pagerank

热门文章

  1. 阿里巴巴提出的中台是什么鬼?终于有人把中台说清楚了
  2. 杀毒软件、HIPS与微点 分析三者区别
  3. 【上班族减肥饮食大法】
  4. 深圳大学计算机技术专业学位,深圳大学(专业学位)计算机技术考研参考书目
  5. Python数据可视化:啥是佩奇
  6. 数学建模与数据分析 || 3. 面向数据的特征提取方法: 探索性数据分析
  7. pajek15-18:网络
  8. android studio集成微博出现错误multiple dex file define Lcon/sina/sso/RemoteSSO解决方法
  9. ipv6服务器搭建网站,IPV6地址访问web项目网站配置
  10. CSS3新选择器,盒子模型,过渡动画transition,2D转换transform