此项目为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 here
ServerSocket 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 here
ClientFrame 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){ //字符串的长度必须大于0
byte[] 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");//获取logo
setIconImage(logo.getImage());//设置logo
ImageIcon 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();//将窗口转换为jpanel
jPanelbg.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() {
@Override
public void mouseClicked(MouseEvent e) {
jsilder jsilder=new jsilder();
setVisible(false);
}
@Override
public void mousePressed(MouseEvent e) {
}
@Override
public void mouseReleased(MouseEvent e) {
}
@Override
public void mouseEntered(MouseEvent e) {
jbstart.setForeground(Color.BLACK);
}
@Override
public void mouseExited(MouseEvent e) {
jbstart.setForeground(SystemColor.orange);
}
});
jbexit.addMouseListener(new MouseListener() {
@Override
public void mouseClicked(MouseEvent e) {
System.exit(0);
}
@Override
public void mousePressed(MouseEvent e) {
}
@Override
public void mouseReleased(MouseEvent e) {
}
@Override
public void mouseEntered(MouseEvent e) {
jbexit.setForeground(Color.black);
}
@Override
public void mouseExited(MouseEvent e) {
jbexit.setForeground(SystemColor.MAGENTA);
}
});
jbregard.addMouseListener(new MouseListener() {
@Override
public void mouseClicked(MouseEvent e) {
Rule rule=new Rule();
}
@Override
public void mousePressed(MouseEvent e) {
}
@Override
public void mouseReleased(MouseEvent e) {
}
@Override
public void mouseEntered(MouseEvent e) {
jbregard.setForeground(Color.orange);
}
@Override
public void mouseExited(MouseEvent e) {
jbregard.setForeground(SystemColor.red);
}
});
jbchongzhi.addMouseListener(new MouseListener() {
@Override
public void mouseClicked(MouseEvent e) {
chongzhi chongzhi=new chongzhi("充值中心");
}
@Override
public void mousePressed(MouseEvent e) {
}
@Override
public void mouseReleased(MouseEvent e) {
}
@Override
public void mouseEntered(MouseEvent e) {
}
@Override
public 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;
}
}
@Override
public 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 ;
}
}
}
@Override
public 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() {
@Override
public 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() {
@Override
public void actionPerformed(ActionEvent e) {
lifeflag=Integer.valueOf(one.getText());
}
});
two.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
lifeflag=Integer.valueOf(two.getText());
}
});
three.addActionListener(new ActionListener() {
@Override
public 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() {
@Override
public 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() {
@Override
public 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");//获取logo
setIconImage(logo.getImage());//设置logo
this.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");//获取logo
setIconImage(logo.getImage());//设置logo
ImageIcon 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();//将窗口转换为jpanel
jPanelbg.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);}
}
@Override
public void keyTyped(KeyEvent e) {
}
@Override
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() * 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();
}
@Override
public void keyReleased(KeyEvent e) {
}
@Override
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();
}
}
}
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);}
}
@Override
public void keyTyped(KeyEvent e) {
}
@Override
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] = 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();
}
@Override
public void keyReleased(KeyEvent e) {
}
@Override
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();
}
}
}