我想做的是制作一个游戏Simon的android版本。我不想使用libgdx,但如果没有其他选择,我可以。
游戏的工作方式是,计算机向玩家显示一个按钮序列,然后玩家重复该序列。除了我似乎无法为玩家制作一个动画序列之外,我已经让它工作得很好了。
我有一个随机数发生器。使用生成的数字,我想更改按钮背景的颜色一小段时间,然后将其更改回来。我把这个循环了很多次。我试过SystemClock。sleep(500),但它只是在显示应用程序之前运行循环。
主要的问题是,所有的事情都必须在onLayoutInflated内部完成,因为磨损必须在圆形和方形面之间进行选择。据我所知,这必须在onCreate方法中完成。
有人知道如何为android穿戴做一些游戏循环和/或动画序列吗。
另外,我会在修复代码后立即添加代码。我一直在摆弄它。
package com.happypantzinc.memory;
import android.app.Activity;
import android.graphics.Color;
import android.graphics.Point;
import android.graphics.PorterDuff;
import android.os.Bundle;
import android.os.Handler;
import android.os.SystemClock;
import android.support.wearable.view.WatchViewStub;
import android.view.Display;
import android.view.MotionEvent;
import android.view.View;
import android.view.animation.AnimationUtils;
import android.widget.ImageButton;
import java.util.ArrayList;
import java.util.Random;
public class MainActivity extends Activity {
private ImageButton mOverlay, mTopLeft, mTopRight, mBotLeft, mBotRight;
private Boolean isPlayerTurn = false;
private Boolean isRunning = false;
private Random random = new Random();
private int level = 10;
private ArrayList<Integer> compSeq;
private ArrayList<Integer> playSeq;
private float screenW, screenH;
//setup colors
final int RED_UP = Color.rgb(180, 0, 0);
final int RED_DOWN = Color.rgb(255, 77, 0);
final int GREEN_UP = Color.rgb(0, 180, 0);
final int GREEN_DOWN = Color.rgb(0, 255, 77);
final int BLUE_UP = Color.rgb(0, 0, 180);
final int BLUE_DOWN = Color.rgb(0, 77, 255);
final int YELLOW_UP = Color.rgb(180, 180, 0);
final int YELLOW_DOWN = Color.rgb(255, 255, 0);
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
compSeq = new ArrayList<Integer>();
playSeq = new ArrayList<Integer>();
setContentView(R.layout.activity_main);
final WatchViewStub stub = (WatchViewStub) findViewById(R.id.watch_view_stub);
stub.setOnLayoutInflatedListener(new WatchViewStub.OnLayoutInflatedListener() {
@Override
public void onLayoutInflated(WatchViewStub stub) {
mOverlay = (ImageButton) stub.findViewById(R.id.overlay);
mTopLeft = (ImageButton) stub.findViewById(R.id.topLeft);
mTopRight = (ImageButton) stub.findViewById(R.id.topRight);
mBotLeft = (ImageButton) stub.findViewById(R.id.botLeft);
mBotRight = (ImageButton) stub.findViewById(R.id.botRight);
setupButtons();
setupButtonActions(mTopLeft, GREEN_UP, GREEN_DOWN);
setupButtonActions(mTopRight, RED_UP, RED_DOWN);
setupButtonActions(mBotLeft, YELLOW_UP, YELLOW_DOWN);
setupButtonActions(mBotRight, BLUE_UP, BLUE_DOWN);
mOverlay.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
isRunning = true;
mOverlay.setClickable(false);
mOverlay.setVisibility(View.GONE);
gameLoop();
}
});
}
});
//get screen size
Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
screenW = size.x;
screenH = size.y;
}
private void gameLoop() {
while(isRunning) {
//Computer turn
while (compSeq.size() < level) {
int val = random.nextInt(4);
if (!mOverlay.isShown()) {
//Computer shows sequence
switch (val) {
case 0:
mTopLeft.getBackground().setColorFilter(GREEN_DOWN, PorterDuff.Mode.MULTIPLY);
System.out.println(val);
SystemClock.sleep(500);
mTopLeft.getBackground().setColorFilter(GREEN_UP, PorterDuff.Mode.MULTIPLY);
break;
case 1:
mTopRight.getBackground().setColorFilter(RED_DOWN, PorterDuff.Mode.MULTIPLY);
System.out.println(val);
SystemClock.sleep(500);
mTopRight.getBackground().setColorFilter(RED_UP, PorterDuff.Mode.MULTIPLY);
break;
case 2:
mBotLeft.getBackground().setColorFilter(YELLOW_DOWN, PorterDuff.Mode.MULTIPLY);
System.out.println(val);
SystemClock.sleep(500);
mBotLeft.getBackground().setColorFilter(YELLOW_UP, PorterDuff.Mode.MULTIPLY);
break;
case 3:
mBotRight.getBackground().setColorFilter(BLUE_DOWN, PorterDuff.Mode.MULTIPLY);
System.out.println(val);
SystemClock.sleep(500);
mBotRight.getBackground().setColorFilter(BLUE_UP, PorterDuff.Mode.MULTIPLY);
break;
default:
break;
}
compSeq.add(val);
SystemClock.sleep(300);
}
}
isPlayerTurn = true;
while (playSeq.size() < level) {
//Check for correct input
if (playSeq.size() > 0 && (playSeq.get(playSeq.size()-1) != compSeq.get(playSeq.size()-1))) {
isRunning = false;
break;
}
}
playSeq.clear();
compSeq.clear();
isPlayerTurn = false;
}
}
private void setupButtons() {
//set width and height of buttons
android.view.ViewGroup.LayoutParams params;
params = mOverlay.getLayoutParams();
params.height = (int) screenH;
params.width = (int) screenW;
mOverlay.setLayoutParams(params);
params = mTopLeft.getLayoutParams();
params.height = (int)(screenH/2);
params.width = (int)(screenW/2);
mTopLeft.setLayoutParams(params);
params = mTopRight.getLayoutParams();
params.height = (int)(screenH/2);
params.width = (int)(screenW/2);
mTopRight.setLayoutParams(params);
params = mBotLeft.getLayoutParams();
params.height = (int)(screenH/2);
params.width = (int)(screenW/2);
mBotLeft.setLayoutParams(params);
params = mBotRight.getLayoutParams();
params.height = (int)(screenH/2);
params.width = (int)(screenW/2);
mBotRight.setLayoutParams(params);
//set position of buttons
mOverlay.setX(0);
mOverlay.setY(0);
mTopLeft.setX(0);
mTopLeft.setY(0);
mTopRight.setX(screenW / 2);
mTopRight.setY(0);
mBotLeft.setX(0);
mBotLeft.setY(screenH / 2);
mBotRight.setX(screenW / 2);
mBotRight.setY(screenH / 2);
//set initial background tints
mTopLeft.getBackground().setColorFilter(GREEN_UP, PorterDuff.Mode.MULTIPLY);
mTopRight.getBackground().setColorFilter(RED_UP, PorterDuff.Mode.MULTIPLY);
mBotLeft.getBackground().setColorFilter(YELLOW_UP, PorterDuff.Mode.MULTIPLY);
mBotRight.getBackground().setColorFilter(BLUE_UP, PorterDuff.Mode.MULTIPLY);
}
private void setupButtonActions(final ImageButton button, final int tint_up, final int tint_down) {
//create button listeners and tints
button.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
if (isPlayerTurn) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
button.getBackground().setColorFilter(tint_down, PorterDuff.Mode.MULTIPLY);
} else if (event.getAction() == MotionEvent.ACTION_UP) {
button.getBackground().setColorFilter(tint_up, PorterDuff.Mode.MULTIPLY);
}
//Enter correct sequence value
int val = 4;
if (button == mTopLeft) {
val = 0;
} else if (button == mTopRight) {
val = 1;
} else if (button == mBotLeft) {
val = 2;
} else if (button == mBotRight) {
val = 3;
}
playSeq.add(val);
}
return false;
}
});
}
}