黄金矿工小游戏制作步骤

Posted BubblesMusic

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了黄金矿工小游戏制作步骤相关的知识,希望对你有一定的参考价值。

1.新建一个普通的java项目

2.新建一个文件夹imags是用来放图片素材

3.在src创建一个文件夹com.sxt是用来放实体类的

步骤

1.窗口绘制

2.绘制图片

3.线绘制

4.线摇摆

5.线抓取

6.创建金块

7.双缓存

8.抓取判定

9.抓取返回

10.创建多个金块

11.石块创建

12.拉取速度

13.创建多种金块

14.物体堆叠

15.线完善

16.积分设置

17.爆破

18.关卡设置

19.游戏状态

20.游戏计时效果

21.成功效果

22.失败效果

23.游戏重置效果

24.商店购物效果

4.在com.sxt文件夹创建六个实体类Bg、Gametest、Gold、line、Object、Rock

源码

Bg类

package com.sxt;

import java.awt.*;

public class Bg {

//关卡数

static int level = 1;

//目标得分

int goal = level*5;

//总分

static int count = 0;

//药水数量

static int waterNum = 3;

//药水状态,默认是F,T表示正在使用

static boolean waterFlag = false;

//开始时间

long startTime;

//结束时间

long emdTime;

//药水价格

int price = (int) (Math.random ()*10);

//是否进入商店f不购买

boolean shop = false;

//载入图片

Image bg = Toolkit.getDefaultToolkit ().getImage ("imags/bg.JPG"); //地面

Image bg1 = Toolkit.getDefaultToolkit ().getImage ("imags/bg1.JPG"); //天空

Image peo = Toolkit.getDefaultToolkit ().getImage ("imags/peo.PNG"); //矿工

Image water = Toolkit.getDefaultToolkit ().getImage ("imags/water.PNG");//药水图片

//绘制

void piantSeif(Graphics g){

g.drawImage (bg,0,200,null);

g.drawImage (bg1,0,0,null);

switch (Gametest.state){

case 0:

drawWord (g,80,Color.green,"准备开始",200,400);

break;

case 1:

g.drawImage (peo,310,50,null);

drawWord (g,30,Color.black,"积分:"+count,30,150);

//药水组件

g.drawImage (water,450,40,null);

drawWord (g,30,Color.black,"*"+waterNum,510,70);

//关卡数

drawWord (g,20,Color.black,"第"+level+"关",30,60);

//目标积分

drawWord (g,30,Color.black,"目标"+goal,30,110);

//时间组件

emdTime = System.currentTimeMillis ();

long tim = 20-(emdTime-startTime) /1000;

drawWord (g,30,Color.black,"时间"+(tim>0?tim:0),520,150);

break;

case 2:

//药水组件

g.drawImage (water,300,400,null);

drawWord (g,30,Color.black,"价格"+price,300,500);

drawWord (g,30,Color.black,"是否购买?",300,550);

if(shop){

count = count-price;

waterNum++;

shop=false;

Gametest.state=1;

startTime=System.currentTimeMillis ();

}

break;

case 3:

drawWord (g,80,Color.cyan,"失败",250,350);

drawWord (g,80,Color.cyan,"积分:"+count,200,450);

break;

case 4:

drawWord (g,80,Color.red,"成功",250,350);

drawWord (g,80,Color.red,"积分:"+count,200,450);

break;

default:

}

}

//t倒计时完成,f正在倒计时

boolean gametime (){

long tim =(emdTime-startTime)/1000;

if(tim>20){

return true;

}

return false;

}

//重置元素

void reGame(){

//关卡数

level = 1;

//目标得分

goal = level*5;

//总分

count = 0;

//药水数量

waterNum = 3;

//药水状态,默认是F,T表示正在使用

waterFlag = false;

}

//绘制字符串

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);

       }

}

Gametest类

package com.sxt;

import java.awt.*;

import java.awt.event.MouseAdapter;

import java.awt.event.MouseEvent;

import java.util.ArrayList;

import java.util.List;

import javax.swing.*;

//继承JFrame类

public class Gametest extends JFrame {

//0未开始 1运行 2商店 3失败 4胜利

static int state;

//存储金块,石块

List<Object> objectList = new ArrayList<> ();

Bg bg = new Bg ();

line line = new line (this);

//金子

//添加金块:无参构造

{

//是否可以放置

boolean isPlace = true;

//金块创建

for (int i = 0; i < 11; i++) {

double random = Math.random ();

Gold gold;//存放当前生产的金块

if(random<0.3){gold = new GoldMini ();}

else if(random<0.7){ gold = new Gold ();}

else {gold = new 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 < 5; 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--;}

}

}

//闪动问题

Image offScreenImage;

void launch(){

this.setVisible (true);

this.setSize (768,1000);

this.setLocationRelativeTo (null);

this.setTitle ("bubbles矿工");

//关闭窗口

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&&line.state==0) {

line.state = 1; }

//抓取返回,点击右键

if(e.getButton ()==3&&line.state==3&&Bg.waterNum>0){

Bg.waterFlag=true;

Bg.waterNum--;

}

break;

case 2:

if(e.getButton ()==1){

bg.shop=true;

}

if(e.getButton ()==3){

state=1;

bg.startTime = System.currentTimeMillis ();

}

break;

case 3:

case 4:

if(e.getButton ()==1){

state=0;

bg.reGame ();

line.reGame ();

}

break;

default:

}

}

});

while (true) {

repaint ();

nextLevel ();

//添加延时效果

try {

Thread.sleep (10);

} catch (InterruptedException e) {

e.printStackTrace ();

}

}

}

//下一关

public void nextLevel(){

if(bg.gametime () && state==1){

if(Bg.count >= bg.goal){

if(Bg.level==5){

state=4;

}

else {

state = 2;

Bg.level++;

}

}else{

state=3;

}

dispose ();

Gametest gametest1 = new Gametest ();

gametest1.launch ();

}

}

//绘制方法

@Override

public void paint(Graphics g) {

offScreenImage = this.createImage (768,1000 );

//添加画笔

Graphics gImage = offScreenImage.getGraphics ();

bg.piantSeif (gImage);

if(state==1){

//绘制物体

for(Object obj:objectList){

obj.paintSeif (gImage);

}

line.pianSelf (gImage);

}

g.drawImage (offScreenImage,0,0,null);

}

public static void main(String[] args) {

Gametest gametest = new Gametest ();

gametest.launch ();

        }

}

Gold类

package com.sxt;

import java.awt.*;
 //金块类
public class Gold extends Object{


    Gold(){
        //设置随机数金块
        this.x =(int)(Math.random()*700);
        this.y =(int)(Math.random()*550+300);
        this.width=52;
        this.height=52;
        this.flag = false;
        //拉取速度
        this.m = 30;
        this.count = 4;
        this.type=1;
        this.img = Toolkit.getDefaultToolkit ().getImage ("imags/gold1.GIF");

    }
}
class GoldMini extends Gold{
     GoldMini(){
        //设置金块宽高
         this.width=36;
         this.height=36;
         this.m= 15;
         this.count = 2;
         this.img = Toolkit.getDefaultToolkit ().getImage ("imags/gold0.GIF");
     }
        }
class GoldPlus extends Gold{
    GoldPlus(){
        this.x =(int)(Math.random()*650);
        //设置金块宽高
        this.width=105;
        this.height=105;
        this.m= 60;
        this.count = 8;
        this.img = Toolkit.getDefaultToolkit ().getImage ("imags/gold2.GIF");
    }
}

line类

package com.sxt;

import java.awt.*;

public class line {

     //起点坐标
     int x = 380;
     int y =180;
     //终点坐标
     int ebdx=500;
     int endy=500;
     //线长
     double length = 100;
      //线长最小值
     double MIN_length = 100;
      //线长最大值
     double MAX_length = 750;
     double n = 0;
     //方向
     int dir = 1;
     //状态 0 摇摆 1 抓取 2 收回 3.抓取返回
    int state;
    //钩爪图片
    Image hook = Toolkit.getDefaultToolkit ().getImage ("imags/hook.PNG");
     Gametest frname;
     line(Gametest frname){ this.frname=frname; }

     //碰撞检测,检测物体是否被抓取
     void logic(){
         for (Object obj:this.frname.objectList) {
             if(ebdx>obj.x && ebdx<obj.x+obj.width
                     && endy>obj.y && endy<obj.y+obj.height){
                 state = 3;
                 obj.flag=true;
             }
         }

     }

     //绘制方法
     void lines(Graphics g){
         ebdx = (int) (x +length*Math.cos (n*Math.PI)); //强制转换(int)()
         endy = (int) (y +length*Math.sin (n*Math.PI)); //强制转换(int)()
         g.setColor (Color.red);
         g.drawLine (x-1,y,ebdx-1,endy);
         g.drawLine (x,y,ebdx,endy);
         g.drawLine (x+1,y,ebdx+1,endy); //传入坐标
         g.drawImage (hook,ebdx-36,endy-2,null);

    }
     void pianSelf(Graphics g){
      //绘制中的判断 logic
       logic ();
        switch (state) {
            case 0:
                if (n < 0.1) {
                    dir = 1;
                } else if (n > 0.9) {
                    dir = -1;
                }
                //让线动起来
                n = n + 0.005 * dir;
                lines (g);
                break;
            case 1:
                if (length < MAX_length) {
                    length = length + 5;
                    lines (g);
                } else {
                    state = 2;
                }

                break;
            case 2:
                //收回
                if (length > MIN_length) {
                    length = length - 5;
                    lines (g);
                } else {
                    state = 0;
                }
                break;
            case 3:
                int m = 1;
                //抓取返回
                if (length > MIN_length) {
                    length = length - 5;
                    lines (g);
                    for (Object obj : this.frname.objectList) {
                        if (obj.flag) {
                            m = obj.m;
                            obj.x = ebdx - obj.getWidth () / 2;
                            obj.y = endy;
                            if (length <= MIN_length) {
                                obj.x = -150;
                                obj.y = -150;
                                obj.flag = false;
                                Bg.waterFlag=false;
                                //加分
                                Bg.count+=obj.count;
                                state = 0;
                            }
                            if (Bg.waterFlag){
                               if (obj.type==1){
                                  m=1;
                               }
                                if(obj.type==2){
                                    obj.x = -150;
                                    obj.y = -150;
                                    obj.flag = false;
                                    Bg.waterFlag=false;
                                    state = 2;
                                }
                            }
                        }

                    }

                }
                //添加延时
         try{
                Thread.sleep (m);
        }catch (InterruptedException e){
             e.printStackTrace ();
         }
           break;
         default:
        }

    }

    //重置线
    void reGame(){
    n=0;
    length=100;
}

}

Objct类

package com.sxt;

import java.awt.*;

public class Object {
   //坐标
   int x;
   int y;


   //宽高
  int width;
  int height;
  //图片
     Image img;
   //标记,是否能移动
    boolean flag;
    //质量
    int m;
    //积分
    int count;
    //类型 1.金块 2.石块
    int type;
    void paintSeif(Graphics g){
       g.drawImage (img,x,y,null);
     }

    public int getWidth() { return width; }
  //获取矩形
    public Rectangle getRec(){
         return new Rectangle (x,y,width,height);
    }
}

Rock类

package com.sxt;

import java.awt.*;

//石块类
public class Rock extends Object {
    Rock(){
        this.x =(int)(Math.random()*700);
        this.y =(int)(Math.random()*550+300);
        this.width=71;
        this.height=71;
        this.flag = false;
        //拉取速度
        this.m =50;
        this.count = 1;
        this.type=2;
        this.img = Toolkit.getDefaultToolkit ().getImage ("imags/rock1.PNG");
    }
}

以上是关于黄金矿工小游戏制作步骤的主要内容,如果未能解决你的问题,请参考以下文章

教你用matlab制作一款黄金矿工小游戏

重回童年4399| 黄金矿工游戏制作+解析

重回童年4399| 黄金矿工游戏制作+解析

重回童年4399| 黄金矿工游戏制作+解析

scratch高阶案例教学scratch黄金矿工 scratch创意编程 少儿编程 手把手教小朋友们从零制作黄金矿工游戏

scratch高阶案例教学scratch黄金矿工 scratch创意编程 少儿编程 手把手教小朋友们从零制作黄金矿工游戏