效果图:

MainActivity.java

public class MainActivity extends AppCompatActivity {private ProgressBar manlifeProgressbar;private ProgressBar manattackProgressbar;private ProgressBar manspeedProgressbar;private TextView manlifeTV;private TextView manattackTV;private TextView manspeedTV;private ProgressBar petlifeProgressbar;private ProgressBar petattackProgressbar;private ProgressBar petspeedProgressbar;private TextView petlifeTV;private TextView petattackTV;private TextView petspeedTV;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);manlifeTV = findViewById(R.id.tv_manlife_progress);manattackTV = findViewById(R.id.tv_manattack_progress);manspeedTV = findViewById(R.id.tv_manspeed_progress);petlifeTV = findViewById(R.id.tv_petlife_progress);petattackTV = findViewById(R.id.tv_petattack_progress);petspeedTV = findViewById(R.id.tv_petspeed_progress);initProgress();}private void initProgress() {manlifeProgressbar = findViewById(R.id.manlife_progressBar);manattackProgressbar = findViewById(R.id.manattack_progressBar);manspeedProgressbar = findViewById(R.id.manspeed_progressBar);manlifeProgressbar.setMax(1000);manattackProgressbar.setMax(1000);manspeedProgressbar.setMax(1000);petlifeProgressbar = findViewById(R.id.petlife_progressBar);petattackProgressbar = findViewById(R.id.petattack_progressBar);petspeedProgressbar = findViewById(R.id.petspeed_progressBar);petlifeProgressbar.setMax(1000);petattackProgressbar.setMax(1000);petspeedProgressbar.setMax(1000);}public void click(View view){Intent intent = new Intent(this,ShopActivity.class);int id = view.getId();if (id == R.id.btn_baby){startActivityForResult(intent,2);}else if (id == R.id.btn_master){startActivityForResult(intent,1);}}@Overrideprotected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {super.onActivityResult(requestCode, resultCode, data);if (data != null){if (requestCode == 1){ItemInfo info = (ItemInfo) data.getSerializableExtra("equipment");updateMasterProgress(info);}else if (requestCode == 2){ItemInfo info = (ItemInfo) data.getSerializableExtra("equipment");updatePetProgress(info);}}}private void updateMasterProgress(ItemInfo info) {int progressLife = manlifeProgressbar.getProgress();int progressAttack = manattackProgressbar.getProgress();int progressSpeed = manspeedProgressbar.getProgress();manlifeProgressbar.setProgress(progressLife+info.getLife());manattackProgressbar.setProgress(progressAttack+info.getAttack());manspeedProgressbar.setProgress(progressSpeed+info.getSpeed());manlifeTV.setText(manattackProgressbar.getProgress()+"");manattackTV.setText(manattackProgressbar.getProgress()+"");manspeedTV.setText(manspeedProgressbar.getProgress()+"");}private void updatePetProgress(ItemInfo info) {int progressLife = petlifeProgressbar.getProgress();int progressAttack = petattackProgressbar.getProgress();int progressSpeed = petspeedProgressbar.getProgress();petlifeProgressbar.setProgress(progressLife+info.getLife());petattackProgressbar.setProgress(progressAttack+info.getAttack());petspeedProgressbar.setProgress(progressSpeed+info.getSpeed());petlifeTV.setText(petlifeProgressbar.getProgress()+"");petattackTV.setText(petattackProgressbar.getProgress()+"");petspeedTV.setText(petspeedProgressbar.getProgress()+"");}
}

ShopActivity.java

public class ShopActivity extends AppCompatActivity implements View.OnClickListener {private ItemInfo itemInfo1;private ItemInfo itemInfo2;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_shop);itemInfo1 = new ItemInfo("金剑",100,20,20);findViewById(R.id.ly_111).setOnClickListener(this);TextView mLifeTV1 = findViewById(R.id.tv_life1);TextView mNameTV1 = findViewById(R.id.tv_name1);TextView mSpeedTV1 = findViewById(R.id.tv_speed1);TextView mAttackTV1 = findViewById(R.id.tv_attack1);mLifeTV1.setText("生命值+"+itemInfo1.getLife());mNameTV1.setText(itemInfo1.getName()+"");mSpeedTV1.setText("敏捷值+"+itemInfo1.getSpeed());mAttackTV1.setText("攻击力+"+itemInfo1.getAttack());itemInfo2 = new ItemInfo("金盾",30,200,30);findViewById(R.id.ly_112).setOnClickListener(this);TextView mLifeTV2 = findViewById(R.id.tv_life2);TextView mNameTV2 = findViewById(R.id.tv_name2);TextView mSpeedTV2 = findViewById(R.id.tv_speed2);TextView mAttackTV2 = findViewById(R.id.tv_attack2);mLifeTV2.setText("生命值+"+itemInfo2.getLife());mNameTV2.setText(itemInfo2.getName()+"");mSpeedTV2.setText("敏捷值+"+itemInfo2.getSpeed());mAttackTV2.setText("攻击力+"+itemInfo2.getAttack());}@Overridepublic void onClick(View view) {Intent intent = new Intent();switch (view.getId()){case R.id.ly_111:intent.putExtra("equipment",itemInfo1);setResult(1,intent);finish();break;case R.id.ly_112:intent.putExtra("equipment",itemInfo2);setResult(2,intent);finish();break;}}
}

ItemInfo.java

public class ItemInfo implements Serializable {private String name;private int attack;private int life;private int speed;public ItemInfo(String name,int attack,int life,int speed){this.name = name;this.attack = attack;this.life = life;this.speed = speed;}public String getName(){return name;}public void setName(String name){this.name = name;}public int getAttack(){return attack;}public void setAttack(int attack){this.attack = attack;}public int getLife(){return life;}public void setLife(int life){this.life = life;}public int getSpeed(){return speed;}public void setSpeed(int speed){this.speed = speed;}
}

activity_main.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:app="http://schemas.android.com/apk/res-auto"xmlns:tools="http://schemas.android.com/tools"android:layout_width="match_parent"android:layout_height="match_parent"android:fadingEdge="vertical"android:scrollbars="vertical"tools:context=".MainActivity"><LinearLayoutandroid:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical" ><LinearLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"android:gravity="center"android:orientation="vertical" ><ImageViewandroid:id="@+id/pet_imgv"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_gravity="center_horizontal"android:src="@drawable/pet" /><TableLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"android:layout_gravity="center" ><TableRowandroid:layout_width="match_parent"android:layout_height="wrap_content" ><TextViewandroid:layout_width="0dip"android:layout_height="wrap_content"android:layout_weight="1"android:text="生命值:"android:textColor="@android:color/black"android:textSize="18sp" /><ProgressBarandroid:id="@+id/petlife_progressBar"style="?android:attr/progressBarStyleHorizontal"android:layout_width="0dip"android:layout_height="wrap_content"android:layout_gravity="center"android:layout_weight="2" /><TextViewandroid:id="@+id/tv_petlife_progress"android:layout_width="0dip"android:layout_height="wrap_content"android:layout_weight="1"android:gravity="center"android:text="0"android:textColor="#000000" /></TableRow><TableRowandroid:layout_width="match_parent"android:layout_height="wrap_content" ><TextViewandroid:layout_width="0dip"android:layout_height="wrap_content"android:layout_weight="1"android:text="攻击力:"android:textColor="@android:color/black"android:textSize="18sp" /><ProgressBarandroid:id="@+id/petattack_progressBar"style="?android:attr/progressBarStyleHorizontal"android:layout_width="0dip"android:layout_height="wrap_content"android:layout_weight="2" /><TextViewandroid:id="@+id/tv_petattack_progress"android:layout_width="0dip"android:layout_height="wrap_content"android:layout_weight="1"android:gravity="center"android:text="0"android:textColor="#000000" /></TableRow><TableRowandroid:layout_width="match_parent"android:layout_height="wrap_content" ><TextViewandroid:layout_width="0dip"android:layout_height="wrap_content"android:layout_weight="1"android:text="敏捷:"android:textColor="@android:color/black"android:textSize="18sp" /><ProgressBarandroid:id="@+id/petspeed_progressBar"style="?android:attr/progressBarStyleHorizontal"android:layout_width="0dip"android:layout_height="wrap_content"android:layout_weight="2" /><TextViewandroid:id="@+id/tv_petspeed_progress"android:layout_width="0dip"android:layout_height="wrap_content"android:layout_weight="1"android:gravity="center"android:text="0"android:textColor="#000000" /></TableRow></TableLayout><Buttonandroid:id="@+id/btn_baby"android:layout_width="wrap_content"android:layout_height="wrap_content"android:drawableRight="@android:drawable/ic_menu_add"android:onClick="click"android:text="给小宝宝购买装备"android:textSize="18sp" /></LinearLayout><LinearLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"android:gravity="center"android:orientation="vertical" ><ImageViewandroid:id="@+id/man_imgv"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_gravity="center_horizontal"android:src="@drawable/man" /><TableLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"android:layout_gravity="center" ><TableRowandroid:layout_width="match_parent"android:layout_height="wrap_content" ><TextViewandroid:layout_width="0dip"android:layout_height="wrap_content"android:layout_weight="1"android:text="生命值:"android:textColor="@android:color/black"android:textSize="18sp" /><ProgressBarandroid:id="@+id/manlife_progressBar"style="?android:attr/progressBarStyleHorizontal"android:layout_width="0dip"android:layout_height="wrap_content"android:layout_gravity="center"android:layout_weight="2" /><TextViewandroid:id="@+id/tv_manlife_progress"android:layout_width="0dip"android:layout_height="wrap_content"android:layout_weight="1"android:gravity="center"android:text="0"android:textColor="#000000" /></TableRow><TableRowandroid:layout_width="match_parent"android:layout_height="wrap_content" ><TextViewandroid:layout_width="0dip"android:layout_height="wrap_content"android:layout_weight="1"android:text="攻击力:"android:textColor="@android:color/black"android:textSize="18sp" /><ProgressBarandroid:id="@+id/manattack_progressBar"style="?android:attr/progressBarStyleHorizontal"android:layout_width="0dip"android:layout_height="wrap_content"android:layout_weight="2" /><TextViewandroid:id="@+id/tv_manattack_progress"android:layout_width="0dip"android:layout_height="wrap_content"android:layout_weight="1"android:gravity="center"android:text="0"android:textColor="#000000" /></TableRow><TableRowandroid:layout_width="match_parent"android:layout_height="wrap_content" ><TextViewandroid:layout_width="0dip"android:layout_height="wrap_content"android:layout_weight="1"android:text="敏捷:"android:textColor="@android:color/black"android:textSize="18sp" /><ProgressBarandroid:id="@+id/manspeed_progressBar"style="?android:attr/progressBarStyleHorizontal"android:layout_width="0dip"android:layout_height="wrap_content"android:layout_weight="2" /><TextViewandroid:id="@+id/tv_manspeed_progress"android:layout_width="0dip"android:layout_height="wrap_content"android:layout_weight="1"android:gravity="center"android:text="0"android:textColor="#000000" /></TableRow></TableLayout><Buttonandroid:id="@+id/btn_master"android:layout_width="wrap_content"android:layout_height="wrap_content"android:drawableRight="@android:drawable/ic_menu_add"android:onClick="click"android:text="主人购买装备"android:textSize="18sp" /></LinearLayout></LinearLayout>
</LinearLayout>

activity_shop.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:app="http://schemas.android.com/apk/res-auto"xmlns:tools="http://schemas.android.com/tools"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical"tools:context=".ShopActivity"><LinearLayoutandroid:id="@+id/ly_111"android:layout_width="match_parent"android:layout_height="wrap_content"android:gravity="center_vertical"android:orientation="horizontal"android:paddingBottom="5dp"android:paddingTop="5dp"><Viewandroid:layout_width="30dp"android:layout_height="30dp"android:background="@android:drawable/ic_menu_info_details"/><TextViewandroid:id="@+id/tv_name1"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginLeft="60dp"android:layout_marginRight="60dp"android:text="商品名称"/><LinearLayoutandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:orientation="vertical"><TextViewandroid:id="@+id/tv_life1"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="生命值"android:textSize="18sp"/><TextViewandroid:id="@+id/tv_attack1"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="攻击力"android:textSize="18sp"/><TextViewandroid:id="@+id/tv_speed1"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="速度"android:textSize="18sp"/></LinearLayout></LinearLayout><LinearLayoutandroid:id="@+id/ly_112"android:layout_width="match_parent"android:layout_height="wrap_content"android:gravity="center_vertical"android:orientation="horizontal"android:paddingTop="5dp"android:paddingBottom="5dp"><Viewandroid:layout_width="30dp"android:layout_height="30dp"android:background="@android:drawable/ic_menu_info_details"/><TextViewandroid:id="@+id/tv_name2"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginLeft="60dp"android:layout_marginRight="60dp"android:text="商品名称"/><LinearLayoutandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:orientation="vertical"><TextViewandroid:id="@+id/tv_life2"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="生命值"android:textSize="18sp"/><TextViewandroid:id="@+id/tv_attack2"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="攻击力"android:textSize="18sp"/><TextViewandroid:id="@+id/tv_speed2"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="速度"android:textSize="18sp"/></LinearLayout></LinearLayout>
</LinearLayout>

Android 主人VS宠物 游戏相关推荐

  1. fritz 使用手册_Fritz对象检测指南:使用机器学习在Android中构建宠物监控应用

    fritz 使用手册 by Eric Hsiao 萧敬轩 Fritz对象检测指南:使用机器学习在Android中构建宠物监控应用 (A guide to Object Detection with F ...

  2. 多态知识整理实现主人与宠物玩耍功能

    文章目录 1.为什么需要多态? 2.多态访问成员的特点: 3.多态的有点&例题 4.多态的缺点&解决办法 1. 多态的缺点: 使用父类引用无法访问子类所特有的方法 2.解决办法: 向下 ...

  3. 编写程序模拟“主人”喂养“宠物”的场景,利用多态的思想!!!

    代码 /*2.1.多态练习题编写程序模拟"主人"喂养"宠物"的场景:提示1:主人类:Master宠物类:Pet宠物类子类:Dog.Cat.YingWu提示2:主 ...

  4. android游戏模式,注重游戏体验 Android 12提供原生游戏模式

    原标题:注重游戏体验 Android 12提供原生游戏模式 [PChome手机频道资讯报道]谷歌方面即将推出新版的Android 12系统,该系统代号Snow Cone,将采用全新的界面设计,并提供多 ...

  5. Android 辅助服务实战-游戏点击器

    Android 辅助服务实战-游戏点击器 背景: 前几年我一直在玩一款氪金养成类手游<<末日危机>>,每天都有任务需要完成,那时游戏里面还没有一键收菜,我去年(接手了公会里面不 ...

  6. 基于Android+servlet的宠物商店【源码+文档+ppt】

    目录 1.文档目录 1.课题研究目的 2.开发技术 2.1 Android技术 2.2 servlet 3.需求分析 3.1 系统模型 3.2 系统用例图 3.3 顶层数据流图 3.4 零层数据流图 ...

  7. android 赛车 源码,android 3D风格赛车游戏源码

    android 3D风格赛车游戏源码,基于Libgdx 框架开发,三维视觉,包含20量敌方车辆和10量我方车辆,支持Admob广告插件,带游戏排行榜和成就系统 ,兼容手机.平板电脑等多种屏幕尺寸 ,游 ...

  8. 第33篇 Android Studio实现五子棋游戏(四)棋子类和主类

    第33篇 Android Studio实现五子棋游戏(四)棋子类和主类 1.棋子类 2.主类 2.1.变量 2.2.重写方法 2.3.代码 3.效果 3.1.进去界面 3.2.赢棋 3.3.提示 3. ...

  9. 使用Android Studio 写骰子游戏

    使用Android Studio 写骰子游戏 骰子游戏## 标题 详细代码如下: class MainActivity : AppCompatActivity() {override fun onCr ...

  10. 多态应用-主人与宠物玩耍案例

    1 package com.szxs.pet; 2 /** 3 * 宠物类 4 * @author 5 * 6 */ 7 public class Pet { 8 private String nam ...

最新文章

  1. Flutter 拨打电话和跳转网页
  2. pyBoard Mini从安装到简单测试
  3. 用隐马尔可夫模型(HMM)做命名实体识别——NER系列(二)
  4. ContentProvider源码分析(原)
  5. Spring+Hibernate+Atomikos集成构建JTA的分布式事务--解决多数据源跨库事务
  6. cad里面f命令用不了_CAD出现命令无效、失灵等问题?不用慌,两招帮你快速解决...
  7. 关于数组首地址a、a+1、a[0]、a[0]+1、*a、*a、a+0的解析
  8. Pytorch BatchNorm
  9. 【springBoot测试】【自定义配置】使用SpringBoot测试框架内容
  10. VSCode设置中文语言
  11. Extjs EditorGridPanel功能
  12. 用python实现基于PANN(retrained Audio Neural Networks)的声音检测方法
  13. Linux cpu为什么会超过100% ?
  14. docker安装时报服务失败,因为控制进程退出并带有错误代码
  15. 计算机语言写信祝福语,写信祝福语
  16. NYOJ 71 独木舟上的旅行
  17. 【Linux】无法读取/挂载U盘
  18. Android Framework 框架系列之PowerManager
  19. BUUCTF 九连环
  20. Java8时间校正器

热门文章

  1. Dij_heap__前向星。
  2. 四 akka学习 四种多线程的解决方案
  3. ThreadLocal,静态变量,实例变量,局部变量的线程安全
  4. WAMP(windows+apache+mysql+php)
  5. 转I give the orders around here.
  6. 【关系抽取】详聊如何用BERT实现关系抽取
  7. 【统计学】10个必知必会的统计学问题 (附答案)
  8. paddle 进行目标检测_猫狗猴的识别
  9. 深度学习5-模型的保存与加载
  10. 深度学习-激活函数总结