Hotunan kunkuru sanannen hanya ce ta gabatar da shirye-shirye ga yara. Da farko wani bangare ne na yaren shirye-shiryen Logo wanda Wally Feurzig da Seymour Papert suka tsara a cikin 1966. A Python, muna da ษakin karatu na kunkuru, wanda ke ba mu damar ฦirฦira da sarrafa zanen kunkuru cikin sauฦi. A cikin wannan labarin, za mu bincika takardun Kunkuru, mu fahimci ayyukanta, kuma mu koyi yadda ake warware takamaiman matsala ta amfani da wannan ษakin karatu.
Kunkuru Library da Muhimmancinsa
The kunkuru ษakin karatu shine madaidaicin ษakin karatu na Python wanda ke ba da sauฦin fahimta don zana siffofi da hotuna akan zane mai kama-da-wane. Yana da amfani musamman ga masu farawa saboda yana ba su damar koyon dabarun tsara shirye-shirye a gani. Samfurin zane na kunkuru yana amfani da kunkuru, wanda shine ainihin siginan kwamfuta a cikin sifar triangle, wanda ke motsawa akan jirgin sama mai girma biyu yana bin takamaiman umarni.
Laburaren kunkuru yana ba mu damar ฦirฦirar hotuna masu rikitarwa da raye-raye, koyar da warware matsala, ฦira, da yadda za a rushe manyan ayyuka zuwa ฦananan matakan sarrafawa. Wasu mahimman fasali da ayyukan ษakin karatu na kunkuru sune:
- Ayyukan zane mai sauฦi don matsawa gaba, baya, juya dama, da juya hagu.
- Sarrafa alฦalamin kunkuru, gami da launi, kauri, da siffofi masu cikawa.
- Nagartattun fasalulluka kamar kunkuru na cloning, canza siffofi, da shirye-shiryen da ke haifar da aukuwa.
Magani ga Matsala: Zana Karkace
A matsayin misali, bari mu yi tunanin muna son zana karkace ta amfani da ษakin karatu na kunkuru. Za mu rubuta shirin Python don ฦirฦirar wannan karkace, tafiya ta kowane mataki don fahimtar lambar da ke ciki. Maganin mu zai ฦunshi amfani da madaukai, ayyukan motsi na kunkuru, da sarrafa bayyanar alkalami.
import turtle wn = turtle.Screen() wn.bgcolor("black") t = turtle.Turtle() t.speed(0) t.color("white") def draw_spiral(t, length, angle): for i in range(100): t.forward(length) t.right(angle) length = length + 2 draw_spiral(t, 5, 89) wn.mainloop()
Bayanin mataki-mataki na Code
Na farko, mu shigo da ษakin karatu na kunkuru kuma ฦirฦirar taga don nuna zane-zanenmu. Mun saita launin baya zuwa baki kuma mun ฦirฦiri wani abu kunkuru mai suna 't' tare da launin farin alฦalami da iyakar gudu.
import turtle wn = turtle.Screen() wn.bgcolor("black") t = turtle.Turtle() t.speed(0) t.color("white")
Na gaba, muna ayyana aiki draw_spiral() wanda ke ษaukar sigogi guda uku: abin kunkuru, tsawon farkon ษangaren karkace, da kusurwar juyawa. A cikin wannan aikin, muna amfani da a don madauki don sake maimaita adadin matakan da ake so, motsa kunkuru gaba, juya shi, da haษaka tsayin sashi.
def draw_spiral(t, length, angle): for i in range(100): t.forward(length) t.right(angle) length = length + 2
A ฦarshe, muna kira da draw_spiral() aiki tare da abin kunkuru, tsayin farawa, da kusurwar juyawa. Daga nan sai mu fara babban madauki na taga zanen kunkuru, wanda ke nuna karkace har sai mun rufe taga.
draw_spiral(t, 5, 89) wn.mainloop()
Wannan shirin Python yana nuna ainihin yadda ake amfani da ษakin karatu na kunkuru, daga kafa yanayi zuwa ayyana aiki don ฦirฦirar karkace. Gwaji tare da tsayin farko da kusurwoyi daban-daban zai haifar da sifofin karkace daban-daban.
A ฦarshe, ษakin karatu na kunkuru wani kayan aiki ne mai mahimmanci ga masu farawa da masana, suna ba da hanya mai ban sha'awa don koyo da koyar da dabarun tsara shirye-shirye. ฦaฦฦarfan ฦaฦฦarfan ฦaฦฦarfan ฦaฦฦarfan ฦaฦฦarfan ฦaฦฦarfan ฦaฦฦarfan ฦaฦฦarfan ฦaฦฦarfan fasali da ayyuka, ya sa ya zama manufa don duba hadaddun ra'ayoyi da ฦirฦirar raye-raye masu ban sha'awa.