为图像赋值

发布于 2024-10-19 06:28:57 字数 4133 浏览 6 评论 0原文

所以我试图创建一个真正简单的骰子游戏......更像是一个骰子滚子游戏,我遇到了一个尚未找到答案的问题。显然,我有 6 个骰子面图像(1-6),但我在某个地方缺少的是如何在“掷骰子”时基本上为这些图像分配值。这样,当我开始实现一些游戏逻辑时,我可以比较骰子面的数量。这是我到目前为止所得到的,像往常一样,任何帮助将不胜感激。

public class CrapsGameActivity extends CrapsActivity {
    private final int rollAnimations = 50;
    private final int delayTime = 15;
    private Resources res;
    private final int[] diceImages = new int[]{R.drawable.die1, R.drawable.die2, 
            R.drawable.die3, R.drawable.die4, R.drawable.die5,R.drawable.die6};
    private Drawable dice[] = new Drawable[6];
    private final Random randomGen = new Random();

    private int diceSum;
    private int roll[] = new int[] {6,6};
    private ImageView die1;
    private TextView die1_Total;
    private int die1_number;
    private ImageView die2;
    private TextView die2_Total;
    private TextView diceTotal;
    private LinearLayout diceContainer;
    private Handler animationHandler;
    private long lastUpdate = -1;
    private float x, y, z;
    private float last_x, last_y, last_z;
    private boolean paused = false;
    private static final int UPDATE_DELAY = 50;


    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        paused = false;
        super.onCreate(savedInstanceState);
        setContentView(R.layout.game);
        setTitle(getString(R.string.app_name));
        res = getResources();

        for (int i = 0; i<6; i++){
            dice[i] = res.getDrawable(diceImages[i]);
        }


        diceContainer = (LinearLayout) findViewById(R.id.diceContainer);
        diceContainer.setOnClickListener(new OnClickListener(){
            @Override
            public void onClick(View v) {
                try{
                    rollDice();
                    die1_Total.setText("" + roll[0]);
                    die2_Total.setText("" + roll[1]);
                    diceTotal.setText(""+ diceSum);
                }catch(Exception e) {}; 
            }//end of onClick
        });//end of onClick listener

        die1 = (ImageView) findViewById(R.id.die1);
        die1_Total = (TextView) findViewById(R.id.TV_die1);
        die2 = (ImageView) findViewById(R.id.die2);
        die2_Total = (TextView) findViewById(R.id.TV_die2);

        diceTotal = (TextView) findViewById(R.id.TV_diceTotal);


        animationHandler = new Handler(){
            public void handleMessage(Message msg){
                die1.setImageDrawable(dice[roll[0]]);
                die2.setImageDrawable(dice[roll[1]]);
            }//end of handle message
        };//end of Handler
    }


    private void rollDice() {
        if(paused) return;
        new Thread(new Runnable(){
            @Override
            public void run(){
                for(int i = 0; i < rollAnimations; i++){
                    doRoll();
                }//end of for statement
            }//end of run()
        }).start();//end of thread
    }//end of rollDice()


    private void doRoll(){//only does a single roll
        roll[0] = randomGen.nextInt(6);
        roll[1] = randomGen.nextInt(6);
        diceSum = roll[0] + roll[1] + 2;    


        synchronized(getLayoutInflater()){
            animationHandler.sendEmptyMessage(0);
        }
        try{//delay for smooth animations
            Thread.sleep(delayTime);
        }catch(final InterruptedException e){
            e.printStackTrace();
        }
    }//end of doRoll()


    public void onResume(){
        super.onResume();
        paused = false;
    }//end of onResume()


    public void onPause(){
        super.onPause();
        paused = true;
    }//end of onPause()

}//end of activity

我可能做得不对,但代码(如下)。当我用它来查看模拟器上的数字时,它们是随机的,并且与骰子的面值不对应。就像如果我想计算骰子面值的总和(现在再次运行)0 代表 6 个面,2 代表 3 个面,这应该给我总的骰子面为 9,但我得到 2

public void onClick(View v) {
                try{
                    rollDice();
                    die1_Total.setText("" + roll[0]);
                    die2_Total.setText("" + roll[1]);
                    diceTotal.setText(""+ diceSum);
                }catch(Exception e) {}; 

So I am trying to create a real simple dice game...more like a dice roller game and I have encountered a problem for which I have yet to find an answer. So obviously I have 6 images for the die faces(1-6), but what I am missing somewhere is how to basically assign values to these images when the "dice are rolled". So that when I begin to implement some game logic I can compare what the numbers of the die faces would be. Here is what I have so far, and as usual any assistance would be much appreciated.

public class CrapsGameActivity extends CrapsActivity {
    private final int rollAnimations = 50;
    private final int delayTime = 15;
    private Resources res;
    private final int[] diceImages = new int[]{R.drawable.die1, R.drawable.die2, 
            R.drawable.die3, R.drawable.die4, R.drawable.die5,R.drawable.die6};
    private Drawable dice[] = new Drawable[6];
    private final Random randomGen = new Random();

    private int diceSum;
    private int roll[] = new int[] {6,6};
    private ImageView die1;
    private TextView die1_Total;
    private int die1_number;
    private ImageView die2;
    private TextView die2_Total;
    private TextView diceTotal;
    private LinearLayout diceContainer;
    private Handler animationHandler;
    private long lastUpdate = -1;
    private float x, y, z;
    private float last_x, last_y, last_z;
    private boolean paused = false;
    private static final int UPDATE_DELAY = 50;


    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        paused = false;
        super.onCreate(savedInstanceState);
        setContentView(R.layout.game);
        setTitle(getString(R.string.app_name));
        res = getResources();

        for (int i = 0; i<6; i++){
            dice[i] = res.getDrawable(diceImages[i]);
        }


        diceContainer = (LinearLayout) findViewById(R.id.diceContainer);
        diceContainer.setOnClickListener(new OnClickListener(){
            @Override
            public void onClick(View v) {
                try{
                    rollDice();
                    die1_Total.setText("" + roll[0]);
                    die2_Total.setText("" + roll[1]);
                    diceTotal.setText(""+ diceSum);
                }catch(Exception e) {}; 
            }//end of onClick
        });//end of onClick listener

        die1 = (ImageView) findViewById(R.id.die1);
        die1_Total = (TextView) findViewById(R.id.TV_die1);
        die2 = (ImageView) findViewById(R.id.die2);
        die2_Total = (TextView) findViewById(R.id.TV_die2);

        diceTotal = (TextView) findViewById(R.id.TV_diceTotal);


        animationHandler = new Handler(){
            public void handleMessage(Message msg){
                die1.setImageDrawable(dice[roll[0]]);
                die2.setImageDrawable(dice[roll[1]]);
            }//end of handle message
        };//end of Handler
    }


    private void rollDice() {
        if(paused) return;
        new Thread(new Runnable(){
            @Override
            public void run(){
                for(int i = 0; i < rollAnimations; i++){
                    doRoll();
                }//end of for statement
            }//end of run()
        }).start();//end of thread
    }//end of rollDice()


    private void doRoll(){//only does a single roll
        roll[0] = randomGen.nextInt(6);
        roll[1] = randomGen.nextInt(6);
        diceSum = roll[0] + roll[1] + 2;    


        synchronized(getLayoutInflater()){
            animationHandler.sendEmptyMessage(0);
        }
        try{//delay for smooth animations
            Thread.sleep(delayTime);
        }catch(final InterruptedException e){
            e.printStackTrace();
        }
    }//end of doRoll()


    public void onResume(){
        super.onResume();
        paused = false;
    }//end of onResume()


    public void onPause(){
        super.onPause();
        paused = true;
    }//end of onPause()

}//end of activity

I may not be doing it properly but the code (below). When I use this to see the numbers on the emulator, they are random and dont correspond to the face values of the dice. So like if I wanted to total the face values of the dice (just ran it again now) 0 is for 6 face, and 2 is for the 3 face, which should give me a total dice face of 9, but I am getting 2

public void onClick(View v) {
                try{
                    rollDice();
                    die1_Total.setText("" + roll[0]);
                    die2_Total.setText("" + roll[1]);
                    diceTotal.setText(""+ diceSum);
                }catch(Exception e) {}; 

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

难理解 2024-10-26 06:28:57
roll[0] = randomGen.nextInt(6);
roll[1] = randomGen.nextInt(6);

有你的价值观。您不希望图像包含数据(或者至少,我不会打扰)。相反,您可以根据这些值渲染图像/动画,然后可以直接使用这些值。

如果您想将值与图像一起存储,您始终可以创建一个对象(GameDie)并将图像和当前值存储在其中。

roll[0] = randomGen.nextInt(6);
roll[1] = randomGen.nextInt(6);

There are your values. You don't want the image to contain the data (or at least, I wouldn't bother). Instead, you render the images/animations based on these values, and can then work with those values directly later.

If you wanted to store the values with the images, you could always make an object (GameDie) and store the images and current value in there.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文