1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
|
#!/usr/bin/env python3
import sys
import time
import pygame
def zpiece():
return ((blue_square, blue_square, None), (None, blue_square, blue_square))
def refresh_screen():
screen.fill(black)
for row_idx, row in enumerate(grid):
for col_idx, element in enumerate(row):
if element is not None:
screen.blit(element, (col_idx * 50 + 1, row_idx * 50 + 1))
# Display the current piece
for row_idx, row in enumerate(current_piece):
for col_idx, element in enumerate(row):
if element is not None:
screen.blit(element, ((col_idx + current_piece_position[1]) * 50 + 1, (row_idx + current_piece_position[0]) * 50 + 1))
pygame.init()
black = (0, 0, 0)
white = (0xff, 0xff, 0xff)
blue = (0x10, 0x20, 0xbb)
white_square = pygame.Surface((48, 48))
white_square.fill(white)
blue_square = pygame.Surface((48, 48))
blue_square.fill(blue)
screen = pygame.display.set_mode((500, 1000))
grid = [[None for _ in range(10)] for _ in range(20)]
# Let's start with a Z piece
current_piece = zpiece()
current_piece_position = [0, 4]
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
elif event.type == pygame.KEYDOWN:
if event.key in (pygame.K_DOWN, pygame.K_j):
current_piece_position[0] += 1
if event.key in (pygame.K_UP, pygame.K_k):
current_piece_position[0] -= 1
if event.key in (pygame.K_RIGHT, pygame.K_l):
current_piece_position[1] += 1
if event.key in (pygame.K_LEFT, pygame.K_h):
current_piece_position[1] -= 1
refresh_screen()
pygame.display.flip()
time.sleep(.1)
|