📄 planegame.java
字号:
count.saveline=710;
}
else//如果敌机靠近我方飞机,则加快发射子弹
{
if(picPE.getX()>picPlane.getX()-60 && picPE.getX()<picPlane.getX()+60)
{
intFirePlace+=70;
}
else
{
intFirePlace+=20;
}
}
//如果两架相撞则都爆炸,如果我方飞机还存在生命,则只显示爆炸效果
if(picPE.getX()>(picPlane.getX()-30) && picPE.getX()<(picPlane.getX()+30) && picPE.getY()>(picPlane.getY()-55) && picPE.getY()<(picPlane.getY()+55)||count.wincontral==true)
{
if(count.lbllife>0)
{
count.lbllife--;
System.out.println("还有生命"+count.lbllife);
lbllife.setText("生命:"+count.lbllife);
}
else
{
picPlane.setVisible(false);
picPlane.setLocation(-100,-100);
new GameOver();
this.stop();
}
for(int i=1;i<16;i++)
{
picPE.setIcon(imgBullet[i]);
try
{
this.sleep(200);
}
catch(InterruptedException e)
{
System.out.println("Sleep Error!! :"+e);
}
}
picPE.setVisible(false);
picPE.setLocation(-100,-100);
planeEVector.removeElement(planeEInfo);
this.stop();
planeEVector.removeElement(planeEInfo);
this.stop();
}
try
{
this.sleep(50);
}
catch(InterruptedException e)
{
System.out.println("Sleep Error!! :"+e);
}
}
}
}
//敌机4的运动线程,随机取得位置
class PlaneEClass4 extends Thread //右向下
{
JLabel picPE; //用于显示敌机,记录敌机位置
Icon imgPE;
Thread planeEThread; //敌机的线程
int intSleepTime; //用于控制敌机子弹的运动速度
EPlaneInfor planeEInfo;
public PlaneEClass4(int s)
{
intSleepTime=s;
imgPE = new ImageIcon("plane\\eplane2.gif");
picPE = new JLabel(imgPE);
picPE.setBounds(0,0,100,100); //敌机位置的初始设置
pnl.add(picPE);
picPE.setLocation(picPE.getX()+400,picPE.getY()-10);
planeEThread=new Thread(this,"p1");
planeEInfo=new EPlaneInfor(planeEThread,picPE);
planeEVector.addElement(planeEInfo); //把敌机信息对象添加到Vector中去
planeEThread.start(); //启动敌机线程
}
public void run()
{
Random way=new Random(1230L); //随机取得敌机运动方向
Random fireSpeed=new Random(123450L); //用于控制敌机子弹的运动速度
int intWay;
int intFirePlace=0;
int intFireSpeed=fireSpeed.nextInt(4);
if(intFireSpeed==0) //放置出错
{
intFireSpeed=5;
}
while(planeEThread==Thread.currentThread())
{
try //随机取得敌机的运动方向
{
intWay=way.nextInt(4);
if(intWay==0)
{
picPE.setLocation(picPE.getX()+15,picPE.getY()+2);
}
else if(intWay==1)
{
picPE.setLocation(picPE.getX()-15,picPE.getY()+2);
}
else if(intWay==2)
{
picPE.setLocation(picPE.getX(),picPE.getY()+2);
}
else if(intWay==3)
{
picPE.setLocation(picPE.getX(),picPE.getY()+2);
}
this.sleep(intSleepTime);
}
catch(InterruptedException e)
{
System.out.println("Sleep Error!! :"+e);
}
if(picPE.getX()<-20||picPE.getX()>550||picPE.getY()<-10||picPE.getY()>700)
{
picPE.setVisible(false);
picPE.setLocation(-100,-100);
planeEVector.removeElement(planeEInfo);
this.stop();
}
//敌机随机发射子弹
if(intFirePlace>(intFireSpeed*300+intSleepTime*5))
{
new genzongEClass(picPE);
intFirePlace=0;
count.saveline=710;
}
else//如果敌机靠近我方飞机,则加快发射子弹
{
if(picPE.getX()>picPlane.getX()-60 && picPE.getX()<picPlane.getX()+60)
{
intFirePlace+=70;
}
else
{
intFirePlace+=20;
}
}
//如果两架相撞则都爆炸,如果我方飞机还存在生命,则只显示爆炸效果
if(picPE.getX()>(picPlane.getX()-30) && picPE.getX()<(picPlane.getX()+30) && picPE.getY()>(picPlane.getY()-55) && picPE.getY()<(picPlane.getY()+55)||count.wincontral==true)
{
if(count.lbllife>0)
{
count.lbllife--;
System.out.println("还有生命"+count.lbllife);
lbllife.setText("生命:"+count.lbllife);
}
else
{
picPlane.setVisible(false);
picPlane.setLocation(-100,-100);
new GameOver();
this.stop();
}
for(int i=1;i<16;i++)
{
picPE.setIcon(imgBullet[i]);
try
{
this.sleep(200);
}
catch(InterruptedException e)
{
System.out.println("Sleep Error!! :"+e);
}
}
picPE.setVisible(false);
picPE.setLocation(-100,-100);
planeEVector.removeElement(planeEInfo);
this.stop();
}
try
{
this.sleep(50);
}
catch(InterruptedException e)
{
System.out.println("Sleep Error!! :"+e);
}
}
}
}
}
class frmLogin //登录界面--连接数据库,进行验证
{
boolean bool; //登录成功,才可以修改个人资料
AllMessage allmessage;
Connection conn; //连接对象
Statement stm; //陈述对象
ResultSet rst; //结果集
String uName;
JFrame frm;
JTextField txtuUserName;
TextField txtuPassword;
JTextArea txtMsg;
JLabel lblBeiJing;
Icon iconLogin,iconExit,iconInsert,iconUpdate,iconDel,iconBeiJing;
JButton btnLogin,btnExit,btnInsert,btnUpdate,btnDel;
frmLogin(AllMessage all)//图形界面
{
bool=false;
new ConnetSql();
allmessage=all;
uName="登陆游戏";
frm=new JFrame(uName);
JPanel pnl = new JPanel();
txtuUserName = new JTextField();
txtuPassword = new TextField();
txtuPassword.setEchoChar('*');
iconLogin=new ImageIcon(getClass().getResource("picture//Login.gif"));
iconExit=new ImageIcon(getClass().getResource("picture//Exit.gif"));
iconInsert=new ImageIcon(getClass().getResource("picture//Insert.gif"));
iconUpdate=new ImageIcon(getClass().getResource("picture//Update.gif"));
iconDel=new ImageIcon(getClass().getResource("picture//Del.gif"));
btnLogin = new JButton(iconLogin);
btnExit = new JButton(iconExit);
btnInsert = new JButton(iconInsert);
btnUpdate = new JButton(iconUpdate);
btnDel = new JButton(iconDel);
txtMsg= new JTextArea();
pnl.setLayout(null);
txtuUserName.setBounds(new Rectangle(55, 43, 225, 24));
txtuPassword.setBounds(new Rectangle(55, 70, 225, 24));
btnLogin.setBounds(new Rectangle(289, 39, 84, 28));
btnExit.setBounds(new Rectangle(289, 70, 84, 28));
btnInsert.setBounds(new Rectangle(55, 335, 84, 28));
btnUpdate.setBounds(new Rectangle(175, 335, 84, 28));
btnDel.setBounds(new Rectangle(287, 335, 84, 28));
//创建并注册【登录】按钮的事件监听器
thLoginAction thLog=new thLoginAction();
btnLogin.addActionListener(thLog);
//创建并注册【退出】按钮的事件监听器
thExitAction thExt=new thExitAction();
btnExit.addActionListener(thExt);
//创建并注册【插入/更新/删除】按钮的事件监听器
thInsertAction thIns=new thInsertAction();
btnInsert.addActionListener(thIns);
btnUpdate.addActionListener(thIns);
btnDel.addActionListener(thIns);
txtMsg.setBorder(BorderFactory.createLineBorder(Color.black));
txtMsg.setBounds(new Rectangle(55, 120, 318, 201));
pnl.add(txtuUserName, null);
pnl.add(txtuPassword, null);
pnl.add(txtMsg, null);
pnl.add(btnLogin, null);
pnl.add(btnExit, null);
pnl.add(btnInsert, null);
pnl.add(btnUpdate, null);
pnl.add(btnDel, null);
iconBeiJing=new ImageIcon("picture//BeiJing.gif");
lblBeiJing=new JLabel(iconBeiJing);
lblBeiJing.setBounds(0,-25,420,420);
pnl.add(lblBeiJing);
frm.getContentPane().add(pnl);
frm.setResizable(false);
frm.setLocation(300,150);
frm.setSize(420,420);
frm.setVisible(true);
}
//连接数据库//服务器
class ConnetSql
{
public ConnetSql()
{
try
{
String strDBDriver="com.microsoft.jdbc.sqlserver.SQLServerDriver";
Class.forName(strDBDriver); //注册驱动
String strConnStr="jdbc:microsoft:sqlserver://localhost;DatabaseName=PlaneGame";
String strUserName = "sa";
String strPassword = "sa";
conn = DriverManager.getConnection(strConnStr,strUserName,strPassword);
stm = conn.createStatement(); //取得陈述对象
System.out.println("连接数据库成功");
}
catch(Exception e)
{
System.out.println(e);
}
}
}
//【登录】按钮的事件类
class thLoginAction implements ActionListener
{
public void actionPerformed(ActionEvent ep)
{
try
{
String strSQL = "select * from UserLogin where uUserName like '"+txtuUserName.getText().trim()+"'";
System.out.println(strSQL);
rst=stm.executeQuery(strSQL); //执行SQL语句,返回结果集
//next()方法用于使游标下移一条纪录;getString()、getInt()等方法用于取得游标指向的记录的某一列的值
if(rst.next()==true)
{
if(rst.getString("uPassword").equals(txtuPassword.getText()))
{
bool=true;
allmessage.lbllife=5;
allmessage.level=0;
allmessage.mark=0;
JOptionPane.showConfirmDialog(null,"欢迎您的登录,你上次登录的时间是"+rst.getString("uLastLogInTime"),"登录成功",JOptionPane.CLOSED_OPTION);
strSQL = "update UserLogin set uLastLogInTime=getdate() where uUserName like '"+txtuUserName.getText().trim()+"'";
stm.executeUpdate(strSQL);
allmessage.UserName=txtuUserName.getText();
MyFrame My=new MyFrame(allmessage.UserName,allmessage);
ResultSet rstShowAll;
strSQL = "select * from UserLogin";
rstShowAll=stm.executeQuery(strSQL);
txtMsg.setText("用户名 密码 上次登录时间\n");
while(rstShowAll.next()==true)
{
if(rstShowAll.getString("uUserName").equals(txtuUserName.getText().trim()))
{
txtMsg.append(rstShowAll.getString("uUserName")+" "+rstShowAll.getString("uPassword")+" "+rstShowAll.getString("uLastLoginTime")+"\n");
}
}
}
else
{
JOptionPane.showConfirmDialog(null,"密码错误,请重新登录","登录错误",JOptionPane.ERROR_MESSAGE);
}
}
else
{
JOptionPane.showConfirmDialog(null,"该用户不存在,请重新登录","登录错误",JOptionPane.ERROR_MESSAGE);
}
}
catch(Exception em)
{
System.out.println("出现异常: " +em);
}
txtuPassword.setText(""); //清空密码框
}
}
//【退出】按钮的事件类
class thExitAction implements ActionListener
{
public void actionPerformed(ActionEvent ep)
{
try
{
//关闭数据库连接
if(rst!=null)
{
rst.close();
}
if(stm!=null)
{
stm.close();
}
if(conn!=null)
{
conn.close();
}
System.exit(0);
}
catch(Exception e)
{
System.out.println(e);
}
}
}
//【注册/更新注册信息/删除更新注册信息】按钮的事件类
class thInsertAction implements ActionListener
{
public void actionPerformed(ActionEvent ep)
{
String strSQL;
try
{
stm = conn.createStatement(); //取得陈述对象
if(ep.getSource()==btnInsert)
{
strSQL = "select * from UserLogin where uUserName like '"+txtuUserName.getText()+"'";
rst=stm.executeQuery(strSQL);
if(rst.next()==true)
{
JOptionPane.showConfirmDialog(null,"该用户名已经存在,请重新输入用户名","插入错误",JOptionPane.ERROR_MESSAGE);
}
else
{
txtMsg.setText("");
strSQL = "insert into UserLogin(uUserName,uPassword) values('"+txtuUserName.getText().trim()+"','"+txtuPassword.getText().trim()+"')";
txtMsg.append("注册成功!!"+"\n"+"用户名:"+txtuUserName.getText()+"\n"+"请记住密码:"+txtuPassword.getText());
stm.executeUpdate(strSQL);
}
}
else if(ep.getSource()==btnUpdate)
{
if(bool==true)
{
strSQL = "update UserLogin set uPassword='"+txtuPassword.getText().trim()+"' where uUserName='"+txtuUserName.getText().trim()+"'";
txtMsg.setText("");
txtMsg.append("修改成功!!"+"\n"+"用户名:"+txtuUserName.getText()+"\n"+"请记住密码:"+txtuPassword.getText());
JOptionPane.showConfirmDialog(null,"已经修改"+(txtuUserName.getText().trim()),"修改信息",JOptionPane.ERROR_MESSAGE);
stm.executeUpdate(strSQL);
}
else
{
JOptionPane.showConfirmDialog(null,"非法用户,请先登录!!"+(txtuUserName.getText().trim()),"错误提示",JOptionPane.ERROR_MESSAGE);
}
}
else if(ep.getSource()==btnDel)
{
if(bool==true)
{
strSQL = "select * from UserLogin where uUserName like '"+txtuUserName.getText()+"'";
rst=stm.executeQuery(strSQL);
if(rst.next()==true)
{
strSQL = "delete from UserLogin where uUserName='"+txtuUserName.getText().trim()+"'";
JOptionPane.showConfirmDialog(null,"已删除"+(txtuUserName.getText().trim()),"删除信息",JOptionPane.ERROR_MESSAGE);;
txtMsg.setText("");
stm.executeUpdate(strSQL);
}
else
{
JOptionPane.showConfirmDialog(null,"该用户不存在,请重新输入用户名","删除错误",JOptionPane.ERROR_MESSAGE);
}
}
else
{
JOptionPane.showConfirmDialog(null,"非法用户,请先登录!!"+(txtuUserName.getText().trim()),"错误提示",JOptionPane.ERROR_MESSAGE);
}
}
}
catch(Exception e)
{
System.out.println(e);
}
}
}
}
public class PlaneGame
{
public static void main(String agr[])
{
AllMessage all=new AllMessage();
JFrame.setDefaultLookAndFeelDecorated(true);//使用最新的SWING外观
frmLogin frm=new frmLogin(all); //登陆界面 登陆成功后才可以玩游戏
// new MyFrame("1",all);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -