Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/398.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 分离游戏逻辑和用户界面_Java_Android - Fatal编程技术网

Java 分离游戏逻辑和用户界面

Java 分离游戏逻辑和用户界面,java,android,Java,Android,这是一个只有三个视图的简单游戏:一个文本视图和两个按钮(红色和绿色按钮) 播放器应该按下与TextView背景色颜色相同的按钮。如果TextView背景颜色为红色,则播放器应按下红色按钮 假设有三种不同的播放模式: 常规:你可以玩一整天。 时间限制:你只有3秒钟的时间回答。 计时试验:在30秒内尽可能多次回答正确 所有模式共享相同的布局(xml) 方法newGame()和newRound()的行为将根据玩家选择的模式而有所不同 “常规”可以有两种状态: 播放=显示颜色,程序正在等待用户输入 给出

这是一个只有三个视图的简单游戏:一个文本视图和两个按钮(红色和绿色按钮)

播放器应该按下与TextView背景色颜色相同的按钮。如果TextView背景颜色为红色,则播放器应按下红色按钮

假设有三种不同的播放模式: 常规:你可以玩一整天。 时间限制:你只有3秒钟的时间回答。 计时试验:在30秒内尽可能多次回答正确

所有模式共享相同的布局(xml)

方法newGame()和newRound()的行为将根据玩家选择的模式而有所不同

“常规”可以有两种状态: 播放=显示颜色,程序正在等待用户输入 给出了错误的答案=玩家按下了错误的按钮。现在该按钮已禁用。玩家必须按正确的按钮才能继续(新回合)

“时间限制”总是从3降到0。当用户按下按钮时,计时器总是重新启动,如果计时器达到0,则会触发新一轮

“Timetrial”将从30倒计时到0,但在其他情况下将像“常规”游戏一样工作

这三种模式都有一些共同的变量,例如: 布尔isRed//要知道TextView是否被屏蔽

在活动和课程方面,这将如何组织/结构?

以下是一些实际代码:

activity_main.xml:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.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">

<Button
    android:id="@+id/btn_red"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Normal game"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

<Button
    android:id="@+id/btn_timer"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Timer game"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/btn_red"
    app:layout_constraintVertical_bias="0.041" />

</android.support.constraint.ConstraintLayout>
PlayActivity.java:

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class MainActivity extends AppCompatActivity implements 
View.OnClickListener {

Button btn_normal;
Button btn_timer;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    btn_normal = findViewById(R.id.btn_red);
    btn_timer = findViewById(R.id.btn_timer);

    btn_normal.setOnClickListener(this);
    btn_timer.setOnClickListener(this);

}

@Override
public void onClick(View v) {
    Intent intent = new Intent(this, PlayActivity.class);
    if (v == btn_normal) {
        intent.putExtra("GAME_MODE", "regular");
    } else if (v == btn_timer) {
        intent.putExtra("GAME_MODE", "timelimit");
    }
    startActivity(intent);
}
}
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class PlayActivity extends AppCompatActivity implements View.OnClickListener {

    static Button btn_red;
    static Button btn_green;
    static TextView txt_colorbox;
    Game game;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_play);

        txt_colorbox = findViewById(R.id.txt_colorbox);

        btn_red = findViewById(R.id.btn_red);
        btn_green = findViewById(R.id.btn_green);

        btn_red.setOnClickListener(this);
        btn_green.setOnClickListener(this);

        String gameMode = getIntent().getStringExtra("GAME_MODE");

        if (gameMode.equals("regular")) {
            game = new RegularGame();
        } else if (gameMode.equals("timelimit")) {
            game = new TimelimitGame();
        }
        else if (gameMode.equals("timetrial")) {
            game = new TimetrialGame();
        }

        game.newGame();
        game.newRound();

    }

    @Override
    public void onClick(View v) {
        if (v == btn_red) {
            System.out.println("pressed Red");
        } else if (v == btn_green) {
            System.out.println("pressed Green");
        }
    }
}
Game.java:

public abstract class Game {

    boolean isRed;

    public void newGame() {
    }

    public void newRound() {
    }

}
public class TimelimitGame extends Game {

    @Override
    public void newGame() {
        System.out.println("New game: timelimit mode");
    }

    @Override
    public void newRound() {
        System.out.println("New Round");
    }

}
public class TimetrialGame extends Game {

    @Override
    public void newGame() {
        System.out.println("New game: timer mode");
    }

    @Override
    public void newRound() {
        System.out.println("New Round");
    }

}
RegularGame.java

public class RegularGame extends Game {

    @Override
    public void newGame() {
        System.out.println("New game: regular mode");
    }

    @Override
    public void newRound() {
        System.out.println("New Round");
    }

}
TimelimitGame.java:

public abstract class Game {

    boolean isRed;

    public void newGame() {
    }

    public void newRound() {
    }

}
public class TimelimitGame extends Game {

    @Override
    public void newGame() {
        System.out.println("New game: timelimit mode");
    }

    @Override
    public void newRound() {
        System.out.println("New Round");
    }

}
public class TimetrialGame extends Game {

    @Override
    public void newGame() {
        System.out.println("New game: timer mode");
    }

    @Override
    public void newRound() {
        System.out.println("New Round");
    }

}
TimetrialGame.java:

public abstract class Game {

    boolean isRed;

    public void newGame() {
    }

    public void newRound() {
    }

}
public class TimelimitGame extends Game {

    @Override
    public void newGame() {
        System.out.println("New game: timelimit mode");
    }

    @Override
    public void newRound() {
        System.out.println("New Round");
    }

}
public class TimetrialGame extends Game {

    @Override
    public void newGame() {
        System.out.println("New game: timer mode");
    }

    @Override
    public void newRound() {
        System.out.println("New Round");
    }

}

我应该在Game()构造函数中提供上下文/活动吗。这样我就可以以这种方式访问TextView和按钮,或者updateUI应该驻留在PlayActivity中,但从游戏类收集信息?

您需要使用界面。创建一个包含发生的事件的接口,并使PlayActivity实现它。将此接口发送到Game()构造函数。然后,在游戏中创建一些逻辑来决定调用接口的哪个方法。@SusmitAgrawal-like MVC?你能提供更多的信息吗?或者是相关链接:)