Giới thiệu sản phẩm
import pygame
import sys
pygame.init()
GRID_WIDTH, GRID_HEIGHT = 10, 20
CELL_SIZE = 30
SCREEN_WIDTH = GRID_WIDTH * CELL_SIZE
SCREEN_HEIGHT = GRID_HEIGHT * CELL_SIZE
screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
pygame.display.set_caption("Tetris Starter – Buổi 1")
clock = pygame.time.Clock()
BLACK = (0, 0, 0)
GRAY = (50, 50, 50)
CYAN = (0, 255, 255)
T_SHAPE = [(1, 0), (0, 1), (1, 1), (2, 1)]
current_shape = T_SHAPE
offset = [3, 0]
# Sự kiện tự rơi mỗi 0.5 giây
FALL_EVENT = pygame.USEREVENT + 1
pygame.time.set_timer(FALL_EVENT, 500)
def draw_grid():
for y in range(GRID_HEIGHT):
for x in range(GRID_WIDTH):
pygame.draw.rect(screen, GRAY, (x * CELL_SIZE, y * CELL_SIZE, CELL_SIZE, CELL_SIZE), 1)
def draw_shape(shape, offset):
for x, y in shape:
rect = pygame.Rect((x + offset[0]) * CELL_SIZE, (y + offset[1]) * CELL_SIZE, CELL_SIZE, CELL_SIZE)
pygame.draw.rect(screen, CYAN, rect)
def check_out_of_bounds(shape, offset):
for x, y in shape:
grid_x = x + offset[0]
grid_y = y + offset[1]
if grid_x < 0 or grid_x >= GRID_WIDTH or grid_y >= GRID_HEIGHT:
return True
return False
running = True
while running:
screen.fill(BLACK)
draw_grid()
draw_shape(current_shape, offset)
pygame.display.flip()
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
elif event.type == pygame.KEYDOWN:
temp_offset = offset.copy()
if event.key == pygame.K_LEFT:
temp_offset[0] -= 1
elif event.key == pygame.K_RIGHT:
temp_offset[0] += 1
elif event.key == pygame.K_DOWN:
temp_offset[1] += 1
if not check_out_of_bounds(current_shape, temp_offset):
offset = temp_offset
elif event.type == FALL_EVENT:
temp_offset = offset.copy()
temp_offset[1] += 1
if not check_out_of_bounds(current_shape, temp_offset):
offset[1] += 1
else:
print("Khối đã chạm đáy!")
clock.tick(60)
pygame.quit()
sys.exit()
import sys
pygame.init()
GRID_WIDTH, GRID_HEIGHT = 10, 20
CELL_SIZE = 30
SCREEN_WIDTH = GRID_WIDTH * CELL_SIZE
SCREEN_HEIGHT = GRID_HEIGHT * CELL_SIZE
screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
pygame.display.set_caption("Tetris Starter – Buổi 1")
clock = pygame.time.Clock()
BLACK = (0, 0, 0)
GRAY = (50, 50, 50)
CYAN = (0, 255, 255)
T_SHAPE = [(1, 0), (0, 1), (1, 1), (2, 1)]
current_shape = T_SHAPE
offset = [3, 0]
# Sự kiện tự rơi mỗi 0.5 giây
FALL_EVENT = pygame.USEREVENT + 1
pygame.time.set_timer(FALL_EVENT, 500)
def draw_grid():
for y in range(GRID_HEIGHT):
for x in range(GRID_WIDTH):
pygame.draw.rect(screen, GRAY, (x * CELL_SIZE, y * CELL_SIZE, CELL_SIZE, CELL_SIZE), 1)
def draw_shape(shape, offset):
for x, y in shape:
rect = pygame.Rect((x + offset[0]) * CELL_SIZE, (y + offset[1]) * CELL_SIZE, CELL_SIZE, CELL_SIZE)
pygame.draw.rect(screen, CYAN, rect)
def check_out_of_bounds(shape, offset):
for x, y in shape:
grid_x = x + offset[0]
grid_y = y + offset[1]
if grid_x < 0 or grid_x >= GRID_WIDTH or grid_y >= GRID_HEIGHT:
return True
return False
running = True
while running:
screen.fill(BLACK)
draw_grid()
draw_shape(current_shape, offset)
pygame.display.flip()
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
elif event.type == pygame.KEYDOWN:
temp_offset = offset.copy()
if event.key == pygame.K_LEFT:
temp_offset[0] -= 1
elif event.key == pygame.K_RIGHT:
temp_offset[0] += 1
elif event.key == pygame.K_DOWN:
temp_offset[1] += 1
if not check_out_of_bounds(current_shape, temp_offset):
offset = temp_offset
elif event.type == FALL_EVENT:
temp_offset = offset.copy()
temp_offset[1] += 1
if not check_out_of_bounds(current_shape, temp_offset):
offset[1] += 1
else:
print("Khối đã chạm đáy!")
clock.tick(60)
pygame.quit()
sys.exit()
Hình ảnh sản phẩm

Sản phẩm cùng tác giả
Sản phẩm liên quan
Không có sản phẩm nào
Đăng nhập để tham gia bình luận