Nội dung bài họcLesson content
Biến (variable) là gì?What is a variable?
Biến (variable) là một "hộp" chứa dữ liệu. Trong game, bạn dùng biến để lưu điểm số. Ví dụ: tạo biến "điểm" = 0. Khi bắt được trái cây, điểm tăng lên 1. Biến có thể thay đổi giá trị trong khi game chạy.A variable is a "box" that holds data. In a game, you use a variable to store the score. For example: create a variable "score" = 0. When you catch a fruit, the score increases by 1. Variables can change value while the game runs.
Tạo biến trong ScratchCreating a variable in Scratch
Vào mục Variables → "Make a Variable" → đặt tên "điểm". Biến sẽ xuất hiện trên sân khấu. Dùng khối "set [điểm] to [0]" để đặt giá trị ban đầu. Dùng "change [điểm] by [1]" để tăng điểm.Go to Variables → "Make a Variable" → name it "score". The variable will appear on the stage. Use "set [score] to [0]" to set the initial value. Use "change [score] by [1]" to increase the score.
when flag clicked
set [điểm] to [0]
forever
move 10 steps
if <touching fruit?> then
change [điểm] by [1]
go to random position
Va chạm (collision) giữa spriteSprite collision detection
Khối "touching [sprite]?" kiểm tra xem sprite có chạm sprite khác không. Trong game bắt trái cây: nếu giỏ chạm trái cây → bắt được → tăng điểm + trái cây bay đi. Dùng khối "if touching [fruit] then" để kiểm tra va chạm.The "touching [sprite]?" block checks if a sprite is touching another sprite. In the fruit-catching game: if the basket touches the fruit → caught → increase score + fruit flies away. Use "if touching [fruit] then" to check collision.
Tạo vật thể rơi ngẫu nhiênCreating randomly falling objects
Dùng khối "go to random position" để trái cây xuất hiện ở vị trí ngẫu nhiên. Dùng "glide [1] secs to x: [random] y: [-180]" để trái cây rơi từ trên xuống dưới. Lặp lại forever để trái cây liên tục rơi.Use "go to random position" to make the fruit appear at a random position. Use "glide [1] secs to x: [random] y: [-180]" to make the fruit fall from top to bottom. Repeat forever to make fruits continuously fall.