因为手机不方便录屏,所以上传的用模拟器来模拟效果,但是在实战中确实输了~
这只是一个很简单的版本,在高铁上突发奇想,然后紧赶慢赶完成的,后续升级版本打算增加各种特效和支持自定义惩罚,文件如下
mainactivity写逻辑
xml文件写界面
color配色
strings设定惩罚
MainActivity
package com.example.couplegame;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import android.content.res.Resources;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import java.util.Random;
public class MainActivity extends AppCompatActivity {
Button mButtonBoy;
Button mButtonGirl;
Button mButtonTimer;
TextView mTextBoy;
TextView mTextGirl;
boolean mIsRunning = false;
int boyCount = 0;
int girlCount = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
init();
}
@Override
protected void onStart() {
super.onStart();
initData();
}
@Override
protected void onResume() {
super.onResume();
initData();
}
/** * 给按钮设置*** */
private void initData() {
mButtonBoy.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
boyCount+=1;
mTextBoy.setText(String.valueOf(boyCount));
}
});
mButtonGirl.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
girlCount+=1;
mTextGirl.setText(String.valueOf(girlCount));
}
});
mButtonTimer.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (!mIsRunning){
mCountDownTimer.start();
}
}
});
}
/** * 初始化 */
private void init() {
mButtonBoy = findViewById(R.id.btn_boy);
mButtonGirl = findViewById(R.id.btn_girl);
mButtonTimer = findViewById(R.id.btn_DownTimer);
mTextBoy = findViewById(R.id.text_boy);
mTextGirl = findViewById(R.id.text_girl);
}
/** * 倒计时 */
CountDownTimer mCountDownTimer=new CountDownTimer(5*1000,1000) {
@Override
public void onTick(long millisUntilFinished) {
mIsRunning=true;
mButtonTimer.setText("还有"+(millisUntilFinished/1000)+"秒");
}
@Override
public void onFinish() {
mIsRunning=false;
mButtonTimer.setText("比赛结束");
gameOver();
}
};
/** * 游戏结束 */
private void gameOver() {
String winner = compare(boyCount, girlCount);
Resources res = getResources();
String[] Punishments = res.getStringArray(R.array.punishments);
Random random = new Random(Punishments.length);
AlertDialog alertDialog = new AlertDialog.Builder(this)
.setTitle(winner)
.setMessage(Punishments[random.nextInt(Punishments.length)])
.setIcon(R.mipmap.ic_launcher)
.create();
alertDialog.show();
}
private String compare(int boyCount, int girlCount) {
return boyCount>girlCount?"男生获胜":"女生获胜";
}
}
activity.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent" >
<Button android:id="@+id/btn_DownTimer" android:text="开始" android:layout_width="wrap_content" android:layout_height="wrap_content">
</Button>
<LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="1" android:gravity="center" android:orientation="vertical" >
<Button android:id="@+id/btn_girl" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@color/pink" android:gravity="center" android:text="女" android:textSize="20sp" >
</Button>
<TextView android:id="@+id/text_girl" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="0" android:textSize="30sp" ></TextView>
</LinearLayout>
<LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="1" android:gravity="center" android:orientation="vertical" >
<Button android:id="@+id/btn_boy" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@color/blue" android:gravity="center" android:text="男" android:textSize="20sp" >
</Button>
<TextView android:id="@+id/text_boy" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="0" android:textSize="30sp" />
</LinearLayout>
</LinearLayout>
strings 这里记录惩罚措施
<resources>
<string name="app_name">coupleGame</string>
<string-array name="punishments">
<item>公主抱走100米</item>
<item>淘宝买买买</item>
<item>抄写名字20遍</item>
<item>在脸上画画</item>
<item>边看片边做俯卧撑</item>
<item>夸到你满意</item>
</string-array>
</resources>
colors 简单的按钮颜色
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="colorPrimary">#008577</color>
<color name="colorPrimaryDark">#00574B</color>
<color name="colorAccent">#D81B60</color>
<color name="green"></color>
<color name="blue">#0000FF</color>
<color name="pink">#FFA0FD</color>
</resources>
2.0版本打算添加更多玩法哈哈哈