pygameでテトリス
Revision | d622fc317431de9728953fef3db6d0213af375e2 (tree) |
---|---|
Time | 2014-11-01 11:07:35 |
Author | stone725 <stone_725@outl...> |
Commiter | stone725 |
modified: game.py
@@ -15,27 +15,27 @@ screen = pygame.display.set_mode(size) | ||
15 | 15 | now_y = 0 |
16 | 16 | a = 0 |
17 | 17 | run = True |
18 | -field = [[1,0,0,0,0,0,0,0,0,1], | |
19 | - [1,0,0,0,0,0,0,0,0,1], | |
20 | - [1,0,0,0,0,0,0,0,0,1], | |
21 | - [1,0,0,0,0,0,0,0,0,1], | |
22 | - [1,0,0,0,0,0,0,0,0,1], | |
23 | - [1,0,0,0,0,0,0,0,0,1], | |
24 | - [1,0,0,0,0,0,0,0,0,1], | |
25 | - [1,0,0,0,0,0,0,0,0,1], | |
26 | - [1,0,0,0,0,0,0,0,0,1], | |
27 | - [1,0,0,0,0,0,0,0,0,1], | |
28 | - [1,0,0,0,0,0,0,0,0,1], | |
29 | - [1,0,0,0,0,0,0,0,0,1], | |
30 | - [1,0,0,0,0,0,0,0,0,1], | |
31 | - [1,0,0,0,0,0,0,0,0,1], | |
32 | - [1,0,0,0,0,0,0,0,0,1], | |
33 | - [1,0,0,0,0,0,0,0,0,1], | |
34 | - [1,0,0,0,0,0,0,0,0,1], | |
35 | - [1,0,0,0,0,0,0,0,0,1], | |
36 | - [1,0,0,0,0,0,0,0,0,1], | |
37 | - [1,0,0,0,0,0,0,0,0,1], | |
38 | - [1,1,1,1,1,1,1,1,1,1]] | |
18 | +field = [[1,0,0,0,0,0,0,0,0,0,0,1], | |
19 | + [1,0,0,0,0,0,0,0,0,0,0,1], | |
20 | + [1,0,0,0,0,0,0,0,0,0,0,1], | |
21 | + [1,0,0,0,0,0,0,0,0,0,0,1], | |
22 | + [1,0,0,0,0,0,0,0,0,0,0,1], | |
23 | + [1,0,0,0,0,0,0,0,0,0,0,1], | |
24 | + [1,0,0,0,0,0,0,0,0,0,0,1], | |
25 | + [1,0,0,0,0,0,0,0,0,0,0,1], | |
26 | + [1,0,0,0,0,0,0,0,0,0,0,1], | |
27 | + [1,0,0,0,0,0,0,0,0,0,0,1], | |
28 | + [1,0,0,0,0,0,0,0,0,0,0,1], | |
29 | + [1,0,0,0,0,0,0,0,0,0,0,1], | |
30 | + [1,0,0,0,0,0,0,0,0,0,0,1], | |
31 | + [1,0,0,0,0,0,0,0,0,0,0,1], | |
32 | + [1,0,0,0,0,0,0,0,0,0,0,1], | |
33 | + [1,0,0,0,0,0,0,0,0,0,0,1], | |
34 | + [1,0,0,0,0,0,0,0,0,0,0,1], | |
35 | + [1,0,0,0,0,0,0,0,0,0,0,1], | |
36 | + [1,0,0,0,0,0,0,0,0,0,0,1], | |
37 | + [1,0,0,0,0,0,0,0,0,0,0,1], | |
38 | + [1,1,1,1,1,1,1,1,1,1,1,1]] | |
39 | 39 | |
40 | 40 | block = [[[[0,0,0,0], # □□□□ |
41 | 41 | [1,0,0,0], # ■□□□ |
@@ -235,7 +235,9 @@ block = [[[[0,0,0,0], # □□□□ | ||
235 | 235 | [2,3,3,-1]]]] |
236 | 236 | e = 0 |
237 | 237 | now_x = 20 |
238 | +x_move = 180 | |
238 | 239 | count_x = 0 |
240 | +font = pygame.font.Font(None, 35) | |
239 | 241 | score = 0 |
240 | 242 | now_block = random.randint(0,6) |
241 | 243 | not_move = False |
@@ -265,29 +267,26 @@ while run: | ||
265 | 267 | for y in range(4): #縦 |
266 | 268 | for x in range(4): #横 |
267 | 269 | if block[now_block][e][y][x] == 1: |
268 | - pygame.draw.rect(sc, (255,255,0),Rect(x * 20 + now_x,y * 20 + now_y,20,20)) | |
270 | + pygame.draw.rect(sc, (255,255,0),Rect(x * 20 + now_x + x_move,y * 20 + now_y,20,20)) | |
269 | 271 | for y in range(21): |
270 | - for x in range(10): | |
272 | + for x in range(12): | |
271 | 273 | if field[y][x] == 1: |
272 | - if y % 20 == 0 or x % 9 == 0: | |
273 | - pygame.draw.rect(sc, (255,0,0),Rect(x * 20, y * 20,20,20)) | |
274 | + if y == 20 or x % 11 == 0: | |
275 | + pygame.draw.rect(sc, (255,0,0),Rect(x * 20 + x_move, y * 20,20,20)) | |
274 | 276 | else: |
275 | - pygame.draw.rect(sc, (255,255,0),Rect(x * 20, y * 20, 20, 20)) | |
277 | + pygame.draw.rect(sc, (255,255,0),Rect(x * 20 + x_move, y * 20, 20, 20)) | |
276 | 278 | a += 1 |
277 | - if a >= rate: | |
278 | - for x in range(4): | |
279 | + for x in range(4): | |
279 | 280 | if block[now_block][e][5][x] != -1: |
280 | - if field[now_y / 20 + block[now_block][e][5][x] + 1][now_x / 20 + x] == 1: | |
281 | - not_move = True | |
282 | - if now_y / 20 + block[now_block][e][5][x] < 3: | |
281 | + for y in range(block[now_block][e][4][x], block[now_block][e][5][x] + 1): | |
282 | + if field[now_y / 20 + y][now_x / 20 + x] == 1: | |
283 | + not_move = True | |
283 | 284 | print "Game Over" |
284 | 285 | print score |
285 | 286 | run = False |
286 | - else: | |
287 | - score += 1 | |
288 | - if not_move == False: | |
289 | - now_y += 20 | |
290 | - a -= rate | |
287 | + break | |
288 | + if run == False: | |
289 | + break | |
291 | 290 | key = pygame.key.get_pressed() |
292 | 291 | if key[K_LEFT]: |
293 | 292 | if count_x > 0: |
@@ -324,12 +323,14 @@ while run: | ||
324 | 323 | if block[now_block][e][5][x] != -1: |
325 | 324 | if field[now_y / 20 + block[now_block][e][5][x] + 1][now_x / 20 + x] == 1: |
326 | 325 | not_move = True |
327 | - if now_y / 20 + block[now_block][e][5][x] < 3: | |
326 | + if now_y / 20 + block[now_block][e][5][x] < 4: | |
328 | 327 | print "Game Over" |
329 | 328 | print score |
330 | 329 | run = False |
330 | + break | |
331 | 331 | else: |
332 | 332 | score += 1 |
333 | + break | |
333 | 334 | if not_move == False: |
334 | 335 | now_y += 20 |
335 | 336 | a -= rate |
@@ -345,19 +346,51 @@ while run: | ||
345 | 346 | not_move = False |
346 | 347 | for y in range(19, 0, -1): |
347 | 348 | can_copy = True |
348 | - for x in range(1,10): | |
349 | + for x in range(1,11): | |
349 | 350 | if field[y][x] == 0: |
350 | 351 | can_copy = False |
351 | 352 | if can_copy == True: |
352 | 353 | score += 10 |
353 | - for x in range(1,9): | |
354 | + for x in range(1,11): | |
354 | 355 | field[0][x] = 0 |
355 | 356 | for yy in range(y,0,-1): |
356 | 357 | field[yy] = copy.copy(field[yy - 1]) |
358 | + y += 1 | |
359 | + if a >= rate: | |
360 | + for x in range(4): | |
361 | + if block[now_block][e][5][x] != -1: | |
362 | + if field[now_y / 20 + block[now_block][e][5][x] + 1][now_x / 20 + x] == 1: | |
363 | + not_move = True | |
364 | + if now_y / 20 + block[now_block][e][5][x] < 3: | |
365 | + print "Game Over" | |
366 | + print score | |
367 | + run = False | |
368 | + break | |
369 | + else: | |
370 | + score += 1 | |
371 | + break | |
372 | + for x in range(4): | |
373 | + if block[now_block][e][5][x] != -1: | |
374 | + for y in range(block[now_block][e][4][x], block[now_block][e][5][x] + 1): | |
375 | + if field[now_y / 20 + y][now_x / 20 + x] == 1: | |
376 | + not_move = True | |
377 | + print "Game Over" | |
378 | + print score | |
379 | + run = False | |
380 | + break | |
381 | + if run == False: | |
382 | + break | |
383 | + | |
384 | + if not_move == False: | |
385 | + now_y += 20 | |
386 | + a -= rate | |
387 | + stri = "Score : " + str(score) | |
388 | + string = font.render(stri, True, (255,255,255)) | |
389 | + screen.blit(string, (220, 420)) | |
390 | + | |
357 | 391 | clock.tick(rate) |
358 | 392 | pygame.display.flip() |
359 | 393 | |
360 | 394 | |
361 | 395 | |
362 | 396 | |
363 | - |