大家好,我正在尝试在Android工作室中为洪水游戏生成一个网格作为我的任务的一部分。每次生成的网格应该是随机的,因此我使用switch语句并绘制以生成颜色,如下所示。
Paint paint1 = new Paint (getResources().getColor(R.color.color1));
Paint paint2 = new Paint (getResources().getColor(R.color.color2));
Paint paint3 = new Paint (getResources().getColor(R.color.color3));
Paint paint4 = new Paint (getResources().getColor(R.color.color4));
Paint paint5 = new Paint (getResources().getColor(R.color.color5));
Paint paint6 = new Paint (getResources().getColor(R.color.color6));
for (int col = 0; col < mGame.getHeight(); col++) {
for (int row = 0; row < mGame.getWidth(); row++) {
Random random = new Random(6);
int num = 1;
int randomResult = random.nextInt();
float cx = separator + (diameterx + separator) * col + diameterx / 2;
float cy = separator + (diametery + separator) * row + diametery / 2;
canvas.drawRect(cx, cy, availableWidth, availableHeight, paint6);
switch (randomResult) {
case 0:
canvas.drawRect(cx, cy, availableWidth/2, availableHeight/2, paint1);
break;
case 1:
canvas.drawRect(cx, cy, availableWidth, availableHeight, paint2);
break;
case 2:
canvas.drawRect(cx, cy, availableWidth, availableHeight, paint3);
break;
case 3:
canvas.drawRect(cx, cy, availableWidth, availableHeight, paint4);
break;
case 4:
canvas.drawRect(cx, cy, availableWidth, availableHeight, paint5);
break;
case 5:
canvas.drawRect(cx, cy, availableWidth, availableHeight, paint6);
break;
}
}
}
问题是switch语句中的代码不起作用(为了演示),switch语句上方的canvas.drawRect(cx, cy, availableWidth, availableHeight, paint6);
just似乎工作正常,因此在该块中的某些问题我无法理解。任何帮助,将不胜感激。
亲切的问候
尝试设置所有颜料:
paint.setStrokeWidth(20);
paint.setStyle(Paint.Style.STROKE);
paint.setAntiAlias(true);