我正在开发一个 simon说项目,我有一个数组,其颜色点亮了按钮,然后我有一个空数组,应该有用户拥有的按钮按下,问题是我真的不知道如何获取用户按钮输入(用户按下哪些按钮以及按什么顺序)我还有条件,如果用户按错了按钮比游戏开始新活动,问题是现在不起作用,在我调整代码之前它几乎立即工作,除非我猜到这里的颜色是我的代码,请帮助。
public function showClassInformation(Request $request)
{
return response($this->ClassInformation()->get());
}
//===============================================================
public function ClassInformation()
{
$classes = MyClass::join('academics','academics.academic_id','=','classes.academic_id')
->join('levels','levels.level_id','=','classes.level_id')
->join('shifts','shifts.shift_id','=','classes.shift_id')
->join('times','times.time_id','=','classes.time_id')
->join('batches','batches.batch_id','=','classes.batch_id')
->join('groups','groups.group_id','=','classes.group_id');
return view('class.classinfo',compact('classes'));
}
好的澄清一下,第一个开关案例是颜色如何点亮并随机化一个数字并点亮相应的数字。
package com.gabie212.simonsays;
import android.content.Context;
import android.content.Intent;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.Timer;
import java.util.TimerTask;
import static android.os.SystemClock.sleep;
public class GameActivity extends AppCompatActivity implements View.OnClickListener {
private int i = 0;
private Thread t = new Thread();
private Button greenButton;
private Button redButton;
private Button blueButton;
private Button yellowButton;
private Button startButton;
private ArrayList<Integer> randomColors = new ArrayList<Integer>();
private ArrayList<Integer> userColors = new ArrayList<Integer>();
private GameManger gm;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
gm = new GameManger(this);
setContentView(R.layout.activity_game);
greenButton = (Button) findViewById(R.id.btnGreen);
redButton = (Button) findViewById(R.id.btnRed);
blueButton = (Button) findViewById(R.id.btnBlue);
yellowButton = (Button) findViewById(R.id.btnYellow);
startButton = (Button) findViewById(R.id.btnStart);
startButton.setOnClickListener(this);
greenButton.setOnClickListener(this);
redButton.setOnClickListener(this);
blueButton.setOnClickListener(this);
yellowButton.setOnClickListener(this);
}
@Override
public void onClick(View v) {
int num;
num = gm.getColor(4);
randomColors.add(num);
android.os.Handler handler = new android.os.Handler();
//TODO if the start button is pressed multiple times simultaneously it starts the lightup loop multiple times simultaneously
if (v.getId() == startButton.getId()) {
for (i = 0; i < randomColors.size(); i++) //light up loop
{
switch (randomColors.get(i)) {
case 1:
greenButton.setBackgroundResource(R.drawable.greenlightup);
handler.postDelayed(new Runnable() {
@Override
public void run() {
greenButton.setBackgroundResource(R.drawable.green);
}
}, 2000);
break;
case 2: redButton.setBackgroundResource(R.drawable.redlightup);
handler.postDelayed(new Runnable() {
@Override
public void run() {
redButton.setBackgroundResource(R.drawable.red);
}
}, 2000);
break;
case 3:
blueButton.setBackgroundResource(R.drawable.bluelightup);
handler.postDelayed(new Runnable() {
@Override
public void run() {
blueButton.setBackgroundResource(R.drawable.blue);
}
}, 2000);
break;
case 4:
yellowButton.setBackgroundResource(R.drawable.yellowlightup);
handler.postDelayed(new Runnable() {
@Override
public void run() {
yellowButton.setBackgroundResource(R.drawable.yellow);
}
}, 2000);
break;
}
}
for(i=0;i<randomColors.size();i++)
{
if(v.getId()==greenButton.getId())
{
userColors.add(1);
}
else
{
if(v.getId()==redButton.getId()){
userColors.add(2);
}
else
{
if(v.getId()==blueButton.getId())
{
userColors.add(3);
}
else
{
userColors.add(4);
}
}
}
}
if(randomColors.size()!=userColors.size()) {
for (i = 0; i < randomColors.size(); i++) {
if (randomColors.get(i) != userColors.get(i)) {
Intent i = new Intent(GameActivity.this, GameOverActivity.class);
startActivity(i);
}
}
}
userColors.clear();
}
}
}
然后有are,if和loop应该得到用户输入的按钮。
@Override
public void onClick(View v) {
int num;
num = gm.getColor(4);
randomColors.add(num);
android.os.Handler handler = new android.os.Handler();
//TODO if the start button is pressed multiple times simultaneously it starts the lightup loop multiple times simultaneously
if (v.getId() == startButton.getId()) {
for (i = 0; i < randomColors.size(); i++) //light up loop
{
switch (randomColors.get(i)) {
case 1: greenButton.setBackgroundResource(R.drawable.greenlightup);
handler.postDelayed(new Runnable() {
@Override
public void run(){
greenButton.setBackgroundResource(R.drawable.green);
}
}, 2000);
break;
case 2:
redButton.setBackgroundResource(R.drawable.redlightup);
handler.postDelayed(new Runnable() {
@Override
public void run() {
redButton.setBackgroundResource(R.drawable.red);
}
}, 2000);
break;
case 3:
blueButton.setBackgroundResource(R.drawable.bluelightup);
handler.postDelayed(new Runnable() {
@Override
public void run() {
blueButton.setBackgroundResource(R.drawable.blue);
}
}, 2000);
break;
case 4:
yellowButton.setBackgroundResource(R.drawable.yellowlightup);
handler.postDelayed(new Runnable() {
@Override
public void run(){
yellowButton.setBackgroundResource(R.drawable.yellow);
}
}, 2000);
break;
}
}
然后最后检查按钮是否为正确按钮的部分,如果没有,它会将用户发送到另一个表示游戏结束的活动
for(i=0;i<randomColors.size();i++)
{
if(v.getId()==greenButton.getId())
{
userColors.add(1);
}
else
{
if(v.getId()==redButton.getId()){
userColors.add(2);
}
else
{
if(v.getId()==blueButton.getId())
{
userColors.add(3);
}
else
{
userColors.add(4);
}
}
}
如何让我的程序以一种可行的方式获取按钮的输入?