Android 开发--利用android studio 制作简单文字打怪升级游戏(伪地牢类)2.主页面及部分事件设计

时隔大半年,总算抽出时间更新这个博客了。去年下半年因为大四忙着备战考研,一直没有心情将第二篇继续更新,今年上半年又忙着搞毕业设计,焦头烂额了属于是orz

由于我的android studio的模拟器实在太卡了(用过的都知道),所以这一次的所有截图录屏都是我自己的手机来操作,看客老爷别太介意。。

言归正题,看过我上一篇文章的就知道,我们已经设计好了游戏前置事件以及设置等页面,现在摆在我们面前的就是对于游戏主体的设计。

一个游戏的趣味性必不可少,我目前给游戏设计的事件暂且有几种,分别是遇到怪物,发现宝箱,赌场事件,神秘守护者,商店老板,可怜的小女孩,小恶魔,每种事件的发生是随机的,在发生事件后,可以选择面对或者离开。除在发现怪物外,离开将不会带来任何影响。

在主页ui设计后,我将对这些事件进行详细的介绍。现在我们先介绍主页设计。

一个打怪升级的游戏必然得有一个较为简洁的主页面,不然看起来便花里胡哨,并且很难操作。为此,我特意录了一小段视频给各位看官老爷看看。视频有点小长,算是实机演示吧。

看完了视频,我接着说下面的内容。

游戏实机演示

首先是游戏的主体,肯定是打怪升级。既然是打怪,必然要有一套合理完备的战斗系统。在这里,我采用了传统的血条,蓝条的系统,包括攻击力,防御力,幸运值的设置。攻击力不足防御力则无法对敌人造成伤害,同理也是如此,当防御力高于敌方时将可能无伤通过战斗。

注意上面说的是可能。因为游戏中还设置了技能系统,作为怪物也可能存在专属的技能,主角也可以通过装备技能来武装自己。当然,在战斗开始前,设置了查看属性的功能,主角可以根据遇到的怪物的属性技能来确定策略,如果选择战斗,战斗中被击败后将直接结束游戏,当然选择逃跑也有可能付出代价:有可能会失去20%到50%的当前血量。下面是查看怪物属性。老鼠王是首领战,对于首领战不能逃跑,成功击败后将会进入下一个场景。

 

 

战斗模块的函数较为繁琐,简单来说,回合制,每回合可以进行普攻或者放技能或者使用恢复药剂,和其他游戏并无太多差异。看客老爷在看完开始的视频大概应该有所了解了。当然说着容易做着难,在真正编写时还是遇到了不少困难。对于怪物而言,其为随机释放技能,因此可能出现多次连续释放技能或者一场战斗一次也不用的情况。这是fight.java的文件。

package com.example.dungeonsimulator;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.KeyEvent;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.ProgressBar;
import android.os.Handler;
import android.os.Message;
import android.view.ViewGroup;
import android.widget.Button;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;
import android.graphics.Color;

import java.util.ArrayList;
import java.util.Random;

import androidx.appcompat.app.AppCompatActivity;



import java.util.Timer;
import java.util.TimerTask;
import android.view.KeyEvent;
public class fight extends AppCompatActivity{
    private static boolean win= true;
    LinearLayout ll1,ll2,ll3,ll4;
    private static int turn=0,num=0;

    TextView monster_name,skill,lead_name,monster_level,monster_words,monster_hp,lead_hp,monster_agress,monster_defend,monster_lucky,lead_agress,lead_defend,lead_lucky,textView,lead_level,lead_mp;
    Button attack;
    String text="";
    monster mon;
    Message ret;
    Handler handler;
    person a;
    ProgressBar progressbar1,progressbar2,progressbar3;
    Timer timer;  @Override
    public boolean onKeyDown(int keyCode, KeyEvent event){
        if(keyCode== android.view.KeyEvent.KEYCODE_BACK)
            return true;

        return super.onKeyDown(keyCode, event);
    }
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.fight);
        turn=0;
        monster_name = findViewById(R.id.monster_name);
        lead_name = findViewById(R.id.lead_name);
        monster_level = findViewById(R.id.monster_level);
        monster_words= findViewById(R.id.monster_words);
        skill=findViewById(R.id.skill);
        lead_name = findViewById(R.id.lead_name);
        monster_hp= findViewById(R.id.monster_hp);
        lead_hp = findViewById(R.id.lead_hp);
        ll1=findViewById(R.id.linear1);
        ll2=findViewById(R.id.ll2);
        ll3=findViewById(R.id.ll3);
        ll4=findViewById(R.id.ll4);
        MainActivity.lead.kill+=1;


        textView=findViewById(R.id.textView);
        lead_level=findViewById(R.id.lead_level);
        monster_agress = findViewById(R.id.monster_agress);
        monster_defend = findViewById(R.id.monster_defend);
        monster_lucky = findViewById(R.id.monster_lucky);
        lead_lucky = findViewById(R.id.lead_lucky);
        lead_agress = findViewById(R.id.lead_agress);
        lead_defend= findViewById(R.id.lead_defend);
        attack= findViewById(R.id.attack);
        progressbar1=findViewById(R.id.progressBar1);
        progressbar2=findViewById(R.id.progressBar2);
        progressbar3=findViewById(R.id.progressBar3);
        lead_mp=findViewById(R.id.lead_mp);
        initmonster.add();
        ArrayList<buff> temp1=new ArrayList<>();
        ArrayList<debuff> temp2=new ArrayList<>();
        a=new person(MainActivity.lead.get_aggress(),MainActivity.lead.get_defend(),MainActivity.lead.get_lucky(),MainActivity.lead.hp,MainActivity.lead.nowhp,MainActivity.lead.magic,MainActivity.lead.nowmagic,temp1,temp2);
        int level=main.monster_level;

        int i=main.monster_index;
        mon=new monster(level,initmonster.monster_index[i][1],initmonster.monster_index[i][2],
                initmonster.monster_index[i][3],initmonster.monster_index[i][4],initmonster.monster_index[i][5],initmonster.monster_index[i][6],initmonster.monster_index[i][7],initmonster.monster_index[i][8],initmonster.monster_index[i][9],initmonster.monster_index[i][10],initmonster.monster_index[i][11],initmonster.monster_index[i][12]);

        set_text();
        set_lead();
        set_monster();
        set_humanhp();
        set_monhp();
        set();
        set_humanmp();




        attack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Random r=new Random();
                String temp1="";
                int random_num=r.nextInt(100);
                if(MainActivity.lead.name.equals("刺客")){
                temp1=fight_process(MainActivity.lead,mon,main.monster_index);}
                else{
                    if(monster_skill.have_skill[main.monster_index].length>0){
                        if(random_num<40){
                            temp1=monster_skill();
                            p_debuff(a);
                            turn+=1;
                        }
                        else{
                            temp1=fight_process(MainActivity.lead,mon,main.monster_index);
                            p_debuff(a);
                        }}
                    else{
                        temp1=fight_process(MainActivity.lead,mon,main.monster_index);
                        p_debuff(a);
                    }
                }
                String temp2="";
                if(MainActivity.lead.nowhp>0&&mon.nowhp>0){

                    if(!MainActivity.lead.name.equals("刺客")){
                        temp2=fight_process(MainActivity.lead,mon,main.monster_index);}
                    else{
                        if(monster_skill.have_skill[main.monster_index].length>0){
                        if(random_num<30){
                            temp2=monster_skill();
                            if(a.nowhp<=0){
                                a.nowhp=0;
                                MainActivity.back=0;

                                timer.cancel();
                                main.second=false;
                                main.first=false;
                                MainActivity.player.release();
                                Intent intent = new Intent(fight.this, die.class);
                                startActivity(intent);

                            }
                            turn+=1;
                        }
                        else{
                            temp2=fight_process(MainActivity.lead,mon,main.monster_index);
                            p_debuff(a);
                        }}
                        else{
                            temp2=fight_process(MainActivity.lead,mon,main.monster_index);
                            p_debuff(a);
                        }

                    }}
                text+=temp1;
                text+=temp2;

            }
        });
        handler=new Handler(){
            @Override
            public void handleMessage(Message msg) {
                super.handleMessage(msg);

                if (msg.what==0){
                    set_text();
                      set_humanhp();
                    set_monhp();
                    set_humanmp();
                    change();




                }

            }
        };

        timer = new Timer();
        timer.schedule(new TimerTask() {
            @Override
            public void run() {

                handler.sendEmptyMessage(0);

            }
        },0,5);

    }

    private void set_text(){
        textView.setText(text);
    }

    private void change(){
        monster_agress.setText(String.valueOf(mon.get_aggress()));
        monster_defend.setText(String.valueOf(mon.get_defend()));
        monster_lucky.setText(String.valueOf(mon.get_lucky()));
    }
    private void set_lead(){
        int level=MainActivity.lead.level;
        lead_name.setText(MainActivity.lead.get_name());
        String slevel="lv"+String.valueOf(level);
        int agress=a.get_aggress();
        int defend=a.get_defend();
        int lucky=a.get_lucky();
        lead_agress.setText(String.valueOf(agress));

        lead_defend.setText(String.valueOf(defend));
        lead_lucky.setText(String.valueOf(lucky));
        lead_level.setText(slevel);





    }
    private void set_monster(){
        String slevel="lv";
        int level=main.monster_level;

        int i=main.monster_index;
        slevel+=String.valueOf(level);
        String temp="";
        if(monster_skill.have_skill[i].length>0){
            for(int index=0;index<monster_skill.have_skill[i].length;index++){
                  temp+=monster_skill.skill_names[monster_skill.have_skill[i][index]];
                  temp+="\n";

            }
            skill.setText(temp);
        }

        monster_level.setText(slevel);
        monster_name.setText(event_box.monsters[main.monster_index]);
        monster_words.setText(initmonster.monster_words[main.monster_index]);

        monster_agress.setText(String.valueOf(mon.get_aggress()));
        monster_defend.setText(String.valueOf(mon.get_defend()));
        monster_lucky.setText(String.valueOf(mon.get_lucky()));






    }
    public String  monster_skill(){
        int i=main.monster_index;
        int len=monster_skill.have_skill[i].length;

        Random r=new Random();
        int next_num=r.nextInt(len);
       String temp= monster_effect(monster_skill.have_skill[i][next_num],monster_skill.skill_index[monster_skill.have_skill[i][next_num]],a,mon,monster_skill.skill_value[monster_skill.have_skill[i][next_num]]);

       return temp;
    }
    public  String monster_effect(int nu,int ind,person a,monster b,int value){
        String temp1=event_box.monsters[main.monster_index];
        temp1+="使用了";
        num+=1;
        temp1+=monster_skill.skill_names[nu];
        temp1+=",";

        if(ind==0){
            value-=a.get_defend();
            temp1+="对你造成了";
            temp1+=String.valueOf(value);
            temp1+="点伤害!\n";
            a.nowhp-=value;
            if(a.nowhp<0){
                a.nowhp=0;
                Intent intent = new Intent(fight.this, MainActivity.class);
                startActivity(intent);

            }
        }
        if(ind==1){
            a.nowhp-=value;
            temp1+="对你造成了";
            temp1+=String.valueOf(value);
            temp1+="点伤害!\n";
            if(a.nowhp<0){
                a.nowhp=0;
                Intent intent = new Intent(fight.this, die.class);
                startActivity(intent);

            }

        }
        if(ind==2) {
            temp1 += "为自身回复了";
            temp1 += String.valueOf(value);
            temp1 += "点血量!\n";
            b.nowhp += value;
            if (b.nowhp > b.hp) {
                b.nowhp = b.hp;
            }
        }
        if(ind==3){
            temp1+="提升了自身";
            temp1+=String.valueOf(value);
            temp1+="点攻击力!\n";
           int temp=b.get_aggress()+value;
           b.set_aggress(temp);
           }
        if(ind==4){
            temp1+="提升了自身";
            temp1+=String.valueOf(value);
            temp1+="点防御力!\n";
            int temp=b.get_defend()+value;
            b.set_defend(temp);
          }
        if(ind==5){
            temp1+="提升了自身";
            temp1+=String.valueOf(value);
            temp1+="点幸运值!\n";
            int temp=b.get_lucky()+value;
            b.set_lucky(temp);
        }
        if(ind==6){
            temp1+="对你造成了";
            temp1+=String.valueOf(value);
            temp1+="点伤害,回复了自身";
            temp1+=String.valueOf(value);
            temp1+="点生命值!\n";
            a.nowhp-=value;
            b.nowhp+=value;
            if(a.nowhp<0){
                a.nowhp=0;
                Intent intent = new Intent(fight.this, die.class);
                startActivity(intent);

            }
            if (b.nowhp > b.hp) {
                b.nowhp = b.hp;
            }

        }
        if(ind==7){
            temp1+="提升了自身";
            temp1+=String.valueOf(value);
            temp1+="%攻击力!\n";
            int temp=b.get_aggress()*(100+value)/100;
            b.set_aggress(temp);
        }
        if(ind==8){
            temp1+="给你施加了debuff:每回合失去";
            temp1+=String.valueOf(value);
            temp1+="%生命值!\n";
            debuff de=new debuff(monster_skill.skill_names[nu],3,value,monster_skill.lasting_time[main.monster_index],monster_skill.image_index[nu]);

            a.pdebuff.add(de);
            LinearLayout.LayoutParams layoutParams1 = new LinearLayout.LayoutParams(20, 20);
            NumImageView iv1 = new NumImageView(fight.this);



            layoutParams1.setMargins(0, 0, 10, 0);
            iv1.setImageResource(debuff.debufflist[de.index]);
            iv1.setNum(de.turn);


            ll4.addView(iv1);
            iv1.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    final AlertDialog.Builder normalDialog =
                            new AlertDialog.Builder(fight.this);
                    normalDialog.setTitle(de.name);
                    String temp1="每回合失去";
                    temp1+=de.value;
                    temp1+="%的生命值";
                    normalDialog.setMessage(temp1);
                    normalDialog.show();
                    ;}});


            }








        if(ind==9){
            temp1+="降低了你";
            temp1+=String.valueOf(value);
            temp1+="%防御力!\n";
            int temp=a.get_defend()*(100-value)/100;
            a.set_defend(temp);
        }
        if(ind==10){
            temp1+="对你造成了两次伤害!\n";
            a.nowhp-=2*(b.aggress-a.defend);

        }

        if(ind==11){
            temp1+="提升了自身";
            temp1+=String.valueOf(value);
            temp1+="%的防御力!\n";
           b.defend=(100+value)/100*b.defend;

        }

        if(ind==12){
            temp1+="对你造成了";
            temp1+=String.valueOf(value);
            temp1+="%的攻击伤害!\n";
            a.nowhp-=value/100*(b.aggress-a.defend);

        }
        if(ind==13){
            temp1+="给你施加了debuff:三回合内不击杀本体,将失去50";
            temp1+="%生命值!\n";
            debuff de=new debuff(monster_skill.skill_names[nu],4,value,monster_skill.lasting_time[nu],monster_skill.image_index[nu]);

            a.pdebuff.add(de);
            LinearLayout.LayoutParams layoutParams1 = new LinearLayout.LayoutParams(20, 20);
            NumImageView iv1 = new NumImageView(fight.this);



            layoutParams1.setMargins(0, 0, 10, 0);
            iv1.setImageResource(debuff.debufflist[de.index]);
            iv1.setNum(de.turn);


            ll4.addView(iv1);
            iv1.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    final AlertDialog.Builder normalDialog =
                            new AlertDialog.Builder(fight.this);
                    normalDialog.setTitle(de.name);
                    String temp=String.valueOf(monster_skill.lasting_time[nu]);
                    temp+="回合后失去";
                    temp+="50%的生命值";
                    normalDialog.setMessage(temp);
                    normalDialog.show();
                    ;}});

        }



        return temp1;

    }

    public String fight_process(person a,monster b,int index){
         String process="";
         int agressa=a.get_aggress();
         int defenda=a.get_defend();
         int magica=a.get_magic();
         int luckya=a.get_lucky();
        int agressb=b.get_aggress();
        int defendb=b.get_defend();
        num+=1;
        int magicb=b.get_magic();
        int luckyb=b.get_lucky();
        Random r = new Random();
        int random1=r.nextInt(100);
        if(!a.name.equals("刺客")){
        if(turn %2==0){
            if(random1<luckya){
                process+="你往旁边一躲,险之又险地闪避了";
                process+=event_box.monsters[index];
                process+="的攻击!\n";
            }
            else{
                int damage=agressb-defenda;
                if(damage<0){
                    damage=0;
                }
                a.nowhp-=damage;
                if(a.nowhp<=0){
                   a.nowhp=0;
                    MainActivity.back=0;

                    timer.cancel();
                    main.second=false;
                    main.first=false;
                    MainActivity.player.release();
                    Intent intent = new Intent(fight.this, die.class);
                    startActivity(intent);

                }
                String temp =String.valueOf(damage);
                process+=event_box.monsters[index];

                process+="上前攻击,对你";
                process+="造成了";
                process+=temp;
                process+="点伤害!\n";}
        }

        else{
            if(random1<luckyb){
                process+=event_box.monsters[index];
                process+="往旁边一躲,一下子闪避了你";
                process+="的攻击!\n";

            }
            else{
                int damage=agressa-defendb;
                if(damage<0){
                    damage=0;
                }
                mon.nowhp-=damage;
                if(mon.nowhp<=0){

                    mon.nowhp=0;
                    timer.cancel();
                    String temp="你战胜了(lv";
                    temp+=String.valueOf(mon.level);
                    temp+=")";
                    MainActivity.lead.nowexp+=b.exp;
                    MainActivity.lead.gold+=b.gold;
                    MainActivity.lead.nowhp=a.nowhp;
                    temp+=event_box.monsters[main.monster_index];
                    temp+=",你获得了";
                    temp+=String.valueOf(b.exp);
                    temp+="点经验值和";
                    temp+=String.valueOf(b.gold);
                    temp+="个金币。\n";
                    main.text+=temp;
                    main.refresh=true;
                    main.index+=1;
                  if(main.second==true){
                      main.first=false;
                      main.second=false;
                      Intent intent = new Intent(fight.this, stage.class);
                      startActivity(intent);
                  }
                  else{
                    Intent intent = new Intent(fight.this, main.class);
                    startActivity(intent);}

                }
                String temp =String.valueOf(damage);
                process+="你上前攻击,对";
                process+=event_box.monsters[index];
                process+="造成了";
                process+=temp;
                process+="点伤害!\n";}

                }}

        else{if(turn %2!=0){
            if(random1<luckya){
                process+="你往旁边一躲,险之又险地闪避了";
                process+=event_box.monsters[index];
                process+="的攻击!\n";
            }
            else{
                int damage=agressb-defenda;
                if(damage<0){
                    damage=0;
                }
                a.nowhp-=damage;
                if(a.nowhp<=0){
                    a.nowhp=0;
                    MainActivity.back=0;
                    main.second=false;
                    main.first=false;
                    MainActivity.player.release();
                    timer.cancel();
                    Intent intent = new Intent(fight.this,die.class);
                    startActivity(intent);

                }
                String temp =String.valueOf(damage);
                process+=event_box.monsters[index];

                process+="上前攻击,对你";
                process+="造成了";
                process+=temp;
                process+="点伤害!\n";}
        }

        else{
            if(random1<luckyb){
                process+=event_box.monsters[index];
                process+="往旁边一躲,一下子闪避了你";
                process+="的攻击!\n";

            }
            else{
                int damage=agressa-defendb;
                if(damage<0){
                    damage=0;
                }
                mon.nowhp-=damage;
                if(mon.nowhp<=0){
                    mon.nowhp=0;
                    timer.cancel();
                    String temp="你战胜了(lv";
                    temp+=String.valueOf(mon.level);
                    temp+=")";
                    MainActivity.lead.nowexp+=b.exp;
                    MainActivity.lead.gold+=b.gold;
                    MainActivity.lead.nowhp=a.nowhp;
                    temp+=event_box.monsters[main.monster_index];
                    temp+=",你获得了";
                    temp+=String.valueOf(b.exp);
                    temp+="点经验值和";
                    temp+=String.valueOf(b.gold);
                    temp+="个金币。\n";
                    main.text+=temp;
                    main.index+=1;
                    main.refresh=true;



                    if(main.second==true){
                        main.first=false;
                        main.second=false;
                        Intent intent = new Intent(fight.this, stage.class);
                        startActivity(intent);
                    }
                    else{
                        Intent intent = new Intent(fight.this, main.class);
                        startActivity(intent);}

                }
                String temp =String.valueOf(damage);
                process+="你上前攻击,对";
                process+=event_box.monsters[index];
                process+="造成了";
                process+=temp;
                process+="点伤害!\n";}

        }}

          turn +=1;

         return process;

    }

    private void set_humanhp(){
        String temp=String.valueOf(a.nowhp);
        temp+="/";
        temp+=String.valueOf(a.hp);
        lead_hp.setText(temp);
        int progress=a.nowhp*100/a.hp;
        progressbar2.setProgress(progress);
    }
    private void set_humanmp(){
        String temp=String.valueOf(a.nowmagic);
        temp+="/";
        temp+=String.valueOf(a.get_magic());
        lead_mp.setText(temp);
        int progress=a.nowmagic*100/a.get_magic();
        progressbar3.setProgress(progress);
    }
    private void get_pet(){
        Random r=new Random();
        int rand=r.nextInt(100);
        if(rand<15){
            int num=r.nextInt(10);
            int qu=0;
            if(num<5){
                qu=1;

            }
            if(num>5&&num<8){
                qu=2;
            }
            if(num>8){
                qu=3;
            }
            String temp="同时你获得了从怪物身上掉落的一枚怪物蛋,你摇了摇头,收了起来。\n";
            main.text+=temp;
            int i=main.monster_index;
            pets mypet=new pets(initpets.names[i],initpets.attribute[i][0],initpets.attribute[i][1],initpets.attribute[i][2],initpets.attribute[i][3],initpets.attribute[i][4],
                    initpets.attribute[i][5],initpets.attribute[i][6],initpets.attribute[i][7],qu);
            MainActivity.lead.have_pets.add(mypet);

        }

    }

    private void set_monhp(){
        String temp=String.valueOf(mon.nowhp);
        temp+="/";
        temp+=String.valueOf(mon.hp);
        monster_hp.setText(temp);
        int progress=mon.nowhp*100/mon.hp;
        progressbar1.setProgress(progress);
    }
    private void set_consume(int i){
        LinearLayout.LayoutParams layoutParams1 = new LinearLayout.LayoutParams(100, 100);
        NumImageView iv1 = new NumImageView(fight.this);



        layoutParams1.setMargins(0, 0, 0, 0);
        iv1.setImageResource(consumables.myconsumeList[i]);
        iv1.setNum(MainActivity.lead.have_consume[i]);


        ll2.addView(iv1);
        iv1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Random r =new Random();
                int random_num=r.nextInt(100);
                 MainActivity.lead.have_consume[i]-=1;
                int type=consumables.con_types[i];
                if(type==0){
                   a.nowhp+=consumables.con_value[i];
                    if(a.nowhp>a.hp){
                       a.nowhp=a.hp;
                    }
                }
                if(type==1){
                    a.nowmagic+=consumables.con_value[i];
                    if(a.nowmagic>a.magic){
                        a.nowmagic=a.magic;
                    }
                }
                String temp="你使用了";
                temp+=consumables.con_names[i];
                temp+=",";

                if(MainActivity.lead.have_consume[i]==0) {
                    ll2.removeView(iv1);

                }
                iv1.setNum(MainActivity.lead.have_consume[i]);
                temp+="你";
                temp+=consumables.introductions[i];
                temp+="\n";
                if(MainActivity.lead.name.equals("刺客")){
                    turn+=1;
                }

            String temp2="";
                if(monster_skill.have_skill[main.monster_index].length>0){
                    if(random_num<30){
                        temp2=monster_skill();
                        turn+=1;
                    }
                    else{
                        temp2=fight_process(a,mon,main.monster_index);
                        p_debuff(a);
                    }}
                else{
                    temp2=fight_process(a,mon,main.monster_index);
                    p_debuff(a);
                }
                if(!MainActivity.lead.name.equals("刺客")){
                    turn+=1;
                }
                text+=temp;
                text+=temp2;

                }});
    }

    private void set_skill(int index) {
        LinearLayout.LayoutParams layoutParams1 = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        ImageView iv1 = new ImageView(fight.this);

       int i=MainActivity.lead.skills[index];

        layoutParams1.setMargins(120, 0, 0, 0);
        iv1.setImageResource(initskills.myskillList[i]);
        iv1.setLayoutParams(layoutParams1);
        ll1.addView(iv1);
        iv1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Random r=new Random();
                int random_num=r.nextInt(40);
                if(a.nowmagic<skills.skill_mp[i]){
                    String temp="你没有足够的魔法值!";
                    Toast toast = Toast.makeText(fight.this, temp, Toast.LENGTH_SHORT);
                    toast.setGravity(Gravity.CENTER, 0, 0);
                    toast.show();
                }
                else{
               String temp1=effect(i,skills.skill_index[i],a,mon,skills.skill_mp[i],skills.skill_value[i]);
                    String temp2="";
                    if(mon.nowhp<=0){
                    mon.nowhp=0;
                    timer.cancel();
                    String temp="你战胜了(lv";
                    temp+=String.valueOf(mon.level);
                    temp+=")";
                    MainActivity.lead.nowexp+=mon.exp;
                    MainActivity.lead.gold+=mon.gold;
                    temp+=event_box.monsters[main.monster_index];
                    temp+=",你获得了";
                    temp+=String.valueOf(mon.exp);
                    temp+="点经验值和";
                    temp+=String.valueOf(mon.gold);
                    temp+="个金币。\n";
                    main.text+=temp;
                    main.index+=1;
                    main.refresh=true;

                        if(main.second==true){
                            main.first=false;
                            main.second=false;
                            Intent intent = new Intent(fight.this, stage.class);
                            startActivity(intent);
                        }
                        else{
                            Intent intent = new Intent(fight.this, main.class);
                            startActivity(intent);}}
               if(MainActivity.lead.name.equals("刺客")){
                   turn+=1;
               }
                    if(MainActivity.lead.nowhp>0&&mon.nowhp>0){
                        if(monster_skill.have_skill[main.monster_index].length>0){
                            if(random_num<30){
                                temp2=monster_skill();
                                p_debuff(a);
                                turn+=1;
                            }
                            else{
                                temp2=fight_process(a,mon,main.monster_index);
                                p_debuff(a);
                            }}
                        else{
                            temp2=fight_process(a,mon,main.monster_index);
                            p_debuff(a);
                        }}
                if(!MainActivity.lead.name.equals("刺客")){
                    turn+=1;
                }
                    text+=temp1;
                    text+=temp2;



            }}
        });









}
   public void show(person a,monster b){

   }

    private  void p_debuff(person a ){
        String temp="";
        if(a.pdebuff.size()==0){
            return ;
        }
        else{
            ll4.removeAllViews();
            for(debuff i: a.pdebuff){
                if(i.type==3){
            if(i.turn>0){
                    a.nowhp-=0.01*i.value*a.hp;
                    temp="你受到了debuff";
                    temp+=i.name;
                    temp+="效果,失去了";
                    temp+=i.value;
                    temp+="%的生命值。\n";
                    text+=temp;





                    i.turn-=1;
                LinearLayout.LayoutParams layoutParams1 = new LinearLayout.LayoutParams(20, 20);
                NumImageView iv1 = new NumImageView(fight.this);



                layoutParams1.setMargins(0, 0, 10, 0);
                iv1.setImageResource(debuff.debufflist[i.index]);
                iv1.setNum(i.turn);


                ll4.addView(iv1);
                iv1.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        final AlertDialog.Builder normalDialog =
                                new AlertDialog.Builder(fight.this);
                        normalDialog.setTitle(i.name);
                        String temp1="每回合失去";
                        temp1+=i.value;
                        temp1+="%的生命值";
                        normalDialog.setMessage(temp1);
                        normalDialog.show();
                        ;}});

                    if(i.turn==0){
                        ll4.removeView(iv1);
                    }




            }}
                if(i.type==4){
                    if(i.turn>0){
                    i.turn-=1;
                    LinearLayout.LayoutParams layoutParams1 = new LinearLayout.LayoutParams(20, 20);
                    NumImageView iv1 = new NumImageView(fight.this);



                    layoutParams1.setMargins(0, 0, 10, 0);
                    iv1.setImageResource(debuff.debufflist[i.index]);
                    iv1.setNum(i.turn);


                    ll4.addView(iv1);
                    iv1.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            final AlertDialog.Builder normalDialog =
                                    new AlertDialog.Builder(fight.this);
                            normalDialog.setTitle(i.name);
                            String temp=String.valueOf(i.turn);
                            temp+="回合后失去";
                            temp+="50%的生命值";
                            normalDialog.setMessage(temp);
                            normalDialog.show();
                            ;}});

                    if(i.turn==0){
                        a.nowhp-=0.01*i.value*a.nowhp;
                        temp="你受到了debuff";
                        temp+=i.name;
                        temp+="效果,失去了";
                        temp+=i.value;
                        temp+="%的生命值。\n";
                        text+=temp;








                            ll4.removeView(iv1);

                }}

            }

        }}


    }

    public  String effect(int nu,int ind,person a,monster b,int mp,int value){
        String temp1="你使用了";
       num+=1;
       temp1+=skills.skill_names[nu];
      temp1+=",";

        if(ind==0){
            value-=b.get_defend();
            temp1+="对敌人造成了";
            temp1+=String.valueOf(value);
            temp1+="点伤害!\n";
            b.nowhp-=value;
            a.nowmagic-=mp;
        }
        if(ind==1){
            b.nowhp-=value;
            temp1+="对敌人造成了";
            temp1+=String.valueOf(value);
            temp1+="点伤害!\n";
            a.nowmagic-=mp;
        }
        if(ind==2){
            temp1+="为自身回复了";
            temp1+=String.valueOf(value);
            temp1+="点血量!\n";
            a.nowhp+=value;
            if(a.nowhp>a.hp){
                a.nowhp=a.hp;
            }
            a.nowmagic-=mp;
        }
        return temp1;

        }



    private void set(){
        for(int i=0;i<2;i++){
            if(MainActivity.lead.skills[i]!=-1){
                set_skill(i);
            }
        }
        for(int i=0;i<MainActivity.lead.have_consume.length;i++){
            if(MainActivity.lead.have_consume[i]!=0){
                set_consume(i);
            }
        }
    }



}

下一个功能也是重头戏。我要说的正是酒馆功能。酒馆中可以进行购买装备,技能,以及恢复药剂。对于这些物品的出现是完全随机的,另外每个酒馆只会出现三件装备,三个技能,以及三瓶恢复药剂,买完或没有合适的只能遇到下一个酒馆在做打算。另外,随着等级的升高,其中的装备也会更新换代,变得更加强大。

 

说到装备,就不得不提一下装备的品质机制。在游戏中的装备生成是随机的,品质也是完全随机的。当然这句话所限定的目标是酒馆中的装备。至于为什么我下面会说明。装备品质分为普通,稀有,大师,和传说。同一件装备随着品质不同属性也会不同。在酒馆中购买时,刷出的装备大概率是稀有打下的,大师装备出现的可能则较小,更别说传说装备了。当然,高品质的装备的售价更贵。下面是酒馆的功能代码。

package com.example.dungeonsimulator;




import android.content.Context;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.graphics.Color;
import android.media.MediaPlayer;
import android.os.Handler;
import android.os.Message;
import android.util.AttributeSet;
import android.util.Log;
import android.app.Activity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.ViewGroup;
import android.widget.Button;
import android.view.View;
import android.app.AlertDialog;
import android.widget.EditText;
import android.media.MediaPlayer;
import androidx.appcompat.app.AppCompatActivity;
import android.database.sqlite.SQLiteDatabase;
import android.database.Cursor;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
import android.graphics.Canvas;
import android.widget.ImageView;
import java.util.Timer;
import java.util.TimerTask;
import android.content.DialogInterface;

public class pub extends AppCompatActivity {
    MediaPlayer player=null;
    Timer timer;
    Button leave;
    LinearLayout ll1,ll2,ll3,ll4,ll5,ll6;
    TextView textview2,gold;
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event){
        if(keyCode== android.view.KeyEvent.KEYCODE_BACK)
            return true;

        return super.onKeyDown(keyCode, event);
    }
    @Override

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.pub);
        ll1=findViewById(R.id.linear1);
        ll2=findViewById(R.id.linear2);
        ll3=findViewById(R.id.linear3);
        ll4=findViewById(R.id.linear4);
        ll5=findViewById(R.id.linear5);
        gold=findViewById(R.id.gold);
        textview2=findViewById(R.id.textView2);

        set_weapon();
        set_weapon();
        set_weapon();
        set_skill();
        set_skill();
        set_skill();
        set_consume();
        set_consume();
        set_consume();


        leave=findViewById(R.id.leave);

        leave.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
              
                timer.cancel();
                main.refresh=true;
                String temp="你离开了酒馆。\n";
                main.index+=1;
                main.text+=temp;
                Intent intent = new Intent(pub.this, main.class);
                startActivity(intent);

            }
        });



        Handler handler=new Handler(){
            @Override
            public void handleMessage(Message msg) {
                super.handleMessage(msg);

                if (msg.what==0){
                    set_gold();


                }

            }
        };

        timer = new Timer();
        timer.schedule(new TimerTask() {
            @Override
            public void run() {

                handler.sendEmptyMessage(0);

            }
        },0,4);



    }
    private void set_gold(){
        int num=MainActivity.lead.gold;
        gold.setText(String.valueOf(num));
    }
    private void set_weapon() {
        LinearLayout.LayoutParams layoutParams1 = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        int i=event_box.choose_weapon();
        int qu=event_box.choose_quality();
        int gold=initweapon.weapon_values[i][4];
        ImageView iv1 = new ImageView(pub.this);

        iv1.setLayoutParams(layoutParams1);
        layoutParams1.setMargins(0, 0, 0, 0);
        iv1.setImageResource(initweapon.myweaponList[i]);
        ll1.addView(iv1);
        iv1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                textview2.setText("酒馆老板:我们家的装备自然是最好的!");
                final AlertDialog.Builder normalDialog =
                        new AlertDialog.Builder(pub.this);
                normalDialog.setTitle(initweapon.weapon_names[i]+"("+initweapon.quality[qu]+")");
                String temp="属性:\n";
                for(int j=0;j<4;j++){
                    temp+=initweapon.attributes[j];
                    temp+=":+";
                    double temp1=initweapon.weapon_values[i][j]*initweapon.indexes[qu];
                    int a=(int)temp1;
                    temp+=a;
                    temp+="\n";
                }
                temp+=initweapon.attributes[4];
                temp+=":";
                double temp1=initweapon.weapon_values[i][4]*initweapon.indexes[qu];
                int a=(int)temp1;
                temp+=a;
                temp+="金币\n";
                if(MainActivity.lead.weapon[initweapon.weapon_values[i][6]]!=-1){
                    int index=MainActivity.lead.weapon[initweapon.weapon_values[i][6]];
                    double temp3=index/4;
                    int temp4=(int)temp3;
                    temp+="\n";
                    temp+="目前装备:\n";
                    temp+=initweapon.weapon_names[index/4];
                    temp+="(";
                    temp+=initweapon.quality[MainActivity.lead.weapon[initweapon.weapon_values[index/4][6]]%4];
                    temp+=")";
                    temp+="\n";
                    for(int j=0;j<4;j++){
                        temp+=initweapon.attributes[j];
                        temp+=":+";
                        double temp2=initweapon.weapon_values[temp4][j]*initweapon.indexes[MainActivity.lead.weapon[initweapon.weapon_values[index/4][6]]%4];
                        int b=(int)temp2;
                        temp+=b;
                        temp+="\n";

                    }}
                normalDialog.setMessage(temp);
                normalDialog.setPositiveButton("购买", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {

                        if(MainActivity.lead.gold<a){
                            String a="你没有足够的金币!";
                            Toast toast = Toast.makeText(pub.this, a, Toast.LENGTH_SHORT);
                            toast.setGravity(Gravity.CENTER, 0, 0);
                            toast.show();

                        }
                        else{
                        MainActivity.lead.gold-=a;
                        String temp="你购买了";
                        temp+=initweapon.weapon_names[i];
                        temp+="(";
                        temp+=initweapon.quality[qu];
                        temp+=")";
                        MainActivity.lead.have_weapons[4*i+qu]+=1;
                        temp+="*1!\n";

                            ll1.removeView(iv1);





                        Toast toast = Toast.makeText(pub.this, temp, Toast.LENGTH_SHORT);
                        toast.setGravity(Gravity.CENTER, 0, 0);
                        toast.show();}
                    }
                });
                normalDialog.setNegativeButton("确定", null);
                normalDialog.show();


            }
        });





                // 显示





}

    private void set_skill() {
        LinearLayout.LayoutParams layoutParams1 = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        int i=event_box.choose_skill();
        int gold=skills.skill_buy[i];
        ImageView iv1 = new ImageView(pub.this);



        layoutParams1.setMargins(0, 0, 0, 0);
        iv1.setLayoutParams(layoutParams1);
        iv1.setImageResource(initskills.myskillList[i]);
        ll4.addView(iv1);
        iv1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                textview2.setText("酒馆老板:我们家的技能自然是最好的!");
                final AlertDialog.Builder normalDialog =
                        new AlertDialog.Builder(pub.this);
                normalDialog.setTitle(skills.skill_names[i]);
                String temp="技能效果:\n";
               temp+= skills.introductions[i];
               temp+="\n";
               temp+="购入价格:";
                temp+=skills.skill_buy[i];
                temp+="金币\n";
                temp+="\n\n\n\n";
                temp+=skills.skill_words[i];


                normalDialog.setMessage(temp);
                normalDialog.setPositiveButton("购买", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {

                        if(MainActivity.lead.gold<skills.skill_buy[i]){
                            String a="你没有足够的金币!";
                            Toast toast = Toast.makeText(pub.this, a, Toast.LENGTH_SHORT);
                            toast.setGravity(Gravity.CENTER, 0, 0);
                            toast.show();

                        }
                        else{
                            MainActivity.lead.gold-=skills.skill_buy[i];
                            String temp="你购买了";
                            temp+=skills.skill_names[i];
                            MainActivity.lead.have_skills[i]+=1;
                            temp+="*1!\n";

                            ll4.removeView(iv1);





                            Toast toast = Toast.makeText(pub.this, temp, Toast.LENGTH_SHORT);
                            toast.setGravity(Gravity.CENTER, 0, 0);
                            toast.show();}
                    }
                });
                normalDialog.setNegativeButton("确定", null);
                normalDialog.show();


            }
        });







    }
    private void set_consume() {
        LinearLayout.LayoutParams layoutParams1 = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        int i=event_box.choose_consume();
        int gold=consumables.con_buy[i];
        ImageView iv1 = new ImageView(pub.this);



        layoutParams1.setMargins(0, 0, 0, 0);
        iv1.setLayoutParams(layoutParams1);
        iv1.setImageResource(consumables.myconsumeList[i]);
        ll5.addView(iv1);
        iv1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                textview2.setText("酒馆老板:我们家的消耗品喝下后永远不死!");
                final AlertDialog.Builder normalDialog =
                        new AlertDialog.Builder(pub.this);
                normalDialog.setTitle(consumables.con_names[i]);
                String temp="消耗品效果:\n";
                temp+= consumables.introductions[i];
                temp+="\n";
                temp+="购入价格:";
                temp+=consumables.con_buy[i];
                temp+="金币\n";


                normalDialog.setMessage(temp);
                normalDialog.setPositiveButton("购买", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {

                        if(MainActivity.lead.gold<consumables.con_buy[i]){
                            String a="你没有足够的金币!";
                            Toast toast = Toast.makeText(pub.this, a, Toast.LENGTH_SHORT);
                            toast.setGravity(Gravity.CENTER, 0, 0);
                            toast.show();

                        }
                        else{
                            MainActivity.lead.gold-=consumables.con_buy[i];
                            String temp="你购买了";
                            temp+=consumables.con_names[i];
                            MainActivity.lead.have_consume[i]+=1;
                            temp+="*1!\n";

                            ll5.removeView(iv1);





                            Toast toast = Toast.makeText(pub.this, temp, Toast.LENGTH_SHORT);
                            toast.setGravity(Gravity.CENTER, 0, 0);
                            toast.show();}
                    }
                });
                normalDialog.setNegativeButton("确定", null);
                normalDialog.show();


            }
        });}


    }

接下来要介绍的是其中可能发生的其他事件。如发现宝箱事件,大概率可以发现一件装备,但是也有可能宝箱中存在一只怪物。因此,开宝箱并不完全是一件收益事件,也很大概率存在着风险。尤其在缺少血量的时候,更需要主角好好斟酌。这也正是为了游戏的趣味性所添加的。下面是开宝箱的函数代码。

 

if(index==2){
            LinearLayout ll=findViewById(R.id.linear);
            LinearLayout.LayoutParams layoutParams1 = new LinearLayout.LayoutParams( ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
            TextView tv1=new TextView(this);
            layoutParams1.setMargins(0,20,0,100);
            tv1.setTextAlignment(View.TEXT_ALIGNMENT_CENTER);
            tv1.setText(event[0]);
            tv1.setTextSize(30);
            tv1.setLayoutParams(layoutParams1);
            tv1.setTextColor(Color.rgb(255, 255, 255));
            ll.addView(tv1);




            LinearLayout.LayoutParams layoutParams2 = new LinearLayout.LayoutParams( ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
            TextView tv2=new TextView(this);
            tv2.setTextAlignment(View.TEXT_ALIGNMENT_CENTER);
            layoutParams2.setMargins(0,10,0,150);
            tv2.setText(event[1]);
            tv2.setTextSize(20);
            tv2.setLayoutParams(layoutParams2);
            tv2.setTextColor(Color.rgb(255, 255, 255));
            ll.addView(tv2);
            LinearLayout.LayoutParams layoutParams3 = new LinearLayout.LayoutParams( ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);

            Button bt1=new Button(this);
            bt1.setTextAlignment(View.TEXT_ALIGNMENT_CENTER);
            layoutParams3.setMargins(0,10,0,80);
            bt1.setText(event[2]);
            bt1.setTextSize(20);
            bt1.setLayoutParams(layoutParams3);
            bt1.setTextColor(Color.rgb(0, 0, 0));
            ll.addView(bt1);

            LinearLayout.LayoutParams layoutParams4 = new LinearLayout.LayoutParams( ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);

            Button bt2=new Button(this);
            bt2.setTextAlignment(View.TEXT_ALIGNMENT_CENTER);
            layoutParams4.setMargins(0,10,0,50);
            bt2.setText(event[3]);
            bt2.setTextSize(20);
            bt2.setLayoutParams(layoutParams4);
            bt2.setTextColor(Color.rgb(0, 0, 0));
            ll.addView(bt2);
            Random r=new Random();
            int random=r.nextInt(100);
            if(random<80){
                bt1.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {

                        main.index+=1;
                            int num=event_box.choose_weapon();
                            int quality=event_box.choose_quality();
                            String qu=initweapon.quality[quality];
                            String name=initweapon.weapon_names[num];
                            name+="(";
                            name+=qu;
                            name+=")";
                            MainActivity.lead.have_weapons[4*num+quality]+=1;
                            String temp="你打开了宝箱,很快宝箱发出了耀眼的光芒,同时你发现宝箱的底部存在着一件宝物,\n";
                            temp+="(你获得了";
                            temp+=name;
                            temp+="*1)。\n";
                            text+=temp;
                        ll.removeView(bt1);
                        ll.removeView(bt2);
                        ll.removeView(tv1);
                        ll.removeView(tv2);
                      refresh=true;
                    }




                });

            }
            else{
                bt1.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        main.index+=1;
                            String temp="你打开了宝箱,很快宝箱发出了诡异的光芒,你发现宝箱里竟是一只";
                        monster_level=com.example.dungeonsimulator.event_box.choose_level();
                        monster_index=com.example.dungeonsimulator.event_box.choose_monster();
                        String a=event_box.monsters[monster_index];
                        temp+=a;
                        temp+=",你不得已与其开始了战斗。\n";
                      
                        timer.cancel();
                        Intent intent = new Intent(main.this, fight.class);
                        startActivity(intent);




                            text+=temp;


                    }
                });

            }
            bt2.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                    escape_box();
                    ll.removeView(bt1);
                    ll.removeView(bt2);
                    ll.removeView(tv1);
                    ll.removeView(tv2);
                    refresh=true;



                }
            });
        }

这次就先介绍到这里吧,要是有人催更我会接着往下更新的,谢谢大家啦,点点赞哈哈,有需要可以加q交流~q:1076051806

  • 7
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值