7-dars: Scratch — sikllar va shartlar
Dars haqida
Davomiyligi: 90 daqiqa Maqsad: Talaba Control bloklarni (forever, repeat, if, if-else, wait, stop) chuqur o'rganishi, ulardan murakkab dasturlar yarata olishi kerak.
1. Control bloklari — boshqaruv
Control (to'q sariq rang) — kodning oqimini boshqaradi.
2. wait — kutish
wait 1 seconds1 sekund kutadi. Keyingi bloklar bajarilmaydi.
when 🟢 clicked
say "Bir" for 1 secs
wait 1 seconds
say "Ikki" for 1 secs
wait 1 seconds
say "Uch" for 1 secs3. repeat — N marta takrorlash
repeat 10
move 10 steps
turn ↻ 36 degrees10 marta:
- 10 piksel oldinga
- 36 daraja aylanish
Natija — o'nburchak chiziladi (10 × 36 = 360 daraja). Tomonlar ko'paysa (masalan, repeat 36, turn 10) — doiraga yaqinlashadi.
Misol: kvadrat chizish
when 🟢 clicked
pen down
repeat 4
move 100 steps
turn ↻ 90 degrees
pen up4 ta tomon, har biri 90 daraja burilish.
Misol: ko'pburchak chizish
repeat 5
move 100 steps
turn ↻ (360 / 5) degreesBeshburchak (360 / 5 = 72).
Universal:
ask [Necha tomon?] and wait
set [tomon] = (answer)
repeat (tomon)
move 100 steps
turn ↻ (360 / (tomon)) degreesFoydalanuvchi raqam beradi — Scratch shu ko'pburchakni chizadi.
4. forever — cheksiz takrorlash
forever
move 5 steps
if on edge, bounceTo'xtagunchaga qadar doim ishlaydi (Stop tugmasi bosilguncha).
forever ichida wait ishlatish
forever ichida wait kerak — aks holda CPU 100% yuklanadi.
forever
move 5 steps
wait 0.05 seconds ← buni unutmang5. if — shart
if <condition> then
... amallarShart rost bo'lsa — amallar bajariladi.
Misol: chetni tekshirish
forever
move 10 steps
if <touching [edge]?> then
turn ↻ 180 degrees6. if-else — shart bilan else
if <condition> then
... A amallari
else
... B amallariShart rost — A. Yolg'on — B.
Misol: o'tdimi?
ask [Sizning ball?] and wait
if <(answer) > 59> then
say [O'tdingiz!] for 2 secs
else
say [Yiqildingiz] for 2 secs7. Nested if (shart ichida shart)
if <(answer) >= 90> then
say [A] for 2 secs
else
if <(answer) >= 80> then
say [B] for 2 secs
else
if <(answer) >= 70> then
say [C] for 2 secs
else
if <(answer) >= 60> then
say [D] for 2 secs
else
say [F] for 2 secsYoki AND/OR ishlatish:
if <((answer) > 60) and ((answer) < 80)> then
say [Yaxshi]8. wait until — biror shartgacha kutish
wait until <key [space] pressed?>
say [Endi space bosildi!] for 2 secsSpace bosilguncha — kutadi. Bosilgandan keyin — davom etadi.
9. repeat until — shart ROST bo'lguncha takrorlash
repeat until <touching [Sprite2]?>
move 5 stepsSprite2'ga tegmaguncha — 5 piksel oldinga harakat qiladi.
10. stop — to'xtatish
3 ta variant:
- stop all — barcha sprite, barcha kod
- stop this script — faqat shu kod
- stop other scripts in sprite — shu sprite'ning boshqa kodlari
if <touching [edge]?> then
say [Tegdi!] for 1 secs
stop all11. clone — sprite nusxasini yaratish
Clone — sprite'ning dinamik nusxasi. Foydali — masalan, kosmik o'yinda olov o'qlari.
when 🟢 clicked
forever
create clone of [myself]
wait 1 secondsHar sekundda sprite'ning yangi nusxasi yaratiladi.
Clone uchun maxsus event
when I start as a clone
go to [random position]
glide 2 secs to x:0 y:0
delete this cloneYangi clone — tasodifiy joyda paydo bo'lib, markazga siljiydi va o'chiriladi.
12. Murakkab misol: Asteroid o'yini
Kosmik o'yin. Asteroidlar yuqoridan tushadi. Raketa strelkalar bilan boshqariladi.
Asteroid (clone bilan)
when 🟢 clicked
hide
forever
create clone of [myself]
wait (pick random 0.5 to 1.5) seconds
when I start as a clone
show
go to x:(pick random -240 to 240) y:180
repeat until <touching [edge]?>
change y by -5
delete this cloneRaketa (strelkalar bilan)
when 🟢 clicked
go to x:0 y:-150
when [left arrow] key pressed
change x by -10
when [right arrow] key pressed
change x by 10Tegishish
when I start as a clone (asteroid uchun)
forever
if <touching [Raketa]?> then
broadcast [oyin tugadi]
delete this clonewhen I receive [oyin tugadi] (Raketa uchun)
say [O'yin tugadi!] for 2 secs
stop all13. Variables — o'zgaruvchilar (kelajakdan tanishuv)
Keyingi darsda batafsil. Hozir — tushuncha.
Variable — qiymat saqlash uchun xotira hujayrasi.
Variable yaratish
- Variables kategoriyasi
- Make a Variable
- Nom bering (masalan,
score) - For all sprites yoki For this sprite only
Variable bilan ishlash
set [score] to 0 - 0 ga belgila
change [score] by 1 - 1 ga oshir
say (score) for 2 secs - qiymatini ko'rsatishMisol: Ball hisoblash
when 🟢 clicked
set [score] to 0
forever
if <touching [Olma]?> then
change [score] by 114. To'liq loyiha: Doira chizish dasturi
Vazifa: Klaviatura strelkalari bilan rang va o'lcham o'zgartirib, doira chizish.
Sprite — chiziqcha (pen sifatida)
when 🟢 clicked
pen up
set pen color to [red]
set pen size to 5
erase all
go to [mouse-pointer]
pen down
when [up arrow] key pressed
change pen size by 2
when [down arrow] key pressed
change pen size by -2
when [r] key pressed
set pen color to [red]
when [g] key pressed
set pen color to [green]
when [b] key pressed
set pen color to [blue]
when [c] key pressed
erase all
forever
go to [mouse-pointer]Foydalanuvchi sichqoncha bilan chizadi. Klavishlar bilan rang va o'lcham.
15. Best Practices — yaxshi amaliyot
1. Blokda tartibli yozing
when 🟢 clicked
go to x:0 y:0
set rotation style [left-right]
point in direction 90
forever
move 5 steps
if on edge, bounceBloklar mantiqiy tartibda: avval boshlang'ich sozlama, keyin sikl.
2. Wait ishlating
forever
move 5 steps
wait 0.05 seconds ← Yetarli sekinlashAks holda — juda tez, ko'rinmaydi.
3. Nomlar aniq bo'lsin
Variable: score, lives, time — aniq. Sprite: Mushuk, Sichqon, Raketa — aniq.
Sprite1, Sprite2, var1 — yomon.
4. Comments — izohlar
Scratch'da bloklar ustida o'ng tugma → Add comment — izoh qo'shish.
Maxsus joylarda izoh — keyingi safar tushunish oson.
Darsdagi topshiriqlar
Topshiriq 1 — Geometrik shakllar
Pen extension qo'shing. Sprite chizadigan:
- Kvadrat (4 tomon, 90°)
- Uchburchak (3 tomon, 120°)
- Beshburchak (5 tomon, 72°)
- Olti burchak (6 tomon, 60°)
- Doira (36 ta kichik tomon, 10°)
Har biri uchun alohida tugma yoki klavish (1, 2, 3, 4, 5).
Saqlang: 1-shakllar
Topshiriq 2 — Tasodifiy spiralar
Loop yordamida spiral chizing:
when 🟢 clicked
pen down
set [size] to 0
repeat 100
move (size) steps
turn ↻ 91 degrees
change [size] by 2Turli burchaklarni sinab ko'ring (90, 89, 91, 121).
Saqlang: 2-spiral
Topshiriq 3 — If-else baho
Talaba ball kiritsin, dastur harf baho chiqarsin:
ask [Ballingiz?] and wait
if <(answer) >= 90> then
say (join [Bahongiz: ] [A]) for 3 secs
else
if <(answer) >= 80> then
...5 ta baho (A, B, C, D, F).
Saqlang: 3-baho
Topshiriq 4 — Tutib oluvchi (clone bilan)
Asteroid o'yiniga o'xshash:
- Olma sprite clone yordamida yuqoridan tushadi
- Savat sprite strelkalar bilan
- Tegishganda — olma o'chadi, score oshadi
- 30 sekund vaqt, eng ko'p olma tutib oling
Saqlang: 4-olma-tutish
Topshiriq 5 — Reaction o'yini
Reaktsiya tezligini tekshirish:
- Yashil bayroq bosilganda — "Hozir..." deyiladi
- Tasodifiy 2-5 sekund kutiladi
- Sprite rangi qizilga o'zgaradi
- Foydalanuvchi space bosishi kerak
- Necha sekundda reaksiya qildi — vaqt o'lchovi (timer)
when 🟢 clicked
say [Hozir...] for 1 secs
wait (pick random 2 to 5) seconds
set [color] effect to 100
reset timer
wait until <key [space] pressed?>
say (join [Vaqt: ] (timer)) for 5 secsSaqlang: 5-reaction
Topshiriq 6 — Birinchi o'yin: Pong (oddiy)
Pong — klassik o'yin.
- Top sprite — har xil yo'nalishda harakat qiladi, chetdan qaytadi
- Plat sprite — strelkalar bilan boshqariladi
- Top platga tegsa — qaytadi
- Top pastga tushib ketsa — o'yin tugaydi
Top:
when 🟢 clicked
point in direction 45
forever
move 5 steps
if on edge, bounce
if <touching [Plat]?> then
turn ↻ 180 degrees
if <(y position) < -170> then
broadcast [oyin tugadi]Saqlang: 6-pong
Topshiriq 7 — Maze (labirint)
- Stage backdrop'ida labirint chizing (qora chiziqlar)
- Sprite — strelkalar bilan
- Qora chiziqqa tegmasin
- Yashil rangga (oxir) yetib borsa — "Yutdingiz!"
forever
if <touching color [#000000]?> then
go to x:-200 y:-150
say [Qaytadan!]
if <touching color [#00FF00]?> then
say [Yutdingiz!] for 5 secs
stop allSaqlang: 7-labirint
Topshiriq 8 — Music maker
Music extension qo'shing.
Klavishlar bilan musiqa chalish:
1→ C nota2→ D nota3→ E nota4→ F nota5→ G nota6→ A nota7→ B nota
when [1] key pressed
play note [60] for 0.5 beats
when [2] key pressed
play note [62] for 0.5 beats
...Ohang yaratib ko'ring — "Twinkle Twinkle".
Saqlang: 8-music
Asosiy tushunchalar (lug'at)
| Termin | Qisqacha izoh |
|---|---|
| wait | Kutish |
| repeat N | N marta takrorlash |
| forever | Cheksiz takrorlash |
| if | Shart |
| if-else | Shart else bilan |
| wait until | Shartgacha kutish |
| repeat until | Shartgacha takrorlash |
| stop | To'xtatish |
| clone | Sprite nusxasi |
| nested | Ichidagi (bir shart ichida boshqa) |
| Variable | O'zgaruvchi |
| Pen | Qalam (chizish) |
| Comment | Kodga izoh |
| Best practice | Yaxshi amaliyot |