闲来无事,写了一个类似于买房记的文字策略游戏
主要包含开始页面和游戏页面
activity_start.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
tools:context=".StartActivity">
<TextView
android:layout_width="300dp"
android:layout_height="180dp"
android:layout_marginTop="160dp"
android:text="英雄"
android:textSize="150dp"
android:layout_centerHorizontal="true"
android:textColor="#BEEC2D2D"
/>
<TextView
android:id="@+id/growth"
android:layout_width="150dp"
android:layout_height="55dp"
android:text="成长记"
android:textSize="45dp"
android:layout_marginTop="460dp"
android:layout_centerHorizontal="true"
android:textColor="#FF5722"
/>
<Button
android:id="@+id/help"
android:layout_width="50dp"
android:layout_height="50dp"
android:text="?"
android:textSize="20dp"
android:textColor="#00BCD4"
/>
<Button
android:id="@+id/start_btn"
android:layout_width="200dp"
android:layout_height="50dp"
android:background="@color/colorPrimary"
android:layout_centerHorizontal="true"
android:layout_marginTop="50dp"
android:text="start"
android:textColor="#70E2F1"
/>
</RelativeLayout>
activity_help.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
tools:context=".Help">
<TextView
android:id="@+id/help"
android:layout_width="match_parent"
android:layout_height="200dp"
android:text="@string/help"
android:textSize="40dp"
android:textColor="#DB1E1E"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
/>
</RelativeLayout>
activity_game_windows.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
tools:context=".GameWindows">
<TextView
android:id="@+id/coin"
android:layout_width="match_parent"
android:layout_height="30dp"
android:layout_marginTop="3dp"
android:text="存活天数:1天 C币:20 目标存活天数:10"
android:textColor="#2196F3"
android:textSize="20dp" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="40dp">
<TextView
android:id="@+id/story_show"
android:layout_width="200dp"
android:layout_height="200dp"
android:layout_marginTop="10dp"
android:text="我叫fw,我是个孤儿,从小没了父母。小小年纪出来乞讨,常年乞讨为生."
android:textSize="20dp" />
<TextView
android:id="@+id/life_value_bar"
android:layout_width="150dp"
android:layout_height="30dp"
android:layout_marginLeft="220dp"
android:layout_marginTop="10dp"
android:text="生命值:100"
android:textSize="20dp" />
<TextView
android:id="@+id/physical_strength_bar"
android:layout_width="150dp"
android:layout_height="30dp"
android:layout_marginLeft="220dp"
android:layout_marginTop="50dp"
android:text="体力值:100"
android:textSize="20dp"
/>
<TextView
android:id="@+id/Hunger_value_bar"
android:layout_width="150dp"
android:layout_height="30dp"
android:layout_marginLeft="220dp"
android:layout_marginTop="90dp"
android:text="饥饿值:20"
android:textSize="20dp" />
<TextView
android:id="@+id/Lucky_value_bar"
android:layout_width="150dp"
android:layout_height="30dp"
android:layout_marginLeft="220dp"
android:layout_marginTop="130dp"
android:text="幸运值:10"
android:textSize="20dp" />
<TextView
android:id="@+id/power_value_bar"
android:layout_width="150dp"
android:layout_height="30dp"
android:layout_marginLeft="220dp"
android:layout_marginTop="170dp"
android:text="势力值:0"
android:textSize="20dp" />
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="150dp"
android:layout_marginTop="260dp"
>
<Button
android:id="@+id/begger_btn"
android:layout_width="70dp"
android:layout_height="40dp"
android:layout_marginLeft="10dp"
android:layout_marginTop="20dp"
android:text="乞讨"
/>
<Button
android:id="@+id/find_garbage_btn"
android:layout_width="70dp"
android:layout_height="40dp"
android:layout_marginLeft="80dp"
android:layout_marginTop="20dp"
android:text="翻垃圾"
/>
<Button
android:id="@+id/Stealing_thing_btn"
android:layout_width="70dp"
android:layout_height="40dp"
android:layout_marginLeft="150dp"
android:layout_marginTop="20dp"
android:text="偷东西"
/>
<Button
android:id="@+id/Pull_gang_btn"
android:layout_width="90dp"
android:layout_height="40dp"
android:layout_marginLeft="220dp"
android:layout_marginTop="20dp"
android:text="拉帮结派"
/>
<Button
android:id="@+id/Eat_restaurant_btn"
android:layout_width="80dp"
android:layout_height="40dp"
android:layout_marginLeft="10dp"
android:layout_marginTop="100dp"
android:text="下馆子"
/>
<Button
android:id="@+id/Hispital_btn"
android:layout_width="100dp"
android:layout_height="40dp"
android:layout_marginLeft="10dp"
android:layout_marginTop="60dp"
android:text="医院检查"
/>
<Button
android:id="@+id/Sleep_Street_btn"
android:layout_width="100dp"
android:layout_height="40dp"
android:layout_marginLeft="110dp"
android:layout_marginTop="60dp"
android:text="露宿街头"
/>
<Button
android:id="@+id/Sleep_hotel_btn"
android:layout_width="100dp"
android:layout_height="40dp"
android:layout_marginLeft="210dp"
android:layout_marginTop="60dp"
android:text="住旅馆"
/>
<Button
android:id="@+id/learning_btn"
android:layout_width="80dp"
android:layout_height="40dp"
android:layout_marginLeft="90dp"
android:layout_marginTop="100dp"
android:text="学习知识"
/>
<Button
android:id="@+id/News_btn"
android:layout_width="80dp"
android:layout_height="40dp"
android:text="社区招聘"
android:layout_marginTop="100dp"
android:layout_marginLeft="170dp"
/>
<Button
android:id="@+id/OutDoorbtn"
android:layout_width="80dp"
android:layout_height="40dp"
android:text="出门闲逛"
android:layout_marginTop="100dp"
android:layout_marginLeft="250dp"
/>
</RelativeLayout>
</RelativeLayout>
Help activity
package com.example.herogrowth;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
public class Help extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_help);
}
}
StartActivity
package com.example.herogrowth;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class StartActivity extends AppCompatActivity implements View.OnClickListener {
private Button start_btn;
private Button help;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_start);
start_btn = findViewById(R.id.start_btn);
help =findViewById(R.id.help);
help.setOnClickListener(this);
start_btn.setOnClickListener(this);
}
@Override
public void onClick(View v) {
if (v == start_btn){
Intent intent = new Intent(this,GameWindows.class);
startActivity(intent);
}
if (v == help){
Intent intent = new Intent(this,Help.class);
startActivity(intent);
}
}
}
GameWindows activity
package com.example.herogrowth;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Color;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.Switch;
import android.widget.TextView;
import android.widget.Toast;
import java.util.Random;
public class GameWindows extends AppCompatActivity implements View.OnClickListener {
private Button Beggerbtn;
private Button FindGaragebtn;
private Button Stealingbtn;
private Button PullGangbtn;
private Button Eat_restaurant_btn;
private Button Hispital_btn;
private Button Sleep_Street_btn;
private Button Sleep_hotel_btn;
private Button learning_btn;
private Button OutDoorbtn;
private Button News_btn;
private int Live_Days = 1;
private int Coin = 20;
private int life_value = 100;
private int physical = 100;
private int hunger = 20;
private int luck_value = 10;
private int power = 0;
private TextView life_bar;
private TextView physical_strength_bar;
private TextView Hunger_value_bar;
private TextView Lucky_value_bar;
private TextView power_value_bar;
private TextView Story_show;
private TextView Coin_show;
private int Luck;
private Boolean Getin = true;
private int CountBegger = 0;
private int CountFind = 0;
private Boolean Sleep = true;
private int goalDays = 30;
private int Sleep_Street = 0;
private int CoundOutDoor = 0;
private int CoundNews = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_game_windows);
Beggerbtn = findViewById(R.id.begger_btn);
FindGaragebtn = findViewById(R.id.find_garbage_btn);
Stealingbtn = findViewById(R.id.Stealing_thing_btn);
PullGangbtn = findViewById(R.id.Pull_gang_btn);
Eat_restaurant_btn = findViewById(R.id.Eat_restaurant_btn);
Hispital_btn = findViewById(R.id.Hispital_btn);
Sleep_Street_btn = findViewById(R.id.Sleep_Street_btn);
Sleep_hotel_btn = findViewById(R.id.Sleep_hotel_btn);
learning_btn = findViewById(R.id.learning_btn);
News_btn = findViewById(R.id.News_btn);
OutDoorbtn = findViewById(R.id.OutDoorbtn);
life_bar = findViewById(R.id.life_value_bar);
physical_strength_bar = findViewById(R.id.physical_strength_bar);
Hunger_value_bar = findViewById(R.id.Hunger_value_bar);
Lucky_value_bar = findViewById(R.id.Lucky_value_bar);
power_value_bar = findViewById(R.id.power_value_bar);
Story_show = findViewById(R.id.story_show);
Coin_show = findViewById(R.id.coin);
Beggerbtn.setOnClickListener(this);
FindGaragebtn.setOnClickListener(this);
Stealingbtn.setOnClickListener(this);
PullGangbtn.setOnClickListener(this);
Eat_restaurant_btn.setOnClickListener(this);
Hispital_btn.setOnClickListener(this);
Sleep_Street_btn.setOnClickListener(this);
Sleep_hotel_btn.setOnClickListener(this);
learning_btn.setOnClickListener(this);
OutDoorbtn.setOnClickListener(this);
News_btn.setOnClickListener(this);
}
@Override
public void onClick(View v) {
if (v == Stealingbtn) {
Steal_thing();
}
if (v == Beggerbtn) {
Begger_Coin();
}
if (v == FindGaragebtn) {
FindGarage();
}
if (v == Eat_restaurant_btn) {
Eat_restaurant();
}
if (v == Hispital_btn) {
Hispital();
}
if (v == PullGangbtn) {
PullGang();
}
if (v == Sleep_Street_btn) {
Sleep_Street_();
}
if (v == Sleep_hotel_btn) {
Sleep_hotel();
}
if (v == learning_btn) {
learning();
}
if (v == News_btn) {
News();
}
if (v == OutDoorbtn) {
OutDoor();
}
}
private void OutDoor() {
if (CoundOutDoor<2){
physical -= (int) (Math.random() * 8 + 3);
hunger += (int) (Math.random() * 7 + 2);
int index = (int) (Math.random() * 100 + 1);
if (index + luck_value > 50) {
Story_show.setText("出门闲逛捡到少量C币!体力值增加,饥饿值减少!");
physical += (int) (Math.random() * 8 + 3);
hunger -= (int) (Math.random() * 7 + 2);
Coin += (int) (Math.random() * 8 + 3);
}
if (index + luck_value < 35) {
Story_show.setText("出门遇到混混,被抢劫!生命值减少,体力值减少,C币减少");
physical -= (int) (Math.random() * 8 + 3);
life_value -= (int) (Math.random() * 7 + 2);
Coin -= (int) (Math.random() * 8 + 3);
}
CoundOutDoor++;
CheckValue();
UpdateText();
}else {
Story_show.setText("达到次数上限!");
}
}
private void News() {
if (CoundNews<2){
int index = (int) (Math.random() * 100 + 1);
if (index % 5 == 1) {
Story_show.setText("社区招募送水工,敬请关注!");
Workinfo(10);
} else if (index % 2 == 0) {
Story_show.setText("社区招募扫地清洁员,敬请关注!");
Workinfo(8);
} else if (index>=95){
Story_show.setText("社区招募保安,敬请关注!");
Workinfo(15);
}
else {
Story_show.setText("无招聘信息,敬请关注!");
}
CoundNews++;
}else {
Story_show.setText("达到次数上限!");
}
}
private void Workinfo(final int Coins) {
AlertDialog dialog = new AlertDialog.Builder(this)
.setTitle("是否参加工作?")
.setCancelable(false)
//设置对话框的按钮
.setPositiveButton("是", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Coin += (int) (Math.random() * Coins+7 + Coins);
physical -= (int) (Math.random() * 15 + 5);
life_value -= (int) (Math.random() * 7 + 2);
hunger += (int) (Math.random() * 8 + 4);
UpdateText();
}
})
.setNegativeButton("否", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
})
.create();
dialog.show();
}
private void Eat_restaurant() {
if (Coin >= 15) {
Coin -= 15;
life_value += (int) (Math.random() * 15 + 5);
physical += (int) (Math.random() * 20 + 7);
hunger -= (int) (Math.random() * 18 + 10);
if (luck_value < 10) {
Coin = 0;
life_value -= (int) (Math.random() * 25 + 15);
Story_show.setText("吃饭时不注意,钱被偷了!生命值减少!");
}
if (luck_value >= 80) {
Coin = 1000;
life_value -= (int) (Math.random() * 10 + 5);
Story_show.setText("原来你的父母并没有死,餐馆老板和你父母是朋友,餐馆老板发现你和你爸爸长得很像,于是确认后发现你真是你父母失散多年的儿子,你从此过上无忧无虑的生活!");
DeathAlert();
}
CheckValue();
} else {
CoinAlert();
}
Sleep = true;
UpdateText();
}
private void Hispital() {
if (Coin >= 25) {
Coin -= 25;
life_value += (int) (Math.random() * 25 + 15);
physical += (int) (Math.random() * 15 + 5);
hunger += (int) (Math.random() * 17 + 10);
Story_show.setText("花费25C币!生命值,体力值,饥饿值,幸运值增加!");
if (luck_value < 7) {
life_value -= (int) (Math.random() * 35 + 20);
physical -= (int) (Math.random() * 55 + 35);
Story_show.setText("你在看病途中,不小心染上了恶心病毒,生命值大大减少!几乎丧失体力!");
}
if (luck_value > 85) {
life_value = 100;
physical = 100;
hunger = 5;
luck_value -= (int) (Math.random() * 35 + 15);
Story_show.setText("花费25C币!医生说你得了癌症,不过由于病例罕见,愿意免![在这里插入图片描述]()费帮你治疗,最终你痊愈了!并继续新的生活!");
}
luck_value += (int) (Math.random() * 15 + 5);
CheckValue();
} else {
CoinAlert();
}
Sleep = true;
UpdateText();
}
private void PullGang() {
if (Coin > 0) {
physical -= (int) (Math.random() * 6 + 4);
hunger += (int) (Math.random() * 10 + 5);
Coin -= (int) (Math.random() * 50 + 25);
power += (int) (Math.random() * 15 + 5);
luck_value -= (int) (Math.random() * 10 + 3);
Sleep = true;
CheckValue();
UpdateText();
} else {
Story_show.setText("没有资金,无法拉帮结派!");
}
}
private void Sleep_Street_() {
if (Sleep) {
Sleep_Street++;
Live_Days++;
life_value -= (int) (Math.random() * 4 + 3);
physical += (int) (Math.random() * 6 + 4);
hunger += (int) (Math.random() * 10 + 4);
Story_show.setText("睡觉睡得腰酸背痛!?");
if (Sleep_Street * luck_value > 60 && power < 35) {
Story_show.setText("由于长时间睡大街上,被小混混盯上,被打劫,生命值减少,体力减少,C币减少");
Coin -= (Math.random() * 8 + 3);
life_value -= (Math.random() * 15 + 8);
hunger += (int) (Math.random() * 12 + 6);
physical -= (int) (Math.random() * 12 + 6);
}
Getin = true;
CoundOutDoor = 0;
CoundNews = 0;
CountFind = 0;
CountBegger = 0;
CheckPower();
CheckValue();
UpdateText();
Sleep = false;
}
}
private void Sleep_hotel() {
if (Sleep) {
if (Coin >= 20) {
Coin -= 20;
Live_Days++;
hunger += (int) (Math.random() * 10 + 5);
life_value += (int) (Math.random() * 15 + 7);
physical += (int) (Math.random() * 15 + 8);
Story_show.setText("生命力旺盛的一天呢!");
if (luck_value <= 5) {
Coin = 0;
Story_show.setText("晚上旅馆遭到打劫,你的C币也未能幸免,生命值减少,饥饿值增加,体力值减少!");
life_value -= (Math.random() * 8 + 5);
hunger += (int) (Math.random() * 10 + 6);
physical -= (int) (Math.random() * 12 + 6);
}
Getin = true;
Sleep = false;
CoundOutDoor = 0;
CoundNews = 0;
CountFind = 0;
CountBegger = 0;
Sleep_Street = 0;
CheckPower();
} else {
CoinAlert();
}
}
CheckValue();
UpdateText();
}
private void learning() {
if (Getin) {
physical -= (int) (Math.random() * 15 + 4);
hunger += (int) (Math.random() * 12 + 3);
luck_value += (int) (Math.random() * 10 + 3);
Story_show.setText("知识增加!幸运值增加!");
if (luck_value > 90) {
Story_show.setText("你发现了书中的经商之道,并且找到了投资商,你走上了人生巅峰!");
DeathAlert();
}
CheckValue();
UpdateText();
} else {
Story_show.setText("到达次数上限!");
}
}
private void FindGarage() {
if (CountFind < 2) {
CountFind++;
hunger += (int) (Math.random() * 8 + 3);
physical -= (int) (Math.random() * 10 + 4);
if (luck_value > 45) {
Coin += (int) (Math.random() * 18 + 8);
Story_show.setText("发现贵重物品,转卖得大量C币!");
} else {
Coin += (int) (Math.random() * 10 + 4);
Story_show.setText("发现物品,转卖得些许C币");
}
} else {
Story_show.setText("到达次数上限!");
}
Sleep = true;
CheckValue();
UpdateText();
}
private void Begger_Coin() {
if (CountBegger < 2) {
if (luck_value * (int) (Math.random() * 20 + 1) >= 35) {
Coin += (int) (Math.random() * 12 + 4);
hunger += (int) (Math.random() * 7 + 3);
physical -= (int) (Math.random() * 10 + 4);
Story_show.setText("乞讨成功!");
CountBegger++;
CheckValue();
UpdateText();
} else {
hunger += (int) (Math.random() * 7 + 3);
physical -= (int) (Math.random() * 10 + 4);
Story_show.setText("乞讨失败!");
CheckValue();
UpdateText();
}
} else {
Story_show.setText("到达次数上限!");
}
Sleep = true;
}
private void Steal_thing() {
if (Getin) {
physical -= (int) (Math.random() * 10 + 3);
hunger += (int) (Math.random() * 10 + 4);
luck_value -= (int) (Math.random() * 4 + 1);
Luck = (int) (Math.random() * 100 + 1);
if (Luck > luck_value * 5) {
Coin -= (int) (Math.random() * 30 + 10);
Story_show.setText("你偷东西被发现了!生命值减少,C币用来赎身!");
life_value -= (int) (Math.random() * 50 + 5);
} else if (luck_value > 50) {
Coin += 20;
Story_show.setText("获得20C币");
} else if (luck_value > 90) {
Story_show.setText("获得贵人帮助!完成学业!");
}
Getin = false;
Sleep = true;
CheckValue();
UpdateText();
} else {
Story_show.setText("到达次数上限!");
}
}
private void CheckPower() {
if (power > 20) {
Coin += (int) (Math.random() * 15 + 5);
Story_show.setText("获得少量帮派收入!");
}
if (power > 50) {
Coin += (int) (Math.random() * 30 + 15);
Story_show.setText("获得一定帮派收入!");
}
if (power > 80) {
Coin += (int) (Math.random() * 45 + 30);
Story_show.setText("获得大量帮派收入!");
}
}
private void CheckValue() {
if (physical >= 100) {
physical = 100;
} else if (physical <= 0) {
physical = 0;
DeathAlert();
}
if (life_value >= 100) {
life_value = 100;
} else if (life_value <= 0) {
life_value = 0;
DeathAlert();
}
if (hunger >= 100) {
hunger = 100;
DeathAlert();
} else if (hunger <= 0) {
hunger = 0;
}
if (power >= 100) {
power = 100;
} else if (power <= 0) {
power = 0;
}
if (luck_value >= 100) {
luck_value = 100;
} else if (luck_value <= 0) {
luck_value = 0;
}
if (Coin <= -100) {
Coin = -100;
Story_show.setText("无法支付帮派费用,被帮派抛弃!");
DeathAlert();
}
}
private void CoinAlert() {
AlertDialog dialog = new AlertDialog.Builder(this)
.setTitle("金币不足,无法进入!")//设置对话框的标题
.setCancelable(false)
//设置对话框的按钮
.setPositiveButton("确认", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
}).create();
dialog.show();
}
private void DeathAlert() {
AlertDialog dialog = new AlertDialog.Builder(this)
.setTitle("游戏结束,重新开始!")
.setCancelable(false)
//设置对话框的按钮
.setPositiveButton("确认", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Live_Days = 1;
Coin = 20;
life_value = 100;
physical = 100;
hunger = 20;
luck_value = 10;
power = 0;
Story_show.setText("我叫fw,我是个孤儿,从小没了父母。小小年纪出来乞讨,常年乞讨为生.");
UpdateText();
}
}).create();
dialog.show();
}
private void UpdateText() {
Coin_show.setText("存活天数:" + Live_Days + "C币:" + Coin + "目标存活天数:" + goalDays);
life_bar.setText("生命值:" + life_value);
physical_strength_bar.setText("体力值:" + physical);
Hunger_value_bar.setText("饥饿值:" + hunger);
Lucky_value_bar.setText("幸运值:" + luck_value);
power_value_bar.setText("势力值:" + power);
}
}
游戏运行效果图
大家如果感兴趣可以自己接着开发,欢迎大家讨论!!