Skip to content

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 seconds

1 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 secs

3. repeat — N marta takrorlash

repeat 10
    move 10 steps
    turn ↻ 36 degrees

10 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 up

4 ta tomon, har biri 90 daraja burilish.

Misol: ko'pburchak chizish

repeat 5
    move 100 steps
    turn ↻ (360 / 5) degrees

Beshburchak (360 / 5 = 72).

Universal:

ask [Necha tomon?] and wait
set [tomon] = (answer)

repeat (tomon)
    move 100 steps
    turn ↻ (360 / (tomon)) degrees

Foydalanuvchi raqam beradi — Scratch shu ko'pburchakni chizadi.

4. forever — cheksiz takrorlash

forever
    move 5 steps
    if on edge, bounce

To'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 unutmang

5. if — shart

if <condition> then
    ... amallar

Shart rost bo'lsa — amallar bajariladi.

Misol: chetni tekshirish

forever
    move 10 steps
    if <touching [edge]?> then
        turn ↻ 180 degrees

6. if-else — shart bilan else

if <condition> then
    ... A amallari
else
    ... B amallari

Shart 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 secs

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

Yoki 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 secs

Space bosilguncha — kutadi. Bosilgandan keyin — davom etadi.

9. repeat until — shart ROST bo'lguncha takrorlash

repeat until <touching [Sprite2]?>
    move 5 steps

Sprite2'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 all

11. 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 seconds

Har 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 clone

Yangi 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 clone

Raketa (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 10

Tegishish

when I start as a clone (asteroid uchun)
    forever
        if <touching [Raketa]?> then
            broadcast [oyin tugadi]
            delete this clone
when I receive [oyin tugadi] (Raketa uchun)
    say [O'yin tugadi!] for 2 secs
    stop all

13. Variables — o'zgaruvchilar (kelajakdan tanishuv)

Keyingi darsda batafsil. Hozir — tushuncha.

Variable — qiymat saqlash uchun xotira hujayrasi.

Variable yaratish

  1. Variables kategoriyasi
  2. Make a Variable
  3. Nom bering (masalan, score)
  4. 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'rsatish

Misol: Ball hisoblash

when 🟢 clicked
    set [score] to 0
    forever
        if <touching [Olma]?> then
            change [score] by 1

14. 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, bounce

Bloklar mantiqiy tartibda: avval boshlang'ich sozlama, keyin sikl.

2. Wait ishlating

forever
    move 5 steps
    wait 0.05 seconds   ← Yetarli sekinlash

Aks 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 tugmaAdd comment — izoh qo'shish.

Maxsus joylarda izoh — keyingi safar tushunish oson.

Darsdagi topshiriqlar

Topshiriq 1 — Geometrik shakllar

Pen extension qo'shing. Sprite chizadigan:

  1. Kvadrat (4 tomon, 90°)
  2. Uchburchak (3 tomon, 120°)
  3. Beshburchak (5 tomon, 72°)
  4. Olti burchak (6 tomon, 60°)
  5. 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 2

Turli 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:

  1. Olma sprite clone yordamida yuqoridan tushadi
  2. Savat sprite strelkalar bilan
  3. Tegishganda — olma o'chadi, score oshadi
  4. 30 sekund vaqt, eng ko'p olma tutib oling

Saqlang: 4-olma-tutish

Topshiriq 5 — Reaction o'yini

Reaktsiya tezligini tekshirish:

  1. Yashil bayroq bosilganda — "Hozir..." deyiladi
  2. Tasodifiy 2-5 sekund kutiladi
  3. Sprite rangi qizilga o'zgaradi
  4. Foydalanuvchi space bosishi kerak
  5. 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 secs

Saqlang: 5-reaction

Topshiriq 6 — Birinchi o'yin: Pong (oddiy)

Pong — klassik o'yin.

  1. Top sprite — har xil yo'nalishda harakat qiladi, chetdan qaytadi
  2. Plat sprite — strelkalar bilan boshqariladi
  3. Top platga tegsa — qaytadi
  4. 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)

  1. Stage backdrop'ida labirint chizing (qora chiziqlar)
  2. Sprite — strelkalar bilan
  3. Qora chiziqqa tegmasin
  4. 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 all

Saqlang: 7-labirint

Topshiriq 8 — Music maker

Music extension qo'shing.

Klavishlar bilan musiqa chalish:

  • 1 → C nota
  • 2 → D nota
  • 3 → E nota
  • 4 → F nota
  • 5 → G nota
  • 6 → A nota
  • 7 → 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)

TerminQisqacha izoh
waitKutish
repeat NN marta takrorlash
foreverCheksiz takrorlash
ifShart
if-elseShart else bilan
wait untilShartgacha kutish
repeat untilShartgacha takrorlash
stopTo'xtatish
cloneSprite nusxasi
nestedIchidagi (bir shart ichida boshqa)
VariableO'zgaruvchi
PenQalam (chizish)
CommentKodga izoh
Best practiceYaxshi amaliyot

Keyingi dars

8-dars: Scratch — o'zgaruvchilar va o'yin loyihasi →

Master IT o'quv markazi — o'qitish rejasi