Android Studio Java开发Simon Says游戏如何设置自动触发按钮前代码延迟
Android Simon Says记忆游戏的自动触发延迟实现
问题描述
在Android Studio环境下使用Java开发Simon Says记忆游戏时,需要实现自动模拟按钮操作的功能:在自动触发完整的按钮提示序列前,需要设置1秒的等待时长,询问对应的正确实现方式。
原始实现代码
public class GameActivity extends AppCompatActivity implements View.OnClickListener { Random rand = new Random(); // 绑定主线程的Handler,用于执行延迟UI任务 Handler handler = new Handler(Looper.getMainLooper()); Button btnGreen, btnRed, btnYellow, btnBlue; TextView tvScore; MediaPlayer mpSound1, mpSound2, mpSound3, mpSound4; Animation animation; boolean gameOver = false; boolean buttonClicked = false; String[] fourColors = {"Green", "Red", "Blue", "Yellow"}; ArrayList<String> allColors = new ArrayList<>(); int count = 0; int scoreToWin = 8; int i = 0; public void ClickGreen(){ btnGreen.startAnimation(animation); mpSound1.start(); } public void ClickRed(){ btnRed.startAnimation(animation); mpSound2.start(); } public void ClickBlue(){ btnBlue.startAnimation(animation); mpSound3.start(); } public void ClickYellow(){ btnYellow.startAnimation(animation); mpSound4.start(); } public void game(){ count = 0; i = 0; int random; random = rand.nextInt(4); allColors.add(fourColors[random]); handler.postDelayed(new Runnable() { @Override public void run() { for(i = 0; i < allColors.size(); i++) { switch (allColors.get(i)) { case "Green": ClickGreen(); Toast.makeText(GameActivity.this, "Green", Toast.LENGTH_SHORT).show(); break; case "Red": ClickRed(); Toast.makeText(GameActivity.this, "Red", Toast.LENGTH_SHORT).show(); break; case "Blue": ClickBlue(); Toast.makeText(GameActivity.this, "Blue", Toast.LENGTH_SHORT).show(); break; case "Yellow": ClickYellow(); Toast.makeText(GameActivity.this, "Yellow", Toast.LENGTH_SHORT).show(); break; } } } }, 1000); } @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_game); animation = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.push_down); tvScore = findViewById(R.id.tvScore); btnGreen = findViewById(R.id.btnGreen); btnRed = findViewById(R.id.btnRed); btnBlue = findViewById(R.id.btnBlue); btnYellow = findViewById(R.id.btnYellow); btnGreen.setOnClickListener(this); btnRed.setOnClickListener(this); btnBlue.setOnClickListener(this); btnYellow.setOnClickListener(this); mpSound1 = MediaPlayer.create(this, R.raw.sound1); mpSound2 = MediaPlayer.create(this, R.raw.sound2); mpSound3 = MediaPlayer.create(this, R.raw.sound3); mpSound4 = MediaPlayer.create(this, R.raw.sound4); game(); } @Override public void onClick(View v) { if(v.equals(btnGreen)) { btnGreen.startAnimation(animation); mpSound1.start(); gameOver = !allColors.get(count).equals("Green"); count++; buttonClicked = true; } else if(v.equals(btnRed)) { btnRed.startAnimation(animation); mpSound2.start(); gameOver = !allColors.get(count).equals("Red"); count++; buttonClicked = true; } else if(v.equals(btnBlue)) { btnBlue.startAnimation(animation); mpSound3.start(); gameOver = !allColors.get(count).equals("Blue"); count++; buttonClicked = true; } else if(v.equals(btnYellow)) { btnYellow.startAnimation(animation); mpSound4.start(); gameOver = !allColors.get(count).equals("Yellow"); count++; buttonClicked = true; } if(buttonClicked){ if (gameOver) { Intent intent = new Intent(GameActivity.this, GameOverActivity.class); startActivity(intent); } else { if (count >= allColors.size()) { tvScore.setText(String.valueOf(allColors.size())); game(); buttonClicked = false; if(allColors.size() == scoreToWin){ Toast.makeText(GameActivity.this, "WON!", Toast.LENGTH_SHORT).show(); } } } } } }
实现说明
Handler.postDelayed()是Android环境下实现主线程延迟的标准方案:将需要执行的UI操作(模拟按钮触发、动画播放、音效启动)封装为Runnable,通过绑定主线程消息队列的Handler设置延迟时长,既可以达到等待效果,又不会因为线程阻塞触发应用无响应(ANR)。- 原始代码已经实现了序列启动前1秒的等待效果,但存在体验缺陷:进入延迟执行的Runnable后,会通过for循环瞬间遍历全部记忆序列,将所有按钮的动画、音效在同一时间点全部触发,效果相互叠加,不符合Simon Says游戏“逐个播放按钮提示供用户记忆”的交互逻辑。
- 调整后的核心逻辑在保留1秒启动延迟的基础上,为每个按钮提示增加固定播放间隔,避免效果叠加:
// 封装单个颜色的触发逻辑,减少重复代码 private void triggerColorEffect(String color) { switch (color) { case "Green": ClickGreen(); break; case "Red": ClickRed(); break; case "Blue": ClickBlue(); break; case "Yellow": ClickYellow(); break; } } public void game(){ count = 0; i = 0; int random; random = rand.nextInt(4); allColors.add(fourColors[random]); // 保留原有的1秒启动等待,再开始播放整个提示序列 handler.postDelayed(new Runnable() { @Override public void run() { // 从序列第一个元素开始逐次播放 playSequenceByIndex(0); } }, 1000); } // 递归逐次播放序列元素,元素间间隔可根据动画时长调整 private void playSequenceByIndex(int index) { // 序列遍历完成即退出 if (index >= allColors.size()) return; handler.postDelayed(new Runnable() { @Override public void run() { triggerColorEffect(allColors.get(index)); // 当前元素触发完成后,间隔800ms触发下一个元素 playSequenceByIndex(index + 1); } }, 800); }
- 必要的内存泄漏防护:在Activity销毁时清除所有待执行的延迟任务,避免页面退出后任务仍在后台执行:
@Override protected void onDestroy() { super.onDestroy(); handler.removeCallbacksAndMessages(null); }
内容的提问来源于stack exchange,提问作者Amit
相关产品推荐
相关产品推荐

