Cấp độ: beginner20 phútminMiễn phíFreeJunior BuilderJunior Builder

An toàn số cơ bảnBasic digital safety

Checklist an toàn số cá nhânA personal digital safety checklist

Bạn sẽ học gìWhat you will learn

  • Tạo mật khẩu mạnhCreate strong passwords
  • Nhận biết email/link lừa đảoRecognize phishing emails/links
  • Hiểu quyền riêng tư onlineUnderstand online privacy

Bạn sẽ tạo gìWhat you will build

Checklist an toàn số cá nhânA personal digital safety checklist

Nội dung bài họcLesson content

Platformer là gì?What is a platformer?

Platformer là game mà nhân vật nhảy qua các nền (platform) để thu thập vật phẩm và tránh kẻ thù. Ví dụ nổi tiếng: Mario, Sonic. Yếu tố chính: trọng lực, nhảy, di chuyển trái/phải, va chạm với nền.A platformer is a game where the character jumps across platforms to collect items and avoid enemies. Famous examples: Mario, Sonic. Key elements: gravity, jumping, left/right movement, platform collision.

Trọng lực (gravity)Gravity

Trong platformer, nhân vật luôn bị kéo xuống dưới. Tạo biến velocity_y (vận tốc dọc). Mỗi lần update(): velocity_y += 0.5 (trọng lực), player.y += velocity_y. Khi chạm nền: velocity_y = 0 (dừng rơi).In a platformer, the character is always pulled downward. Create a velocity_y variable. Each update(): velocity_y += 0.5 (gravity), player.y += velocity_y. When touching the ground: velocity_y = 0 (stop falling).

python
                      
                        velocity_y = 0
GRAVITY = 0.5
GROUND_Y = 500

def update():
    global velocity_y
    # Trọng lực
    velocity_y += GRAVITY
    player.y += velocity_y
    
    # Chạm đất
    if player.y > GROUND_Y:
        player.y = GROUND_Y
        velocity_y = 0
    
    # Nhảy
    if keyboard.space and player.y == GROUND_Y:
        velocity_y = -12  # Nhảy lên
                      
                    

Di chuyển trái/phảiLeft/right movement

Di chuyển ngang đơn giản: if keyboard.left: player.x -= 5, if keyboard.right: player.x += 5. Không có trọng lực ngang nên di chuyển trái/phải dễ hơn nhảy. Có thể thêm giới hạn màn hình để player không chạy ra ngoài.Horizontal movement is simple: if keyboard.left: player.x -= 5, if keyboard.right: player.x += 5. No horizontal gravity so left/right is easier than jumping. You can add screen limits so the player does not run off-screen.

Thêm coin và kẻ thùAdding coins and enemies

Tạo danh sách coin: coins = [Actor("coin", (x, y)) for ...]. Trong update(), kiểm tra va chạm với từng coin. Tạo enemy di chuyển qua lại. Chạm enemy → mất mạng hoặc game over.Create a coin list: coins = [Actor("coin", (x, y)) for ...]. In update(), check collision with each coin. Create an enemy that moves back and forth. Touching the enemy → lose a life or game over.

Bài tập thực hànhHands-on exercises

1Hoàn thiện platformer: player di chuyển trái/phải, nhảy bằng space, có trọng lực, chạm coin → +1 điểm.Complete the platformer: player moves left/right, jumps with space, has gravity, touching coin → +1 score.

Gợi ýHint

Kết hợp: keyboard.left/right + space + gravity + colliderect.Combine: keyboard.left/right + space + gravity + colliderect.

Lời giảiSolution
python
                      
                        import random
from pgzero.actor import Actor

player = Actor("player")
player.x = 100
player.y = 500
velocity_y = 0
GRAVITY = 0.5
GROUND_Y = 500
diem = 0

coins = [Actor("coin", (random.randint(100, 700), random.randint(100, 400))) for _ in range(5)]

def update():
    global velocity_y, diem
    if keyboard.left: player.x -= 5
    if keyboard.right: player.x += 5
    velocity_y += GRAVITY
    player.y += velocity_y
    if player.y > GROUND_Y:
        player.y = GROUND_Y
        velocity_y = 0
    if keyboard.space and player.y == GROUND_Y:
        velocity_y = -12
    for coin in coins:
        if player.colliderect(coin):
            diem += 1
            coin.x = random.randint(100, 700)
            coin.y = random.randint(100, 400)

def draw():
    screen.fill((100, 150, 255))
    player.draw()
    for coin in coins:
        coin.draw()
    screen.draw.text(f"Điểm: {diem}", (10, 10), color="white")
                      
                    

Kiểm traQuiz

Chọn đáp án đúng cho mỗi câu. Nhấn để xem giải thích.Choose the correct answer for each question. Click to see explanation.

Câu 1: Platformer là game gì?Q1: What kind of game is a platformer?

Câu 2: Trọng lực (gravity) trong game ảnh hưởng đến trục nào?Q2: Which axis does gravity affect in a game?

Câu 3: Để nhân vật nhảy, velocity_y nên có giá trị thế nào?Q3: To make the character jump, what should velocity_y be?