質問編集履歴

3

変更

2018/07/31 14:23

投稿

退会済みユーザー
test CHANGED
@@ -1 +1 @@
1
- 【android studio】 テトリスでブロックを0.5秒ごとに落下させたいのですがThread.sleep(500);を使ってもうまくいきません原因は何でしょうか
1
+ 【android studio】 ブロックを0.5秒ごとに落下させたいのですがThread.sleep(500);を使ってもうまくいきません原因は何でしょうか
test CHANGED
File without changes

2

別スレッド化とはこういう事でしょうか?

2018/07/31 14:23

投稿

退会済みユーザー
test CHANGED
File without changes
test CHANGED
@@ -212,10 +212,168 @@
212
212
 
213
213
  ```java
214
214
 
215
+ import android.support.v7.app.AppCompatActivity;
216
+
217
+ import android.os.Bundle;
218
+
219
+ import android.widget.TextView;
220
+
221
+
222
+
223
+
224
+
225
+ public class MainActivity extends AppCompatActivity {
226
+
227
+ String screen;
228
+
229
+ int random, i, j, x, y, z;
230
+
231
+
232
+
233
+ int field[][] = //パズルの中身
234
+
235
+ { // 0 1 2 3 4 5 6 7 8 9 10 11 12
236
+
237
+ {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2}, //0
238
+
239
+ {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2}, //1
240
+
241
+ {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2}, //2
242
+
243
+ {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2}, //3
244
+
245
+ {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2}, //4
246
+
247
+ {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2}, //5
248
+
249
+ {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2}, //6
250
+
251
+ {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2}, //7
252
+
253
+ {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2}, //8
254
+
255
+ {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2}, //9
256
+
257
+ {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2}, //10
258
+
259
+ {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2}, //11
260
+
261
+ {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2}, //12
262
+
263
+ {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2}, //13
264
+
265
+ {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2}, //14
266
+
267
+ {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2}, //15
268
+
269
+ {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2}, //16
270
+
271
+ {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2}, //17
272
+
273
+ {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2}, //18
274
+
275
+ {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2}, //19
276
+
277
+ {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2}, //20
278
+
279
+ };
280
+
281
+
282
+
283
+ int blocktype[][][][] = { //ブロックタイプ 7種 回転 0度、90度、180度、360度
284
+
285
+ {
286
+
287
+ { //0-0
288
+
289
+ {0, 1, 0, 0},
290
+
291
+ {0, 1, 0, 0},
292
+
293
+ {0, 1, 0, 0},
294
+
295
+ {0, 1, 0, 0}
296
+
297
+ },
298
+
299
+ -------------------------------------省略
300
+
301
+ { //6-3
302
+
303
+ {0, 1, 0, 0},
304
+
305
+ {1, 1, 1, 0},
306
+
307
+ {0, 1, 0, 0},
308
+
309
+ {0, 0, 0, 0}
310
+
311
+ }
312
+
313
+ }
314
+
315
+ };
316
+
317
+
318
+
319
+ @Override
320
+
321
+ protected void onCreate(Bundle savedInstanceState) {
322
+
323
+ super.onCreate(savedInstanceState);
324
+
325
+ setContentView(R.layout.activity_main);
326
+
327
+
328
+
329
+ blockoutput();
330
+
331
+ }
332
+
333
+
334
+
335
+ protected void display() { //画面に表示
336
+
337
+ TextView textView = findViewById(R.id.puzzle);
338
+
339
+ screen = "";
340
+
341
+
342
+
343
+ for (i = 0; i <= 20; i++) {
344
+
345
+ for (j = 0; j <= 12; j++) {
346
+
347
+ if (field[i][j] == 1) {
348
+
349
+ screen = screen + '■';
350
+
351
+ } else if (field[i][j] == 0) {
352
+
353
+ screen = screen + '□';
354
+
355
+ } else {
356
+
357
+ screen = screen + '\n';
358
+
359
+ }
360
+
361
+ }
362
+
363
+ }
364
+
365
+ textView.setText(screen);
366
+
367
+ }
368
+
369
+
370
+
215
- protected void blockoutput() { //7種のブロックをrandomに1つ取り出して表示
371
+ protected void blockoutput() { //7種のブロックをrandomに1つ取り出して表示
216
372
 
217
373
  random = (int) (Math.random() * 10 % 6);
218
374
 
375
+ time tm = new time();
376
+
219
377
 
220
378
 
221
379
  display();
@@ -238,9 +396,7 @@
238
396
 
239
397
  display();
240
398
 
241
- for (x = 0; x < 1000000000; x++) {
399
+ tm.start();
242
-
243
- }
244
400
 
245
401
  for (x = 0; x <= 3; x++) {
246
402
 
@@ -256,6 +412,26 @@
256
412
 
257
413
  }
258
414
 
415
+ }
416
+
417
+
418
+
419
+
420
+
421
+ class time extends Thread {
422
+
423
+ public void run() {
424
+
259
- コード
425
+ try {
426
+
427
+ Thread.sleep(500);
428
+
429
+ } catch (InterruptedException e) {
430
+
431
+ }
432
+
433
+ }
434
+
435
+ }
260
436
 
261
437
  ```

1

コードを修正して「Thread.sleep(500);」ではなく「for (x = 0; x < 1000000000; x++){}」に変えてみました。

2018/07/30 08:31

投稿

退会済みユーザー
test CHANGED
File without changes
test CHANGED
@@ -207,3 +207,55 @@
207
207
  }
208
208
 
209
209
  ```
210
+
211
+ ###修正後
212
+
213
+ ```java
214
+
215
+ protected void blockoutput() { //7種のブロックをrandomに1つ取り出して表示
216
+
217
+ random = (int) (Math.random() * 10 % 6);
218
+
219
+
220
+
221
+ display();
222
+
223
+ for (z = 0; z + 3 <= 20; z++) {
224
+
225
+ for (x = 0; x <= 3; x++) {
226
+
227
+ for (y = 0; y <= 3; y++) {
228
+
229
+ if (blocktype[random][0][x][y] == 1) {
230
+
231
+ field[z + x][4 + y] = blocktype[random][0][x][y];
232
+
233
+ }
234
+
235
+ }
236
+
237
+ }
238
+
239
+ display();
240
+
241
+ for (x = 0; x < 1000000000; x++) {
242
+
243
+ }
244
+
245
+ for (x = 0; x <= 3; x++) {
246
+
247
+ for (y = 0; y <= 3; y++) {
248
+
249
+ field[z + x][4 + y] = 0;
250
+
251
+ }
252
+
253
+ }
254
+
255
+ }
256
+
257
+ }
258
+
259
+ コード
260
+
261
+ ```