安卓简单小游戏开发:比较数字大小(Number Game)
程序员文章站
2024-02-26 17:53:28
...
安卓简单小游戏开发:比较数字大小(Number Game)
简单介绍:
1.画面会出现2个数字
2.点击你认为大的数字,结果正确你的分数会增加,错误你的分数会减少
3.一道题解决之后自动出现下一道题
我先发截图先看一下效果!
1.画面显示图
2.点击左侧6按钮,分数会增加一分
3.为了确认重新再试验一下,再次选择右侧的数字8,当然分数又会增加一分,会变2分
4.最后我们要看一下,我们选择错误数字的时候也是否会执行我们所要的结果
5.是的一切没问题!
xml代码
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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=".MainActivity">
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Number Game"
android:textSize="30sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.139" />
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="252dp"
android:layout_marginRight="252dp"
android:onClick="buttonOnClick"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="252dp"
android:layout_marginLeft="252dp"
android:onClick="buttonOnClick2"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="52dp"
android:text="맞춘 개수:0"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
<TextView
android:id="@+id/textView3"
android:layout_width="409dp"
android:layout_height="wrap_content"
android:layout_marginTop="181dp"
android:text="어느게 더 클 까 ?"
android:textAlignment="center"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
java代码
package com.example.numbergame;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import java.util.Random;
public class MainActivity extends AppCompatActivity {
private int num1;
private int num2;
private int points;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
roll();
}
private void roll(){
Random r=new Random();
num1=r.nextInt(9);
num2=r.nextInt(9);
while (num2==num1){ //当出现两位相同的数字的时候第2位数字会改变,
num2=r.nextInt(9); //直到两位数字不一样为止
}
Button btn1=(Button)findViewById(R.id.button);
Button btn2=(Button)findViewById(R.id.button2);
btn1.setText(""+ num1);
btn2.setText(""+ num2);
}
public void buttonOnClick(View view) {
if(num1>num2){
Toast.makeText(this,"correct",Toast.LENGTH_SHORT).show();
points++;
TextView txt=(TextView)findViewById(R.id.textView2);
txt.setText(""+ points);
}
else {
Toast.makeText(this, "incorrect", Toast.LENGTH_SHORT).show();
points--;
TextView txt = (TextView) findViewById(R.id.textView2);
txt.setText("" + points);
}
roll();
}
public void buttonOnClick2(View view) {
if(num1<num2){
Toast.makeText(this,"correct",Toast.LENGTH_SHORT).show();
points++;
TextView txt=(TextView)findViewById(R.id.textView2);
txt.setText(""+points);
}else {
Toast.makeText(this, "incorrect", Toast.LENGTH_SHORT).show();
points--;
TextView txt = (TextView) findViewById(R.id.textView2);
txt.setText("" + points);
}
roll();
}
}
至于xml代码里为什么有韩语:因为这是在来韩国当交换生的时候写的简单的Android Homework
上一篇: 链表(一) -- 单链表与双向链表