Compare commits

..

No commits in common. 'login' and 'main' have entirely different histories.
login ... main

@ -0,0 +1,106 @@
package com.Drj.Bg;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Toolkit;
import java.util.ArrayList;
import com.Drj.gameWin.gameWindrj;
import com.Lhy.login.Login;
import com.Sq.phb.Phb;
public class Bgdrj {
//关卡数
public static int level=1;
//每一关目标得分
public int goal=level*(15+((int)Math.random()*level*level+1));
//总分
public static int count=0;
//药水数量
public static int water=3;
//药水状态
public static boolean wf=false;
//开始时间
public long startTime;
//结束时间
public long endTime;
//药水价格
public int price =(int)(Math.random()*10)+1;
//是否购买
public boolean shop=false;
//图片输入
Image bg =Toolkit.getDefaultToolkit().getImage("image/bg.jpg");
Image tk =Toolkit.getDefaultToolkit().getImage("image/tk.jpg");
Image poe =Toolkit.getDefaultToolkit().getImage("image/poe.png");
Image s =Toolkit.getDefaultToolkit().getImage("image/water.png");
public void paint(Graphics g) {////绘制方法添加背景drawImage(地址x轴位置y轴位置状态)
g.drawImage(tk,0,0,null);
g.drawImage(bg,0,200,null);
switch (gameWindrj.state) {
case 0://打印开始界面
word(g,80,Color.red,"点击右键开始",100,400);
break;
case 1:
g.drawImage(poe,310,50,null);
g.drawImage(s,450,40,null);
//打印积分
word(g,30,Color.red,"积分"+count,30,150);
//药水数量
word(g,30,Color.black,"*"+water,510,70);
//关卡数
word(g,20,Color.black,"第"+level+"关",30,60);
//目标积分
word(g,30,Color.red,"目标积分:"+goal,30,110);
//时间
endTime=System.currentTimeMillis();//给结束时间负值
long time=20-(endTime-startTime)/1000;//定义临时变量判断用时除1000把毫秒换算成秒
word(g,30,Color.red,"剩余时间:"+(time>0?time:0),520,150);//如果时间大于零打印时间否则就打印0
break;
case 2://商店界面
g.drawImage(s,300,400,null);
word(g,30,Color.black,"价格:"+price,300,500);
word(g,30,Color.black,"左键是否购买?",250,550);
if(shop) {
count=count-price;//找零
water++;
shop=false;
gameWindrj.state=1;
startTime=System.currentTimeMillis();//给开始时间赋值为系统时间
}
break;
case 3://进入失败页面
Phb p=new Phb();
p.paint(g, count);
break;
}
}
public static void word(Graphics g,int size,Color color,String str,int x,int y) {
g.setColor(color);//设置积分颜色
g.setFont(new Font("仿宋",Font.BOLD,size));//设置积分字体和大小
g.drawString(str, x, y);//设置积分输出格式和位置
}
//true倒计时完成 false正在倒计时
public boolean gametime(){
long time=(endTime-startTime)/1000;//定义临时变量判断用时除1000把毫秒换算成秒
if(time>20) {
return true;
}return false;
}
//重置元素
public void reGame() {
//关卡数
level=1;
//每一关目标得分
goal=level*15;
//总分
count=0;
//药水数量
water=3;
//药水状态
wf=false;
}
}

@ -0,0 +1,136 @@
package com.Drj.Line;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Toolkit;
import com.Drj.Bg.Bgdrj;
import com.Drj.Object.Objectdrj;
import com.Drj.gameWin.gameWindrj;
public class Linedrj {
//起点坐标
public int x=380;
public int y=180;
//终点坐标
public int endx;
public int endy;
//线长
public double length=100;
public double j=0;
//方向
public int dir=1;
//状态 为0摇摆 为1抓取 为2收回 为3抓到返回
public int state;
//钩爪图片
Image hook =Toolkit.getDefaultToolkit().getImage("image/hook.png");
//为了判断抓取构建有参构建方法
public gameWindrj frame;
public Linedrj(gameWindrj frame){this.frame=frame;}
void logic() {//构建判断函数
for(Objectdrj obj:this.frame.golds) {
if(endx>=obj.x && endx<=obj.x+obj.width
&&endy>=obj.y && endy<=obj.y+obj.height) {//判断钩子是否碰到物体条件
state=3;//将状态变为3抓到返回
obj.f =true;//金矿已被抓取
}
}
}
public void paint(Graphics g) {//绘制方法
g.setColor(Color.pink);//修改线的颜色
logic();//判断钩子是否碰到物体
switch(state){//分析状态并运行
case 0:if(j<0.1) {dir=1;}//线到达18度使进行顺时针旋转
else if(j>0.9){dir=-1;}//线到达162度使进行逆时针旋转
j+=0.005*dir;//线进行旋转角度制的公式
endx=x+(int)(length*Math.cos(j*Math.PI));//终点坐标x的表达公式
endy=y+(int)(length*Math.sin(j*Math.PI));//终点坐标y的表达公式
g.drawLine(x, y, endx, endy);//drewLine(起始坐标x起始坐标y终点坐标x终点坐标y)
g.drawLine(x-1, y, endx-1, endy);
g.drawLine(x+1, y, endx+1, endy);//给线加粗
g.drawImage(hook, endx-36, endy-2, null);//钩爪位置
break;
case 1:
if(length<750) {//线的延长范围
length=length+5;//使线延长
endx=x+(int)(length*Math.cos(j*Math.PI));//终点坐标x的表达公式
endy=y+(int)(length*Math.sin(j*Math.PI));//终点坐标y的表达公式
g.drawLine(x, y, endx, endy);//drewLine(起始坐标x起始坐标y终点坐标x终点坐标y)
g.drawLine(x-1, y, endx-1, endy);
g.drawLine(x+1, y, endx+1, endy);//给线加粗
g.drawImage(hook, endx-36, endy-2, null);//钩爪位置
}else {state=2;//延长后收回
}
break;
case 2:
if(length>100) {//线的收回范围
length=length-5;//使线收回
endx=x+(int)(length*Math.cos(j*Math.PI));//终点坐标x的表达公式
endy=y+(int)(length*Math.sin(j*Math.PI));//终点坐标y的表达公式
g.drawLine(x, y, endx, endy);//drewLine(起始坐标x起始坐标y终点坐标x终点坐标y)
g.drawLine(x-1, y, endx-1, endy);
g.drawLine(x+1, y, endx+1, endy);//给线加粗
g.drawImage(hook, endx-36, endy-2, null);//钩爪位置
}else {
state=0;//收回后继续开始摇摆
}break;
case 3:
int m=1;
if(length>100) {//线的收回范围
length=length-5;//使线收回
endx=x+(int)(length*Math.cos(j*Math.PI));//终点坐标x的表达公式
endy=y+(int)(length*Math.sin(j*Math.PI));//终点坐标y的表达公式
g.drawLine(x, y, endx, endy);//drewLine(起始坐标x起始坐标y终点坐标x终点坐标y)
g.drawLine(x-1, y, endx-1, endy);
g.drawLine(x+1, y, endx+1, endy);//给线加粗
g.drawImage(hook, endx-36, endy-2, null);//钩爪位置
for(Objectdrj obj:this.frame.golds) {
m=obj.m;//接收物体质量
if(obj.f==true) {//判断金矿是否被抓取
obj.x=endx-obj.getWidth()/2;//金矿收回时的x坐标
obj.y=endy;//金矿收回时的y坐标
if(Bgdrj.wf) {//判断是否使用了药水
if(obj.type==1) {//如果抓到的时金矿进行如下操作
m=1;
break;
}
if(obj.type==2) {//如果抓到的是石头使用药水将石头爆破
obj.x=-1500;
obj.y=-1500;
obj.f=false;
Bgdrj.wf=false;
state=2;
}
}}
}
}else {
for(Objectdrj obj:this.frame.golds) {
if(obj.f==true) {//判断金矿是否被抓取
obj.x=-1500;
obj.y=-1500;//将金矿抓回后移除
obj.f=false;
Bgdrj.wf=false;
Bgdrj.count+=obj.count;//加分
}
}
state=0;//收回后继续开始摇摆
}
try {
Thread.sleep(m);//设置延时来降低刷新频率使抓取速度变慢
} catch (InterruptedException e) {
e.printStackTrace();
}
break;
}
}
//重置线
public void reGame() {
j=0;
length=100;
}
}

@ -0,0 +1,13 @@
package com.Drj.Object;
import java.awt.Toolkit;
public class GoldMinidrj extends Golddrj{
public GoldMinidrj() {
this.width=36;
this.height =36;//由于图片大小不同重新设置一个宽高
this.m=15;//由于质量大小不同重新设置一个质量
this.count=2;//设置积分
this.img =Toolkit.getDefaultToolkit().getImage("image/gold0.gif");//由于图片不同重新设置一个路径
}
}

@ -0,0 +1,16 @@
package com.Drj.Object;
import java.awt.Toolkit;
public class GoldPlusdrj extends Golddrj{
public GoldPlusdrj() {
//由于大金矿过大会溢出画面重新设置其坐标
this.x=(int)(Math.random()*650);//金矿x轴随机位置的函数
this.y=(int)(Math.random()*550+300);//金矿y轴随机位置的函数
this.width=105;
this.height =105;//由于图片大小不同重新设置一个宽高
this.m=60;//由于质量大小不同重新设置一个质量
this.count=8;//设置积分
this.img =Toolkit.getDefaultToolkit().getImage("image/gold2.gif");//由于图片不同重新设置一个路径
}
}

@ -0,0 +1,20 @@
package com.Drj.Object;
import java.awt.Toolkit;
public class Golddrj extends Objectdrj{
boolean f=false;//用于判断是否被抓取
public Golddrj() {
this.x=(int)(Math.random()*700);//金矿x轴随机位置的函数
this.y=(int)(Math.random()*550+300);//金矿y轴随机位置的函数
this.width=52;//设置宽
this.height =52;//设置高
this.f=false;
this.m=30;//设置质量
this.count=4;//设置积分
this.type=1;
this.img =Toolkit.getDefaultToolkit().getImage("image/gold1.gif");
}
}

@ -0,0 +1,35 @@
package com.Drj.Object;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Rectangle;
import java.awt.Toolkit;
public class Objectdrj {
//坐标
public int x;
public int y;
//宽高
public int width;
public int height;
//图片
Image img;
//用于判断是否被抓取
public boolean f;
//质量用于修改抓取速度
public int m;
//积分
public int count;
//类型 为1金矿 为2石头
public int type;
public void paint(Graphics g) {//绘制方法添加抓取物drawImage(地址x轴位置y轴位置状态)
g.drawImage(img,x,y,null);
}
public int getWidth() {
return width;
}
//获取矩形
public Rectangle getRec() {
return new Rectangle(x,y,width,height);
}
}

@ -0,0 +1,18 @@
package com.Drj.Object;
import java.awt.Toolkit;
public class Rockdrj extends Objectdrj{
boolean f=false;//用于判断是否被抓取
public Rockdrj() {
this.x=(int)(Math.random()*700);//石头x轴随机位置的函数
this.y=(int)(Math.random()*550+300);//石头y轴随机位置的函数
this.width=71;
this.height =71;
this.f=false;
this.m=50;
this.count=1;//设置积分
this.type=2;
this.img =Toolkit.getDefaultToolkit().getImage("image/rock1.png");
}
}

@ -0,0 +1,167 @@
package com.Drj.gameWin;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import javax.swing.JFrame;
import com.Drj.Bg.Bgdrj;
import com.Drj.Line.Linedrj;
import com.Drj.Object.Golddrj;
import com.Drj.Object.GoldMinidrj;
import com.Drj.Object.GoldPlusdrj;
import com.Drj.Object.Objectdrj;
import com.Drj.Object.Rockdrj;
import com.Drj.shop.Shopdrj;
import com.Lhy.login.Login;
public class gameWindrj extends JFrame{
//0未开始 1运行中 2商店 3失败 4胜利
public static int state;
public static String username;
public ArrayList<Objectdrj> golds=new ArrayList<Objectdrj>();//用于存储金矿,石头
Bgdrj bg=new Bgdrj();
Linedrj l=new Linedrj(this);
{
for(int i=0;i<11;i++) {
boolean fl=true;//用于判断该位置是否存在重叠现象
double random=Math.random();//创造随机数用于随机生成金矿
Golddrj gold;//存放当前生成的金矿
if(random<0.3) {
gold=new GoldMinidrj();
}
else if(random>=0.3&&random<0.7){
gold=new Golddrj();
}
else {
gold=new GoldPlusdrj();
}//添加金矿
for(Objectdrj obj:golds) {
if(gold.getRec().intersects(obj.getRec())) {//用于判断是否可以放置
fl=false;//不可放置,需重新生成
}
}
if(fl) {golds.add(gold);}
else {fl=true;i--;}
}
for(int i=0;i<3;i++) {
boolean fl=true;//用于判断该位置是否存在重叠现象
Rockdrj rock;
rock=new Rockdrj();
for(Objectdrj obj:golds) {
if(rock.getRec().intersects(obj.getRec())) {//用于判断是否可以放置
fl=false;//不可放置,需重新生成
}
}
if(fl) {golds.add(rock);}//添加石头
else {fl=true;i--;}
}
}
void luanch() {//用于初始化窗口
this.setVisible(true);//用于设置窗口是否可见
this.setSize(768,1000);//用于设置窗口大小
this.setLocationRelativeTo(null);//用于窗口位置,使窗口居中
this.setTitle("超级无敌好玩黄金矿工");//用于设置窗口标题
setDefaultCloseOperation(EXIT_ON_CLOSE);//用于关闭窗口
addMouseListener(new MouseAdapter() {//添加鼠标监听
@Override
public void mouseClicked(MouseEvent e) {//捕捉鼠标事件
super.mouseClicked(e);//捕捉单击事件
switch (state) {
case 0:if(e.getButton()==3) {//点击右键开始游戏
state=1;
bg.startTime=System.currentTimeMillis();//给开始时间赋值为系统时间
}
break;
case 1://游戏运行中
//左右摇摆时,点击左键抓取
if(e.getButton()==1&&l.state==0) {
l.state=1;
}
//抓取返回,点击右键使用药水
if(e.getButton()==3&&l.state==3&&Bgdrj.water>0) {
Bgdrj.wf=true;
Bgdrj.water--;
}
break;
case 2://是否购买左键购买右键退出商店
Shopdrj shop=new Shopdrj();
shop.mouseClicked(e, bg);
break;
case 3://重置游戏
if(e.getButton()==1) {
state=0;
bg.reGame();
l.reGame();
}
break;
}
}
}
);
while(true) {
repaint();//重新绘制
nextLevel();//判断是否可以进行下一关
try {
Thread.sleep(10);//用于延迟使线缓慢旋转
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public void nextLevel() {//用于判断是否可以进行下一关
if (bg.gametime() && state == 1) {//游戏运行中,进行如下操作
if (Bgdrj.count >= bg.goal) {
state=2;
Bgdrj.level++;
} else {
state = 3;
}
dispose();//关闭之前关卡窗口
gameWindrj gw1=new gameWindrj();//建立新的gamewin用于创建新的窗口
gw1.luanch();
}
}
Image offScreenImage;//新建画布运用双缓存来解决物体闪烁
public void paint(Graphics g) {//绘制方法Graphics类是所有图形上下文的抽象基类它允许应用程序在各种设备上实现的组件上绘制以及在屏幕外的图像上绘制。
offScreenImage=this.createImage(768,1000);//新建画布使其大小与窗口大小相同
Graphics gImage=offScreenImage.getGraphics();//调用Graphics类在画布上显示物品
bg.paint(gImage);//显示背景
if(state==1) {//线和物体仅在游戏当中出现
for(Objectdrj obj:golds) {
obj.paint(gImage);//显示金矿
}
l.paint(gImage);//显示线
}
g.drawImage(offScreenImage, 0, 0, null);//将画布放入窗口
}
public static void main(String[] args) {
Login d=new Login();
gameWindrj gw=new gameWindrj();//创建一个gameWin变量为gw
while(!d.flagdl) {
try {
Thread.sleep(100);
} catch (InterruptedException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
}
username=d.getName();
if(d.flagdl==true) {
d.setVisible(false);
gw.luanch();//用于打开窗口
}
}
}

@ -0,0 +1,17 @@
package com.Drj.shop;
import java.awt.event.MouseEvent;
import com.Drj.Bg.Bgdrj;
import com.Drj.gameWin.gameWindrj;
public class Shopdrj {
public void mouseClicked(MouseEvent e,Bgdrj bg) {
if(e.getButton()==1) {
bg.shop=true;
}
if(e.getButton()==3) {
gameWindrj.state=1;
bg.startTime=System.currentTimeMillis();//给开始时间赋值为系统时间
}
}
}

@ -1,137 +0,0 @@
package com.Lhy.login;
import javax.swing.*;
import com.Zsn.zhuce.Zhuce;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
public class Login extends JFrame implements ActionListener,MouseListener {
public boolean flagdl=false;
public static String name;
private JTextField usernameText;
private JPasswordField passwordText;
private JButton loginButton;
private JButton cancelButton;
private JButton zhuceButton;
private JLabel usernameLabel;
private JLabel passwordLabel;
public Login() {
this.setSize(768,1000);
this.setUndecorated(true);
this.setLocationRelativeTo(null);
usernameLabel = new JLabel("用户:");
usernameLabel.setBounds(250, 300, 90, 35);
usernameLabel.setForeground(Color.white);
usernameLabel.setFont(new Font("微软雅黑", Font.PLAIN, 18));
this.add(usernameLabel);
usernameText = new JTextField();
usernameText.setBounds(350, 300, 150, 35);
usernameText.setFont(new Font("微软雅黑",Font.PLAIN,16));
usernameText.setOpaque(false);
usernameText.setBorder(null);
usernameText.addMouseListener(this);
this.add(usernameText);
passwordLabel = new JLabel("密码:");
passwordLabel.setBounds(250, 400, 90, 35);
passwordLabel.setForeground(Color.white);
passwordLabel.setFont(new Font("微软雅黑", Font.PLAIN, 18));
this.add(passwordLabel);
passwordText = new JPasswordField();
passwordText.setBounds(350, 400, 150, 35);
passwordText.setOpaque(false);
passwordText.setBorder(null);
passwordText.addMouseListener(this);
this.add(passwordText);
loginButton = new JButton("登陆");
loginButton.setFont(new Font("微软雅黑",Font.PLAIN,18));
loginButton.setBounds(300, 500, 100, 35);
loginButton.setForeground(Color.white);
loginButton.setOpaque(false);
loginButton.setContentAreaFilled(false);
this.add(loginButton);
loginButton.addActionListener(this);
cancelButton = new JButton("退出");
cancelButton.setFont(new Font("微软雅黑",Font.PLAIN,18));
cancelButton.setBounds(450, 500, 100, 35);
cancelButton.setOpaque(false);
cancelButton.setContentAreaFilled(false);
cancelButton.setForeground(Color.white);
this.add(cancelButton);
cancelButton.addActionListener(this);
loginButton.addActionListener(this);
zhuceButton= new JButton("注册");
zhuceButton.setFont(new Font("微软雅黑",Font.PLAIN,18));
zhuceButton.setBounds(375, 600, 100, 35);
zhuceButton.setOpaque(false);
zhuceButton.setContentAreaFilled(false);
zhuceButton.setForeground(Color.white);
this.add(zhuceButton);
zhuceButton.addActionListener(this);
Loginbg a= new Loginbg();
this.add(a);
this.setVisible(true);
}
public String getName() {
return name;
}
public void setName(String name) {
Login.name = name;
}
@Override
public void actionPerformed(ActionEvent e){
if (e.getSource() == cancelButton) {
JOptionPane.showMessageDialog(this, "欢迎下次游玩");
System.exit(0);
}
if (e.getSource() == loginButton) {
name = usernameText.getText().trim();
char[] password = passwordText.getPassword();
String pass=String.valueOf(password);
boolean a=Users.check(name,pass);
if(a) {
JOptionPane.showMessageDialog(this, "登录成功!");
this.flagdl=true;
this.setVisible(false);
}else {
JOptionPane.showMessageDialog(this, "输入错误,请重新输入");
usernameText.setText("");
passwordText.setText("");
}
}
if(e.getSource() == zhuceButton) {
Zhuce z=new Zhuce();
}
}
@Override
public void mouseClicked(MouseEvent e) {
}
@Override
public void mousePressed(MouseEvent e) {
}
@Override
public void mouseReleased(MouseEvent e) {
}
@Override
public void mouseEntered(MouseEvent e) {
}
@Override
public void mouseExited(MouseEvent e) {
}
}

@ -1,18 +0,0 @@
package com.Lhy.login;
import javax.swing.*;
import java.awt.*;
import java.io.IOException;
public class Loginbg extends JPanel {
private Image bg =Toolkit.getDefaultToolkit().getImage("image/bg.jpg");
private Image tk =Toolkit.getDefaultToolkit().getImage("image/tk.jpg");
@Override
public void paint(Graphics g) {
super.paint(g);
g.drawImage(tk,0,0,null);
g.drawImage(bg,0,200,null);
}
}

@ -1,25 +0,0 @@
package com.Lhy.login;
public class User{
String username;
String password;
public User() {}
public User(String username, String password) {
super();
this.username = username;
this.password = password;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}

@ -1,37 +0,0 @@
package com.Lhy.login;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
public class Users {
public static boolean check(String username,String password) {
// TODO 自动生成的方法存根
boolean flaguser=false;
try {
Class.forName("com.mysql.cj.jdbc.Driver");
String url="jdbc:mysql://localhost:3306/gold?userSSL=flase&serverTimezone=Asia/Shanghai";
String user="root";
String password1="asdasd123";
Connection conn =DriverManager.getConnection(url,user,password1);
String sql ="select *from user where username=? and password=?";
PreparedStatement prestm=conn.prepareStatement(sql);
prestm.setString(1,username);
prestm.setString(2,password);
ResultSet rs=prestm.executeQuery();
if(rs.next()) {
flaguser=true;
}
rs.close();
prestm.close();
conn.close();
}
catch(Exception e) {
e.printStackTrace();
}
return flaguser;
}
}
Loading…
Cancel
Save