Dưới Đây Là Một Ví Dụ Đơn Giản Về Code Cho Trò Chơi Xếp Gạch Sử Dụng Ngôn Ngữ Python

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 6

Dưới đây là một ví dụ đơn giản về code cho trò chơi xếp gạch sử dụng ngôn ngữ Python:

```python

import random

import pygame

import sys

# Thiết lập kích thước màn hình

screen_width = 800

screen_height = 600

# Thiết lập kích thước của một ô trong xếp gạch

block_size = 30

# Tạo một lớp Block để biểu diễn các khối gạch

class Block:

def __init__(self, x, y):

self.x = x

self.y = y

def draw(self, screen):

pygame.draw.rect(screen, (255, 255, 255), (self.x, self.y, block_size, block_size))

pygame.draw.rect(screen, (0, 0, 0), (self.x, self.y, block_size, block_size), 1)

# Tạo một lớp Game để quản lý trạng thái và logic của trò chơi

class Game:

def __init__(self):

self.screen = pygame.display.set_mode((screen_width, screen_height))

self.clock = pygame.time.Clock()

self.blocks = []

self.current_block = None
self.score = 0

def start(self):

pygame.init()

self.screen.fill((0, 0, 0))

pygame.display.set_caption("Trò chơi xếp gạch")

self.new_block()

while True:

for event in pygame.event.get():

if event.type == pygame.QUIT:

pygame.quit()

sys.exit()

elif event.type == pygame.KEYDOWN:

if event.key == pygame.K_LEFT:

self.move_block(-block_size, 0)

elif event.key == pygame.K_RIGHT:

self.move_block(block_size, 0)

elif event.key == pygame.K_DOWN:

self.move_block(0, block_size)

self.screen.fill((0, 0, 0))

self.update()

self.draw()

pygame.display.flip()

self.clock.tick(10)
def new_block(self):

x = random.randint(0, (screen_width // block_size) - 1) * block_size

self.current_block = Block(x, 0)

def move_block(self, dx, dy):

new_x = self.current_block.x + dx

new_y = self.current_block.y + dy

if self.can_move(new_x, new_y):

self.current_block.x = new_x

self.current_block.y = new_y

else:

self.merge_block()

self.clear_rows()

def can_move(self, x, y):

if x < 0 or x >= screen_width or y >= screen_height or self.is_collision(x, y):

return False

return True

def is_collision(self, x, y):

for block in self.blocks:

if block.x == x and block.y == y:

return True

return False

def merge_block(self):

self.blocks.append(self.current_block)

self.new_block()

def clear_rows(self):
rows_to_clear = []

for y in range(0, screen_height, block_size):

row_blocks = [block for block in self.blocks if block.y == y]

if len(row_blocks) == screen_width // block_size:

rows_to_clear.append(y)

if rows_to_clear:

self.score += len(rows_to_clear)

self.remove_rows(rows_to_clear)

def remove_rows(self, rows):

self.blocks = [block for block in self.blocks if block.y not in rows]

for row in rows:

for block in self.blocks:

if block.y < row:

block.y += block_size

def update(self):

self.current_block.y += block_size

if not self.can_move(self.current_block.x, self.current_block.y):

self.merge_block()

self.clear_rows()

if self.current_block.y <= 0:

pygame.quit()

sys.exit()

def draw(self):

for block in self.blocks:

block.draw(self.screen)

self.current_block.draw(self.screen)

# Khởi chạy trò chơi


game = Game()

game.start()

```

Lưu ý rằng code trên sử dụng thư viện pygame để hiển thị trò chơi trên màn hình. Để chạy code, bạn
cần cài đặt pygame thông qua lệnh `pip install pygame`. Sau đó, bạn có thể chạy code trên để trải
nghiệm trò chơi xếp gạch đơn giản.Dưới đây là một ví dụ đơn giản về code cho trò chơi xếp gạch sử
dụng ngôn ngữ Python:

```python

import random

import pygame

import sys

# Thiết lập kích thước màn hình

screen_width = 800

screen_height = 600

# Thiết lập kích thước của một ô trong xếp gạch

block_size = 30

# Tạo một lớp Block để biểu diễn các khối gạch

class Block:

def __init__(self, x, y):

self.x = x

self.y = y

def draw(self, screen):

pygame.draw.rect(screen, (255, 255, 255), (self.x, self.y, block_size, block_size))

pygame.draw.rect(screen, (0, 0, 0), (self.x, self.y, block_size, block_size), 1)

# Tạo một lớp Game để quản lý trạng thái và logic của trò chơi
class Game:

def __init__(self):

self.screen = pygame.display.set_mode((screen_width, screen_height))

self.clock = pygame.time.Cloc

You might also like