[ν°ν κ·Έλν½] μ΄λ²€νΈμ ν¨μ
1. μ΄λ²€νΈλ?
- μ΄λ²€νΈλ νλ‘κ·Έλ¨μ μν΄ κ°μ§λκ³ μ²λ¦¬λ μ μλ λμμ λ§ν©λλ€.
μ) ν€λ³΄λλ₯Ό λλ μ λ, λ§μ°μ€λ₯Ό ν΄λ¦νμ λ
- νΉμ μ΄λ²€νΈκ° λ°μνλ©΄ μ ν΄μ§ μ½λκ°μ κ°μ§ ν¨μκ° μ€νλ©λλ€.
μ΄λ²€νΈ μμ 1
- Turtle λͺ¨λμ ν€λ³΄λ μ λ ₯μ Screen κ°μ²΄λ₯Ό μ¬μ©ν΄μ μ²λ¦¬ν©λλ€.
- Screen κ°μ²΄μλ ν€λ³΄λμ λ§μ°μ€ μ λ ₯μ μ μ΄νλ λ€μν ν¨μμ κΈ°λ₯μ΄ μμ΅λλ€.
- Screen κ°μ²΄λ₯Ό λ§λ€μ§ μκ³ λ°λ‘ μ΄λ²€νΈλ₯Ό μ²λ¦¬νλ λ°©λ²λ μμ΅λλ€.
κΈ°λ³Έ μμ | ν€λ³΄λ ν€(Key) | ||
screen = turtle.Screen() screen.onkey(ν¨μλͺ , 'μ½λ') screen.listen() |
μμͺ½ νμ΄ν | 'Up' | |
μλμͺ½ νμ΄ν | 'Down' | ||
μΌμͺ½ νμ΄ν | 'Left' | ||
μ€λ₯Έμͺ½ νμ΄ν | 'Right' |
# ν€λ³΄λ ν€ : μνλ²³ μλ¬Έμ, space λ± ...
# onkey(ν¨μλͺ , 'μ½λ') - ν€λ³΄λλ₯Ό μ€λ«λμ λλ¬λ νλ²λ§ μ΄λ²€νΈκ° λ°μ
# onkeypress(ν¨μλͺ ,'μ½λ') - ν€λ³΄λλ₯Ό μ€λ«λμ λλ₯΄λ©΄ μ§μμ μΌλ‘ μ΄λ²€νΈκ° λ°μ
μμ μμ€(22-key_event.py) | μμ μμ€(22-key_event2.py) |
import turtle as t t.shape('turtle') scr = t.Screen() def right(): t.setheading(0) t.forward(50) def up(): t.setheading(90) t.forward(50) def left(): t.setheading(180) t.forward(50) def down(): t.setheading(270) t.forward(50) scr.onkey(right,'Right') scr.onkey(up,'Up') scr.onkey(left,'Left') scr.onkey(down,'Down') scr.listen() #t.mainloop() |
import turtle as t t.shape('turtle') def right(): t.setheading(0) t.forward(50) def up(): t.setheading(90) t.forward(50) def left(): t.setheading(180) t.forward(50) def down(): t.setheading(270) t.forward(50) t.onkey(right,'Right') t.onkey(up,'Up') t.onkey(left,'Left') t.onkey(down,'Down') t.listen() #t.mainloop() |
μ€μ΅1) ν€λ³΄λ μνλ²³μ μ λ ₯νλ©΄ μ μ μκΉμ΄ λ°λλλ‘ νλ‘κ·Έλ¨μ μμ ν΄ λ΄ μλ€.
μ€μ΅2) 'space' ν€λ₯Ό λλ₯΄λ©΄ κ·Έλ¦Όμ΄ μ§μμ§λλ‘ νλ‘κ·Έλ¨μ μμ ν΄ λ΄ μλ€.
μ΄λ²€νΈ μμ 2
- λ§μ°μ€λ‘ νλ©΄μ ν΄λ¦νμ λμ μ΄λ²€νΈλ₯Ό μ²λ¦¬ν μ μμ΅λλ€.
- λ§μ°μ€λ₯Ό ν΄λ¦ν μ μ μ’ν(x,y)λ₯Ό μΈμλ‘ ν¨μκ° μ€νλ©λλ€.
μμ μμ€(23-click_event.py) | |
import turtle as t t.speed(0) t.pensize(2) t.hideturtle() t.onscreenclick(t.goto) t.listen() #t.mainloop() |
t.goto #κ±°λΆμ΄μ 미리 μ μλ goto ν¨μλ₯Ό νΈμΆν©λλ€. #λ§μ°μ€ ν΄λ¦ν μμΉλ‘ μμ§μ |
2. λ³ κ·Έλ¦¬κΈ°
λ³μ 그리λ ν¨μλ₯Ό μ μνμ¬ νλ©΄μ λ³μ κ·Έλ €λ³΄λλ‘ νκ² μ΅λλ€. 첫 λ²μ§Έ μμ μμλ μ§μ ν μμΉμ λ³μ κ·Έλ €λ³΄κ³ , λ λ²μ§Έ μμ μμλ μ΄λ₯Ό μμ νμ¬ μμμ μμΉμ μ¬λ¬ κ°μ§ ν¬κΈ°μ λ³ 20κ°λ₯Ό κ·Έλ €λ³΄λ €κ³ ν©λλ€. λ§μ§λ§ μμ μμλ λ§μ°μ€λ₯Ό ν΄λ¦νλ κ³³μ λ³μ κ·Έλ €λ³΄κ² μ΅λλ€.
μμ μμ€(24-star_def.py) | |
import turtle as t def star(): t.begin_fill() for i in range(5): t.forward(30) t.left(144) t.end_fill() t.bgcolor('black') t.color('yellow') t.ht() t.speed(10) star() t.penup() t.goto(100,100) t.pendown() star() |
![]() |
μμ μμ€(24-star_def2.py) | |
import turtle as t import random as r def star(x,y): t.penup() t.goto(x,y) t.pendown() size = r.randint(15,30) t.begin_fill() for i in range(5): t.forward(size) t.left(144) t.end_fill() t.bgcolor('black') t.color('yellow') t.ht() t.speed(10) for i in range(20): star(r.randint(-300,300),r.randint(50,300)) |
![]() |
μμ μμ€(25-star_click.py) | |
import turtle as t import random as r def star(x,y): t.penup() t.goto(x,y) t.pendown() size = r.randint(15,30) t.begin_fill() for i in range(5): t.forward(size) t.left(144) t.end_fill() t.bgcolor('black') t.color('yellow') t.ht() t.speed(10) t.onscreenclick(star) t.listen() #t.mainloop() |
![]() |
3. λμ‘μ΄ κ·Έλ¦¬κΈ°
μμ μμ€(26-snow_def.py) | |
import turtle as t import random as r def snow(): snowsize = r.randint(8,12) for i in range(12): t.forward(snowsize) t.backward(snowsize) t.right(360/12) t.bgcolor('black') t.color('white') t.pensize(2) t.ht() t.speed(0) for j in range(20): t.penup() x = r.randint(-350,350) y = r.randint(0,350) t.goto(x,y) t.pendown() snow() |
![]() |