Giới thiệu sản phẩm
import pygame
import random
pygame.init()
WINDOW_WIDTH = 600
WINDOW_HEGHT = 400
window = pygame.display.set_mode((WINDOW_WIDTH,WINDOW_HEGHT))
pygame.display.set_caption("Tetris")
BLACK=(0,0,0)
WHITE=(255,255,255)
RED=(255,0,0)
GREEN=(0,255,0)
BLUE=(0,0,255)
CYAN=(0,255,255)
MAGENTA=(255,0,255)
YELLOW=(255,255,0)
ORANGE=(255,165,0)
COLORS=[RED,GREEN,BLUE,YELLOW,ORANGE,CYAN,MAGENTA]
GRID_WIDTH = 10
GRID_HEGHT = 18
CELL_SIZE = 20
SHAPES = [
[[1,1,1,1]],
[[1,1],[1,1]],
[[1,1,0],[0,1,1]],
[[0,1,1],[1,1,0]],
[[1,1,1],[0,1,0]],
[[1,1,1],[1,0,0]],
[[1,1,1],[0,0,1]],
]
def draw_game_over():
font = pygame.font.Font("none", 64)
text = font.render("Game Over", True, RED)
text_rect=text.get_rect(center=(WINDOW_WIDTH//2,WINDOW_HEGHT//2))
window.blit(text,text_rect)
INITAL_X = GRID_WIDTH//2 - 2
INITAL_Y = 0
clock = pygame.time.Clock()
def draw_grid():
for x in range(0,WINDOW_WIDTH,CELL_SIZE):
pygame.draw.line(window,WHITE,(x,0),(x,WINDOW_HEGHT))
for y in range(0,WINDOW_HEGHT,CELL_SIZE):
pygame.draw.line(window,WHITE,(0,y),(WINDOW_WIDTH,y))
def draw_tetromino(tetromino,x,y,color):
for row in range(len(tetromino)):
for col in range(len(tetromino[row])):
if tetromino[row][col] == 1:
pygame.draw.rect(window,color,(x+col*CELL_SIZE,y+row*CELL_SIZE,CELL_SIZE,CELL_SIZE))
def check_collision(tetromino,x,y,grid):
for row in range(len(tetromino)):
for col in range(len(tetromino[row])):
if tetromino[row][col]==1:
if (x + col<0 or x + col>=GRID_WIDTH or y + row>=GRID_HEGHT or grid[y+row][x+col]!= BLACK):
return True
return False
def rotate_tetromino(tetromino):
return list(zip(*reversed(tetromino)))
def clear_rows(grid):
rows_cleared = 0
for row in range(GRID_HEGHT):
if all (cell != BLACK for cell in grid[row]):
del grid[row]
grid.insert(0,[BLACK]*GRID_WIDTH)
rows_cleared += 1
return rows_cleared
def tetris():
grid = [[BLACK] * GRID_WIDTH for _ in range(GRID_HEGHT)]
tetromino = random.choice(SHAPES)
tetromino_color = random.choice(COLORS)
x = INITAL_X
y = INITAL_Y
game_over=False
score = 0
while not game_over:
for event in pygame.event.get():
if event.type == pygame.QUIT:
game_over = True
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
if not check_collision(tetromino,x - 1,y,grid):
x -= 1
elif event.key == pygame.K_RIGHT:
if not check_collision(tetromino,x + 1,y,grid):
x += 1
elif event.key == pygame.K_DOWN:
if not check_collision(tetromino,x,y + 1,grid):
y += 1
elif event.key == pygame.K_UP:
rotated_tetromino = rotate_tetromino(tetromino)
if not check_collision(rotated_tetromino,x,y,grid):
tetromino = rotated_tetromino
if not check_collision(tetromino,x,y+1,grid):
y += 1
else:
for row in range(len(tetromino)):
for col in range(len(tetromino[row])):
if tetromino[row][col] == 1:
grid[y+row][x+col] = tetromino_color
rows_cleared = clear_rows(grid)
score += rows_cleared
tetromino = random.choice(SHAPES)
tetromino_color = random.choice(COLORS)
x = INITAL_X
y = INITAL_Y
if check_collision(tetromino,x,y,grid):
game_over = True
window.fill(BLACK)
draw_grid()
draw_tetromino(tetromino,x*CELL_SIZE,
y*CELL_SIZE,tetromino_color)
for row in range(GRID_HEGHT):
for col in range(GRID_WIDTH):
if grid[row][col] != BLACK:
pygame.draw.rect(window,grid[row][col],(col*CELL_SIZE,row*CELL_SIZE,CELL_SIZE,CELL_SIZE))
pygame.display.update()
clock.tick(5)
draw_game_over
pygame.display.update
pygame.time.wait(2000)
tetris()
pygame.quit
import random
pygame.init()
WINDOW_WIDTH = 600
WINDOW_HEGHT = 400
window = pygame.display.set_mode((WINDOW_WIDTH,WINDOW_HEGHT))
pygame.display.set_caption("Tetris")
BLACK=(0,0,0)
WHITE=(255,255,255)
RED=(255,0,0)
GREEN=(0,255,0)
BLUE=(0,0,255)
CYAN=(0,255,255)
MAGENTA=(255,0,255)
YELLOW=(255,255,0)
ORANGE=(255,165,0)
COLORS=[RED,GREEN,BLUE,YELLOW,ORANGE,CYAN,MAGENTA]
GRID_WIDTH = 10
GRID_HEGHT = 18
CELL_SIZE = 20
SHAPES = [
[[1,1,1,1]],
[[1,1],[1,1]],
[[1,1,0],[0,1,1]],
[[0,1,1],[1,1,0]],
[[1,1,1],[0,1,0]],
[[1,1,1],[1,0,0]],
[[1,1,1],[0,0,1]],
]
def draw_game_over():
font = pygame.font.Font("none", 64)
text = font.render("Game Over", True, RED)
text_rect=text.get_rect(center=(WINDOW_WIDTH//2,WINDOW_HEGHT//2))
window.blit(text,text_rect)
INITAL_X = GRID_WIDTH//2 - 2
INITAL_Y = 0
clock = pygame.time.Clock()
def draw_grid():
for x in range(0,WINDOW_WIDTH,CELL_SIZE):
pygame.draw.line(window,WHITE,(x,0),(x,WINDOW_HEGHT))
for y in range(0,WINDOW_HEGHT,CELL_SIZE):
pygame.draw.line(window,WHITE,(0,y),(WINDOW_WIDTH,y))
def draw_tetromino(tetromino,x,y,color):
for row in range(len(tetromino)):
for col in range(len(tetromino[row])):
if tetromino[row][col] == 1:
pygame.draw.rect(window,color,(x+col*CELL_SIZE,y+row*CELL_SIZE,CELL_SIZE,CELL_SIZE))
def check_collision(tetromino,x,y,grid):
for row in range(len(tetromino)):
for col in range(len(tetromino[row])):
if tetromino[row][col]==1:
if (x + col<0 or x + col>=GRID_WIDTH or y + row>=GRID_HEGHT or grid[y+row][x+col]!= BLACK):
return True
return False
def rotate_tetromino(tetromino):
return list(zip(*reversed(tetromino)))
def clear_rows(grid):
rows_cleared = 0
for row in range(GRID_HEGHT):
if all (cell != BLACK for cell in grid[row]):
del grid[row]
grid.insert(0,[BLACK]*GRID_WIDTH)
rows_cleared += 1
return rows_cleared
def tetris():
grid = [[BLACK] * GRID_WIDTH for _ in range(GRID_HEGHT)]
tetromino = random.choice(SHAPES)
tetromino_color = random.choice(COLORS)
x = INITAL_X
y = INITAL_Y
game_over=False
score = 0
while not game_over:
for event in pygame.event.get():
if event.type == pygame.QUIT:
game_over = True
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
if not check_collision(tetromino,x - 1,y,grid):
x -= 1
elif event.key == pygame.K_RIGHT:
if not check_collision(tetromino,x + 1,y,grid):
x += 1
elif event.key == pygame.K_DOWN:
if not check_collision(tetromino,x,y + 1,grid):
y += 1
elif event.key == pygame.K_UP:
rotated_tetromino = rotate_tetromino(tetromino)
if not check_collision(rotated_tetromino,x,y,grid):
tetromino = rotated_tetromino
if not check_collision(tetromino,x,y+1,grid):
y += 1
else:
for row in range(len(tetromino)):
for col in range(len(tetromino[row])):
if tetromino[row][col] == 1:
grid[y+row][x+col] = tetromino_color
rows_cleared = clear_rows(grid)
score += rows_cleared
tetromino = random.choice(SHAPES)
tetromino_color = random.choice(COLORS)
x = INITAL_X
y = INITAL_Y
if check_collision(tetromino,x,y,grid):
game_over = True
window.fill(BLACK)
draw_grid()
draw_tetromino(tetromino,x*CELL_SIZE,
y*CELL_SIZE,tetromino_color)
for row in range(GRID_HEGHT):
for col in range(GRID_WIDTH):
if grid[row][col] != BLACK:
pygame.draw.rect(window,grid[row][col],(col*CELL_SIZE,row*CELL_SIZE,CELL_SIZE,CELL_SIZE))
pygame.display.update()
clock.tick(5)
draw_game_over
pygame.display.update
pygame.time.wait(2000)
tetris()
pygame.quit
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
Tuyệt vời🧡
Đăng nhập để tham gia bình luận
Tuyệt vời👏👏👏
Đăng nhập để tham gia bình luận
✨Đỉnh vậy chèn
Đăng nhập để tham gia bình luận
👌👌 không phí tiền mạng tí nào
Đăng nhập để tham gia bình luận
toả sáng 🤘🤘🤘
Đăng nhập để tham gia bình luận
Đăng nhập để tham gia bình luận