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).
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.