📄 game.java
字号:
import java.awt.*;
import javax.swing.*;
import javax.swing.border.*;
import java.awt.event.*;
import javax.swing.event.*;
import java.net.*;
import java.applet.*;
import java.io.*;
/**
* @author 木耳
* @version 2.1
*/
public class Game
{ /*main方法*/
public static void main(String[] args)
{ new Mygame();}
}
class Mygame extends JFrame implements KeyListener,ActionListener,MouseListener,Runnable
{ //每一关的正确个数
int pass=0;
//放大镜个数
int numjing=5;
//延时个数
int numtime=2;
//time总分
int numbonus=0;
//关数
int numstage=1;
//火车移动速度
int numspeed=500;
//速度差值
int speeds=30;
//难度分数
int numleave=500;
//随机数组(关1/20)
int[] array=new int[21];
//随机数组(错误坐标5/10)
int[] s_array=new int[6];
//总关数
final int SNUM=17;
//游戏开始
boolean gamestart=false;
//使火车移动的循环变量
boolean boo=true;
//多线程的循环
boolean booend=true;
//玩家姓名(英文1-3个)
public static String playname="";
//总分
public static int numtotal=0;
//临时变量
int int1=0;
Timer timer;
//第二个线程
Thread ready=new Thread(this);
//火车坐标
Point p=new Point();
//定义声音
AudioClip Sinit,Sready,Strue,Sfalse,Sjing,Sover,Swin,Stime,Scom,Sbomb,Strain,Sstage,Sscores,Swarn,Smove;
//定义几种格式的字体
Font font=new Font("Times New Roman",Font.BOLD,75);
Font afont=new Font("楷体",Font.BOLD,35);
Font bfont=new Font("Century",Font.BOLD,40);
Font cfont=new Font("楷体",Font.BOLD,76);
//界面控件的初使化
CardLayout card = new CardLayout();
Container pane=getContentPane();
paneScores panescores = new paneScores();
paneName panename = new paneName();
paneOption paneoption = new paneOption();
JPanel panecard1 = new JPanel();
JPanel panecard2 = new JPanel();
JPanel panestage = new JPanel();
JPanel paneleft = new JPanel();
JPanel paneright = new JPanel();
JLabel readygo=new JLabel("");
JLabel left1=new JLabel("");
JLabel left2=new JLabel("");
JLabel left3=new JLabel("");
JLabel left4=new JLabel("");
JLabel left5=new JLabel("");
JLabel right1=new JLabel("");
JLabel right2=new JLabel("");
JLabel right3=new JLabel("");
JLabel right4=new JLabel("");
JLabel right5=new JLabel("");
JLabel boom = new JLabel(new ImageIcon(""));
JLabel left = new JLabel(new ImageIcon("photo/1a.jpg"));
JLabel right = new JLabel(new ImageIcon("photo/1b.jpg"));
JLabel apple = new JLabel(new ImageIcon("photo/apple.gif"));
JLabel mg = new JLabel(new ImageIcon("photo/mg.gif"));
JLabel stage = new JLabel("1",new ImageIcon("photo/mg.gif"),JLabel.RIGHT);
JLabel train = new JLabel(new ImageIcon("photo/train.gif"));
JLabel life = new JLabel("×",new ImageIcon("photo/train.gif"),JLabel.LEFT);
JLabel lifefen = new JLabel("5");
JLabel fire=new JLabel(new ImageIcon("photo/fire.gif"));
JLabel jing = new JLabel("×",new ImageIcon("photo/to1.jpg"),JLabel.LEFT);
JLabel jingfen = new JLabel("5");
JLabel time = new JLabel("×",new ImageIcon("photo/to2.jpg"),JLabel.LEFT);
JLabel timefen = new JLabel("2");
JLabel total=new JLabel("0",JLabel.RIGHT);
JLabel sum=new JLabel("BOUNS");
JLabel snum=new JLabel("",JLabel.RIGHT);
JLabel road=new JLabel(new ImageIcon("photo/road.jpg"));
JLabel back = new JLabel(new ImageIcon("photo/back.jpg"));
JLabel font1 = new JLabel("开始游戏");
JLabel font2 = new JLabel(" 最高分");
JLabel font3 = new JLabel(" 选项");
JLabel font4 = new JLabel("退出游戏");
JLabel top=new JLabel(new ImageIcon("photo/top1.jpg"));
JLabel good=new JLabel("STAGE ");
JPanel mid=new JPanel();
public static final Toolkit toolkit = Toolkit.getDefaultToolkit();
/*构造方法*/
public Mygame()
{
numtotal=0;
timer = new Timer(40,this);
setSize(1024, 768);
Image img= Toolkit.getDefaultToolkit().getImage("photo/cur.png");
setCursor(this,img);
addWindowListener(new WindowAdapter()
{public void windowClosing( WindowEvent e)
{
System.exit(0);
}
});
setUndecorated(true);
//生成SNUM个不相等的随机数用来选关
for(int i=1;i<=SNUM;i++)
{ array[i]=(int)(Math.random()*SNUM)+1;
for (int j=1;j<i ; j++)
{ if (array[i]==array[j])
i--;
}
}
System.out.print("关数: ");
for (int i=1;i<=SNUM ;i++ )
{
System.out.print(array[i]+",");
}
System.out.println("");
//设置容器的布局方式
pane.setLayout(card);
panecard1.setLayout(null);
panecard2.setLayout(null);
panestage.setLayout(null);
paneleft.setLayout(null);
paneright.setLayout(null);
//设置字体
left1.setFont(cfont);
left2.setFont(cfont);
left3.setFont(cfont);
left4.setFont(cfont);
left5.setFont(cfont);
right1.setFont(cfont);
right2.setFont(cfont);
right3.setFont(cfont);
right4.setFont(cfont);
right5.setFont(cfont);
jing.setFont(afont);
time.setFont(afont);
jingfen.setFont(afont);
timefen.setFont(afont);
lifefen.setFont(afont);
life.setFont(afont);
readygo.setFont(font);
total.setFont(bfont);
good.setFont(font);
sum.setFont(font);
snum.setFont(font);
stage.setFont(bfont);
font1.setFont(new Font("宋体",Font.BOLD,36));
font2.setFont(new Font("宋休",Font.BOLD,36));
font3.setFont(new Font("宋休",Font.BOLD,36));
font4.setFont(new Font("宋休",Font.BOLD,36));
//是否可见
paneoption.setVisible(false);
panename.setVisible(false);
panescores.setVisible(false);
left.setVisible(false);
right.setVisible(false);
train.setVisible(false);
fire.setVisible(false);
good.setVisible(false);
sum.setVisible(false);
snum.setVisible(false);
//设置前景颜色
font1.setForeground(new Color(153,51,51));
font2.setForeground(new Color(153,51,51));
font3.setForeground(new Color(153,51,51));
font4.setForeground(new Color(153,51,51));
//font1.setForeground(new Color(102,0,0));
//font2.setForeground(new Color(102,0,0));
//font3.setForeground(new Color(102,0,0));
//font4.setForeground(new Color(102,0,0));
pane.setBackground(Color.orange);
left1.setForeground(Color.green);
left2.setForeground(Color.green);
left3.setForeground(Color.green);
left4.setForeground(Color.green);
left5.setForeground(Color.green);
right1.setForeground(Color.green);
right2.setForeground(Color.green);
right3.setForeground(Color.green);
right4.setForeground(Color.green);
right5.setForeground(Color.green);
left1.setHorizontalTextPosition(0);
left1.setVerticalTextPosition(0);
left2.setHorizontalTextPosition(0);
left2.setVerticalTextPosition(0);
left3.setHorizontalTextPosition(0);
left3.setVerticalTextPosition(0);
left4.setHorizontalTextPosition(0);
left4.setVerticalTextPosition(0);
left5.setHorizontalTextPosition(0);
left5.setVerticalTextPosition(0);
stage.setForeground(new Color(255,255,204));
readygo.setForeground(Color.yellow);
mid.setBackground(new Color(153,102,0));
jing.setForeground(Color.white);
time.setForeground(Color.white);
life.setForeground(Color.white);
jingfen.setForeground(new Color(153,255,153));
timefen.setForeground(new Color(153,255,153));
lifefen.setForeground(new Color(153,255,153));
total.setForeground(new Color(255,255,204));
good.setForeground(new Color(153,51,51));
sum.setForeground(new Color(153,51,51));
snum.setForeground(new Color(153,51,51));
panecard2.setBackground(new Color(227,230,199));
panestage.setBackground(new Color(255,204,51));
paneleft.setBackground(Color.gray);
paneright.setBackground(Color.gray);
//设置控件的位置和大小
paneoption.setBounds(250,300,510,350);
panename.setBounds(300,300,400,200);
panescores.setBounds(150,160,698,568);
boom.setBounds(280,70,75,75);
mg.setBounds(900,630,69,62);
stage.setBounds(0,5,130,62);
back.setBounds(0,0, 1024, 768);
top.setBounds(0,0,1024,170);
road.setBounds(310,101,400,30);
train.setBounds(660,90,50,30);
life.setBounds(880,90,80,30);
lifefen.setBounds(960,90,22,32);
jingfen.setBounds(80,89,22,32);
timefen.setBounds(162,89,22,32);
jing.setBounds(20,89,62,32);
time.setBounds(102,89,62,32);
total.setBounds(860,25,120,40);
apple.setBounds(800,20,57,57);
font1.setBounds(420, 340, 200, 45);
font2.setBounds(420, 412, 200, 45);
font3.setBounds(420, 484, 200, 45);
font4.setBounds(420, 556, 200, 45);
//panestage.setBounds(0,133,1024,570);
panestage.setBounds(0,173,1024,565);
left.setBounds(0,1,500,550);
right.setBounds(0,1,500,550);
mid.setBounds(508,5,6,552);
paneleft.setBounds(4,5,500,552);
paneright.setBounds(518,5,500,552);
readygo.setBounds(360,120,550,100);
fire.setBounds(450,250,100,100);
good.setBounds(320,170,600,100);
sum.setBounds(200,400,350,100);
snum.setBounds(500,400,300,100);
//为控件注册事件监听器
left.addMouseListener(this);
left1.addMouseListener(this);
left2.addMouseListener(this);
left3.addMouseListener(this);
left4.addMouseListener(this);
left5.addMouseListener(this);
right.addMouseListener(this);
right1.addMouseListener(this);
right2.addMouseListener(this);
right3.addMouseListener(this);
right4.addMouseListener(this);
right5.addMouseListener(this);
jing.addMouseListener(this);
time.addMouseListener(this);
font1.addMouseListener(this);
font2.addMouseListener(this);
font3.addMouseListener(this);
font4.addMouseListener(this);
panescores.addMouseListener(this);
addKeyListener(this);
//向容器中添加控件
paneleft.add(left1);
paneleft.add(left2);
paneleft.add(left3);
paneleft.add(left4);
paneleft.add(left5);
paneleft.add(left);
paneright.add(right1);
paneright.add(right2);
paneright.add(right3);
paneright.add(right4);
paneright.add(right5);
paneright.add(right);
panestage.add(readygo);
panestage.add(paneleft);
panestage.add(paneright);
panestage.add(mid);
panecard1.add(paneoption);
panecard1.add(mg);
panecard1.add(panescores);
panecard1.add(font1);
panecard1.add(font2);
panecard1.add(font3);
panecard1.add(font4);
panecard1.add(back);
panecard2.add(panename);
panecard2.add(stage);
panecard2.add(total);
panecard2.add(apple);
panecard2.add(jing);
panecard2.add(time);
panecard2.add(jingfen);
panecard2.add(timefen);
panecard2.add(boom);
panecard2.add(train);
panecard2.add(lifefen);
panecard2.add(life);
panecard2.add(road);
panecard2.add(top);
panecard2.add(fire);
panecard2.add(snum);
panecard2.add(sum);
panecard2.add(good);
panecard2.add(panestage);
pane.add("1",panecard1);
pane.add("2",panecard2);
p=train.getLocation();
show();
//声音对象的初使化
try
{ Scom = Applet.newAudioClip(new File("sound/complete.au").toURL());
Sinit = Applet.newAudioClip(new File("sound/init.wav").toURL());
Sscores = Applet.newAudioClip(new File("sound/scores.wav").toURL());
Sready= Applet.newAudioClip(new File("sound/ready.wav").toURL());
Strue= Applet.newAudioClip(new File("sound/true.au").toURL());
Sfalse= Applet.newAudioClip(new File("sound/false.au").toURL());
Swin= Applet.newAudioClip(new File("sound/win.au").toURL());
Sover= Applet.newAudioClip(new File("sound/over.au").toURL());
Sjing= Applet.newAudioClip(new File("sound/jing.au").toURL());
Stime= Applet.newAudioClip(new File("sound/time.wav").toURL());
Strain=Applet.newAudioClip(new File("sound/train.au").toURL());
Sbomb=Applet.newAudioClip(new File("sound/bomb.au").toURL());
Swarn=Applet.newAudioClip(new File("sound/warn.wav").toURL());
Smove=Applet.newAudioClip(new File("sound/move.wav").toURL());
}
catch (MalformedURLException ace) {System.out.println("err");}
Sinit.play();
}
//设置光标
public static void setCursor(Component c, Image image) {
Cursor cursor = toolkit.createCustomCursor(image, new Point(10, 10), null);
c.setCursor(cursor);
}
/*从8处错误中随机选择5处*/
public void selectWrong()
{
//生成5个不相等的随机数(1~10)
for(int i=1;i<6;i++)
{ s_array[i]=(int)(Math.random()*10)+1;
for (int j=1;j<i ; j++)
{ if (s_array[i]==s_array[j])
i--;
}
}
System.out.print("错误值: ");
for (int i=1;i<6 ;i++ )
{
System.out.print(s_array[i]+",");
}
System.out.println("");
}
public void stage1()
{ int sint[][] ={{0,0},{17,19},{76,470},{133,388},{397,411},{369,203},{390,103},{254,0},{103,80},{65,240},{260,166}};
left.setIcon(new ImageIcon("photo/1.jpg"));
right.setIcon(new ImageIcon("photo/1.jpg"));
left1.setBounds(sint[s_array[1]][0],sint[s_array[1]][1],80,80);
left1.setIcon(new ImageIcon("photo/1_"+String.valueOf(s_array[1])+".jpg"));
left2.setBounds(sint[s_array[2]][0],sint[s_array[2]][1],80,80);
left2.setIcon(new ImageIcon("photo/1_"+String.valueOf(s_array[2])+".jpg"));
left3.setBounds(sint[s_array[3]][0],sint[s_array[3]][1],80,80);
left3.setIcon(new ImageIcon("photo/1_"+String.valueOf(s_array[3])+".jpg"));
left4.setBounds(sint[s_array[4]][0],sint[s_array[4]][1],80,80);
left4.setIcon(new ImageIcon("photo/1_"+String.valueOf(s_array[4])+".jpg"));
left5.setBounds(sint[s_array[5]][0],sint[s_array[5]][1],80,80);
left5.setIcon(new ImageIcon("photo/1_"+String.valueOf(s_array[5])+".jpg"));
try
{
Sstage=Applet.newAudioClip(new File("sound/S1.au").toURL());
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -