Skip to content

Instantly share code, notes, and snippets.

@nattybear
Last active May 18, 2017 05:45
Show Gist options
  • Save nattybear/2f8628c9733d78e94d91675711ad318d to your computer and use it in GitHub Desktop.
Save nattybear/2f8628c9733d78e94d91675711ad318d to your computer and use it in GitHub Desktop.
거북이 대포 게임 만들기
import turtle as t
import random
def turn_up():
t.left(2)
def turn_down():
t.right(2)
def fire():
ang = t.heading()
while t.ycor() > 0:
t.forward(15)
t.right(5)
d = t.distance(target, 0)
t.sety(random.randint(10, 100))
if d < 25:
t.color("blue")
t.write("Good!", False, "center", ("", 15))
else:
t.color("red")
t.write("Bad!", False, "center", ("", 15))
t.color("black")
t.goto(-200, 10)
t.setheading(ang)
t.goto(-300, 0)
t.down()
t.goto(300, 0)
target = random.randint(50, 150)
t.pensize(3)
t.color("green")
t.up()
t.goto(target - 25, 2)
t.down()
t.goto(target +25, 2)
t.color("black")
t.up()
t.goto(-200, 10)
t.setheading(20)
t.onkeypress(turn_up, "Up")
t.onkeypress(turn_down, "Down")
t.onkeypress(fire, "space")
t.listen()
t.mainloop()
@nattybear
Copy link
Author

nattybear commented May 18, 2017

출처 : 모두의 파이썬 - 이승찬 지음, (주)도서출판 길벗

좌표

  • pos()
  • goto()
import turtle as t
t.pos()
t.goto(100, 50)
t.pos()

각도

  • heading()
  • setheading()

글자 쓰기

  • write()

t.write("문자열", False, "center", ("", 15))

  • False : 거북이 위치 옮기지 않음
  • center : 문장 가운데 정렬
  • 15 : 글자 크기

프로젝트 구조

  • turn_up / turn_down
  • fire : 사용자가 SpaceBar 누르면 작동
  • onkeypress

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment