继上篇文章内容
一、石块创建 创建Rock类import java.awt.*;
public class Rock extends Object{
Rock(){
this.x = (int)(Math.random()*750);//防止生成在窗体
this.y = (int)(Math.random()*550+300);//防止生成在天上
this.width = 74;
this.height = 72;
this.flag = false;
this.img = Toolkit.getDefaultToolkit().getImage("imgs/rock1.png");
}
}
GameWin类增加
代码块内添加
for (int i = 0; i < 3; i++) {
objectList.add(new Rock());
}
Object类增加
public int getWidth(){
return width;
}
Line类修改
switch case3需要修改
obj.x = endx - obj.getWidth()/2;二、拉取速度
通过线程的sleep实现拉取的速度,sleep时间越长速度越慢,通过一个变量m决定时间长短。
抓取速度分为三种:1、空勾时的速度2、抓取金块的速度3、抓取岩石的速度
Object类增加m决定sleep时间
int m;//质量,决定金块和石块拉取速度Rock类增加
this.m = 80;Gold类增加
this.m = 30;Line类增加
switch case3方法增加
m=1表示空勾时的速度。
int m = 1;
if添加
如果能移动则改变m。
m = obj.m;
最后添加
通过线程的sleep方法睡眠达到速度快慢效果。
try {
Thread.sleep(m);
} catch (InterruptedException e) {
e.printStackTrace();
}
三、多种金块
前面实现了多个金块,现在要添加多个不同大小的金块。
创建另一金块类继承金块类重写金块类变量。
Gold类增加继承金块类,重写宽高、速度和图片
static class GoldMini extends Gold{
GoldMini(){
this.width = 32;
this.height = 38;
this.m = 15;
this.img = Toolkit.getDefaultToolkit().getImage("imgs/gold3.png");
}
}
static class GoldPlus extends Gold{
GoldPlus(){
this.x = (int)(Math.random()*650);//重置大金块生成的x范围
this.width = 120;
this.height = 132;
this.m = 60;
this.img = Toolkit.getDefaultToolkit().getImage("imgs/gold1.png");
}
}
GameWin类修改
代码块
使用随机数,设置生成概率
for (int i = 0; i < 5; i++) {
double random = Math.random();
if(random<0.3){
objectList.add(new Gold.GoldMini());
}else if(random < 0.7){
objectList.add(new Gold());
}else{
objectList.add(new Gold.GoldPlus());
}
}
四、避免物体堆叠
获取物体矩形,通过判断矩形是否重合从而判断物体是否堆叠。
Object类增加获取画出来的矩形。
public Rectangle getRec(){//获取物体矩形
return new Rectangle(x,y,width,height);
}
GameWin类修改
isPlace用来标识是否可以放置;
先将生成的金块保存;
然后循环使用intersects方法判断是否重合,重合则设置isPlace为false;
如果isPlace状态为true则表示可以放置,添加到集合中,否则重置isPlace,i减一。
{
boolean isPlace = true;//是否可以放置
for (int i = 0; i < 5; i++) {
double random = Math.random();
Gold gold;//存放当前生成金块
if(random<0.3){
gold = new Gold.GoldMini();
}else if(random < 0.7){
gold = new Gold();
}else{
gold = new Gold.GoldPlus();
}
for(Object obj:objectList){
if(gold.getRec().intersects(obj.getRec())){//如果重叠,不可放置
isPlace = false;
}
}
if(isPlace){
objectList.add(gold);
}else{
isPlace = true;i--;
}
}
for (int i = 0; i < 3; i++) {
Rock rock = new Rock();
for (Object obj:objectList){
if(rock.getRec().intersects(obj.getRec())){
isPlace = false;
}
}
if(isPlace){
objectList.add(rock);
}
else{
isPlace = true;i--;
}
}
}
五、完善红线
添加钩爪
Line类增加
获取图片,画出图片
Image hook = Toolkit.getDefaultToolkit().getImage("imgs/hook.png");
lines方法增加
g.drawImage(hook,endx-35,endy-2,null);红线加粗 Line类增加
lines方法
g.drawLine(x,y,endx-1,endy);
g.drawLine(x,y,endx,endy);
g.drawLine(x,y,endx+1,endy);
六、积分设置
Object类增加
共有属性count
int count;//分数Rock类增加
石头加1分
this.count = 1;Gold类增加
金块分别按照大小加2、4、8分
this.count = 2;
this.count = 4;
this.count = 8;Bg类增加
背景类设置count为总分
static int count = 0;//总分
由于下面会多次用到绘制文字方法,所以定义一个方法
public static void drawWord(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);
}
paintSelf方法增加
drawWord(g,30,Color.black,"积分:"+count,30,150);Line类增加
Bg.count+=obj.count;//积分变化七、抓取快速返回 GameWin类增加
鼠标点击事件增加
鼠标点击右键并且状态为抓取返回中,则修改addSpeed为true表示快速拉取
if(e.getButton() == 3&&line.state == 3){
line.addSpeed = true;
}
Line类增加
boolean addSpeed = false;//快速拉取
switch case3修改
if(obj.flag){//判断是否能够移动
if(addSpeed){//是否点击右键快速移动
m = 10;
}else{
m = obj.m;
}
obj.x = endx - obj.getWidth()/2;
obj.y = endy;
if(length<=100){
obj.x = -150;//移除到屏幕外
obj.y = -150;
obj.flag = false;
addSpeed = false;//重置
Bg.count+=obj.count;//积分变化
state = 0;
}
}
八、重新生成
GameWin类增加
amount表示已经抓取的数量,根据是否还有资源重新刷新窗口
int amount = 0;//抓取数量
当窗口没有资源时,先释放当前窗口,再新建窗口
if(amount >= 8){
dispose();//释放当前窗口
GameWin gameWin = new GameWin();
gameWin.launch();
}
Line类增加
抓取成功amount+1
frame.amount++;
至此,已完成黄金矿工小游戏大致内容,可自行添加更多玩法,如:设置关卡、计时效果、成功、失败、商店购物等。



