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

Điều kiện và quyết địnhConditions and decisions

Game đèn giao thông tương tácAn interactive traffic light game

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

  • Hiểu câu lệnh if-thenUnderstand if-then statements
  • Dùng khối if trong ScratchUse the if block in Scratch
  • Lập trình nhân vật phản ứngProgram character reactions

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

Game đèn giao thông tương tácAn interactive traffic light game

Nội dung bài họcLesson content

Điều kiện là gì?What is a condition?

Điều kiện là cách bảo máy tính: "NẾU điều này đúng, THÌ làm việc A, NẾU SAI, THÌ làm việc B". Ví dụ: NẾU đèn xanh THÌ đi, NẾU đèn đỏ THÌ dừng. Máy tính kiểm tra điều kiện và quyết định hành động.A condition is a way to tell the computer: "IF this is true, THEN do action A, IF false, THEN do action B." For example: IF green light THEN go, IF red light THEN stop. The computer checks the condition and decides what to do.

Khối if trong ScratchThe if block in Scratch

Khối "if <điều kiện> then" sẽ chạy các khối bên trong chỉ khi điều kiện đúng. Khối "if <điều kiện> then else" cho phép làm việc khác khi điều kiện sai. Điều kiện thường dùng khối sensing như "touching color" hoặc "key pressed".The "if <condition> then" block runs the blocks inside only when the condition is true. The "if <condition> then else" block lets you do something else when the condition is false. Conditions often use sensing blocks like "touching color" or "key pressed".

scratch
                      
                        when flag clicked
if <touching color red?> then
  say "Đèn đỏ! Dừng lại!" for 2 seconds
else
  say "Đi tiếp!" for 2 seconds
                      
                    

Game đèn giao thôngTraffic light game

Tạo game: sprite xe sẽ dừng khi chạm màu đỏ và đi khi chạm màu xanh. Dùng backdrop có đèn giao thông. Lập trình: if touching red → say "Dừng!", else → say "Đi!". Thay đổi màu đèn và xem xe phản ứng.Create a game: the car sprite stops when touching red and goes when touching green. Use a backdrop with a traffic light. Program: if touching red → say "Stop!", else → say "Go!". Change the light color and watch the car react.

Nhiều điều kiện với else ifMultiple conditions with else if

Trong Scratch, bạn có thể xếp chồng nhiều khối if để kiểm tra nhiều điều kiện. Ví dụ: if touching red → dừng, if touching yellow → chậm lại, if touching green → đi. Máy tính kiểm tra từng điều kiện từ trên xuống.In Scratch, you can stack multiple if blocks to check multiple conditions. For example: if touching red → stop, if touching yellow → slow down, if touching green → go. The computer checks each condition from top to bottom.

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

1Lập trình sprite đổi màu khi nhấn phím: nếu nhấn phím R → đỏ, phím G → xanh, phím B → xanh dương.Program the sprite to change color on key press: if key R → red, key G → green, key B → blue.

Gợi ýHint

Dùng 3 khối "when key pressed" riêng biệt, mỗi khối đổi màu khác nhau.Use 3 separate "when key pressed" blocks, each changing to a different color.

Lời giảiSolution
scratch
                      
                        when [r] key pressed
  set color effect to 0
when [g] key pressed
  set color effect to 60
when [b] key pressed
  set color effect to 120
                      
                    

2Tạo game: nếu sprite chạm cạnh màn hình thì nói "Ôi!" và quay lại.Create a game: if the sprite touches the screen edge, say "Ouch!" and bounce back.

Gợi ýHint

Dùng khối "if touching edge then" và "bounce off edge".Use "if touching edge then" and "bounce off edge" blocks.

Lời giảiSolution
scratch
                      
                        when flag clicked
forever
  move 10 steps
  if <touching edge?> then
    say "Ôi! / Ouch!" for 1 second
    turn 180 degrees
                      
                    

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: Khối "if then" làm gì?Q1: What does the "if then" block do?

Câu 2: Trong game đèn giao thông, xe nên làm gì khi đèn vàng?Q2: In a traffic light game, what should the car do on yellow light?

Câu 3: Khối "else" dùng để làm gì?Q3: What is the "else" block used for?