Cấp độ: beginner35 phútminMiễn phíFreeCreative ExplorerCreative Explorer

Mini-game: Bắt trái câyMini-game: Catch the fruits

Mini-game bắt trái cây có tính điểmA fruit-catching mini-game with score

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

  • Dùng biến để lưu điểmUse variables to store score
  • Lập trình va chạm giữa spriteProgram sprite collision
  • Tạo vật thể rơi ngẫu nhiênCreate randomly falling objects

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

Mini-game bắt trái cây có tính điểmA fruit-catching mini-game with score

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.

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

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

1Tạo game bắt trái cây: giỏ di chuyển bằng phím trái/phải, trái cây rơi từ trên, chạm giỏ → +1 điểm.Create a fruit-catching game: basket moves with left/right keys, fruit falls from top, touching basket → +1 score.

Gợi ýHint

Giỏ: when key pressed → move. Trái cây: forever → glide down → if touching basket → +1 score.Basket: when key pressed → move. Fruit: forever → glide down → if touching basket → +1 score.

Lời giảiSolution
scratch
                      
                        -- Giỏ (Basket) --
when [left arrow] key pressed
  change x by -10
when [right arrow] key pressed
  change x by 10

-- Trái cây (Fruit) --
when flag clicked
set [điểm] to [0]
forever
  go to x: (pick random -200 to 200) y: 180
  glide 2 secs to x: (pick random -200 to 200) y: -180
  if <touching basket?> then
    change [điểm] by [1]
                      
                    

2Thêm điều kiện thua: nếu trái cây chạm đáy màn hình (y < -170) mà không chạm giỏ → nói "Thua!" và dừng game.Add a losing condition: if the fruit touches the bottom (y < -170) without touching the basket → say "Game Over!" and stop.

Gợi ýHint

Kiểm tra y position của trái cây. Dùng khối "stop all".Check the y position of the fruit. Use the "stop all" block.

Lời giảiSolution
scratch
                      
                        -- Thêm vào Trái cây --
  if <(y position) < -170> then
    say "Thua! / Game Over!" for 2 seconds
    stop [all]
                      
                    

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: Biến (variable) dùng để làm gì trong game?Q1: What is a variable used for in a game?

Câu 2: Khối "change [điểm] by [1]" làm gì?Q2: What does "change [score] by [1]" do?

Câu 3: Làm sao để trái cây rơi ngẫu nhiên?Q3: How do you make a fruit fall randomly?

Câu 4: Khối nào kiểm tra va chạm giữa 2 sprite?Q4: Which block checks collision between 2 sprites?