android studio猜数字游戏
时间: 2023-09-14 16:05:00 浏览: 96
好的,这里是一个简单的猜数字游戏的Android Studio代码示例:
首先,我们需要一个布局文件来显示游戏界面。在res/layout文件夹中创建一个新的XML文件guess_game_layout.xml,将以下代码复制到文件中:
```
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Guess a number between 1 and 100" />
<EditText
android:id="@+id/editText1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="number" />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Guess" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Game" />
</LinearLayout>
```
这个布局文件包含一个文本视图,一个编辑文本框,两个按钮和另一个文本视图。第一个文本视图用于显示游戏提示,编辑文本框用于输入猜测数字,第一个按钮用于提交猜测,第二个文本视图用于显示结果,第二个按钮用于开始新的游戏。
接下来,我们需要一个Java类来实现游戏逻辑。在app/src/main/java文件夹中创建一个新的Java类GuessGame,将以下代码复制到文件中:
```
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
import java.util.Random;
public class GuessGame extends AppCompatActivity {
private int number;
private int attempts;
private TextView textView1;
private EditText editText1;
private Button button1;
private TextView textView2;
private Button button2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.guess_game_layout);
textView1 = (TextView) findViewById(R.id.textView1);
editText1 = (EditText) findViewById(R.id.editText1);
button1 = (Button) findViewById(R.id.button1);
textView2 = (TextView) findViewById(R.id.textView2);
button2 = (Button) findViewById(R.id.button2);
newGame();
}
public void guess(View view) {
String guessString = editText1.getText().toString();
int guess;
try {
guess = Integer.parseInt(guessString);
} catch (NumberFormatException e) {
textView2.setText("Invalid input");
return;
}
attempts++;
if (guess < number) {
textView2.setText("Try higher");
} else if (guess > number) {
textView2.setText("Try lower");
} else {
textView2.setText("You win in " + attempts + " attempts!");
button1.setEnabled(false);
editText1.setEnabled(false);
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Congratulations!");
builder.setMessage("You won in " + attempts + " attempts. Play again?");
builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
newGame();
button1.setEnabled(true);
editText1.setEnabled(true);
}
});
builder.setNegativeButton("No", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
finish();
}
});
builder.show();
}
}
public void newGame() {
Random random = new Random();
number = random.nextInt(100) + 1;
attempts = 0;
textView1.setText("Guess a number between 1 and 100");
textView2.setText("");
editText1.setText("");
}
}
```
这个类包含一个新游戏方法和一个猜测方法。新游戏方法生成一个随机数字并重置游戏状态。猜测方法获取输入的猜测,检查它是否有效,并根据猜测的大小更新游戏状态。如果玩家赢得了游戏,将显示一个消息框询问玩家是否想再玩一次。
最后,我们需要连接布局文件和Java类。在AndroidManifest.xml文件中添加以下代码:
```
<activity android:name=".GuessGame"
android:label="Guess Game" />
```
这将在应用程序中注册GuessGame活动。现在,您可以运行应用程序并开始猜数字游戏。
希望这可以帮助你开始编写自己的Android游戏!