gpt4 book ai didi

java - 从随机位置获取坐标并使用它们

转载 作者:行者123 更新时间:2023-12-01 11:47:49 25 4
gpt4 key购买 nike

我的代码应该像这样工作:

您会看到一个黑色按钮(可点击,[start_time])和一个灰色按钮(不可点击,[buttongame1])。 Buttongame1 预测 start_time 接下来将移动到哪里。当您单击 start_time 时,start_time 移动到 Buttongame1 的 x 和 y。同时,buttongame1随机设置了新的位置。现在,当您单击 start_time 时,start_time 会移动到 Buttongame1 的新 x 和 y。同时,buttongame1随机设置新的位置,依此类推。

我已经尝试过这个,但问题是,我无法从buttongame1获取或使用x和y

public void onClick(View v) {

int x0;
int y0;
int x1;
int y1;
int x2;
int y2;
int x3;
int y3;
int x4;
int y4;
int x5;
int y5;
int x6;
int y6;
textview1 = (TextView) findViewById(R.id.over);


Random r = new Random();
RelativeLayout decorView = (RelativeLayout) buttongame1.getParent();

int screenWidth = decorView.getWidth();
int screenHeight = decorView.getHeight();

i++;

if(v == gameover){
i--;
btncounter.setText("GAME OVER");
start_time.setVisibility(View.GONE);
textview1.setVisibility(View.VISIBLE);
Animation anim = new AlphaAnimation(0.0f, 1.0f);
anim.setDuration(50); //You can manage the time of the blink with this parameter
anim.setStartOffset(20);
anim.setRepeatMode(Animation.REVERSE);
anim.setRepeatCount(Animation.INFINITE);
textview1.startAnimation(anim);
Toast.makeText(this, "GAME OVER", Toast.LENGTH_SHORT).show();

finish();
}

if (i == 0 ) {
btncounter.setText("0");
buttongame1.setX(r.nextInt(screenHeight - buttongame1.getWidth()));
buttongame1.setY(r.nextInt(screenHeight - buttongame1.getHeight()));
int x0 = (int) buttongame1.getX();
int y0 = (int) buttongame1.getY();
}

if (i == 1 ) {
btncounter.setText("1");
startTime = System.currentTimeMillis();
buttongame1.setX(r.nextInt(screenHeight - buttongame1.getWidth()));
buttongame1.setY(r.nextInt(screenHeight - buttongame1.getHeight()));
int x1 = (int) buttongame1.getX();
int y1 = (int) buttongame1.getY();
start_time.setX((x1));
start_time.setY(y1);

// ... and so on

最佳答案

如果我理解得很好,只需在更改之前从buttongame1获取X和Y即可。

     if (i == 1 ) {
btncounter.setText("1");
startTime = System.currentTimeMillis();
int x1 = (int) buttongame1.getX();
int y1 = (int) buttongame1.getY();
buttongame1.setX(r.nextInt(screenHeight - buttongame1.getWidth()));
buttongame1.setY(r.nextInt(screenHeight - buttongame1.getHeight()));

start_time.setX((x1));
start_time.setY(y1);
}

此代码还:

   if (i == 0 ) {
btncounter.setText("0");
int x0 = (int) buttongame1.getX();
int y0 = (int) buttongame1.getY();
buttongame1.setX(r.nextInt(screenHeight - buttongame1.getWidth()));
buttongame1.setY(r.nextInt(screenHeight - buttongame1.getHeight()));

}

关于java - 从随机位置获取坐标并使用它们,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29020162/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com